1 ; RUN: opt -S -loop-vectorize -force-vector-width=4 -force-vector-interleave=1 < %s 2>&1 | FileCheck %s
3 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5 ; Check that the vectorizer identifies the %p.09 phi,
6 ; as an induction variable, despite the potential overflow
7 ; due to the truncation from 32bit to 8bit.
8 ; SCEV will detect the pattern "sext(trunc(%p.09)) + %step"
9 ; and generate the required runtime checks under which
10 ; we can assume no overflow. We check here that we generate
11 ; exactly two runtime checks:
12 ; 1) an overflow check:
13 ; {0,+,(trunc i32 %step to i8)}<%for.body> Added Flags: <nssw>
14 ; 2) an equality check verifying that the step of the induction
15 ; is equal to sext(trunc(step)):
16 ; Equal predicate: %step == (sext i8 (trunc i32 %step to i8) to i32)
21 ; void doit1(int n, int step) {
24 ; for (i = 0; i < n; i++) {
32 ; CHECK: vector.scevcheck
33 ; CHECK: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
34 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
35 ; CHECK: %[[TEST:[0-9]+]] = or i1 {{.*}}, %mul.overflow
36 ; CHECK: %[[NTEST:[0-9]+]] = or i1 false, %[[TEST]]
37 ; CHECK: %ident.check = icmp ne i32 {{.*}}, %{{.*}}
38 ; CHECK: %{{.*}} = or i1 %[[NTEST]], %ident.check
39 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
43 @a = common local_unnamed_addr global [250 x i32] zeroinitializer, align 16
45 ; Function Attrs: norecurse nounwind uwtable
46 define void @doit1(i32 %n, i32 %step) local_unnamed_addr {
48 %cmp7 = icmp sgt i32 %n, 0
49 br i1 %cmp7, label %for.body.preheader, label %for.end
52 %wide.trip.count = zext i32 %n to i64
56 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %for.body.preheader ]
57 %p.09 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
58 %sext = shl i32 %p.09, 24
59 %conv = ashr exact i32 %sext, 24
60 %arrayidx = getelementptr inbounds [250 x i32], [250 x i32]* @a, i64 0, i64 %indvars.iv
61 store i32 %conv, i32* %arrayidx, align 4
62 %add = add nsw i32 %conv, %step
63 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
64 %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
65 br i1 %exitcond, label %for.end.loopexit, label %for.body
74 ; Same as above, but for checking the SCEV "zext(trunc(%p.09)) + %step".
75 ; Here we expect the following two predicates to be added for runtime checking:
76 ; 1) {0,+,(trunc i32 %step to i8)}<%for.body> Added Flags: <nusw>
77 ; 2) Equal predicate: %step == (sext i8 (trunc i32 %step to i8) to i32)
80 ; void doit2(int n, int step) {
82 ; unsigned char p = 0;
83 ; for (i = 0; i < n; i++) {
91 ; CHECK: vector.scevcheck
92 ; CHECK: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
93 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
94 ; CHECK: %[[TEST:[0-9]+]] = or i1 {{.*}}, %mul.overflow
95 ; CHECK: %[[NTEST:[0-9]+]] = or i1 false, %[[TEST]]
96 ; CHECK: %[[EXT:[0-9]+]] = sext i8 {{.*}} to i32
97 ; CHECK: %ident.check = icmp ne i32 {{.*}}, %[[EXT]]
98 ; CHECK: %{{.*}} = or i1 %[[NTEST]], %ident.check
99 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
100 ; CHECK: vector.body:
103 ; Function Attrs: norecurse nounwind uwtable
104 define void @doit2(i32 %n, i32 %step) local_unnamed_addr {
106 %cmp7 = icmp sgt i32 %n, 0
107 br i1 %cmp7, label %for.body.preheader, label %for.end
110 %wide.trip.count = zext i32 %n to i64
114 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %for.body.preheader ]
115 %p.09 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
116 %conv = and i32 %p.09, 255
117 %arrayidx = getelementptr inbounds [250 x i32], [250 x i32]* @a, i64 0, i64 %indvars.iv
118 store i32 %conv, i32* %arrayidx, align 4
119 %add = add nsw i32 %conv, %step
120 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
121 %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
122 br i1 %exitcond, label %for.end.loopexit, label %for.body
131 ; Here we check that the same phi scev analysis would fail
132 ; to create the runtime checks because the step is not invariant.
133 ; As a result vectorization will fail.
136 ; void doit3(int n, int step) {
139 ; for (i = 0; i < n; i++) {
147 ; CHECK-LABEL: @doit3
148 ; CHECK-NOT: vector.scevcheck
149 ; CHECK-NOT: vector.body:
150 ; CHECK-LABEL: for.body:
152 ; Function Attrs: norecurse nounwind uwtable
153 define void @doit3(i32 %n, i32 %step) local_unnamed_addr {
155 %cmp9 = icmp sgt i32 %n, 0
156 br i1 %cmp9, label %for.body.preheader, label %for.end
159 %wide.trip.count = zext i32 %n to i64
163 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %for.body.preheader ]
164 %p.012 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
165 %step.addr.010 = phi i32 [ %add3, %for.body ], [ %step, %for.body.preheader ]
166 %sext = shl i32 %p.012, 24
167 %conv = ashr exact i32 %sext, 24
168 %arrayidx = getelementptr inbounds [250 x i32], [250 x i32]* @a, i64 0, i64 %indvars.iv
169 store i32 %conv, i32* %arrayidx, align 4
170 %add = add nsw i32 %conv, %step.addr.010
171 %add3 = add nsw i32 %step.addr.010, 2
172 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
173 %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
174 br i1 %exitcond, label %for.end.loopexit, label %for.body
184 ; Lastly, we also check the case where we can tell at compile time that
185 ; the step of the induction is equal to sext(trunc(step)), in which case
186 ; we don't have to check this equality at runtime (we only need the
187 ; runtime overflow check). Therefore only the following overflow predicate
188 ; will be added for runtime checking:
189 ; {0,+,%cstep}<%for.body> Added Flags: <nssw>
192 ; void doit4(int n, char cstep) {
196 ; for (i = 0; i < n; i++) {
202 ; CHECK-LABEL: @doit4
203 ; CHECK: vector.scevcheck
204 ; CHECK: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
205 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
206 ; CHECK: %{{.*}} = or i1 {{.*}}, %mul.overflow
207 ; CHECK-NOT: %ident.check = icmp ne i32 {{.*}}, %{{.*}}
208 ; CHECK-NOT: %{{.*}} = or i1 %{{.*}}, %ident.check
209 ; CHECK-NOT: %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 {{.*}}, i8 {{.*}})
210 ; CHECK: vector.body:
213 ; Function Attrs: norecurse nounwind uwtable
214 define void @doit4(i32 %n, i8 signext %cstep) local_unnamed_addr {
216 %conv = sext i8 %cstep to i32
217 %cmp10 = icmp sgt i32 %n, 0
218 br i1 %cmp10, label %for.body.preheader, label %for.end
221 %wide.trip.count = zext i32 %n to i64
225 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %for.body.preheader ]
226 %p.011 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
227 %sext = shl i32 %p.011, 24
228 %conv2 = ashr exact i32 %sext, 24
229 %arrayidx = getelementptr inbounds [250 x i32], [250 x i32]* @a, i64 0, i64 %indvars.iv
230 store i32 %conv2, i32* %arrayidx, align 4
231 %add = add nsw i32 %conv2, %conv
232 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
233 %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
234 br i1 %exitcond, label %for.end.loopexit, label %for.body