guards.ll
15.4 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
; RUN: opt -S -early-cse < %s | FileCheck %s
; RUN: opt < %s -S -basicaa -early-cse-memssa | FileCheck %s
declare void @llvm.experimental.guard(i1,...)
declare void @llvm.assume(i1)
define i32 @test0(i32* %ptr, i1 %cond) {
; We can do store to load forwarding over a guard, since it does not
; clobber memory
; CHECK-LABEL: @test0(
; CHECK-NEXT: store i32 40, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
; CHECK-NEXT: ret i32 40
store i32 40, i32* %ptr
call void(i1,...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
%rval = load i32, i32* %ptr
ret i32 %rval
}
define i32 @test1(i32* %val, i1 %cond) {
; We can CSE loads over a guard, since it does not clobber memory
; CHECK-LABEL: @test1(
; CHECK-NEXT: %val0 = load i32, i32* %val
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
; CHECK-NEXT: ret i32 0
%val0 = load i32, i32* %val
call void(i1,...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
%val1 = load i32, i32* %val
%rval = sub i32 %val0, %val1
ret i32 %rval
}
define i32 @test2() {
; Guards on "true" get removed
; CHECK-LABEL: @test2(
; CHECK-NEXT: ret i32 0
call void(i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ]
ret i32 0
}
define i32 @test3(i32 %val) {
; After a guard has executed the condition it was guarding is known to
; be true.
; CHECK-LABEL: @test3(
; CHECK-NEXT: %cond0 = icmp slt i32 %val, 40
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
; CHECK-NEXT: ret i32 -1
%cond0 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
%cond1 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
%cond2 = icmp slt i32 %val, 40
%rval = sext i1 %cond2 to i32
ret i32 %rval
}
define i32 @test3.unhandled(i32 %val) {
; After a guard has executed the condition it was guarding is known to
; be true.
; CHECK-LABEL: @test3.unhandled(
; CHECK-NEXT: %cond0 = icmp slt i32 %val, 40
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
; CHECK-NEXT: %cond1 = icmp sge i32 %val, 40
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
; CHECK-NEXT: ret i32 0
; Demonstrates a case we do not yet handle (it is legal to fold %cond2
; to false)
%cond0 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
%cond1 = icmp sge i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
ret i32 0
}
define i32 @test4(i32 %val, i1 %c) {
; Same as test3, but with some control flow involved.
; CHECK-LABEL: @test4(
; CHECK: entry:
; CHECK-NEXT: %cond0 = icmp slt i32 %val, 40
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond0
; CHECK-NEXT: br label %bb0
; CHECK: bb0:
; CHECK-NEXT: %cond2 = icmp ult i32 %val, 200
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond2
; CHECK-NEXT: br i1 %c, label %left, label %right
; CHECK: left:
; CHECK-NEXT: ret i32 0
; CHECK: right:
; CHECK-NEXT: ret i32 20
entry:
%cond0 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
%cond1 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
br label %bb0
bb0:
%cond2 = icmp ult i32 %val, 200
call void(i1,...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
br i1 %c, label %left, label %right
left:
%cond3 = icmp ult i32 %val, 200
call void(i1,...) @llvm.experimental.guard(i1 %cond3) [ "deopt"() ]
ret i32 0
right:
ret i32 20
}
define i32 @test5(i32 %val, i1 %c) {
; Same as test4, but the %left block has mutliple predecessors.
; CHECK-LABEL: @test5(
; CHECK: entry:
; CHECK-NEXT: %cond0 = icmp slt i32 %val, 40
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond0
; CHECK-NEXT: br label %bb0
; CHECK: bb0:
; CHECK-NEXT: %cond2 = icmp ult i32 %val, 200
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cond2
; CHECK-NEXT: br i1 %c, label %left, label %right
; CHECK: left:
; CHECK-NEXT: br label %right
; CHECK: right:
; CHECK-NEXT: br label %left
entry:
%cond0 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond0) [ "deopt"() ]
%cond1 = icmp slt i32 %val, 40
call void(i1,...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
br label %bb0
bb0:
%cond2 = icmp ult i32 %val, 200
call void(i1,...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
br i1 %c, label %left, label %right
left:
%cond3 = icmp ult i32 %val, 200
call void(i1,...) @llvm.experimental.guard(i1 %cond3) [ "deopt"() ]
br label %right
right:
br label %left
}
define void @test6(i1 %c, i32* %ptr) {
; Check that we do not DSE over calls to @llvm.experimental.guard.
; Guard intrinsics do _read_ memory, so th call to guard below needs
; to see the store of 500 to %ptr
; CHECK-LABEL: @test6(
; CHECK-NEXT: store i32 500, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %c) [ "deopt"() ]
; CHECK-NEXT: store i32 600, i32* %ptr
store i32 500, i32* %ptr
call void(i1,...) @llvm.experimental.guard(i1 %c) [ "deopt"() ]
store i32 600, i32* %ptr
ret void
}
define void @test07(i32 %a, i32 %b) {
; Check that we are able to remove the guards on the same condition even if the
; condition is not being recalculated.
; CHECK-LABEL: @test07(
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: ret void
%cmp = icmp eq i32 %a, %b
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
ret void
}
define void @test08(i32 %a, i32 %b, i32* %ptr) {
; Check that we deal correctly with stores when removing guards in the same
; block in case when the condition is not recalculated.
; CHECK-LABEL: @test08(
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: store i32 100, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: ret void
%cmp = icmp eq i32 %a, %b
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 200, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
ret void
}
define void @test09(i32 %a, i32 %b, i1 %c, i32* %ptr) {
; Similar to test08, but with more control flow.
; TODO: Can we get rid of the store in the end of entry given that it is
; post-dominated by other stores?
; CHECK-LABEL: @test09(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: store i32 100, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: store i32 500, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: store i32 600, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 200, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
br i1 %c, label %if.true, label %if.false
if.true:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 500, i32* %ptr
br label %merge
if.false:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 600, i32* %ptr
br label %merge
merge:
ret void
}
define void @test10(i32 %a, i32 %b, i1 %c, i32* %ptr) {
; Make sure that non-dominating guards do not cause other guards removal.
; CHECK-LABEL: @test10(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: store i32 100, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: store i32 200, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: store i32 300, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
br i1 %c, label %if.true, label %if.false
if.true:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
br label %merge
if.false:
store i32 200, i32* %ptr
br label %merge
merge:
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
ret void
}
define void @test11(i32 %a, i32 %b, i32* %ptr) {
; Make sure that branching condition is applied to guards.
; CHECK-LABEL: @test11(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: br i1 %cmp, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
br i1 %cmp, label %if.true, label %if.false
if.true:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
br label %merge
if.false:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
br label %merge
merge:
ret void
}
define void @test12(i32 %a, i32 %b) {
; Check that the assume marks its condition as being true (and thus allows to
; eliminate the dominated guards).
; CHECK-LABEL: @test12(
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: ret void
%cmp = icmp eq i32 %a, %b
call void @llvm.assume(i1 %cmp)
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
ret void
}
define void @test13(i32 %a, i32 %b, i32* %ptr) {
; Check that we deal correctly with stores when removing guards due to assume.
; CHECK-LABEL: @test13(
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: ret void
%cmp = icmp eq i32 %a, %b
call void @llvm.assume(i1 %cmp)
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 200, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
ret void
}
define void @test14(i32 %a, i32 %b, i1 %c, i32* %ptr) {
; Similar to test13, but with more control flow.
; TODO: Can we get rid of the store in the end of entry given that it is
; post-dominated by other stores?
; CHECK-LABEL: @test14(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: store i32 500, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: store i32 600, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
call void @llvm.assume(i1 %cmp)
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 200, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
br i1 %c, label %if.true, label %if.false
if.true:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 500, i32* %ptr
br label %merge
if.false:
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 600, i32* %ptr
br label %merge
merge:
ret void
}
define void @test15(i32 %a, i32 %b, i1 %c, i32* %ptr) {
; Make sure that non-dominating assumes do not cause guards removal.
; CHECK-LABEL: @test15(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: store i32 100, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: store i32 200, i32* %ptr
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: store i32 300, i32* %ptr
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
; CHECK-NEXT: store i32 400, i32* %ptr
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
br i1 %c, label %if.true, label %if.false
if.true:
call void @llvm.assume(i1 %cmp)
store i32 100, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
br label %merge
if.false:
store i32 200, i32* %ptr
br label %merge
merge:
store i32 300, i32* %ptr
call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
store i32 400, i32* %ptr
ret void
}
define void @test16(i32 %a, i32 %b) {
; Check that we don't bother to do anything with assumes even if we know the
; condition being true.
; CHECK-LABEL: @test16(
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: ret void
%cmp = icmp eq i32 %a, %b
call void @llvm.assume(i1 %cmp)
call void @llvm.assume(i1 %cmp)
ret void
}
define void @test17(i32 %a, i32 %b, i1 %c, i32* %ptr) {
; Check that we don't bother to do anything with assumes even if we know the
; condition being true or false (includes come control flow).
; CHECK-LABEL: @test17(
; CHECK: entry:
; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
; CHECK: if.true:
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: br label %merge
; CHECK: if.false:
; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
; CHECK-NEXT: br label %merge
; CHECK: merge:
; CHECK-NEXT: ret void
entry:
%cmp = icmp eq i32 %a, %b
br i1 %c, label %if.true, label %if.false
if.true:
call void @llvm.assume(i1 %cmp)
br label %merge
if.false:
call void @llvm.assume(i1 %cmp)
br label %merge
merge:
ret void
}
define void @test18(i1 %c) {
; Check that we don't bother to do anything with assumes even if we know the
; condition being true and not being an instruction.
; CHECK-LABEL: @test18(
; CHECK-NEXT: call void @llvm.assume(i1 %c)
; CHECK-NEXT: call void @llvm.assume(i1 %c)
; CHECK-NEXT: ret void
call void @llvm.assume(i1 %c)
call void @llvm.assume(i1 %c)
ret void
}