1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -indvars -S < %s | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
6 @X = external global [0 x double]
8 ; Indvars should be able to simplify simple comparisons involving
11 define void @foo(i64 %n, i32* nocapture %p) nounwind {
14 ; CHECK-NEXT: [[CMP9:%.*]] = icmp sgt i64 [[N:%.*]], 0
15 ; CHECK-NEXT: br i1 [[CMP9]], label [[PRE:%.*]], label [[RETURN:%.*]]
17 ; CHECK-NEXT: [[T3:%.*]] = load i32, i32* [[P:%.*]]
18 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp ne i32 [[T3]], 0
19 ; CHECK-NEXT: br label [[LOOP:%.*]]
21 ; CHECK-NEXT: [[I:%.*]] = phi i64 [ 0, [[PRE]] ], [ [[INC:%.*]], [[FOR_INC:%.*]] ]
22 ; CHECK-NEXT: [[COND:%.*]] = and i1 [[TOBOOL_NOT]], true
23 ; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[FOR_INC]]
25 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr [0 x double], [0 x double]* @X, i64 0, i64 [[I]]
26 ; CHECK-NEXT: store double 3.200000e+00, double* [[ARRAYIDX]]
27 ; CHECK-NEXT: br label [[FOR_INC]]
29 ; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I]], 1
30 ; CHECK-NEXT: [[EXITCOND1:%.*]] = icmp eq i64 [[INC]], [[N]]
31 ; CHECK-NEXT: br i1 [[EXITCOND1]], label [[RETURN_LOOPEXIT:%.*]], label [[LOOP]]
32 ; CHECK: return.loopexit:
33 ; CHECK-NEXT: br label [[RETURN]]
35 ; CHECK-NEXT: ret void
38 %cmp9 = icmp sgt i64 %n, 0
39 br i1 %cmp9, label %pre, label %return
42 %t3 = load i32, i32* %p
43 %tobool.not = icmp ne i32 %t3, 0
47 %i = phi i64 [ 0, %pre ], [ %inc, %for.inc ]
48 %cmp6 = icmp slt i64 %i, %n
49 %cond = and i1 %tobool.not, %cmp6
50 br i1 %cond, label %if.then, label %for.inc
53 %arrayidx = getelementptr [0 x double], [0 x double]* @X, i64 0, i64 %i
54 store double 3.200000e+00, double* %arrayidx
58 %inc = add nsw i64 %i, 1
59 %exitcond = icmp sge i64 %inc, %n
60 br i1 %exitcond, label %return, label %loop
66 ; Don't eliminate an icmp that's contributing to the loop exit test though.
68 define i32 @_ZNK4llvm5APInt3ultERKS0_(i32 %tmp2.i1, i64** %tmp65, i64** %tmp73, i64** %tmp82, i64** %tmp90) {
69 ; CHECK-LABEL: @_ZNK4llvm5APInt3ultERKS0_(
71 ; CHECK-NEXT: br label [[BB18:%.*]]
73 ; CHECK-NEXT: [[TMP66:%.*]] = load i64*, i64** [[TMP65:%.*]], align 4
74 ; CHECK-NEXT: [[TMP68:%.*]] = getelementptr inbounds i64, i64* [[TMP66]], i32 [[I:%.*]]
75 ; CHECK-NEXT: [[TMP69:%.*]] = load i64, i64* [[TMP68]], align 4
76 ; CHECK-NEXT: [[TMP74:%.*]] = load i64*, i64** [[TMP73:%.*]], align 4
77 ; CHECK-NEXT: [[TMP76:%.*]] = getelementptr inbounds i64, i64* [[TMP74]], i32 [[I]]
78 ; CHECK-NEXT: [[TMP77:%.*]] = load i64, i64* [[TMP76]], align 4
79 ; CHECK-NEXT: [[TMP78:%.*]] = icmp ugt i64 [[TMP69]], [[TMP77]]
80 ; CHECK-NEXT: br i1 [[TMP78]], label [[BB20_LOOPEXIT:%.*]], label [[BB15:%.*]]
82 ; CHECK-NEXT: [[TMP83:%.*]] = load i64*, i64** [[TMP82:%.*]], align 4
83 ; CHECK-NEXT: [[TMP85:%.*]] = getelementptr inbounds i64, i64* [[TMP83]], i32 [[I]]
84 ; CHECK-NEXT: [[TMP86:%.*]] = load i64, i64* [[TMP85]], align 4
85 ; CHECK-NEXT: [[TMP91:%.*]] = load i64*, i64** [[TMP90:%.*]], align 4
86 ; CHECK-NEXT: [[TMP93:%.*]] = getelementptr inbounds i64, i64* [[TMP91]], i32 [[I]]
87 ; CHECK-NEXT: [[TMP94:%.*]] = load i64, i64* [[TMP93]], align 4
88 ; CHECK-NEXT: [[TMP95:%.*]] = icmp ult i64 [[TMP86]], [[TMP94]]
89 ; CHECK-NEXT: br i1 [[TMP95]], label [[BB20_LOOPEXIT]], label [[BB17:%.*]]
91 ; CHECK-NEXT: [[TMP97:%.*]] = add nsw i32 [[I]], -1
92 ; CHECK-NEXT: br label [[BB18]]
94 ; CHECK-NEXT: [[I]] = phi i32 [ [[TMP2_I1:%.*]], [[ENTRY:%.*]] ], [ [[TMP97]], [[BB17]] ]
95 ; CHECK-NEXT: [[TMP99:%.*]] = icmp sgt i32 [[I]], -1
96 ; CHECK-NEXT: br i1 [[TMP99]], label [[BB13:%.*]], label [[BB20_LOOPEXIT]]
97 ; CHECK: bb20.loopexit:
98 ; CHECK-NEXT: [[TMP_0_PH:%.*]] = phi i32 [ 0, [[BB18]] ], [ 1, [[BB15]] ], [ 0, [[BB13]] ]
99 ; CHECK-NEXT: ret i32 [[TMP_0_PH]]
105 %tmp66 = load i64*, i64** %tmp65, align 4
106 %tmp68 = getelementptr inbounds i64, i64* %tmp66, i32 %i
107 %tmp69 = load i64, i64* %tmp68, align 4
108 %tmp74 = load i64*, i64** %tmp73, align 4
109 %tmp76 = getelementptr inbounds i64, i64* %tmp74, i32 %i
110 %tmp77 = load i64, i64* %tmp76, align 4
111 %tmp78 = icmp ugt i64 %tmp69, %tmp77
112 br i1 %tmp78, label %bb20.loopexit, label %bb15
115 %tmp83 = load i64*, i64** %tmp82, align 4
116 %tmp85 = getelementptr inbounds i64, i64* %tmp83, i32 %i
117 %tmp86 = load i64, i64* %tmp85, align 4
118 %tmp91 = load i64*, i64** %tmp90, align 4
119 %tmp93 = getelementptr inbounds i64, i64* %tmp91, i32 %i
120 %tmp94 = load i64, i64* %tmp93, align 4
121 %tmp95 = icmp ult i64 %tmp86, %tmp94
122 br i1 %tmp95, label %bb20.loopexit, label %bb17
125 %tmp97 = add nsw i32 %i, -1
129 %i = phi i32 [ %tmp2.i1, %entry ], [ %tmp97, %bb17 ]
130 %tmp99 = icmp sgt i32 %i, -1
131 br i1 %tmp99, label %bb13, label %bb20.loopexit
134 %tmp.0.ph = phi i32 [ 0, %bb18 ], [ 1, %bb15 ], [ 0, %bb13 ]
138 ; Indvars should eliminate the icmp here.
141 define void @func_10() nounwind {
142 ; CHECK-LABEL: @func_10(
144 ; CHECK-NEXT: br label [[LOOP:%.*]]
146 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
147 ; CHECK-NEXT: store i64 [[INDVARS_IV]], i64* null
148 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
149 ; CHECK-NEXT: br i1 false, label [[LOOP]], label [[RETURN:%.*]]
151 ; CHECK-NEXT: ret void
157 %i = phi i32 [ %i.next, %loop ], [ 0, %entry ]
158 %t0 = icmp slt i32 %i, 0
159 %t1 = zext i1 %t0 to i32
160 %t2 = add i32 %t1, %i
161 %u3 = zext i32 %t2 to i64
162 store i64 %u3, i64* null
163 %i.next = add i32 %i, 1
164 br i1 undef, label %loop, label %return
171 ; Indvars should not turn the second loop into an infinite one.
174 define i32 @func_11() nounwind uwtable {
175 ; CHECK-LABEL: @func_11(
177 ; CHECK-NEXT: br label [[FORCOND:%.*]]
179 ; CHECK-NEXT: [[__KEY6_0:%.*]] = phi i32 [ 2, [[ENTRY:%.*]] ], [ [[TMP37:%.*]], [[NOASSERT:%.*]] ]
180 ; CHECK-NEXT: [[EXITCOND1:%.*]] = icmp ne i32 [[__KEY6_0]], 10
181 ; CHECK-NEXT: br i1 [[EXITCOND1]], label [[NOASSERT]], label [[FORCOND38_PREHEADER:%.*]]
182 ; CHECK: forcond38.preheader:
183 ; CHECK-NEXT: br label [[FORCOND38:%.*]]
185 ; CHECK-NEXT: [[TMP13:%.*]] = sdiv i32 -32768, [[__KEY6_0]]
186 ; CHECK-NEXT: [[TMP2936:%.*]] = shl i32 [[TMP13]], 24
187 ; CHECK-NEXT: [[SEXT23:%.*]] = shl i32 [[TMP13]], 24
188 ; CHECK-NEXT: [[TMP32:%.*]] = icmp eq i32 [[TMP2936]], [[SEXT23]]
189 ; CHECK-NEXT: [[TMP37]] = add nuw nsw i32 [[__KEY6_0]], 1
190 ; CHECK-NEXT: br i1 [[TMP32]], label [[FORCOND]], label [[ASSERT33:%.*]]
192 ; CHECK-NEXT: tail call void @llvm.trap()
193 ; CHECK-NEXT: unreachable
195 ; CHECK-NEXT: [[__KEY8_0:%.*]] = phi i32 [ [[TMP81:%.*]], [[NOASSERT68:%.*]] ], [ 2, [[FORCOND38_PREHEADER]] ]
196 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[__KEY8_0]], 10
197 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[NOASSERT68]], label [[UNROLLEDEND:%.*]]
199 ; CHECK-NEXT: [[TMP57:%.*]] = sdiv i32 -32768, [[__KEY8_0]]
200 ; CHECK-NEXT: [[SEXT34:%.*]] = shl i32 [[TMP57]], 16
201 ; CHECK-NEXT: [[SEXT21:%.*]] = shl i32 [[TMP57]], 16
202 ; CHECK-NEXT: [[TMP76:%.*]] = icmp eq i32 [[SEXT34]], [[SEXT21]]
203 ; CHECK-NEXT: [[TMP81]] = add nuw nsw i32 [[__KEY8_0]], 1
204 ; CHECK-NEXT: br i1 [[TMP76]], label [[FORCOND38]], label [[ASSERT77:%.*]]
206 ; CHECK-NEXT: tail call void @llvm.trap()
207 ; CHECK-NEXT: unreachable
208 ; CHECK: unrolledend:
209 ; CHECK-NEXT: ret i32 0
214 forcond: ; preds = %noassert, %entry
215 %__key6.0 = phi i32 [ 2, %entry ], [ %tmp37, %noassert ]
216 %tmp5 = icmp slt i32 %__key6.0, 10
217 br i1 %tmp5, label %noassert, label %forcond38.preheader
219 forcond38.preheader: ; preds = %forcond
222 noassert: ; preds = %forbody
223 %tmp13 = sdiv i32 -32768, %__key6.0
224 %tmp2936 = shl i32 %tmp13, 24
225 %sext23 = shl i32 %tmp13, 24
226 %tmp32 = icmp eq i32 %tmp2936, %sext23
227 %tmp37 = add i32 %__key6.0, 1
228 br i1 %tmp32, label %forcond, label %assert33
230 assert33: ; preds = %noassert
231 tail call void @llvm.trap()
234 forcond38: ; preds = %noassert68, %forcond38.preheader
235 %__key8.0 = phi i32 [ %tmp81, %noassert68 ], [ 2, %forcond38.preheader ]
236 %tmp46 = icmp slt i32 %__key8.0, 10
237 br i1 %tmp46, label %noassert68, label %unrolledend
239 noassert68: ; preds = %forbody39
240 %tmp57 = sdiv i32 -32768, %__key8.0
241 %sext34 = shl i32 %tmp57, 16
242 %sext21 = shl i32 %tmp57, 16
243 %tmp76 = icmp eq i32 %sext34, %sext21
244 %tmp81 = add i32 %__key8.0, 1
245 br i1 %tmp76, label %forcond38, label %assert77
247 assert77: ; preds = %noassert68
248 tail call void @llvm.trap()
251 unrolledend: ; preds = %forcond38
255 declare void @llvm.trap() noreturn nounwind
257 ; In this case the second loop only has a single iteration, fold the header away
258 define i32 @func_12() nounwind uwtable {
259 ; CHECK-LABEL: @func_12(
261 ; CHECK-NEXT: br label [[FORCOND:%.*]]
263 ; CHECK-NEXT: [[__KEY6_0:%.*]] = phi i32 [ 2, [[ENTRY:%.*]] ], [ [[TMP37:%.*]], [[NOASSERT:%.*]] ]
264 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[__KEY6_0]], 10
265 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[NOASSERT]], label [[FORCOND38_PREHEADER:%.*]]
266 ; CHECK: forcond38.preheader:
267 ; CHECK-NEXT: br label [[FORCOND38:%.*]]
269 ; CHECK-NEXT: [[TMP13:%.*]] = sdiv i32 -32768, [[__KEY6_0]]
270 ; CHECK-NEXT: [[TMP2936:%.*]] = shl i32 [[TMP13]], 24
271 ; CHECK-NEXT: [[SEXT23:%.*]] = shl i32 [[TMP13]], 24
272 ; CHECK-NEXT: [[TMP32:%.*]] = icmp eq i32 [[TMP2936]], [[SEXT23]]
273 ; CHECK-NEXT: [[TMP37]] = add nuw nsw i32 [[__KEY6_0]], 1
274 ; CHECK-NEXT: br i1 [[TMP32]], label [[FORCOND]], label [[ASSERT33:%.*]]
276 ; CHECK-NEXT: tail call void @llvm.trap()
277 ; CHECK-NEXT: unreachable
279 ; CHECK-NEXT: br i1 true, label [[NOASSERT68:%.*]], label [[UNROLLEDEND:%.*]]
281 ; CHECK-NEXT: br i1 false, label [[FORCOND38]], label [[ASSERT77:%.*]]
283 ; CHECK-NEXT: tail call void @llvm.trap()
284 ; CHECK-NEXT: unreachable
285 ; CHECK: unrolledend:
286 ; CHECK-NEXT: ret i32 0
291 forcond: ; preds = %noassert, %entry
292 %__key6.0 = phi i32 [ 2, %entry ], [ %tmp37, %noassert ]
293 %tmp5 = icmp slt i32 %__key6.0, 10
294 br i1 %tmp5, label %noassert, label %forcond38.preheader
296 forcond38.preheader: ; preds = %forcond
299 noassert: ; preds = %forbody
300 %tmp13 = sdiv i32 -32768, %__key6.0
301 %tmp2936 = shl i32 %tmp13, 24
302 %sext23 = shl i32 %tmp13, 24
303 %tmp32 = icmp eq i32 %tmp2936, %sext23
304 %tmp37 = add i32 %__key6.0, 1
305 br i1 %tmp32, label %forcond, label %assert33
307 assert33: ; preds = %noassert
308 tail call void @llvm.trap()
311 forcond38: ; preds = %noassert68, %forcond38.preheader
312 %__key8.0 = phi i32 [ %tmp81, %noassert68 ], [ 2, %forcond38.preheader ]
313 %tmp46 = icmp slt i32 %__key8.0, 10
314 br i1 %tmp46, label %noassert68, label %unrolledend
316 noassert68: ; preds = %forbody39
317 %tmp57 = sdiv i32 -32768, %__key8.0
318 %sext34 = shl i32 %tmp57, 16
319 %sext21 = shl i32 %tmp57, 16
320 %tmp76 = icmp ne i32 %sext34, %sext21
321 %tmp81 = add i32 %__key8.0, 1
322 br i1 %tmp76, label %forcond38, label %assert77
324 assert77: ; preds = %noassert68
325 tail call void @llvm.trap()
328 unrolledend: ; preds = %forcond38
332 declare void @side_effect()
334 define void @func_13(i32* %len.ptr) {
335 ; CHECK-LABEL: @func_13(
337 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_PTR:%.*]], !range !0
338 ; CHECK-NEXT: [[LEN_IS_ZERO:%.*]] = icmp eq i32 [[LEN]], 0
339 ; CHECK-NEXT: br i1 [[LEN_IS_ZERO]], label [[LEAVE:%.*]], label [[LOOP_PREHEADER:%.*]]
340 ; CHECK: loop.preheader:
341 ; CHECK-NEXT: br label [[LOOP:%.*]]
343 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
344 ; CHECK-NEXT: call void @side_effect()
345 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
346 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
348 ; CHECK-NEXT: call void @side_effect()
349 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LEN]]
350 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
351 ; CHECK: leave.loopexit:
352 ; CHECK-NEXT: br label [[LEAVE]]
354 ; CHECK-NEXT: ret void
357 %len = load i32, i32* %len.ptr, !range !0
358 %len.sub.1 = add i32 %len, -1
359 %len.is.zero = icmp eq i32 %len, 0
360 br i1 %len.is.zero, label %leave, label %loop
363 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
364 call void @side_effect()
365 %iv.inc = add i32 %iv, 1
366 %iv.cmp = icmp ult i32 %iv, %len
367 br i1 %iv.cmp, label %be, label %leave
370 call void @side_effect()
371 %be.cond = icmp ult i32 %iv, %len.sub.1
372 br i1 %be.cond, label %loop, label %leave
378 define void @func_14(i32* %len.ptr) {
379 ; CHECK-LABEL: @func_14(
381 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_PTR:%.*]], !range !0
382 ; CHECK-NEXT: [[LEN_IS_ZERO:%.*]] = icmp eq i32 [[LEN]], 0
383 ; CHECK-NEXT: [[LEN_IS_INT_MIN:%.*]] = icmp eq i32 [[LEN]], -2147483648
384 ; CHECK-NEXT: [[NO_ENTRY:%.*]] = or i1 [[LEN_IS_ZERO]], [[LEN_IS_INT_MIN]]
385 ; CHECK-NEXT: br i1 [[NO_ENTRY]], label [[LEAVE:%.*]], label [[LOOP_PREHEADER:%.*]]
386 ; CHECK: loop.preheader:
387 ; CHECK-NEXT: br label [[LOOP:%.*]]
389 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
390 ; CHECK-NEXT: call void @side_effect()
391 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
392 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
394 ; CHECK-NEXT: call void @side_effect()
395 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LEN]]
396 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
397 ; CHECK: leave.loopexit:
398 ; CHECK-NEXT: br label [[LEAVE]]
400 ; CHECK-NEXT: ret void
403 %len = load i32, i32* %len.ptr, !range !0
404 %len.sub.1 = add i32 %len, -1
405 %len.is.zero = icmp eq i32 %len, 0
406 %len.is.int_min = icmp eq i32 %len, 2147483648
407 %no.entry = or i1 %len.is.zero, %len.is.int_min
408 br i1 %no.entry, label %leave, label %loop
411 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
412 call void @side_effect()
413 %iv.inc = add i32 %iv, 1
414 %iv.cmp = icmp slt i32 %iv, %len
415 br i1 %iv.cmp, label %be, label %leave
418 call void @side_effect()
419 %be.cond = icmp slt i32 %iv, %len.sub.1
420 br i1 %be.cond, label %loop, label %leave
426 define void @func_15(i32* %len.ptr) {
427 ; CHECK-LABEL: @func_15(
429 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_PTR:%.*]], !range !0
430 ; CHECK-NEXT: [[LEN_ADD_1:%.*]] = add i32 [[LEN]], 1
431 ; CHECK-NEXT: [[LEN_ADD_1_IS_ZERO:%.*]] = icmp eq i32 [[LEN_ADD_1]], 0
432 ; CHECK-NEXT: br i1 [[LEN_ADD_1_IS_ZERO]], label [[LEAVE:%.*]], label [[LOOP_PREHEADER:%.*]]
433 ; CHECK: loop.preheader:
434 ; CHECK-NEXT: br label [[LOOP:%.*]]
436 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
437 ; CHECK-NEXT: call void @side_effect()
438 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
439 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
441 ; CHECK-NEXT: call void @side_effect()
442 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LEN_ADD_1]]
443 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
444 ; CHECK: leave.loopexit:
445 ; CHECK-NEXT: br label [[LEAVE]]
447 ; CHECK-NEXT: ret void
450 %len = load i32, i32* %len.ptr, !range !0
451 %len.add.1 = add i32 %len, 1
452 %len.add.1.is.zero = icmp eq i32 %len.add.1, 0
453 br i1 %len.add.1.is.zero, label %leave, label %loop
456 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
457 call void @side_effect()
458 %iv.inc = add i32 %iv, 1
459 %iv.cmp = icmp ult i32 %iv, %len.add.1
460 br i1 %iv.cmp, label %be, label %leave
463 call void @side_effect()
464 %be.cond = icmp ult i32 %iv, %len
465 br i1 %be.cond, label %loop, label %leave
471 define void @func_16(i32* %len.ptr) {
472 ; CHECK-LABEL: @func_16(
474 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_PTR:%.*]], !range !0
475 ; CHECK-NEXT: [[LEN_ADD_5:%.*]] = add i32 [[LEN]], 5
476 ; CHECK-NEXT: [[ENTRY_COND_0:%.*]] = icmp slt i32 [[LEN]], 2147483643
477 ; CHECK-NEXT: [[ENTRY_COND_1:%.*]] = icmp slt i32 4, [[LEN_ADD_5]]
478 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = and i1 [[ENTRY_COND_0]], [[ENTRY_COND_1]]
479 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
480 ; CHECK: loop.preheader:
481 ; CHECK-NEXT: [[TMP0:%.*]] = add nuw nsw i32 [[LEN]], 1
482 ; CHECK-NEXT: br label [[LOOP:%.*]]
484 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
485 ; CHECK-NEXT: call void @side_effect()
486 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
487 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
489 ; CHECK-NEXT: call void @side_effect()
490 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[TMP0]]
491 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
492 ; CHECK: leave.loopexit:
493 ; CHECK-NEXT: br label [[LEAVE]]
495 ; CHECK-NEXT: ret void
498 %len = load i32, i32* %len.ptr, !range !0
499 %len.add.5 = add i32 %len, 5
500 %entry.cond.0 = icmp slt i32 %len, 2147483643
501 %entry.cond.1 = icmp slt i32 4, %len.add.5
502 %entry.cond = and i1 %entry.cond.0, %entry.cond.1
503 br i1 %entry.cond, label %loop, label %leave
506 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
507 call void @side_effect()
508 %iv.inc = add i32 %iv, 1
509 %iv.add.4 = add i32 %iv, 4
510 %iv.cmp = icmp slt i32 %iv.add.4, %len.add.5
511 br i1 %iv.cmp, label %be, label %leave
514 call void @side_effect()
515 %be.cond = icmp slt i32 %iv, %len
516 br i1 %be.cond, label %loop, label %leave
522 define void @func_17(i32* %len.ptr) {
523 ; CHECK-LABEL: @func_17(
525 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_PTR:%.*]]
526 ; CHECK-NEXT: [[LEN_ADD_5:%.*]] = add i32 [[LEN]], -5
527 ; CHECK-NEXT: [[ENTRY_COND_0:%.*]] = icmp slt i32 [[LEN]], -2147483643
528 ; CHECK-NEXT: [[ENTRY_COND_1:%.*]] = icmp slt i32 -6, [[LEN_ADD_5]]
529 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = and i1 [[ENTRY_COND_0]], [[ENTRY_COND_1]]
530 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
531 ; CHECK: loop.preheader:
532 ; CHECK-NEXT: br label [[LOOP:%.*]]
534 ; CHECK-NEXT: [[IV_2:%.*]] = phi i32 [ [[IV_2_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
535 ; CHECK-NEXT: call void @side_effect()
536 ; CHECK-NEXT: [[IV_2_INC]] = add nuw i32 [[IV_2]], 1
537 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
539 ; CHECK-NEXT: call void @side_effect()
540 ; CHECK-NEXT: [[BE_COND:%.*]] = icmp slt i32 [[IV_2]], [[LEN]]
541 ; CHECK-NEXT: br i1 [[BE_COND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
542 ; CHECK: leave.loopexit:
543 ; CHECK-NEXT: br label [[LEAVE]]
545 ; CHECK-NEXT: ret void
548 %len = load i32, i32* %len.ptr
549 %len.add.5 = add i32 %len, -5
550 %entry.cond.0 = icmp slt i32 %len, 2147483653 ;; 2147483653 == INT_MIN - (-5)
551 %entry.cond.1 = icmp slt i32 -6, %len.add.5
552 %entry.cond = and i1 %entry.cond.0, %entry.cond.1
553 br i1 %entry.cond, label %loop, label %leave
556 %iv.2 = phi i32 [ 0, %entry ], [ %iv.2.inc, %be ]
557 %iv = phi i32 [ -6, %entry ], [ %iv.inc, %be ]
558 call void @side_effect()
559 %iv.inc = add i32 %iv, 1
560 %iv.2.inc = add i32 %iv.2, 1
561 %iv.cmp = icmp slt i32 %iv, %len.add.5
563 ; Deduces {-5,+,1} s< (-5 + %len) from {0,+,1} < %len
564 ; since %len s< INT_MIN - (-5) from the entry condition
565 br i1 %iv.cmp, label %be, label %leave
568 call void @side_effect()
569 %be.cond = icmp slt i32 %iv.2, %len
570 br i1 %be.cond, label %loop, label %leave
576 define i1 @func_18(i16* %tmp20, i32* %len.addr) {
577 ; CHECK-LABEL: @func_18(
579 ; CHECK-NEXT: [[LEN:%.*]] = load i32, i32* [[LEN_ADDR:%.*]], !range !0
580 ; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i32 [[LEN]], 0
581 ; CHECK-NEXT: br i1 [[TMP18]], label [[BB2:%.*]], label [[BB0_PREHEADER:%.*]]
582 ; CHECK: bb0.preheader:
583 ; CHECK-NEXT: br label [[BB0:%.*]]
585 ; CHECK-NEXT: [[VAR_0_IN:%.*]] = phi i32 [ [[VAR_0:%.*]], [[BB1:%.*]] ], [ [[LEN]], [[BB0_PREHEADER]] ]
586 ; CHECK-NEXT: [[VAR_1:%.*]] = phi i32 [ [[TMP30:%.*]], [[BB1]] ], [ 0, [[BB0_PREHEADER]] ]
587 ; CHECK-NEXT: [[VAR_0]] = add nsw i32 [[VAR_0_IN]], -1
588 ; CHECK-NEXT: br i1 true, label [[STAY:%.*]], label [[BB2_LOOPEXIT:%.*]]
590 ; CHECK-NEXT: [[TMP25:%.*]] = getelementptr inbounds i16, i16* [[TMP20:%.*]], i32 [[VAR_1]]
591 ; CHECK-NEXT: [[TMP26:%.*]] = load i16, i16* [[TMP25]]
592 ; CHECK-NEXT: [[TMP29:%.*]] = icmp eq i16 [[TMP26]], 0
593 ; CHECK-NEXT: br i1 [[TMP29]], label [[BB1]], label [[BB2_LOOPEXIT]]
595 ; CHECK-NEXT: [[TMP30]] = add nuw i32 [[VAR_1]], 1
596 ; CHECK-NEXT: [[TMP31:%.*]] = icmp eq i32 [[VAR_0]], 0
597 ; CHECK-NEXT: br i1 [[TMP31]], label [[BB3:%.*]], label [[BB0]]
598 ; CHECK: bb2.loopexit:
599 ; CHECK-NEXT: br label [[BB2]]
601 ; CHECK-NEXT: ret i1 false
603 ; CHECK-NEXT: ret i1 true
606 %len = load i32, i32* %len.addr, !range !0
607 %tmp18 = icmp eq i32 %len, 0
608 br i1 %tmp18, label %bb2, label %bb0.preheader
614 %var_0.in = phi i32 [ %var_0, %bb1 ], [ %len, %bb0.preheader ]
615 %var_1 = phi i32 [ %tmp30, %bb1 ], [ 0, %bb0.preheader ]
616 %var_0 = add nsw i32 %var_0.in, -1
617 %tmp23 = icmp ult i32 %var_1, %len
618 br i1 %tmp23, label %stay, label %bb2
621 %tmp25 = getelementptr inbounds i16, i16* %tmp20, i32 %var_1
622 %tmp26 = load i16, i16* %tmp25
623 %tmp29 = icmp eq i16 %tmp26, 0
624 br i1 %tmp29, label %bb1, label %bb2
627 %tmp30 = add i32 %var_1, 1
628 %tmp31 = icmp eq i32 %var_0, 0
629 br i1 %tmp31, label %bb3, label %bb0
638 define void @func_19(i32* %length.ptr) {
639 ; CHECK-LABEL: @func_19(
641 ; CHECK-NEXT: [[LENGTH:%.*]] = load i32, i32* [[LENGTH_PTR:%.*]], !range !0
642 ; CHECK-NEXT: [[LENGTH_IS_NONZERO:%.*]] = icmp ne i32 [[LENGTH]], 0
643 ; CHECK-NEXT: br i1 [[LENGTH_IS_NONZERO]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
644 ; CHECK: loop.preheader:
645 ; CHECK-NEXT: br label [[LOOP:%.*]]
647 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
648 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
649 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
651 ; CHECK-NEXT: call void @side_effect()
652 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LENGTH]]
653 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
654 ; CHECK: leave.loopexit:
655 ; CHECK-NEXT: br label [[LEAVE]]
657 ; CHECK-NEXT: ret void
660 %length = load i32, i32* %length.ptr, !range !0
661 %length.is.nonzero = icmp ne i32 %length, 0
662 br i1 %length.is.nonzero, label %loop, label %leave
665 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
666 %iv.inc = add i32 %iv, 1
667 %range.check = icmp ult i32 %iv, %length
668 br i1 %range.check, label %be, label %leave
671 call void @side_effect()
672 %be.cond = icmp slt i32 %iv.inc, %length
673 br i1 %be.cond, label %loop, label %leave
679 ; Like @func_19, but %length is no longer provably positive, so
680 ; %range.check cannot be proved to be always true.
681 define void @func_20(i32* %length.ptr) {
682 ; CHECK-LABEL: @func_20(
684 ; CHECK-NEXT: [[LENGTH:%.*]] = load i32, i32* [[LENGTH_PTR:%.*]]
685 ; CHECK-NEXT: [[LENGTH_IS_NONZERO:%.*]] = icmp ne i32 [[LENGTH]], 0
686 ; CHECK-NEXT: br i1 [[LENGTH_IS_NONZERO]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
687 ; CHECK: loop.preheader:
688 ; CHECK-NEXT: br label [[LOOP:%.*]]
690 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
691 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
692 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV]], [[LENGTH]]
693 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
695 ; CHECK-NEXT: call void @side_effect()
696 ; CHECK-NEXT: [[BE_COND:%.*]] = icmp slt i32 [[IV_INC]], [[LENGTH]]
697 ; CHECK-NEXT: br i1 [[BE_COND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
698 ; CHECK: leave.loopexit:
699 ; CHECK-NEXT: br label [[LEAVE]]
701 ; CHECK-NEXT: ret void
704 %length = load i32, i32* %length.ptr
705 %length.is.nonzero = icmp ne i32 %length, 0
706 br i1 %length.is.nonzero, label %loop, label %leave
709 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
710 %iv.inc = add i32 %iv, 1
711 %range.check = icmp ult i32 %iv, %length
712 br i1 %range.check, label %be, label %leave
715 call void @side_effect()
716 %be.cond = icmp slt i32 %iv.inc, %length
717 br i1 %be.cond, label %loop, label %leave
723 ; This checks that the backedge condition, (I + 1) < Length - 1 implies
725 define void @func_21(i32* %length.ptr) {
726 ; CHECK-LABEL: @func_21(
728 ; CHECK-NEXT: [[LENGTH:%.*]] = load i32, i32* [[LENGTH_PTR:%.*]], !range !0
729 ; CHECK-NEXT: [[LIM:%.*]] = sub i32 [[LENGTH]], 1
730 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp sgt i32 [[LENGTH]], 1
731 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
732 ; CHECK: loop.preheader:
733 ; CHECK-NEXT: br label [[LOOP:%.*]]
735 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
736 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
737 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
739 ; CHECK-NEXT: call void @side_effect()
740 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LIM]]
741 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
742 ; CHECK: leave.loopexit:
743 ; CHECK-NEXT: br label [[LEAVE]]
745 ; CHECK-NEXT: ret void
748 %length = load i32, i32* %length.ptr, !range !0
749 %lim = sub i32 %length, 1
750 %entry.cond = icmp sgt i32 %length, 1
751 br i1 %entry.cond, label %loop, label %leave
754 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
755 %iv.inc = add i32 %iv, 1
756 %range.check = icmp slt i32 %iv, %length
757 br i1 %range.check, label %be, label %leave
760 call void @side_effect()
761 %be.cond = icmp slt i32 %iv.inc, %lim
762 br i1 %be.cond, label %loop, label %leave
768 ; This checks that the backedge condition, (I + 1) < Length - 1 implies
770 define void @func_22(i32* %length.ptr) {
771 ; CHECK-LABEL: @func_22(
773 ; CHECK-NEXT: [[LENGTH:%.*]] = load i32, i32* [[LENGTH_PTR:%.*]], !range !0
774 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp sgt i32 [[LENGTH]], 1
775 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
776 ; CHECK: loop.preheader:
777 ; CHECK-NEXT: br label [[LOOP:%.*]]
779 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
780 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
781 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
783 ; CHECK-NEXT: call void @side_effect()
784 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LENGTH]]
785 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
786 ; CHECK: leave.loopexit:
787 ; CHECK-NEXT: br label [[LEAVE]]
789 ; CHECK-NEXT: ret void
792 %length = load i32, i32* %length.ptr, !range !0
793 %lim = sub i32 %length, 1
794 %entry.cond = icmp sgt i32 %length, 1
795 br i1 %entry.cond, label %loop, label %leave
798 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
799 %iv.inc = add i32 %iv, 1
800 %range.check = icmp sle i32 %iv, %length
801 br i1 %range.check, label %be, label %leave
804 call void @side_effect()
805 %be.cond = icmp sle i32 %iv.inc, %lim
806 br i1 %be.cond, label %loop, label %leave
812 define void @func_23(i32* %length.ptr) {
813 ; CHECK-LABEL: @func_23(
815 ; CHECK-NEXT: [[LENGTH:%.*]] = load i32, i32* [[LENGTH_PTR:%.*]], !range !0
816 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp ult i32 4, [[LENGTH]]
817 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
818 ; CHECK: loop.preheader:
819 ; CHECK-NEXT: br label [[LOOP:%.*]]
821 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_INC:%.*]], [[BE:%.*]] ], [ 4, [[LOOP_PREHEADER]] ]
822 ; CHECK-NEXT: [[IV_INC]] = add nuw nsw i32 [[IV]], 1
823 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
825 ; CHECK-NEXT: call void @side_effect()
826 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_INC]], [[LENGTH]]
827 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
828 ; CHECK: leave.loopexit:
829 ; CHECK-NEXT: br label [[LEAVE]]
831 ; CHECK-NEXT: ret void
834 %length = load i32, i32* %length.ptr, !range !0
835 %entry.cond = icmp ult i32 4, %length
836 br i1 %entry.cond, label %loop, label %leave
839 %iv = phi i32 [ 4, %entry ], [ %iv.inc, %be ]
840 %iv.inc = add i32 %iv, 1
841 %range.check = icmp slt i32 %iv, %length
842 br i1 %range.check, label %be, label %leave
845 call void @side_effect()
846 %be.cond = icmp slt i32 %iv.inc, %length
847 br i1 %be.cond, label %loop, label %leave
853 define void @func_24(i32* %init.ptr) {
854 ; CHECK-LABEL: @func_24(
856 ; CHECK-NEXT: [[INIT:%.*]] = load i32, i32* [[INIT_PTR:%.*]], !range !0
857 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp ugt i32 [[INIT]], 4
858 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
859 ; CHECK: loop.preheader:
860 ; CHECK-NEXT: br label [[LOOP:%.*]]
862 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_DEC:%.*]], [[BE:%.*]] ], [ [[INIT]], [[LOOP_PREHEADER]] ]
863 ; CHECK-NEXT: [[IV_DEC]] = add nsw i32 [[IV]], -1
864 ; CHECK-NEXT: br i1 true, label [[BE]], label [[LEAVE_LOOPEXIT:%.*]]
866 ; CHECK-NEXT: call void @side_effect()
867 ; CHECK-NEXT: [[BE_COND:%.*]] = icmp sgt i32 [[IV_DEC]], 4
868 ; CHECK-NEXT: br i1 [[BE_COND]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
869 ; CHECK: leave.loopexit:
870 ; CHECK-NEXT: br label [[LEAVE]]
872 ; CHECK-NEXT: ret void
875 %init = load i32, i32* %init.ptr, !range !0
876 %entry.cond = icmp ugt i32 %init, 4
877 br i1 %entry.cond, label %loop, label %leave
880 %iv = phi i32 [ %init, %entry ], [ %iv.dec, %be ]
881 %iv.dec = add i32 %iv, -1
882 %range.check = icmp sgt i32 %iv, 4
883 br i1 %range.check, label %be, label %leave
886 call void @side_effect()
887 %be.cond = icmp sgt i32 %iv.dec, 4
888 br i1 %be.cond, label %loop, label %leave
894 !0 = !{i32 0, i32 2147483647}