1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
| ; RUN: opt -S -indvars %s | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define void @test1(i64 %start) {
; CHECK-LABEL: @test1
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
; CHECK: %cmp1 = icmp slt i64 %start, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
define void @test2(i64 %start) {
; CHECK-LABEL: @test2
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
; CHECK: %cmp1 = icmp sle i64 %start, -1
%cmp1 = icmp sle i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
; As long as the test dominates the backedge, we're good
define void @test3(i64 %start) {
; CHECK-LABEL: @test3
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %for.end
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp slt i64 %start, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
define void @test4(i64 %start) {
; CHECK-LABEL: @test4
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %for.end
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp sgt i64 %start, -1
%cmp1 = icmp sgt i64 %indvars.iv, -1
br i1 %cmp1, label %loop, label %for.end
for.end: ; preds = %if.end, %entry
ret void
}
define void @test5(i64 %start) {
; CHECK-LABEL: @test5
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nuw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %for.end
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp ugt i64 %start, 100
%cmp1 = icmp ugt i64 %indvars.iv, 100
br i1 %cmp1, label %loop, label %for.end
for.end: ; preds = %if.end, %entry
ret void
}
define void @test6(i64 %start) {
; CHECK-LABEL: @test6
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nuw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %for.end
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp ult i64 %start, 100
%cmp1 = icmp ult i64 %indvars.iv, 100
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
define void @test7(i64 %start, i64* %inc_ptr) {
; CHECK-LABEL: @test7
entry:
%inc = load i64, i64* %inc_ptr, !range !0
%ok = icmp sge i64 %inc, 0
br i1 %ok, label %loop, label %for.end
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, %inc
; CHECK: %cmp1 = icmp slt i64 %start, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
; Negative test - we can't show that the internal branch executes, so we can't
; fold the test to a loop invariant one.
define void @test1_neg(i64 %start) {
; CHECK-LABEL: @test1_neg
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %skip
skip:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp slt i64 %indvars.iv, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %backedge
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
br label %loop
for.end: ; preds = %if.end, %entry
ret void
}
; Slightly subtle version of @test4 where the icmp dominates the backedge,
; but the exit branch doesn't.
define void @test2_neg(i64 %start) {
; CHECK-LABEL: @test2_neg
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
; CHECK: %cmp1 = icmp slt i64 %indvars.iv, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp, label %backedge, label %skip
skip:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
br i1 %cmp1, label %for.end, label %backedge
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
br label %loop
for.end: ; preds = %if.end, %entry
ret void
}
; The branch has to exit the loop if the condition is true
define void @test3_neg(i64 %start) {
; CHECK-LABEL: @test3_neg
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
; CHECK: %cmp1 = icmp slt i64 %indvars.iv, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %loop, label %for.end
for.end: ; preds = %if.end, %entry
ret void
}
define void @test4_neg(i64 %start) {
; CHECK-LABEL: @test4_neg
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %backedge ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
%cmp = icmp eq i64 %indvars.iv.next, 25
br i1 %cmp, label %backedge, label %for.end
backedge:
; prevent flattening, needed to make sure we're testing what we intend
call void @foo()
; CHECK: %cmp1 = icmp sgt i64 %indvars.iv, -1
%cmp1 = icmp sgt i64 %indvars.iv, -1
; %cmp1 can be made loop invariant only if the branch below goes to
; %the header when %cmp1 is true.
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
define void @test5_neg(i64 %start, i64 %inc) {
; CHECK-LABEL: @test5_neg
entry:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, %inc
; CHECK: %cmp1 = icmp slt i64 %indvars.iv, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
define void @test8(i64 %start, i64* %inc_ptr) {
; CHECK-LABEL: @test8
entry:
%inc = load i64, i64* %inc_ptr, !range !1
%ok = icmp sge i64 %inc, 0
br i1 %ok, label %loop, label %for.end
loop:
%indvars.iv = phi i64 [ %start, %entry ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, %inc
; CHECK: %cmp1 = icmp slt i64 %indvars.iv, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
; check to handle loops without preheaders, but invariant operands
; (we handle this today by inserting a preheader)
define void @test9(i1 %cnd, i64 %start) {
; CHECK-LABEL: @test9
; CHECK-LABEL: loop.preheader:
entry:
br i1 %cnd, label %entry1, label %entry2
entry1:
br label %loop
entry2:
br label %loop
loop:
%indvars.iv = phi i64 [ %start, %entry1 ],[ %start, %entry2 ], [ %indvars.iv.next, %loop ]
%indvars.iv.next = add nsw i64 %indvars.iv, 1
; CHECK: %cmp1 = icmp slt i64 %start, -1
%cmp1 = icmp slt i64 %indvars.iv, -1
br i1 %cmp1, label %for.end, label %loop
for.end: ; preds = %if.end, %entry
ret void
}
declare void @use(i1 %x)
; check that we handle conditions with loop invariant operands which
; *aren't* in the header - this is a very rare and fragile case where
; we have a "loop" which is known to run exactly one iteration but
; haven't yet simplified the uses of the IV
define void @test10() {
; CHECK-LABEL: @test10
entry:
br label %loop
loop:
%phi1 = phi i32 [ %phi2, %latch ], [ 0, %entry ]
%dec = add i32 %phi1, -1
br i1 false, label %left, label %right
left:
br label %latch
right:
br label %latch
latch:
%phi2 = phi i32 [ %phi1, %left ], [ %dec, %right ]
; CHECK: %cmp = icmp slt i32 -1, undef
%cmp = icmp slt i32 %phi2, undef
br i1 true, label %exit, label %loop
exit:
call void @use(i1 %cmp)
ret void
}
; check that we can figure out that iv.next > 1 from the facts that iv >= 0 and
; iv.start != 0.
define void @test11(i64* %inc_ptr) {
; CHECK-LABEL: @test11
entry:
%inc = load i64, i64* %inc_ptr, !range !0
%ne.cond = icmp ne i64 %inc, 0
br i1 %ne.cond, label %loop, label %exit
loop:
%iv = phi i64 [ %inc, %entry ], [ %iv.next, %backedge ]
%iv.next = add i64 %iv, 1
%brcond = icmp sgt i64 %iv.next, 1
; CHECK: br i1 true, label %if.true, label %if.false
br i1 %brcond, label %if.true, label %if.false
if.true:
br label %backedge
if.false:
br label %backedge
backedge:
%loopcond = icmp slt i64 %iv, 200
br i1 %loopcond, label %loop, label %exit
exit:
ret void
}
; check that we can prove that a recurrency is greater than another recurrency
; in the same loop, with the same step, and with smaller starting value.
define void @test12(i64* %inc_ptr) {
; CHECK-LABEL: @test12
entry:
%inc = load i64, i64* %inc_ptr, !range !0
%inc.minus.1 = sub i64 %inc, 1
br label %loop
loop:
%iv = phi i64 [ %inc, %entry ], [ %iv.next, %backedge ]
%iv.minus.1 = phi i64 [ %inc.minus.1, %entry ], [ %iv.minus.1.next, %backedge ]
%iv.next = add i64 %iv, 1
%iv.minus.1.next = add i64 %iv.minus.1, 1
%brcond = icmp sgt i64 %iv.next, %iv.minus.1.next
; CHECK: br i1 true, label %if.true, label %if.false
br i1 %brcond, label %if.true, label %if.false
if.true:
br label %backedge
if.false:
br label %backedge
backedge:
%loopcond = icmp slt i64 %iv, 200
br i1 %loopcond, label %loop, label %exit
exit:
ret void
}
!0 = !{i64 0, i64 100}
!1 = !{i64 -1, i64 100}
declare void @foo()
|