1 ; RUN: opt < %s -indvars -S | FileCheck %s
2 target triple = "aarch64--linux-gnu"
4 ; Provide legal integer types.
5 target datalayout = "n8:16:32:64"
8 ; Check the loop exit i32 compare instruction and operand are widened to i64
9 ; instead of truncating IV before its use in the i32 compare instruction.
11 @idx = common global i32 0, align 4
12 @e = common global i32 0, align 4
13 @ptr = common global i32* null, align 8
16 ; CHECK: for.body.lr.ph:
25 store i32 -1, i32* @idx, align 4
26 %0 = load i32, i32* @e, align 4
27 %cmp4 = icmp slt i32 %0, 0
28 br i1 %cmp4, label %for.end.loopexit, label %for.body.lr.ph
31 %1 = load i32*, i32** @ptr, align 8
32 %2 = load i32, i32* @e, align 4
36 %inc = add nsw i32 %i.05, 1
37 %cmp = icmp slt i32 %i.05, %2
38 br i1 %cmp, label %for.body, label %for.cond.for.end.loopexit_crit_edge
41 %i.05 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.cond ]
42 %idxprom = sext i32 %i.05 to i64
43 %arrayidx = getelementptr inbounds i32, i32* %1, i64 %idxprom
44 %3 = load i32, i32* %arrayidx, align 4
45 %tobool = icmp eq i32 %3, 0
46 br i1 %tobool, label %if.then, label %for.cond
49 %i.05.lcssa = phi i32 [ %i.05, %for.body ]
50 store i32 %i.05.lcssa, i32* @idx, align 4
53 for.cond.for.end.loopexit_crit_edge:
54 br label %for.end.loopexit
60 %4 = load i32, i32* @idx, align 4
66 ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
67 ; CHECK: %cmp2.us = icmp ult i64
68 ; CHECK-NOT: %2 = trunc i64 %indvars.iv.next to i32
69 ; CHECK-NOT: %cmp2.us = icmp slt i32
71 define void @test2([8 x i8]* %a, i8* %b, i8 %limit) {
73 %conv = zext i8 %limit to i32
74 br i1 undef, label %for.cond1.preheader, label %for.cond1.preheader.us
76 for.cond1.preheader.us:
77 %storemerge5.us = phi i32 [ 0, %entry ], [ %inc14.us, %for.inc13.us ]
78 br i1 true, label %for.body4.lr.ph.us, label %for.inc13.us
81 %inc14.us = add nsw i32 %storemerge5.us, 1
82 %cmp.us = icmp slt i32 %inc14.us, 4
83 br i1 %cmp.us, label %for.cond1.preheader.us, label %for.end
86 %storemerge14.us = phi i32 [ 0, %for.body4.lr.ph.us ], [ %inc.us, %for.body4.us ]
87 %idxprom.us = sext i32 %storemerge14.us to i64
88 %arrayidx6.us = getelementptr inbounds [8 x i8], [8 x i8]* %a, i64 %idxprom5.us, i64 %idxprom.us
89 %0 = load i8, i8* %arrayidx6.us, align 1
90 %idxprom7.us = zext i8 %0 to i64
91 %arrayidx8.us = getelementptr inbounds i8, i8* %b, i64 %idxprom7.us
92 %1 = load i8, i8* %arrayidx8.us, align 1
93 store i8 %1, i8* %arrayidx6.us, align 1
94 %inc.us = add nsw i32 %storemerge14.us, 1
95 %cmp2.us = icmp slt i32 %inc.us, %conv
96 br i1 %cmp2.us, label %for.body4.us, label %for.inc13.us
99 %idxprom5.us = sext i32 %storemerge5.us to i64
100 br label %for.body4.us
103 %storemerge5 = phi i32 [ 0, %entry ], [ %inc14, %for.inc13 ]
104 br i1 false, label %for.inc13, label %for.inc13
107 %inc14 = add nsw i32 %storemerge5, 1
108 %cmp = icmp slt i32 %inc14, 4
109 br i1 %cmp, label %for.cond1.preheader, label %for.end
115 ; CHECK-LABEL: @test3
119 ; CHECK: icmp slt i64
121 define i32 @test3(i32* %a, i32 %b) {
126 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
127 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
128 %cmp = icmp slt i32 %i.0, %b
129 br i1 %cmp, label %for.body, label %for.end
132 %idxprom = sext i32 %i.0 to i64
133 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
134 %0 = load i32, i32* %arrayidx, align 4
135 %add = add nsw i32 %sum.0, %0
136 %inc = add nsw i32 %i.0, 1
143 declare i32 @fn1(i8 signext)
146 ; CHECK-LABEL: @test4
151 define i32 @test4(i32 %a) {
156 %c.07 = phi i8 [ -3, %entry ], [ %dec, %for.body ]
157 %conv6 = zext i8 %c.07 to i32
158 %or = or i32 %a, %conv6
159 %conv3 = trunc i32 %or to i8
160 %call = call i32 @fn1(i8 signext %conv3)
161 %dec = add i8 %c.07, -1
162 %cmp = icmp sgt i8 %dec, -14
163 br i1 %cmp, label %for.body, label %for.end
169 ; CHECK-LABEL: @test5
173 ; CHECK: icmp ule i64
175 define i32 @test5(i32* %a, i32 %b) {
180 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
181 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
182 %cmp = icmp ule i32 %i.0, %b
183 br i1 %cmp, label %for.body, label %for.end
186 %idxprom = zext i32 %i.0 to i64
187 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
188 %0 = load i32, i32* %arrayidx, align 4
189 %add = add nsw i32 %sum.0, %0
190 %inc = add nsw i32 %i.0, 1
197 define i32 @test6(i32* %a, i32 %b) {
198 ; CHECK-LABEL: @test6(
199 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
201 ; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]]
207 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
208 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
209 %cmp = icmp sle i32 %i.0, %b
210 br i1 %cmp, label %for.body, label %for.end
213 %idxprom = zext i32 %i.0 to i64
214 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
215 %0 = load i32, i32* %arrayidx, align 4
216 %add = add nsw i32 %sum.0, %0
217 %inc = add nsw i32 %i.0, 1
224 define i32 @test7(i32* %a, i32 %b) {
225 ; CHECK-LABEL: @test7(
226 ; CHECK: [[B_ZEXT:%[a-z0-9]+]] = zext i32 %b to i64
227 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
229 ; CHECK: icmp ule i64 %indvars.iv, [[B_ZEXT]]
231 ; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]]
237 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
238 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
239 %cmp = icmp ule i32 %i.0, %b
240 br i1 %cmp, label %for.body, label %for.end
243 %idxprom = sext i32 %i.0 to i64
244 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
245 %0 = load i32, i32* %arrayidx, align 4
246 %add = add nsw i32 %sum.0, %0
247 %inc = add nsw i32 %i.0, 1
248 %cmp2 = icmp sle i32 %i.0, %b
249 br i1 %cmp2, label %for.cond, label %for.end
255 define i32 @test8(i32* %a, i32 %b, i32 %init) {
256 ; CHECK-LABEL: @test8(
257 ; CHECK: [[INIT_SEXT:%[a-z0-9]+]] = sext i32 %init to i64
258 ; CHECK: [[B_ZEXT:%[a-z0-9]+]] = zext i32 %b to i64
260 ; Note: %indvars.iv is the sign extension of %i.0
261 ; CHECK: %indvars.iv = phi i64 [ [[INIT_SEXT]], %for.cond.preheader ], [ %indvars.iv.next, %for.body ]
262 ; CHECK: icmp ule i64 %indvars.iv, [[B_ZEXT]]
265 %e = icmp sgt i32 %init, 0
266 br i1 %e, label %for.cond, label %leave
269 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
270 %i.0 = phi i32 [ %init, %entry ], [ %inc, %for.body ]
271 %cmp = icmp ule i32 %i.0, %b
272 br i1 %cmp, label %for.body, label %for.end
275 %idxprom = sext i32 %i.0 to i64
276 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
277 %0 = load i32, i32* %arrayidx, align 4
278 %add = add nsw i32 %sum.0, %0
279 %inc = add nsw i32 %i.0, 1
280 %cmp2 = icmp slt i32 0, %inc
281 br i1 %cmp2, label %for.cond, label %for.end
290 define i32 @test9(i32* %a, i32 %b, i32 %init) {
291 ; CHECK-LABEL: @test9(
292 ; CHECK: [[INIT_ZEXT:%[a-z0-9]+]] = zext i32 %init to i64
293 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
295 ; Note: %indvars.iv is the zero extension of %i.0
296 ; CHECK: %indvars.iv = phi i64 [ [[INIT_ZEXT]], %for.cond.preheader ], [ %indvars.iv.next, %for.body ]
297 ; CHECK: icmp slt i64 %indvars.iv, [[B_SEXT]]
300 %e = icmp sgt i32 %init, 0
301 br i1 %e, label %for.cond, label %leave
304 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
305 %i.0 = phi i32 [ %init, %entry ], [ %inc, %for.body ]
306 %cmp = icmp slt i32 %i.0, %b
307 br i1 %cmp, label %for.body, label %for.end
310 %idxprom = zext i32 %i.0 to i64
311 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
312 %0 = load i32, i32* %arrayidx, align 4
313 %add = add nsw i32 %sum.0, %0
314 %inc = add nsw i32 %i.0, 1
315 %cmp2 = icmp slt i32 0, %inc
316 br i1 %cmp2, label %for.cond, label %for.end
325 declare void @consume.i64(i64)
326 declare void @consume.i1(i1)
328 define i32 @test10(i32 %v) {
329 ; CHECK-LABEL: @test10(
335 ; CHECK: [[WIDE_V:%[a-z0-9]+]] = sext i32 %v to i64
337 ; CHECK: %indvars.iv = phi i64 [ %indvars.iv.next, %loop ], [ 0, %entry ]
338 ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
339 ; CHECK: [[MUL:%[a-z0-9]+]] = mul nsw i64 %indvars.iv, -1
340 ; CHECK: [[CMP:%[a-z0-9]+]] = icmp eq i64 [[MUL]], [[WIDE_V]]
341 ; CHECK: call void @consume.i1(i1 [[CMP]])
343 %i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
344 %i.inc = add i32 %i, 1
346 %cmp = icmp eq i32 %iv, %v
347 call void @consume.i1(i1 %cmp)
348 %be.cond = icmp slt i32 %i.inc, 11
349 %ext = sext i32 %iv to i64
350 call void @consume.i64(i64 %ext)
351 br i1 %be.cond, label %loop, label %leave