aboutsummaryrefslogtreecommitdiff
path: root/test/Transforms/InstCombine/phi.ll
blob: 1c307d430f3f854a59be24f78d089c4c2ce533e7 (plain)
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
; This test makes sure that these instructions are properly eliminated.
;
; RUN: opt < %s -instcombine -S | FileCheck %s

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128:n8:16:32:64"

define i32 @test1(i32 %A, i1 %b) {
BB0:
        br i1 %b, label %BB1, label %BB2

BB1:
        ; Combine away one argument PHI nodes
        %B = phi i32 [ %A, %BB0 ]               
        ret i32 %B

BB2:
        ret i32 %A
; CHECK: @test1
; CHECK: BB1:
; CHECK-NEXT: ret i32 %A
}

define i32 @test2(i32 %A, i1 %b) {
BB0:
        br i1 %b, label %BB1, label %BB2

BB1:
        br label %BB2

BB2:
        ; Combine away PHI nodes with same values
        %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ]         
        ret i32 %B
; CHECK: @test2
; CHECK: BB2:
; CHECK-NEXT: ret i32 %A
}

define i32 @test3(i32 %A, i1 %b) {
BB0:
        br label %Loop

Loop:
        ; PHI has same value always.
        %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ]
        br i1 %b, label %Loop, label %Exit

Exit:
        ret i32 %B
; CHECK: @test3
; CHECK: Exit:
; CHECK-NEXT: ret i32 %A
}

define i32 @test4(i1 %b) {
BB0:
        ; Loop is unreachable
        ret i32 7

Loop:           ; preds = %L2, %Loop
        ; PHI has same value always.
        %B = phi i32 [ %B, %L2 ], [ %B, %Loop ]         
        br i1 %b, label %L2, label %Loop

L2:             ; preds = %Loop
        br label %Loop
; CHECK: @test4
; CHECK: Loop:
; CHECK-NEXT: br i1 %b
}

define i32 @test5(i32 %A, i1 %b) {
BB0:
        br label %Loop

Loop:           ; preds = %Loop, %BB0
        ; PHI has same value always.
        %B = phi i32 [ %A, %BB0 ], [ undef, %Loop ]             
        br i1 %b, label %Loop, label %Exit

Exit:           ; preds = %Loop
        ret i32 %B
; CHECK: @test5
; CHECK: Loop:
; CHECK-NEXT: br i1 %b
; CHECK: Exit:
; CHECK-NEXT: ret i32 %A
}

define i32 @test6(i16 %A, i1 %b) {
BB0:
        %X = zext i16 %A to i32              
        br i1 %b, label %BB1, label %BB2

BB1:           
        %Y = zext i16 %A to i32              
        br label %BB2

BB2:           
        ;; Suck casts into phi
        %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]         
        ret i32 %B
; CHECK: @test6
; CHECK: BB2:
; CHECK: zext i16 %A to i32
; CHECK-NEXT: ret i32
}

define i32 @test7(i32 %A, i1 %b) {
BB0:
        br label %Loop

Loop:           ; preds = %Loop, %BB0
        ; PHI is dead.
        %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ]                
        %C = add i32 %B, 123            
        br i1 %b, label %Loop, label %Exit

Exit:           ; preds = %Loop
        ret i32 0
; CHECK: @test7
; CHECK: Loop:
; CHECK-NEXT: br i1 %b
}

define i32* @test8({ i32, i32 } *%A, i1 %b) {
BB0:
        %X = getelementptr inbounds { i32, i32 } *%A, i32 0, i32 1
        br i1 %b, label %BB1, label %BB2

BB1:
        %Y = getelementptr { i32, i32 } *%A, i32 0, i32 1
        br label %BB2

BB2:
        ;; Suck GEPs into phi
        %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ]
        ret i32* %B
; CHECK: @test8
; CHECK-NOT: phi
; CHECK: BB2:
; CHECK-NEXT: %B = getelementptr { i32, i32 }* %A 
; CHECK-NEXT: ret i32* %B
}

define i32 @test9(i32* %A, i32* %B) {
entry:
  %c = icmp eq i32* %A, null
  br i1 %c, label %bb1, label %bb

bb:
  %C = load i32* %B, align 1
  br label %bb2

bb1:
  %D = load i32* %A, align 1
  br label %bb2

bb2:
  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
  ret i32 %E
; CHECK: @test9
; CHECK:       bb2:
; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
; CHECK-NEXT:   %E = load i32* %{{[^,]*}}, align 1
; CHECK-NEXT:   ret i32 %E

}

