1 ; RUN: opt -verify-loop-info -irce-print-changed-loops -passes=irce -S < %s 2>&1 | FileCheck %s
2 ; RUN: opt -verify-loop-info -irce-print-changed-loops -passes='require<branch-prob>,irce' -S < %s 2>&1 | FileCheck %s
4 ; The test demonstrates that incorrect behavior of Clamp may lead to incorrect
5 ; calculation of post-loop exit condition.
7 ; CHECK-LABEL: irce: in function test_01: constrained Loop at depth 1 containing: %loop<header><exiting>,%in_bounds<exiting>,%not_zero<latch><exiting>
8 ; CHECK-NOT: irce: in function test_02: constrained Loop
10 define void @test_01() {
12 ; CHECK-LABEL: test_01
15 %indvars.iv.next467 = add nuw nsw i64 2, 1
16 %length.i167 = load i32, ptr addrspace(1) undef, align 8
17 %tmp21 = zext i32 %length.i167 to i64
18 %tmp34 = load atomic i32, ptr addrspace(1) undef unordered, align 4
19 %tmp35 = add i32 %tmp34, -9581
20 %tmp36 = icmp ugt i32 %length.i167, 1
21 br i1 %tmp36, label %preheader, label %exit
23 exit: ; preds = %in_bounds, %loop, %not_zero, %entry
26 preheader: ; preds = %entry
28 ; CHECK-NEXT: %length_gep.i146 = getelementptr inbounds i8, ptr addrspace(1) undef, i64 8
29 ; CHECK-NEXT: %tmp43 = icmp ult i64 %indvars.iv.next467, %tmp21
30 ; CHECK-NEXT: %exit.mainloop.at = call i64 @llvm.umax.i64(i64 %tmp21, i64 1)
31 ; CHECK-NEXT: [[C1:%[^ ]+]] = icmp ult i64 1, %exit.mainloop.at
32 ; CHECK-NEXT: br i1 [[C1]], label %loop.preheader, label %main.pseudo.exit
34 %length_gep.i146 = getelementptr inbounds i8, ptr addrspace(1) undef, i64 8
35 %tmp43 = icmp ult i64 %indvars.iv.next467, %tmp21
38 not_zero: ; preds = %in_bounds
40 ; CHECK: %tmp56 = icmp ult i64 %indvars.iv.next, %tmp21
41 ; CHECK-NEXT: [[COND:%[^ ]+]] = icmp ult i64 %indvars.iv.next, %exit.mainloop.at
42 ; CHECK-NEXT: br i1 [[COND]], label %loop, label %main.exit.selector
44 %tmp51 = trunc i64 %indvars.iv.next to i32
45 %tmp53 = mul i32 %tmp51, %tmp51
46 %tmp54 = add i32 %tmp53, -9582
47 %tmp55 = add i32 %tmp54, %tmp62
48 %tmp56 = icmp ult i64 %indvars.iv.next, %tmp21
49 br i1 %tmp56, label %loop, label %exit
51 loop: ; preds = %not_zero, %preheader
52 %tmp62 = phi i32 [ 1, %preheader ], [ %tmp55, %not_zero ]
53 %indvars.iv750 = phi i64 [ 1, %preheader ], [ %indvars.iv.next, %not_zero ]
54 %length.i148 = load i32, ptr addrspace(1) undef, align 8
55 %tmp68 = zext i32 %length.i148 to i64
56 %tmp97 = icmp ult i64 2, %tmp68
57 %or.cond = and i1 %tmp43, %tmp97
58 %tmp99 = icmp ult i64 %indvars.iv750, %tmp21
59 %or.cond1 = and i1 %or.cond, %tmp99
60 br i1 %or.cond1, label %in_bounds, label %exit
62 in_bounds: ; preds = %loop
63 %indvars.iv.next = add nuw nsw i64 %indvars.iv750, 3
64 %tmp107 = icmp ult i64 %indvars.iv.next, 2
65 br i1 %tmp107, label %not_zero, label %exit
68 define void @test_02() {
70 ; Now IRCE is smart enough to understand that the safe range here is empty.
71 ; Previously it executed the entire loop in safe preloop and never actually
72 ; entered the main loop.
77 loop: ; preds = %in_bounds, %entry
78 %iv1 = phi i64 [ 3, %entry ], [ %iv1.next, %in_bounds ]
79 %iv2 = phi i64 [ 4294967295, %entry ], [ %iv2.next, %in_bounds ]
80 %iv2.offset = add i64 %iv2, 1
81 %rc = icmp ult i64 %iv2.offset, 400
82 br i1 %rc, label %in_bounds, label %bci_321
84 bci_321: ; preds = %in_bounds, %loop
87 in_bounds: ; preds = %loop
88 %iv1.next = add nuw nsw i64 %iv1, 2
89 %iv2.next = add nuw nsw i64 %iv2, 2
90 %cond = icmp ugt i64 %iv1, 204
91 br i1 %cond, label %bci_321, label %loop