max-backedge-taken-count-guard-info.ll
11.3 KB
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
; RUN: opt -analyze -scalar-evolution %s -enable-new-pm=0 | FileCheck %s
; RUN: opt -passes='print<scalar-evolution>' -disable-output %s 2>&1 | FileCheck %s
; Test case for PR40961. The loop guard limit the max backedge-taken count.
define void @test_guard_less_than_16(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_less_than_16
; CHECK-NEXT: Loop %loop: backedge-taken count is (15 + (-1 * %i))
; CHECK-NEXT: Loop %loop: max backedge-taken count is 15
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (15 + (-1 * %i))
;
entry:
%cmp3 = icmp ult i64 %i, 16
br i1 %cmp3, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ %i, %entry ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv.next, 16
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_guard_less_than_16_operands_swapped(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_less_than_16_operands_swapped
; CHECK-NEXT: Loop %loop: backedge-taken count is (15 + (-1 * %i))
; CHECK-NEXT: Loop %loop: max backedge-taken count is 15
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (15 + (-1 * %i))
;
entry:
%cmp3 = icmp ugt i64 16, %i
br i1 %cmp3, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ %i, %entry ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv.next, 16
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_guard_less_than_16_branches_flipped(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_less_than_16_branches_flipped
; CHECK-NEXT: Loop %loop: backedge-taken count is (15 + (-1 * %i))
; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (15 + (-1 * %i))
;
entry:
%cmp3 = icmp ult i64 %i, 16
br i1 %cmp3, label %exit, label %loop
loop:
%iv = phi i64 [ %iv.next, %loop ], [ %i, %entry ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv.next, 16
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_guard_uge_16_branches_flipped(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_uge_16_branches_flipped
; CHECK-NEXT: Loop %loop: backedge-taken count is (15 + (-1 * %i))
; CHECK-NEXT: Loop %loop: max backedge-taken count is 15
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (15 + (-1 * %i))
;
entry:
%cmp3 = icmp uge i64 %i, 16
br i1 %cmp3, label %exit, label %loop
loop:
%iv = phi i64 [ %iv.next, %loop ], [ %i, %entry ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv.next, 16
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_guard_eq_12(i32* nocapture %a, i64 %N) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_eq_12
; CHECK-NEXT: Loop %loop: backedge-taken count is %N
; CHECK-NEXT: Loop %loop: max backedge-taken count is 12
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %N
;
entry:
%c.1 = icmp eq i64 %N, 12
br i1 %c.1, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %N
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_multiple_const_guards_order1(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: @test_multiple_const_guards_order1
; CHECK: Loop %loop: backedge-taken count is %i
; CHECK-NEXT: Loop %loop: max backedge-taken count is 9
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
;
entry:
%c.1 = icmp ult i64 %i, 16
br i1 %c.1, label %guardbb, label %exit
guardbb:
%c.2 = icmp ult i64 %i, 10
br i1 %c.2, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %i
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_multiple_const_guards_order2(i32* nocapture %a, i64 %i) {
; CHECK-LABEL: @test_multiple_const_guards_order2
; CHECK: Loop %loop: backedge-taken count is %i
; CHECK-NEXT: Loop %loop: max backedge-taken count is 9
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
;
entry:
%c.1 = icmp ult i64 %i, 10
br i1 %c.1, label %guardbb, label %exit
guardbb:
%c.2 = icmp ult i64 %i, 16
br i1 %c.2, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %i
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
; TODO: Currently we miss getting the tightest max backedge-taken count (11).
define void @test_multiple_var_guards_order1(i32* nocapture %a, i64 %i, i64 %N) {
; CHECK-LABEL: @test_multiple_var_guards_order1
; CHECK: Loop %loop: backedge-taken count is %i
; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
;
entry:
%c.1 = icmp ult i64 %N, 12
br i1 %c.1, label %guardbb, label %exit
guardbb:
%c.2 = icmp ult i64 %i, %N
br i1 %c.2, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %i
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
; TODO: Currently we miss getting the tightest max backedge-taken count (11).
define void @test_multiple_var_guards_order2(i32* nocapture %a, i64 %i, i64 %N) {
; CHECK-LABEL: Determining loop execution counts for: @test_multiple_var_guards_order2
; CHECK-NEXT: Loop %loop: backedge-taken count is %i
; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
;
entry:
%c.1 = icmp ult i64 %i, %N
br i1 %c.1, label %guardbb, label %exit
guardbb:
%c.2 = icmp ult i64 %N, 12
br i1 %c.2, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %i
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
; The guards here reference each other in a cycle.
define void @test_multiple_var_guards_cycle(i32* nocapture %a, i64 %i, i64 %N) {
; CHECK-LABEL: Determining loop execution counts for: @test_multiple_var_guards_cycle
; CHECK-NEXT: Loop %loop: backedge-taken count is %N
; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %N
;
entry:
%c.1 = icmp ult i64 %N, %i
br i1 %c.1, label %guardbb, label %exit
guardbb:
%c.2 = icmp ult i64 %i, %N
br i1 %c.2, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %a, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw nsw i64 %iv, 1
%exitcond = icmp eq i64 %iv, %N
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @test_guard_ult_ne(i32* nocapture readonly %data, i64 %count) {
; CHECK-LABEL: @test_guard_ult_ne
; CHECK: Loop %loop: backedge-taken count is (-1 + %count)
; CHECK-NEXT: Loop %loop: max backedge-taken count is 3
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %count)
;
entry:
%cmp.ult = icmp ult i64 %count, 5
br i1 %cmp.ult, label %guardbb, label %exit
guardbb:
%cmp.ne = icmp ne i64 %count, 0
br i1 %cmp.ne, label %loop, label %exit
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
%idx = getelementptr inbounds i32, i32* %data, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw i64 %iv, 1
%exitcond.not = icmp eq i64 %iv.next, %count
br i1 %exitcond.not, label %exit, label %loop
exit:
ret void
}
; Test case for PR47247. Both the guard condition and the assume limit the
; max backedge-taken count.
define void @test_guard_and_assume(i32* nocapture readonly %data, i64 %count) {
; CHECK-LABEL: @test_guard_and_assume
; CHECK: Loop %loop: backedge-taken count is (-1 + %count)
; CHECK-NEXT: Loop %loop: max backedge-taken count is 3
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %count)
;
entry:
%cmp = icmp ult i64 %count, 5
tail call void @llvm.assume(i1 %cmp)
%cmp18.not = icmp eq i64 %count, 0
br i1 %cmp18.not, label %exit, label %loop
loop:
%iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ]
%idx = getelementptr inbounds i32, i32* %data, i64 %iv
store i32 1, i32* %idx, align 4
%iv.next = add nuw i64 %iv, 1
%exitcond.not = icmp eq i64 %iv.next, %count
br i1 %exitcond.not, label %exit, label %loop
exit:
ret void
}
; Function Attrs: nounwind willreturn
declare void @llvm.assume(i1 noundef)
define void @guard_pessimizes_analysis(i1 %c, i32 %N) {
; CHECK-LABEL: @guard_pessimizes_analysis
; CHECK: Loop %loop: backedge-taken count is (9 + (-1 * %init)<nsw>)<nsw>
; CHECK-NEXT: Loop %loop: max backedge-taken count is 7
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (9 + (-1 * %init)<nsw>)<nsw>
;
entry:
br i1 %c, label %bb1, label %guard
bb1:
br label %guard
guard:
%init = phi i32 [ 2, %entry ], [ 3, %bb1 ]
%c.1 = icmp ult i32 %init, %N
br i1 %c.1, label %loop.ph, label %exit
loop.ph:
br label %loop
loop:
%iv = phi i32 [ %iv.next, %loop ], [ %init, %loop.ph ]
%iv.next = add i32 %iv, 1
%exitcond = icmp eq i32 %iv.next, 10
br i1 %exitcond, label %exit, label %loop
exit:
ret void
}
define void @crash(i8* %ptr) {
; CHECK-LABEL: @crash
; CHECK: Loop %while.body125: backedge-taken count is {(-2 + (-1 * %ptr)),+,-1}<nw><%while.cond111>
; CHECK-NEXT: Loop %while.body125: max backedge-taken count is -1
; CHECK-NEXT: Loop %while.body125: Predicated backedge-taken count is {(-2 + (-1 * %ptr)),+,-1}<nw><%while.cond111>
;
entry:
br label %while.body
while.body:
br label %while.cond111
while.cond111:
%text.addr.5 = phi i8* [ %incdec.ptr112, %while.cond111 ], [ null, %while.body ]
%incdec.ptr112 = getelementptr inbounds i8, i8* %text.addr.5, i64 -1
br i1 false, label %while.end117, label %while.cond111
while.end117:
%cmp118 = icmp ult i8* %ptr, %incdec.ptr112
br i1 %cmp118, label %while.body125, label %while.cond134.preheader
while.cond134.preheader:
br label %while.body
while.body125:
%lastout.2271 = phi i8* [ %incdec.ptr126, %while.body125 ], [ %ptr, %while.end117 ]
%incdec.ptr126 = getelementptr inbounds i8, i8* %lastout.2271, i64 1
%exitcond.not = icmp eq i8* %incdec.ptr126, %incdec.ptr112
br i1 %exitcond.not, label %while.end129, label %while.body125
while.end129: ; preds = %while.body125
ret void
}