define i32 @test10(i32* %A, i32* %B) {
entry:
  %c = icmp eq i32* %A, null
  br i1 %c, label %bb1, label %bb

bb:
  %C = load i32* %B, align 16
  br label %bb2

bb1:
  %D = load i32* %A, align 32
  br label %bb2

bb2:
  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
  ret i32 %E
; CHECK: @test10
; CHECK:       bb2:
; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
; CHECK-NEXT:   %E = load i32* %{{[^,]*}}, align 16
; CHECK-NEXT:   ret i32 %E
}


; PR1777
declare i1 @test11a()

define i1 @test11() {
entry:
  %a = alloca i32
  %i = ptrtoint i32* %a to i64
  %b = call i1 @test11a()
  br i1 %b, label %one, label %two

one:
  %x = phi i64 [%i, %entry], [%y, %two]
  %c = call i1 @test11a()
  br i1 %c, label %two, label %end

two:
  %y = phi i64 [%i, %entry], [%x, %one]
  %d = call i1 @test11a()
  br i1 %d, label %one, label %end

end:
  %f = phi i64 [ %x, %one], [%y, %two]
  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
  ; even though %f must equal %i at this point
  %g = inttoptr i64 %f to i32*
  store i32 10, i32* %g
  %z = call i1 @test11a()
  ret i1 %z
; CHECK: @test11
; CHECK-NOT: phi i32
; CHECK: ret i1 %z
}


