1 ; RUN: opt -verify-loop-info -irce-print-changed-loops -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, i32 addrspace(1)* undef, align 8
17 %tmp21 = zext i32 %length.i167 to i64
18 %tmp34 = load atomic i32, i32 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, i8 addrspace(1)* undef, i64 8
29 ; CHECK-NEXT: %length_gep_typed.i147 = bitcast i8 addrspace(1)* undef to i32 addrspace(1)*
30 ; CHECK-NEXT: %tmp43 = icmp ult i64 %indvars.iv.next467, %tmp21
31 ; CHECK-NEXT: %exit.mainloop.at = call i64 @llvm.umax.i64(i64 %tmp21, i64 1)
32 ; CHECK-NEXT: [[C1:%[^ ]+]] = icmp ult i64 1, %exit.mainloop.at
33 ; CHECK-NEXT: br i1 [[C1]], label %loop.preheader, label %main.pseudo.exit
35 %length_gep.i146 = getelementptr inbounds i8, i8 addrspace(1)* undef, i64 8
36 %length_gep_typed.i147 = bitcast i8 addrspace(1)* undef to i32 addrspace(1)*
37 %tmp43 = icmp ult i64 %indvars.iv.next467, %tmp21
40 not_zero: ; preds = %in_bounds
42 ; CHECK: %tmp56 = icmp ult i64 %indvars.iv.next, %tmp21
43 ; CHECK-NEXT: [[COND:%[^ ]+]] = icmp ult i64 %indvars.iv.next, %exit.mainloop.at
44 ; CHECK-NEXT: br i1 [[COND]], label %loop, label %main.exit.selector
46 %tmp51 = trunc i64 %indvars.iv.next to i32
47 %tmp53 = mul i32 %tmp51, %tmp51
48 %tmp54 = add i32 %tmp53, -9582
49 %tmp55 = add i32 %tmp54, %tmp62
50 %tmp56 = icmp ult i64 %indvars.iv.next, %tmp21
51 br i1 %tmp56, label %loop, label %exit
53 loop: ; preds = %not_zero, %preheader
54 %tmp62 = phi i32 [ 1, %preheader ], [ %tmp55, %not_zero ]
55 %indvars.iv750 = phi i64 [ 1, %preheader ], [ %indvars.iv.next, %not_zero ]
56 %length.i148 = load i32, i32 addrspace(1)* %length_gep_typed.i147, align 8
57 %tmp68 = zext i32 %length.i148 to i64
58 %tmp97 = icmp ult i64 2, %tmp68
59 %or.cond = and i1 %tmp43, %tmp97
60 %tmp99 = icmp ult i64 %indvars.iv750, %tmp21
61 %or.cond1 = and i1 %or.cond, %tmp99
62 br i1 %or.cond1, label %in_bounds, label %exit
64 in_bounds: ; preds = %loop
65 %indvars.iv.next = add nuw nsw i64 %indvars.iv750, 3
66 %tmp107 = icmp ult i64 %indvars.iv.next, 2
67 br i1 %tmp107, label %not_zero, label %exit
70 define void @test_02() {
72 ; Now IRCE is smart enough to understand that the safe range here is empty.
73 ; Previously it executed the entire loop in safe preloop and never actually
74 ; entered the main loop.
79 loop: ; preds = %in_bounds, %entry
80 %iv1 = phi i64 [ 3, %entry ], [ %iv1.next, %in_bounds ]
81 %iv2 = phi i64 [ 4294967295, %entry ], [ %iv2.next, %in_bounds ]
82 %iv2.offset = add i64 %iv2, 1
83 %rc = icmp ult i64 %iv2.offset, 400
84 br i1 %rc, label %in_bounds, label %bci_321
86 bci_321: ; preds = %in_bounds, %loop
89 in_bounds: ; preds = %loop
90 %iv1.next = add nuw nsw i64 %iv1, 2
91 %iv2.next = add nuw nsw i64 %iv2, 2
92 %cond = icmp ugt i64 %iv1, 204
93 br i1 %cond, label %bci_321, label %loop