1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -loop-unroll %s | FileCheck %s
4 ; In both cases, we have one unpredictable exit and one IV-based exit with
5 ; known trip count. We can fully unroll against the latter. In one of the
6 ; examples the IV-based exit is the latch exit, in the other the non-latch
7 ; exit. After full unrolling, the functions fold to ret i1 true.
9 define i1 @test_latch() {
10 ; CHECK-LABEL: @test_latch(
12 ; CHECK-NEXT: [[A1:%.*]] = alloca [2 x i64], align 8
13 ; CHECK-NEXT: [[A2:%.*]] = alloca [2 x i64], align 8
14 ; CHECK-NEXT: [[A1_0:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 0
15 ; CHECK-NEXT: store i64 -5015437470765251660, i64* [[A1_0]], align 8
16 ; CHECK-NEXT: [[A1_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 1
17 ; CHECK-NEXT: store i64 -8661621401413125213, i64* [[A1_1]], align 8
18 ; CHECK-NEXT: [[A2_0:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 0
19 ; CHECK-NEXT: store i64 -5015437470765251660, i64* [[A2_0]], align 8
20 ; CHECK-NEXT: [[A2_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 1
21 ; CHECK-NEXT: store i64 -8661621401413125213, i64* [[A2_1]], align 8
22 ; CHECK-NEXT: br label [[LOOP:%.*]]
24 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 0
25 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 0
26 ; CHECK-NEXT: [[LOAD1:%.*]] = load i64, i64* [[GEP1]], align 8
27 ; CHECK-NEXT: [[LOAD2:%.*]] = load i64, i64* [[GEP2]], align 8
28 ; CHECK-NEXT: [[EXITCOND2:%.*]] = icmp eq i64 [[LOAD1]], [[LOAD2]]
29 ; CHECK-NEXT: br i1 [[EXITCOND2]], label [[LATCH:%.*]], label [[EXIT:%.*]]
31 ; CHECK-NEXT: [[GEP1_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 1
32 ; CHECK-NEXT: [[GEP2_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 1
33 ; CHECK-NEXT: [[LOAD1_1:%.*]] = load i64, i64* [[GEP1_1]], align 8
34 ; CHECK-NEXT: [[LOAD2_1:%.*]] = load i64, i64* [[GEP2_1]], align 8
35 ; CHECK-NEXT: [[EXITCOND2_1:%.*]] = icmp eq i64 [[LOAD1_1]], [[LOAD2_1]]
36 ; CHECK-NEXT: br i1 [[EXITCOND2_1]], label [[LATCH_1:%.*]], label [[EXIT]]
38 ; CHECK-NEXT: [[EXIT_VAL:%.*]] = phi i1 [ false, [[LOOP]] ], [ false, [[LATCH]] ], [ true, [[LATCH_1]] ]
39 ; CHECK-NEXT: ret i1 [[EXIT_VAL]]
41 ; CHECK-NEXT: br label [[EXIT]]
44 %a1 = alloca [2 x i64], align 8
45 %a2 = alloca [2 x i64], align 8
46 %a1.0 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 0
47 store i64 -5015437470765251660, i64* %a1.0, align 8
48 %a1.1 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 1
49 store i64 -8661621401413125213, i64* %a1.1, align 8
50 %a2.0 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 0
51 store i64 -5015437470765251660, i64* %a2.0, align 8
52 %a2.1 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 1
53 store i64 -8661621401413125213, i64* %a2.1, align 8
57 %iv = phi i64 [ 0, %start ], [ %iv.next, %latch ]
58 %gep1 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 %iv
59 %gep2 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 %iv
60 %load1 = load i64, i64* %gep1, align 8
61 %load2 = load i64, i64* %gep2, align 8
62 %exitcond2 = icmp eq i64 %load1, %load2
63 br i1 %exitcond2, label %latch, label %exit
66 %iv.next = add nuw nsw i64 %iv, 1
67 %exitcond = icmp eq i64 %iv.next, 2
68 br i1 %exitcond, label %exit, label %loop
71 %exit.val = phi i1 [ true, %latch ], [ false, %loop ]
75 define i1 @test_non_latch() {
76 ; CHECK-LABEL: @test_non_latch(
78 ; CHECK-NEXT: [[A1:%.*]] = alloca [2 x i64], align 8
79 ; CHECK-NEXT: [[A2:%.*]] = alloca [2 x i64], align 8
80 ; CHECK-NEXT: [[A1_0:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 0
81 ; CHECK-NEXT: store i64 -5015437470765251660, i64* [[A1_0]], align 8
82 ; CHECK-NEXT: [[A1_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 1
83 ; CHECK-NEXT: store i64 -8661621401413125213, i64* [[A1_1]], align 8
84 ; CHECK-NEXT: [[A2_0:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 0
85 ; CHECK-NEXT: store i64 -5015437470765251660, i64* [[A2_0]], align 8
86 ; CHECK-NEXT: [[A2_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 1
87 ; CHECK-NEXT: store i64 -8661621401413125213, i64* [[A2_1]], align 8
88 ; CHECK-NEXT: br label [[LOOP:%.*]]
90 ; CHECK-NEXT: br label [[LATCH:%.*]]
92 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 0
93 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 0
94 ; CHECK-NEXT: [[LOAD1:%.*]] = load i64, i64* [[GEP1]], align 8
95 ; CHECK-NEXT: [[LOAD2:%.*]] = load i64, i64* [[GEP2]], align 8
96 ; CHECK-NEXT: [[EXITCOND2:%.*]] = icmp eq i64 [[LOAD1]], [[LOAD2]]
97 ; CHECK-NEXT: br i1 [[EXITCOND2]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
99 ; CHECK-NEXT: [[EXIT_VAL:%.*]] = phi i1 [ false, [[LATCH]] ], [ false, [[LATCH_1:%.*]] ], [ true, [[LOOP_2:%.*]] ], [ false, [[LATCH_2:%.*]] ]
100 ; CHECK-NEXT: ret i1 [[EXIT_VAL]]
102 ; CHECK-NEXT: br label [[LATCH_1]]
104 ; CHECK-NEXT: [[GEP1_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A1]], i64 0, i64 1
105 ; CHECK-NEXT: [[GEP2_1:%.*]] = getelementptr inbounds [2 x i64], [2 x i64]* [[A2]], i64 0, i64 1
106 ; CHECK-NEXT: [[LOAD1_1:%.*]] = load i64, i64* [[GEP1_1]], align 8
107 ; CHECK-NEXT: [[LOAD2_1:%.*]] = load i64, i64* [[GEP2_1]], align 8
108 ; CHECK-NEXT: [[EXITCOND2_1:%.*]] = icmp eq i64 [[LOAD1_1]], [[LOAD2_1]]
109 ; CHECK-NEXT: br i1 [[EXITCOND2_1]], label [[LOOP_2]], label [[EXIT]]
111 ; CHECK-NEXT: br i1 true, label [[EXIT]], label [[LATCH_2]]
113 ; CHECK-NEXT: br label [[EXIT]]
116 %a1 = alloca [2 x i64], align 8
117 %a2 = alloca [2 x i64], align 8
118 %a1.0 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 0
119 store i64 -5015437470765251660, i64* %a1.0, align 8
120 %a1.1 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 1
121 store i64 -8661621401413125213, i64* %a1.1, align 8
122 %a2.0 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 0
123 store i64 -5015437470765251660, i64* %a2.0, align 8
124 %a2.1 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 1
125 store i64 -8661621401413125213, i64* %a2.1, align 8
129 %iv = phi i64 [ 0, %start ], [ %iv.next, %latch ]
130 %exitcond = icmp eq i64 %iv, 2
131 br i1 %exitcond, label %exit, label %latch
134 %iv.next = add nuw nsw i64 %iv, 1
135 %gep1 = getelementptr inbounds [2 x i64], [2 x i64]* %a1, i64 0, i64 %iv
136 %gep2 = getelementptr inbounds [2 x i64], [2 x i64]* %a2, i64 0, i64 %iv
137 %load1 = load i64, i64* %gep1, align 8
138 %load2 = load i64, i64* %gep2, align 8
139 %exitcond2 = icmp eq i64 %load1, %load2
140 br i1 %exitcond2, label %loop, label %exit
143 %exit.val = phi i1 [ false, %latch ], [ true, %loop ]