define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) {
entry:
  %tmp41 = ptrtoint i8* %Ptr to i64
  %tmp42 = zext i64 %tmp41 to i128
  br i1 %cond, label %end, label %two

two:
  %tmp36 = zext i64 %Val to i128            ; <i128> [#uses=1]
  %tmp37 = shl i128 %tmp36, 64                    ; <i128> [#uses=1]
  %ins39 = or i128 %tmp42, %tmp37                 ; <i128> [#uses=1]
  br label %end

end:
  %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
  %tmp32 = trunc i128 %tmp869.0 to i64            ; <i64> [#uses=1]
  %tmp29 = lshr i128 %tmp869.0, 64                ; <i128> [#uses=1]
  %tmp30 = trunc i128 %tmp29 to i64               ; <i64> [#uses=1]

  %tmp2 = add i64 %tmp32, %tmp30
  ret i64 %tmp2
; CHECK: @test12
; CHECK-NOT: zext
; CHECK: end:
; CHECK-NEXT: phi i64 [ 0, %entry ], [ %Val, %two ]
; CHECK-NOT: phi
; CHECK: ret i64
}

declare void @test13f(double, i32)

define void @test13(i1 %cond, i32 %V1, double %Vald) {
entry:
  %tmp42 = zext i32 %V1 to i128
  br i1 %cond, label %end, label %two

two:
  %Val = bitcast double %Vald to i64
  %tmp36 = zext i64 %Val to i128            ; <i128> [#uses=1]
  %tmp37 = shl i128 %tmp36, 64                    ; <i128> [#uses=1]
  %ins39 = or i128 %tmp42, %tmp37                 ; <i128> [#uses=1]
  br label %end

end:
  %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
  %tmp32 = trunc i128 %tmp869.0 to i32
  %tmp29 = lshr i128 %tmp869.0, 64                ; <i128> [#uses=1]
  %tmp30 = trunc i128 %tmp29 to i64               ; <i64> [#uses=1]
  %tmp31 = bitcast i64 %tmp30 to double
  
  call void @test13f(double %tmp31, i32 %tmp32)
  ret void
; CHECK: @test13
; CHECK-NOT: zext
; CHECK: end:
; CHECK-NEXT: phi double [ 0.000000e+00, %entry ], [ %Vald, %two ]
; CHECK-NEXT: call void @test13f(double {{[^,]*}}, i32 %V1)
; CHECK: ret void
}

define i640 @test14a(i320 %A, i320 %B, i1 %b1) {
BB0:
        %a = zext i320 %A to i640
        %b = zext i320 %B to i640
        br label %Loop

Loop:
        %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ]             
        br i1 %b1, label %Loop, label %Exit

Exit:           ; preds = %Loop
        ret i640 %C
; CHECK: @test14a
; CHECK: Loop:
; CHECK-NEXT: phi i320
}

define i160 @test14b(i320 %A, i320 %B, i1 %b1) {
BB0:
        %a = trunc i320 %A to i160
        %b = trunc i320 %B to i160
        br label %Loop

Loop:
        %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ]             
        br i1 %b1, label %Loop, label %Exit

Exit:           ; preds = %Loop
        ret i160 %C
; CHECK: @test14b
; CHECK: Loop:
; CHECK-NEXT: phi i160
}

declare i64 @test15a(i64)

define i64 @test15b(i64 %A, i1 %b) {
; CHECK: @test15b
entry:
  %i0 = zext i64 %A to i128
  %i1 = shl i128 %i0, 64
  %i = or i128 %i1, %i0
  br i1 %b, label %one, label %two
; CHECK: entry:
; CHECK-NEXT: br i1 %b

one:
  %x = phi i128 [%i, %entry], [%y, %two]
  %x1 = lshr i128 %x, 64
  %x2 = trunc i128 %x1 to i64
  %c = call i64 @test15a(i64 %x2)
  %c1 = zext i64 %c to i128
  br label %two

; CHECK: one:
; CHECK-NEXT: phi i64
; CHECK-NEXT: %c = call i64 @test15a

two:
  %y = phi i128 [%i, %entry], [%c1, %one]
  %y1 = lshr i128 %y, 64
  %y2 = trunc i128 %y1 to i64
  %d = call i64 @test15a(i64 %y2)
  %d1 = trunc i64 %d to i1
  br i1 %d1, label %one, label %end

; CHECK: two:
; CHECK-NEXT: phi i64
; CHECK-NEXT: phi i64
; CHECK-NEXT: %d = call i64 @test15a

end:
  %g = trunc i128 %y to i64
  ret i64 %g
; CHECK: end: 
; CHECK-NEXT: ret i64
}

; PR6512 - Shouldn't merge loads from different addr spaces.
define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2)
nounwind {
entry:
  %retval = alloca i32, align 4                   ; <i32*> [#uses=2]
  %pointer1.addr = alloca i32 addrspace(1)*, align 4 ; <i32 addrspace(1)**>
  %flag.addr = alloca i32, align 4                ; <i32*> [#uses=2]
  %pointer2.addr = alloca i32*, align 4           ; <i32**> [#uses=2]
  %res = alloca i32, align 4                      ; <i32*> [#uses=4]
  store i32 addrspace(1)* %pointer1, i32 addrspace(1)** %pointer1.addr
  store i32 %flag, i32* %flag.addr
  store i32* %pointer2, i32** %pointer2.addr
  store i32 10, i32* %res
  %tmp = load i32* %flag.addr                     ; <i32> [#uses=1]
  %tobool = icmp ne i32 %tmp, 0                   ; <i1> [#uses=1]
  br i1 %tobool, label %if.then, label %if.else

return:                                           ; preds = %if.end
  %tmp7 = load i32* %retval                       ; <i32> [#uses=1]
  ret i32 %tmp7

if.end:                                           ; preds = %if.else, %if.then
  %tmp6 = load i32* %res                          ; <i32> [#uses=1]
  store i32 %tmp6, i32* %retval
  br label %return

if.then:                                          ; preds = %entry
  %tmp1 = load i32 addrspace(1)** %pointer1.addr  ; <i32 addrspace(1)*>
  %arrayidx = getelementptr i32 addrspace(1)* %tmp1, i32 0 ; <i32 addrspace(1)*> [#uses=1]
  %tmp2 = load i32 addrspace(1)* %arrayidx        ; <i32> [#uses=1]
  store i32 %tmp2, i32* %res
  br label %if.end

if.else:                                          ; preds = %entry
  %tmp3 = load i32** %pointer2.addr               ; <i32*> [#uses=1]
  %arrayidx4 = getelementptr i32* %tmp3, i32 0    ; <i32*> [#uses=1]
  %tmp5 = load i32* %arrayidx4                    ; <i32> [#uses=1]
  store i32 %tmp5, i32* %res
  br label %if.end
}

; PR4413
declare i32 @ext()
; CHECK: @test17
define i32 @test17(i1 %a) {
entry:
    br i1 %a, label %bb1, label %bb2

bb1:        ; preds = %entry
    %0 = tail call i32 @ext()        ; <i32> [#uses=1]
    br label %bb2

bb2:        ; preds = %bb1, %entry
    %cond = phi i1 [ true, %bb1 ], [ false, %entry ]        ; <i1> [#uses=1]
; CHECK-NOT: %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]
    %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]        ; <i32> [#uses=1]
    %res = select i1 %cond, i32 %val, i32 0        ; <i32> [#uses=1]
; CHECK: ret i32 %cond
    ret i32 %res
}

define i1 @test18(i1 %cond) {
  %zero = alloca i32
  %one = alloca i32
  br i1 %cond, label %true, label %false
true:
  br label %ret
false:
  br label %ret
ret:
  %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
  %isnull = icmp eq i32* %ptr, null
  ret i1 %isnull
; CHECK: @test18
; CHECK: ret i1 false
}

define i1 @test19(i1 %cond, double %x) {
  br i1 %cond, label %true, label %false
true:
  br label %ret
false:
  br label %ret
ret:
  %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
  %cmp = fcmp ule double %x, %p
  ret i1 %cmp
; CHECK: @test19
; CHECK: ret i1 true
}

define i1 @test20(i1 %cond) {
  %a = alloca i32
  %b = alloca i32
  %c = alloca i32
  br i1 %cond, label %true, label %false
true:
  br label %ret
false:
  br label %ret
ret:
  %p = phi i32* [ %a, %true ], [ %b, %false ]
  %r = icmp eq i32* %p, %c
  ret i1 %r
; CHECK: @test20
; CHECK: ret i1 false
}

define i1 @test21(i1 %c1, i1 %c2) {
  %a = alloca i32
  %b = alloca i32
  %c = alloca i32
  br i1 %c1, label %true, label %false
true:
  br label %loop
false:
  br label %loop
loop:
  %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
  %r = icmp eq i32* %p, %c
  br i1 %c2, label %ret, label %loop
ret:
  ret i1 %r
; CHECK: @test21
; CHECK: ret i1 false
}

define void @test22() {
; CHECK: @test22
entry:
  br label %loop
loop:
  %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
  %y = add i32 %phi, 1
  %o = or i32 %y, %phi
  %e = icmp eq i32 %o, %y
  br i1 %e, label %loop, label %ret
; CHECK: br i1 %e
ret:
  ret void
}

define i32 @test23(i32 %A, i1 %b, i32 * %P) {
BB0:
        br label %Loop

Loop:           ; preds = %Loop, %BB0
        ; PHI has same value always.
        %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ]
        %D = add i32 %B, 19
        store i32 %D, i32* %P
        br i1 %b, label %Loop, label %Exit

Exit:           ; preds = %Loop
        %E = add i32 %B, 19
        ret i32 %E
; CHECK: @test23
; CHECK: %phitmp = add i32 %A, 19
; CHECK: Loop:
; CHECK-NEXT: %B = phi i32 [ %phitmp, %BB0 ], [ 61, %Loop ]
; CHECK: Exit:
; CHECK-NEXT: ret i32 %B
}

define i32 @test24(i32 %A, i1 %cond) {
BB0:
        %X = add nuw nsw i32 %A, 1
        br i1 %cond, label %BB1, label %BB2

BB1:
        %Y = add nuw i32 %A, 1
        br label %BB2

BB2:
        %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
        ret i32 %C
; CHECK: @test24
; CHECK-NOT: phi
; CHECK: BB2:
; CHECK-NEXT: %C = add nuw i32 %A, 1
; CHECK-NEXT: ret i32 %C
}

; Same as test11, but used to be missed due to a bug.
declare i1 @test25a()

define i1 @test25() {
entry:
  %a = alloca i32
  %i = ptrtoint i32* %a to i64
  %b = call i1 @test25a()
  br i1 %b, label %one, label %two

one:
  %x = phi i64 [%y, %two], [%i, %entry]
  %c = call i1 @test25a()
  br i1 %c, label %two, label %end

two:
  %y = phi i64 [%x, %one], [%i, %entry]
  %d = call i1 @test25a()
  br i1 %d, label %one, label %end

end:
  %f = phi i64 [ %x, %one], [%y, %two]
  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
  ; even though %f must equal %i at this point
  %g = inttoptr i64 %f to i32*
  store i32 10, i32* %g
  %z = call i1 @test25a()
  ret i1 %z
; CHECK: @test25
; CHECK-NOT: phi i32
; CHECK: ret i1 %z
}

declare i1 @test26a()

define i1 @test26(i32 %n) {
entry:
  %a = alloca i32
  %i = ptrtoint i32* %a to i64
  %b = call i1 @test26a()
  br label %one

one:
  %x = phi i64 [%y, %two], [%w, %three], [%i, %entry]
  %c = call i1 @test26a()
  switch i32 %n, label %end [
          i32 2, label %two
          i32 3, label %three
  ]

two:
  %y = phi i64 [%x, %one], [%w, %three]
  %d = call i1 @test26a()
  switch i32 %n, label %end [
          i32 10, label %one
          i32 30, label %three
  ]

three:
  %w = phi i64 [%y, %two], [%x, %one]
  %e = call i1 @test26a()
  br i1 %e, label %one, label %two

end:
  %f = phi i64 [ %x, %one], [%y, %two]
  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
  ; even though %f must equal %i at this point
  %g = inttoptr i64 %f to i32*
  store i32 10, i32* %g
  %z = call i1 @test26a()
  ret i1 %z
; CHECK: @test26
; CHECK-NOT: phi i32
; CHECK: ret i1 %z
}

; CHECK: @test27(
; CHECK: ret i32 undef
define i32 @test27(i1 %b) {
entry:
  br label %done
done:
  %y = phi i32 [ undef, %entry ]
  ret i32 %y
}