1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=loop-unroll -unroll-runtime -unroll-count=4 -S | FileCheck %s
3 ; RUN: opt < %s -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime -unroll-count=4 -S | FileCheck %s
5 ; Check that loop unroll pass correctly handle loops with
6 ; single exiting block not the loop header or latch.
8 define void @test1(ptr noalias %A) {
11 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A:%.*]], align 4
12 ; CHECK-NEXT: call void @bar(i32 [[TMP0]])
13 ; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
15 ; CHECK-NEXT: call void @bar(i32 [[TMP0]])
16 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
18 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE:%.*]]
19 ; CHECK: for.body.for.body_crit_edge:
20 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 1
21 ; CHECK-NEXT: [[DOTPRE:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT]], align 4
22 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE]])
23 ; CHECK-NEXT: br label [[FOR_BODY_1:%.*]]
25 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE_1:%.*]]
26 ; CHECK: for.body.for.body_crit_edge.1:
27 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_1:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 2
28 ; CHECK-NEXT: [[DOTPRE_1:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_1]], align 4
29 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_1]])
30 ; CHECK-NEXT: br label [[FOR_BODY_2:%.*]]
32 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE_2:%.*]]
33 ; CHECK: for.body.for.body_crit_edge.2:
34 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_2:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 3
35 ; CHECK-NEXT: [[DOTPRE_2:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_2]], align 4
36 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_2]])
37 ; CHECK-NEXT: br label [[FOR_BODY_3:%.*]]
39 ; CHECK-NEXT: br i1 false, label [[FOR_BODY_FOR_BODY_CRIT_EDGE_3:%.*]], label [[FOR_END:%.*]]
40 ; CHECK: for.body.for.body_crit_edge.3:
41 ; CHECK-NEXT: unreachable
43 ; CHECK-NEXT: ret void
46 %0 = load i32, ptr %A, align 4
47 call void @bar(i32 %0)
51 %1 = phi i32 [ %0, %entry ], [ %.pre, %for.body.for.body_crit_edge ]
52 %i = phi i64 [ 0, %entry ], [ %inc, %for.body.for.body_crit_edge ]
53 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %i
54 call void @bar(i32 %1)
58 %inc = add nsw i64 %i, 1
59 %cmp = icmp slt i64 %inc, 4
60 br i1 %cmp, label %for.body.for.body_crit_edge, label %for.end
62 for.body.for.body_crit_edge:
63 %arrayidx.phi.trans.insert = getelementptr inbounds i32, ptr %A, i64 %inc
64 %.pre = load i32, ptr %arrayidx.phi.trans.insert, align 4
71 ; Check that loop unroll pass correctly handle loops with
72 ; (1) exiting block not dominating the loop latch; and
73 ; (2) exiting terminator instructions cannot be simplified to unconditional.
75 define void @test2(ptr noalias %A) {
76 ; CHECK-LABEL: @test2(
78 ; CHECK-NEXT: br i1 true, label [[FOR_PREHEADER:%.*]], label [[FOR_END:%.*]]
79 ; CHECK: for.preheader:
80 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A:%.*]], align 4
81 ; CHECK-NEXT: call void @bar(i32 [[TMP0]])
82 ; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
84 ; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[TMP0]], [[FOR_PREHEADER]] ], [ [[DOTPRE_3:%.*]], [[FOR_BODY_FOR_BODY_CRIT_EDGE_3:%.*]] ]
85 ; CHECK-NEXT: [[I:%.*]] = phi i64 [ 0, [[FOR_PREHEADER]] ], [ [[INC_3:%.*]], [[FOR_BODY_FOR_BODY_CRIT_EDGE_3]] ]
86 ; CHECK-NEXT: call void @bar(i32 [[TMP1]])
87 ; CHECK-NEXT: [[INC:%.*]] = add nuw nsw i64 [[I]], 1
88 ; CHECK-NEXT: br i1 true, label [[FOR_BODY:%.*]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE:%.*]]
90 ; CHECK-NEXT: [[CMP:%.*]] = call i1 @foo(i64 [[I]])
91 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE]], label [[FOR_END_LOOPEXIT:%.*]]
92 ; CHECK: for.body.for.body_crit_edge:
93 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INC]]
94 ; CHECK-NEXT: [[DOTPRE:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT]], align 4
95 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE]])
96 ; CHECK-NEXT: [[INC_1:%.*]] = add nuw nsw i64 [[I]], 2
97 ; CHECK-NEXT: br i1 true, label [[FOR_BODY_1:%.*]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_1:%.*]]
99 ; CHECK-NEXT: [[CMP_1:%.*]] = call i1 @foo(i64 [[INC]])
100 ; CHECK-NEXT: br i1 [[CMP_1]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_1]], label [[FOR_END_LOOPEXIT]]
101 ; CHECK: for.body.for.body_crit_edge.1:
102 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_1:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INC_1]]
103 ; CHECK-NEXT: [[DOTPRE_1:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_1]], align 4
104 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_1]])
105 ; CHECK-NEXT: [[INC_2:%.*]] = add nuw nsw i64 [[I]], 3
106 ; CHECK-NEXT: br i1 true, label [[FOR_BODY_2:%.*]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_2:%.*]]
108 ; CHECK-NEXT: [[CMP_2:%.*]] = call i1 @foo(i64 [[INC_1]])
109 ; CHECK-NEXT: br i1 [[CMP_2]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_2]], label [[FOR_END_LOOPEXIT]]
110 ; CHECK: for.body.for.body_crit_edge.2:
111 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_2:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INC_2]]
112 ; CHECK-NEXT: [[DOTPRE_2:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_2]], align 4
113 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_2]])
114 ; CHECK-NEXT: [[INC_3]] = add nsw i64 [[I]], 4
115 ; CHECK-NEXT: br i1 true, label [[FOR_BODY_3:%.*]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_3]]
117 ; CHECK-NEXT: [[CMP_3:%.*]] = call i1 @foo(i64 [[INC_2]])
118 ; CHECK-NEXT: br i1 [[CMP_3]], label [[FOR_BODY_FOR_BODY_CRIT_EDGE_3]], label [[FOR_END_LOOPEXIT]]
119 ; CHECK: for.body.for.body_crit_edge.3:
120 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_3:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INC_3]]
121 ; CHECK-NEXT: [[DOTPRE_3]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_3]], align 4
122 ; CHECK-NEXT: br label [[FOR_HEADER]], !llvm.loop [[LOOP0:![0-9]+]]
123 ; CHECK: for.end.loopexit:
124 ; CHECK-NEXT: br label [[FOR_END]]
126 ; CHECK-NEXT: ret void
129 br i1 true, label %for.preheader, label %for.end
132 %0 = load i32, ptr %A, align 4
133 call void @bar(i32 %0)
137 %1 = phi i32 [ %0, %for.preheader ], [ %.pre, %for.body.for.body_crit_edge ]
138 %i = phi i64 [ 0, %for.preheader ], [ %inc, %for.body.for.body_crit_edge ]
139 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %i
140 call void @bar(i32 %1)
141 %inc = add nsw i64 %i, 1
142 br i1 true, label %for.body, label %for.body.for.body_crit_edge
145 %cmp = call i1 @foo(i64 %i)
146 br i1 %cmp, label %for.body.for.body_crit_edge, label %for.end
148 for.body.for.body_crit_edge:
149 %arrayidx.phi.trans.insert = getelementptr inbounds i32, ptr %A, i64 %inc
150 %.pre = load i32, ptr %arrayidx.phi.trans.insert, align 4
157 ; Check that loop unroll pass correctly handle loops with
158 ; (1) multiple exiting blocks; and
159 ; (2) loop latch is not an exiting block.
161 define void @test3(ptr noalias %A, i1 %cond) {
162 ; CHECK-LABEL: @test3(
164 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A:%.*]], align 4
165 ; CHECK-NEXT: call void @bar(i32 [[TMP0]])
166 ; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
168 ; CHECK-NEXT: call void @bar(i32 [[TMP0]])
169 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
171 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE:%.*]]
172 ; CHECK: for.body.for.body_crit_edge:
173 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 1
174 ; CHECK-NEXT: [[DOTPRE:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT]], align 4
175 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE]])
176 ; CHECK-NEXT: br i1 [[COND]], label [[FOR_BODY_1:%.*]], label [[FOR_END]]
178 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE_1:%.*]]
179 ; CHECK: for.body.for.body_crit_edge.1:
180 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_1:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 2
181 ; CHECK-NEXT: [[DOTPRE_1:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_1]], align 4
182 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_1]])
183 ; CHECK-NEXT: br i1 [[COND]], label [[FOR_BODY_2:%.*]], label [[FOR_END]]
185 ; CHECK-NEXT: br label [[FOR_BODY_FOR_BODY_CRIT_EDGE_2:%.*]]
186 ; CHECK: for.body.for.body_crit_edge.2:
187 ; CHECK-NEXT: [[ARRAYIDX_PHI_TRANS_INSERT_2:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 3
188 ; CHECK-NEXT: [[DOTPRE_2:%.*]] = load i32, ptr [[ARRAYIDX_PHI_TRANS_INSERT_2]], align 4
189 ; CHECK-NEXT: call void @bar(i32 [[DOTPRE_2]])
190 ; CHECK-NEXT: br i1 [[COND]], label [[FOR_BODY_3:%.*]], label [[FOR_END]]
192 ; CHECK-NEXT: br i1 false, label [[FOR_BODY_FOR_BODY_CRIT_EDGE_3:%.*]], label [[FOR_END]]
193 ; CHECK: for.body.for.body_crit_edge.3:
194 ; CHECK-NEXT: unreachable
196 ; CHECK-NEXT: ret void
199 %0 = load i32, ptr %A, align 4
200 call void @bar(i32 %0)
204 %1 = phi i32 [ %0, %entry ], [ %.pre, %for.body.for.body_crit_edge ]
205 %i = phi i64 [ 0, %entry ], [ %inc, %for.body.for.body_crit_edge ]
206 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %i
207 call void @bar(i32 %1)
208 br i1 %cond, label %for.body, label %for.end
211 %inc = add nsw i64 %i, 1
212 %cmp = icmp slt i64 %inc, 4
213 br i1 %cmp, label %for.body.for.body_crit_edge, label %for.end
215 for.body.for.body_crit_edge:
216 %arrayidx.phi.trans.insert = getelementptr inbounds i32, ptr %A, i64 %inc
217 %.pre = load i32, ptr %arrayidx.phi.trans.insert, align 4
224 ; Test it doesn't crash.
225 define void @test4(i32 %arg) {
226 ; CHECK-LABEL: @test4(
228 ; CHECK-NEXT: br label [[BB1:%.*]]
230 ; CHECK-NEXT: br i1 false, label [[BB4:%.*]], label [[BB1_1:%.*]]
232 ; CHECK-NEXT: br i1 false, label [[BB4]], label [[BB1_2:%.*]]
234 ; CHECK-NEXT: br i1 false, label [[BB4]], label [[BB1_3:%.*]]
236 ; CHECK-NEXT: br i1 false, label [[BB4]], label [[BB1]], !llvm.loop [[LOOP2:![0-9]+]]
238 ; CHECK-NEXT: unreachable
243 bb1: ; preds = %bb1, %bb
244 %tmp = phi i64 [ 0, %bb ], [ 65, %bb1 ]
245 %tmp2 = phi i32 [ %arg, %bb ], [ %tmp3, %bb1 ]
246 %tmp3 = add i32 0, -1880031232
247 br i1 false, label %bb4, label %bb1
254 declare void @bar(i32)