select.ll
14.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
; RUN: opt -S -jump-threading < %s | FileCheck %s
declare void @foo()
declare void @bar()
declare void @baz()
declare void @quux()
; Jump threading of branch with select as condition.
; Mostly theoretical since instruction combining simplifies all selects of
; booleans where at least one operand is true/false/undef.
; CHECK-LABEL: @test_br(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 %cond, label %L1,
define void @test_br(i1 %cond, i1 %value) nounwind {
entry:
br i1 %cond, label %L0, label %L3
L0:
%expr = select i1 %cond, i1 true, i1 %value
br i1 %expr, label %L1, label %L2
L1:
call void @foo()
ret void
L2:
call void @bar()
ret void
L3:
call void @baz()
br label %L0
}
; Jump threading of switch with select as condition.
; CHECK-LABEL: @test_switch(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 %cond, label %L1,
define void @test_switch(i1 %cond, i8 %value) nounwind {
entry:
br i1 %cond, label %L0, label %L4
L0:
%expr = select i1 %cond, i8 1, i8 %value
switch i8 %expr, label %L3 [i8 1, label %L1 i8 2, label %L2]
L1:
call void @foo()
ret void
L2:
call void @bar()
ret void
L3:
call void @baz()
ret void
L4:
call void @quux()
br label %L0
}
; Make sure the blocks in the indirectbr test aren't trivially removable as
; successors by taking their addresses.
@anchor = constant [3 x i8*] [
i8* blockaddress(@test_indirectbr, %L1),
i8* blockaddress(@test_indirectbr, %L2),
i8* blockaddress(@test_indirectbr, %L3)
]
; Jump threading of indirectbr with select as address.
; CHECK-LABEL: @test_indirectbr(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 %cond, label %L1, label %L3
define void @test_indirectbr(i1 %cond, i8* %address) nounwind {
entry:
br i1 %cond, label %L0, label %L3
L0:
%indirect.goto.dest = select i1 %cond, i8* blockaddress(@test_indirectbr, %L1), i8* %address
indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
L1:
call void @foo()
ret void
L2:
call void @bar()
ret void
L3:
call void @baz()
ret void
}
; Jump threading of indirectbr with select as address. Test increased
; duplication threshold for cases where indirectbr is being threaded
; through.
; CHECK-LABEL: @test_indirectbr_thresh(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 %cond, label %L1, label %L3
; CHECK-NOT: indirectbr
define void @test_indirectbr_thresh(i1 %cond, i8* %address) nounwind {
entry:
br i1 %cond, label %L0, label %L3
L0:
%indirect.goto.dest = select i1 %cond, i8* blockaddress(@test_indirectbr_thresh, %L1), i8* %address
call void @quux()
call void @quux()
call void @quux()
indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
L1:
call void @foo()
ret void
L2:
call void @bar()
ret void
L3:
call void @baz()
ret void
}
; A more complicated case: the condition is a select based on a comparison.
; CHECK-LABEL: @test_switch_cmp(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 %cond, label %L0, label %[[THREADED:[A-Za-z.0-9]+]]
; CHECK: [[THREADED]]:
; CHECK-NEXT: call void @quux
; CHECK-NEXT: br label %L1
define void @test_switch_cmp(i1 %cond, i32 %val, i8 %value) nounwind {
entry:
br i1 %cond, label %L0, label %L4
L0:
%val.phi = phi i32 [%val, %entry], [-1, %L4]
%cmp = icmp slt i32 %val.phi, 0
%expr = select i1 %cmp, i8 1, i8 %value
switch i8 %expr, label %L3 [i8 1, label %L1 i8 2, label %L2]
L1:
call void @foo()
ret void
L2:
call void @bar()
ret void
L3:
call void @baz()
ret void
L4:
call void @quux()
br label %L0
}
; Make sure the edge value of %0 from entry to L2 includes 0 and L3 is
; reachable.
; CHECK: test_switch_default
; CHECK: entry:
; CHECK: load
; CHECK: switch
; CHECK: [[THREADED:[A-Za-z.0-9]+]]:
; CHECK: store
; CHECK: br
; CHECK: L2:
; CHECK-SAME: preds = %entry, %entry
; CHECK-NEXT: phi i32
define void @test_switch_default(i32* nocapture %status) nounwind {
entry:
%0 = load i32, i32* %status, align 4
switch i32 %0, label %L2 [
i32 5061, label %L1
i32 0, label %L2
]
L1:
store i32 10025, i32* %status, align 4
br label %L2
L2:
%1 = load i32, i32* %status, align 4
%cmp57.i = icmp eq i32 %1, 0
br i1 %cmp57.i, label %L3, label %L4
L3:
store i32 10000, i32* %status, align 4
br label %L4
L4:
ret void
}
define void @unfold1(double %x, double %y) nounwind {
entry:
%sub = fsub double %x, %y
%cmp = fcmp ogt double %sub, 1.000000e+01
br i1 %cmp, label %cond.end4, label %cond.false
cond.false: ; preds = %entry
%add = fadd double %x, %y
%cmp1 = fcmp ogt double %add, 1.000000e+01
%add. = select i1 %cmp1, double %add, double 0.000000e+00
br label %cond.end4
cond.end4: ; preds = %entry, %cond.false
%cond5 = phi double [ %add., %cond.false ], [ %sub, %entry ]
%cmp6 = fcmp oeq double %cond5, 0.000000e+00
br i1 %cmp6, label %if.then, label %if.end
if.then: ; preds = %cond.end4
call void @foo()
br label %if.end
if.end: ; preds = %if.then, %cond.end4
ret void
; CHECK-LABEL: @unfold1
; CHECK: br i1 %cmp, label %cond.end4, label %cond.false
; CHECK: br i1 %cmp1, label %cond.end4, label %if.then
; CHECK: br i1 %cmp6, label %if.then, label %if.end
; CHECK: br label %if.end
}
define void @unfold2(i32 %x, i32 %y) nounwind {
entry:
%sub = sub nsw i32 %x, %y
%cmp = icmp sgt i32 %sub, 10
br i1 %cmp, label %cond.end4, label %cond.false
cond.false: ; preds = %entry
%add = add nsw i32 %x, %y
%cmp1 = icmp sgt i32 %add, 10
%add. = select i1 %cmp1, i32 0, i32 %add
br label %cond.end4
cond.end4: ; preds = %entry, %cond.false
%cond5 = phi i32 [ %add., %cond.false ], [ %sub, %entry ]
%cmp6 = icmp eq i32 %cond5, 0
br i1 %cmp6, label %if.then, label %if.end
if.then: ; preds = %cond.end4
call void @foo()
br label %if.end
if.end: ; preds = %if.then, %cond.end4
ret void
; CHECK-LABEL: @unfold2
; CHECK: br i1 %cmp, label %if.end, label %cond.false
; CHECK: br i1 %cmp1, label %if.then, label %cond.end4
; CHECK: br i1 %cmp6, label %if.then, label %if.end
; CHECK: br label %if.end
}
define i32 @unfold3(i32 %u, i32 %v, i32 %w, i32 %x, i32 %y, i32 %z, i32 %j) nounwind {
entry:
%add3 = add nsw i32 %j, 2
%cmp.i = icmp slt i32 %u, %v
br i1 %cmp.i, label %.exit, label %cond.false.i
cond.false.i: ; preds = %entry
%cmp4.i = icmp sgt i32 %u, %v
br i1 %cmp4.i, label %.exit, label %cond.false.6.i
cond.false.6.i: ; preds = %cond.false.i
%cmp8.i = icmp slt i32 %w, %x
br i1 %cmp8.i, label %.exit, label %cond.false.10.i
cond.false.10.i: ; preds = %cond.false.6.i
%cmp13.i = icmp sgt i32 %w, %x
br i1 %cmp13.i, label %.exit, label %cond.false.15.i
cond.false.15.i: ; preds = %cond.false.10.i
%phitmp = icmp sge i32 %y, %z
br label %.exit
.exit: ; preds = %entry, %cond.false.i, %cond.false.6.i, %cond.false.10.i, %cond.false.15.i
%cond23.i = phi i1 [ false, %entry ], [ true, %cond.false.i ], [ false, %cond.false.6.i ], [ %phitmp, %cond.false.15.i ], [ true, %cond.false.10.i ]
%j.add3 = select i1 %cond23.i, i32 %j, i32 %add3
ret i32 %j.add3
; CHECK-LABEL: @unfold3
; CHECK: br i1 %cmp.i, label %.exit.thread3, label %cond.false.i
; CHECK: br i1 %cmp4.i, label %.exit.thread, label %cond.false.6.i
; CHECK: br i1 %cmp8.i, label %.exit.thread3, label %cond.false.10.i
; CHECK: br i1 %cmp13.i, label %.exit.thread, label %.exit
; CHECK: br i1 %phitmp, label %.exit.thread, label %.exit.thread3
; CHECK: br label %.exit.thread3
}
define i32 @unfold4(i32 %u, i32 %v, i32 %w, i32 %x, i32 %y, i32 %z, i32 %j) nounwind {
entry:
%add3 = add nsw i32 %j, 2
%cmp.i = icmp slt i32 %u, %v
br i1 %cmp.i, label %.exit, label %cond.false.i
cond.false.i: ; preds = %entry
%cmp4.i = icmp sgt i32 %u, %v
br i1 %cmp4.i, label %.exit, label %cond.false.6.i
cond.false.6.i: ; preds = %cond.false.i
%cmp8.i = icmp slt i32 %w, %x
br i1 %cmp8.i, label %.exit, label %cond.false.10.i
cond.false.10.i: ; preds = %cond.false.6.i
%cmp13.i = icmp sgt i32 %w, %x
br i1 %cmp13.i, label %.exit, label %cond.false.15.i
cond.false.15.i: ; preds = %cond.false.10.i
%cmp19.i = icmp sge i32 %y, %z
%conv = zext i1 %cmp19.i to i32
br label %.exit
.exit: ; preds = %entry, %cond.false.i, %cond.false.6.i, %cond.false.10.i, %cond.false.15.i
%cond23.i = phi i32 [ 1, %entry ], [ 0, %cond.false.i ], [ 1, %cond.false.6.i ], [ %conv, %cond.false.15.i ], [ 0, %cond.false.10.i ]
%lnot.i18 = icmp eq i32 %cond23.i, 1
%j.add3 = select i1 %lnot.i18, i32 %j, i32 %add3
ret i32 %j.add3
; CHECK-LABEL: @unfold4
; CHECK: br i1 %cmp.i, label %.exit.thread, label %cond.false.i
; CHECK: br i1 %cmp4.i, label %.exit.thread4, label %cond.false.6.i
; CHECK: br i1 %cmp8.i, label %.exit.thread, label %cond.false.10.i
; CHECK: br i1 %cmp13.i, label %.exit.thread4, label %.exit
; CHECK: br i1 %lnot.i18, label %.exit.thread, label %.exit.thread4
; CHECK: br label %.exit.thread4
}
define i32 @unfold5(i32 %u, i32 %v, i32 %w, i32 %x, i32 %y, i32 %z, i32 %j) nounwind {
entry:
%add3 = add nsw i32 %j, 2
%cmp.i = icmp slt i32 %u, %v
br i1 %cmp.i, label %.exit, label %cond.false.i
cond.false.i: ; preds = %entry
%cmp4.i = icmp sgt i32 %u, %v
br i1 %cmp4.i, label %.exit, label %cond.false.6.i
cond.false.6.i: ; preds = %cond.false.i
%cmp8.i = icmp slt i32 %w, %x
br i1 %cmp8.i, label %.exit, label %cond.false.10.i
cond.false.10.i: ; preds = %cond.false.6.i
%cmp13.i = icmp sgt i32 %w, %x
br i1 %cmp13.i, label %.exit, label %cond.false.15.i
cond.false.15.i: ; preds = %cond.false.10.i
%cmp19.i = icmp sge i32 %y, %z
%conv = zext i1 %cmp19.i to i32
br label %.exit
.exit: ; preds = %entry, %cond.false.i, %cond.false.6.i, %cond.false.10.i, %cond.false.15.i
%cond23.i = phi i32 [ 2, %entry ], [ 3, %cond.false.i ], [ 1, %cond.false.6.i ], [ %conv, %cond.false.15.i ], [ 7, %cond.false.10.i ]
%lnot.i18 = icmp sgt i32 %cond23.i, 5
%j.add3 = select i1 %lnot.i18, i32 %j, i32 %cond23.i
ret i32 %j.add3
; CHECK-LABEL: @unfold5
; CHECK: br i1 %cmp.i, label %.exit, label %cond.false.i
; CHECK: br i1 %cmp4.i, label %.exit, label %cond.false.6.i
; CHECK: br i1 %cmp8.i, label %.exit, label %cond.false.10.i
; CHECK: br i1 %cmp13.i, label %.exit, label %cond.false.15.i
; CHECK: br label %.exit
}
; When a select has a constant operand in one branch, and it feeds a phi node
; and the phi node feeds a switch we unfold the select
define void @test_func(i32* nocapture readonly %a, i32* nocapture readonly %b, i32* nocapture readonly %c, i32 %n) local_unnamed_addr #0 {
entry:
br label %for.cond
for.cond: ; preds = %sw.default, %entry
%i.0 = phi i32 [ 0, %entry ], [ %inc, %sw.default ]
%cmp = icmp slt i32 %i.0, %n
br i1 %cmp, label %for.body, label %for.cond.cleanup
for.cond.cleanup: ; preds = %for.cond
ret void
for.body: ; preds = %for.cond
%0 = zext i32 %i.0 to i64
%arrayidx = getelementptr inbounds i32, i32* %a, i64 %0
%1 = load i32, i32* %arrayidx, align 4
%cmp1 = icmp eq i32 %1, 4
br i1 %cmp1, label %land.lhs.true, label %if.end
land.lhs.true: ; preds = %for.body
%arrayidx3 = getelementptr inbounds i32, i32* %b, i64 %0
%2 = load i32, i32* %arrayidx3, align 4
%arrayidx5 = getelementptr inbounds i32, i32* %c, i64 %0
%3 = load i32, i32* %arrayidx5, align 4
%cmp6 = icmp eq i32 %2, %3
%spec.select = select i1 %cmp6, i32 2, i32 4
br label %if.end
if.end: ; preds = %land.lhs.true, %for.body
%local_var.0 = phi i32 [ %1, %for.body ], [ %spec.select, %land.lhs.true ]
switch i32 %local_var.0, label %sw.default [
i32 2, label %sw.bb
i32 4, label %sw.bb7
i32 5, label %sw.bb8
i32 7, label %sw.bb9
]
sw.bb: ; preds = %if.end
call void @foo()
br label %sw.bb7
sw.bb7: ; preds = %if.end, %sw.bb
call void @bar()
br label %sw.bb8
sw.bb8: ; preds = %if.end, %sw.bb7
call void @baz()
br label %sw.bb9
sw.bb9: ; preds = %if.end, %sw.bb8
call void @quux()
br label %sw.default
sw.default: ; preds = %if.end, %sw.bb9
call void @baz()
%inc = add nuw nsw i32 %i.0, 1
br label %for.cond
; CHECK-LABEL: @test_func(
; CHECK: [[REG:%[0-9]+]] = load
; CHECK-NOT: select
; CHECK: br i1
; CHECK-NOT: select
; CHECK: br i1 {{.*}}, label [[DEST1:%.*]], label [[DEST2:%.*]]
; The following line checks existence of a phi node, and makes sure
; it only has one incoming value. To do this, we check every '%'. Note
; that REG and REG2 each contain one '%;. There is another one in the
; beginning of the incoming block name. After that there should be no other '%'.
; CHECK: [[REG2:%.*]] = phi i32 {{[^%]*}}[[REG]]{{[^%]*%[^%]*}}
; CHECK: switch i32 [[REG2]]
; CHECK: i32 2, label [[DEST1]]
; CHECK: i32 4, label [[DEST2]]
}
; FIXME: This is an invalid transform. If %b is false and %x is poison,
; then the select produces poison (the result of the program is poison).
; But with this transform, we may be branching on poison, and that is UB.
define i32 @TryToUnfoldSelectInCurrBB(i1 %b, i1 %ui, i32 %s, i1 %x) {
; CHECK-LABEL: @TryToUnfoldSelectInCurrBB(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[B:%.*]], label [[IF_END_THREAD:%.*]], label [[IF_END:%.*]]
; CHECK: if.end:
; CHECK-NEXT: br i1 [[X:%.*]], label [[TMP0:%.*]], label [[IF_END_THREAD]]
; CHECK: 0:
; CHECK-NEXT: br label [[IF_END_THREAD]]
; CHECK: if.end.thread:
; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[S:%.*]], [[TMP0]] ], [ 42, [[IF_END]] ], [ 42, [[ENTRY:%.*]] ]
; CHECK-NEXT: ret i32 [[TMP1]]
;
entry:
br i1 %b, label %if.end, label %if.else
if.else:
br label %if.end
if.end:
%v = phi i1 [ %x, %if.else ], [ false, %entry ]
%v1 = select i1 %v, i32 %s, i32 42
ret i32 %v1
}