1 ; RUN: opt < %s -sroa -S | FileCheck %s
2 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64"
10 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
11 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
14 %v0 = load i32, i32* %a0
15 %v1 = load i32, i32* %a1
19 %cond = icmp sle i32 %v0, %v1
20 br i1 %cond, label %then, label %exit
26 %phi = phi i32* [ %a1, %then ], [ %a0, %entry ]
27 ; CHECK: phi i32 [ 1, %{{.*}} ], [ 0, %{{.*}} ]
29 %result = load i32, i32* %phi
34 ; CHECK-LABEL: @test2(
39 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
40 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
43 %v0 = load i32, i32* %a0
44 %v1 = load i32, i32* %a1
48 %cond = icmp sle i32 %v0, %v1
49 %select = select i1 %cond, i32* %a1, i32* %a0
50 ; CHECK: select i1 %{{.*}}, i32 1, i32 0
52 %result = load i32, i32* %select
56 ; If bitcast isn't considered a safe phi/select use, the alloca
57 ; remains as an array.
58 ; FIXME: Why isn't this identical to test2?
60 ; CHECK-LABEL: @test2_bitcast(
62 ; CHECK-NEXT: alloca i32
64 ; CHECK: %select = select i1 %cond, i32* %a.sroa.3, i32* %a.sroa.0
65 ; CHECK-NEXT: %select.bc = bitcast i32* %select to float*
66 ; CHECK-NEXT: %result = load float, float* %select.bc, align 4
67 define float @test2_bitcast() {
70 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
71 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
74 %v0 = load i32, i32* %a0
75 %v1 = load i32, i32* %a1
76 %cond = icmp sle i32 %v0, %v1
77 %select = select i1 %cond, i32* %a1, i32* %a0
78 %select.bc = bitcast i32* %select to float*
79 %result = load float, float* %select.bc
83 ; CHECK-LABEL: @test2_addrspacecast(
85 ; CHECK-NEXT: alloca i32
87 ; CHECK: %select = select i1 %cond, i32* %a.sroa.3, i32* %a.sroa.0
88 ; CHECK-NEXT: %select.asc = addrspacecast i32* %select to i32 addrspace(1)*
89 ; CHECK-NEXT: load i32, i32 addrspace(1)* %select.asc, align 4
90 define i32 @test2_addrspacecast() {
93 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
94 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
97 %v0 = load i32, i32* %a0
98 %v1 = load i32, i32* %a1
99 %cond = icmp sle i32 %v0, %v1
100 %select = select i1 %cond, i32* %a1, i32* %a0
101 %select.asc = addrspacecast i32* %select to i32 addrspace(1)*
102 %result = load i32, i32 addrspace(1)* %select.asc
106 define i32 @test3(i32 %x) {
107 ; CHECK-LABEL: @test3(
109 %a = alloca [2 x i32]
112 ; Note that we build redundant GEPs here to ensure that having different GEPs
113 ; into the same alloca partation continues to work with PHI speculation. This
114 ; was the underlying cause of PR13926.
115 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
116 %a0b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
117 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
118 %a1b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
119 store i32 0, i32* %a0
120 store i32 1, i32* %a1
123 switch i32 %x, label %bb0 [ i32 1, label %bb1
149 %phi = phi i32* [ %a1, %bb0 ], [ %a0, %bb1 ], [ %a0, %bb2 ], [ %a1, %bb3 ],
150 [ %a1b, %bb4 ], [ %a0b, %bb5 ], [ %a0b, %bb6 ], [ %a1b, %bb7 ]
151 ; CHECK: phi i32 [ 1, %{{.*}} ], [ 0, %{{.*}} ], [ 0, %{{.*}} ], [ 1, %{{.*}} ], [ 1, %{{.*}} ], [ 0, %{{.*}} ], [ 0, %{{.*}} ], [ 1, %{{.*}} ]
153 %result = load i32, i32* %phi
157 define i32 @test4() {
158 ; CHECK-LABEL: @test4(
160 %a = alloca [2 x i32]
163 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
164 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
165 store i32 0, i32* %a0
166 store i32 1, i32* %a1
167 %v0 = load i32, i32* %a0
168 %v1 = load i32, i32* %a1
172 %cond = icmp sle i32 %v0, %v1
173 %select = select i1 %cond, i32* %a0, i32* %a0
176 %result = load i32, i32* %select
181 define i32 @test5(i32* %b) {
182 ; CHECK-LABEL: @test5(
184 %a = alloca [2 x i32]
187 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
188 store i32 1, i32* %a1
191 %select = select i1 true, i32* %a1, i32* %b
194 %result = load i32, i32* %select
201 declare void @f(i32*, i32*)
203 define i32 @test6(i32* %b) {
204 ; CHECK-LABEL: @test6(
206 %a = alloca [2 x i32]
210 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
211 store i32 1, i32* %a1
213 %select = select i1 true, i32* %a1, i32* %b
214 %select2 = select i1 false, i32* %a1, i32* %b
215 %select3 = select i1 false, i32* %c, i32* %b
216 ; CHECK: %[[select2:.*]] = select i1 false, i32* undef, i32* %b
217 ; CHECK: %[[select3:.*]] = select i1 false, i32* undef, i32* %b
219 ; Note, this would potentially escape the alloca pointer except for the
220 ; constant folding of the select.
221 call void @f(i32* %select2, i32* %select3)
222 ; CHECK: call void @f(i32* %[[select2]], i32* %[[select3]])
225 %result = load i32, i32* %select
228 %dead = load i32, i32* %c
234 define i32 @test7() {
235 ; CHECK-LABEL: @test7(
240 br i1 undef, label %good, label %bad
243 %Y1 = getelementptr i32, i32* %X, i64 0
244 store i32 0, i32* %Y1
248 %Y2 = getelementptr i32, i32* %X, i64 1
249 store i32 0, i32* %Y2
253 %P = phi i32* [ %Y1, %good ], [ %Y2, %bad ]
254 ; CHECK: %[[phi:.*]] = phi i32 [ 0, %good ],
255 %Z2 = load i32, i32* %P
257 ; CHECK: ret i32 %[[phi]]
260 define i32 @test8(i32 %b, i32* %ptr) {
261 ; Ensure that we rewrite allocas to the used type when that use is hidden by
262 ; a PHI that can be speculated.
263 ; CHECK-LABEL: @test8(
266 ; CHECK: %[[value:.*]] = load i32, i32* %ptr
268 ; CHECK: %[[result:.*]] = phi i32 [ undef, %else ], [ %[[value]], %then ]
269 ; CHECK-NEXT: ret i32 %[[result]]
273 %test = icmp ne i32 %b, 0
274 br i1 %test, label %then, label %else
280 %bitcast = bitcast float* %f to i32*
284 %phi = phi i32* [ %bitcast, %else ], [ %ptr, %then ]
285 %loaded = load i32, i32* %phi, align 4
289 define i32 @test9(i32 %b, i32* %ptr) {
290 ; Same as @test8 but for a select rather than a PHI node.
291 ; CHECK-LABEL: @test9(
294 ; CHECK: %[[value:.*]] = load i32, i32* %ptr
296 ; CHECK: %[[result:.*]] = select i1 %{{.*}}, i32 undef, i32 %[[value]]
297 ; CHECK-NEXT: ret i32 %[[result]]
301 store i32 0, i32* %ptr
302 %test = icmp ne i32 %b, 0
303 %bitcast = bitcast float* %f to i32*
304 %select = select i1 %test, i32* %bitcast, i32* %ptr
305 %loaded = load i32, i32* %select, align 4
309 define float @test10(i32 %b, float* %ptr) {
310 ; Don't try to promote allocas which are not elligible for it even after
311 ; rewriting due to the necessity of inserting bitcasts when speculating a PHI
313 ; CHECK-LABEL: @test10(
314 ; CHECK: %[[alloca:.*]] = alloca
315 ; CHECK: %[[argvalue:.*]] = load float, float* %ptr
316 ; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
317 ; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
318 ; CHECK: %[[result:.*]] = phi float [ %[[allocavalue]], %else ], [ %[[argvalue]], %then ]
319 ; CHECK-NEXT: ret float %[[result]]
323 store double 0.0, double* %f
324 %test = icmp ne i32 %b, 0
325 br i1 %test, label %then, label %else
331 %bitcast = bitcast double* %f to float*
335 %phi = phi float* [ %bitcast, %else ], [ %ptr, %then ]
336 %loaded = load float, float* %phi, align 4
340 define float @test11(i32 %b, float* %ptr) {
341 ; Same as @test10 but for a select rather than a PHI node.
342 ; CHECK-LABEL: @test11(
343 ; CHECK: %[[alloca:.*]] = alloca
344 ; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
345 ; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
346 ; CHECK: %[[argvalue:.*]] = load float, float* %ptr
347 ; CHECK: %[[result:.*]] = select i1 %{{.*}}, float %[[allocavalue]], float %[[argvalue]]
348 ; CHECK-NEXT: ret float %[[result]]
352 store double 0.0, double* %f
353 store float 0.0, float* %ptr
354 %test = icmp ne i32 %b, 0
355 %bitcast = bitcast double* %f to float*
356 %select = select i1 %test, float* %bitcast, float* %ptr
357 %loaded = load float, float* %select, align 4
361 define i32 @test12(i32 %x, i32* %p) {
362 ; Ensure we don't crash or fail to nuke dead selects of allocas if no load is
364 ; CHECK-LABEL: @test12(
371 store i32 %x, i32* %a
372 %dead = select i1 undef, i32* %a, i32* %p
373 %load = load i32, i32* %a
377 define i32 @test13(i32 %x, i32* %p) {
378 ; Ensure we don't crash or fail to nuke dead phis of allocas if no load is ever
380 ; CHECK-LABEL: @test13(
387 store i32 %x, i32* %a
391 %phi = phi i32* [ %p, %entry ], [ %a, %loop ]
392 br i1 undef, label %loop, label %exit
395 %load = load i32, i32* %a
399 define i32 @test14(i1 %b1, i1 %b2, i32* %ptr) {
400 ; Check for problems when there are both selects and phis and one is
401 ; speculatable toward promotion but the other is not. That should block all of
403 ; CHECK-LABEL: @test14(
417 %f.select = select i1 %b1, i32* %f, i32* %ptr
418 br i1 %b2, label %then, label %else
427 %f.phi = phi i32* [ %f, %then ], [ %f.select, %else ]
428 %g.phi = phi i32* [ %g, %then ], [ %ptr, %else ]
429 %f.loaded = load i32, i32* %f.phi
430 %g.select = select i1 %b1, i32* %g, i32* %g.phi
431 %g.loaded = load i32, i32* %g.select
432 %result = add i32 %f.loaded, %g.loaded
436 define i32 @PR13905() {
437 ; Check a pattern where we have a chain of dead phi nodes to ensure they are
438 ; deleted and promotion can proceed.
439 ; CHECK-LABEL: @PR13905(
440 ; CHECK-NOT: alloca i32
441 ; CHECK: ret i32 undef
446 br i1 undef, label %loop1, label %exit
449 %phi1 = phi i32* [ null, %entry ], [ %h, %loop1 ], [ %h, %loop2 ]
450 br i1 undef, label %loop1, label %loop2
453 br i1 undef, label %loop1, label %exit
456 %phi2 = phi i32* [ %phi1, %loop2 ], [ null, %entry ]
460 define i32 @PR13906() {
461 ; Another pattern which can lead to crashes due to failing to clear out dead
462 ; PHI nodes or select nodes. This triggers subtly differently from the above
463 ; cases because the PHI node is (recursively) alive, but the select is dead.
464 ; CHECK-LABEL: @PR13906(
473 %d.0 = phi i32* [ undef, %entry ], [ %c, %if.then ], [ %d.0, %for.cond ]
474 br i1 undef, label %if.then, label %for.cond
477 %tmpcast.d.0 = select i1 undef, i32* %c, i32* %d.0
481 define i64 @PR14132(i1 %flag) {
482 ; CHECK-LABEL: @PR14132(
483 ; Here we form a PHI-node by promoting the pointer alloca first, and then in
484 ; order to promote the other two allocas, we speculate the load of the
485 ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
486 ; alloca. While this is a bit dubious, we were asserting on trying to
487 ; rewrite it. The trick is that the code using the value may carefully take
488 ; steps to only use the not-undef bits, and so we need to at least loosely
491 %a = alloca i64, align 8
492 %b = alloca i8, align 8
493 %ptr = alloca i64*, align 8
496 %ptr.cast = bitcast i64** %ptr to i8**
497 store i64 0, i64* %a, align 8
498 store i8 1, i8* %b, align 8
499 store i64* %a, i64** %ptr, align 8
500 br i1 %flag, label %if.then, label %if.end
503 store i8* %b, i8** %ptr.cast, align 8
506 ; CHECK: %[[ext:.*]] = zext i8 1 to i64
509 %tmp = load i64*, i64** %ptr, align 8
510 %result = load i64, i64* %tmp, align 8
512 ; CHECK: %[[result:.*]] = phi i64 [ %[[ext]], %if.then ], [ 0, %entry ]
515 ; CHECK-NEXT: ret i64 %[[result]]
518 define float @PR16687(i64 %x, i1 %flag) {
519 ; CHECK-LABEL: @PR16687(
520 ; Check that even when we try to speculate the same phi twice (in two slices)
521 ; on an otherwise promotable construct, we don't get ahead of ourselves and try
522 ; to promote one of the slices prior to speculating it.
525 %a = alloca i64, align 8
526 store i64 %x, i64* %a
527 br i1 %flag, label %then, label %else
530 ; CHECK: %[[lo:.*]] = trunc i64 %x to i32
531 ; CHECK: %[[shift:.*]] = lshr i64 %x, 32
532 ; CHECK: %[[hi:.*]] = trunc i64 %[[shift]] to i32
535 %a.f = bitcast i64* %a to float*
537 ; CHECK: %[[lo_cast:.*]] = bitcast i32 %[[lo]] to float
540 %a.raw = bitcast i64* %a to i8*
541 %a.raw.4 = getelementptr i8, i8* %a.raw, i64 4
542 %a.raw.4.f = bitcast i8* %a.raw.4 to float*
544 ; CHECK: %[[hi_cast:.*]] = bitcast i32 %[[hi]] to float
547 %a.phi.f = phi float* [ %a.f, %then ], [ %a.raw.4.f, %else ]
548 %f = load float, float* %a.phi.f
550 ; CHECK: %[[phi:.*]] = phi float [ %[[lo_cast]], %then ], [ %[[hi_cast]], %else ]
552 ; CHECK: ret float %[[phi]]
555 ; Verifies we fixed PR20425. We should be able to promote all alloca's to
556 ; registers in this test.
560 ; %2 = phi(%0, %1) // == slice
561 define float @simplify_phi_nodes_that_equal_slice(i1 %cond, float* %temp) {
562 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice(
564 %arr = alloca [4 x float], align 4
566 br i1 %cond, label %then, label %else
569 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
570 store float 1.000000e+00, float* %0, align 4
574 %1 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
575 store float 2.000000e+00, float* %1, align 4
579 %2 = phi float* [ %0, %then ], [ %1, %else ]
580 store float 0.000000e+00, float* %temp, align 4
581 %3 = load float, float* %2, align 4
585 ; A slightly complicated example for PR20425.
588 ; %1 = phi(%0) // == slice
590 ; %3 = phi(%1, %2) // == slice
591 define float @simplify_phi_nodes_that_equal_slice_2(i1 %cond, float* %temp) {
592 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice_2(
594 %arr = alloca [4 x float], align 4
596 br i1 %cond, label %then, label %else
599 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
600 store float 1.000000e+00, float* %0, align 4
604 %1 = phi float* [ %0, %then ]
605 store float 2.000000e+00, float* %1, align 4
609 %2 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
610 store float 3.000000e+00, float* %2, align 4
614 %3 = phi float* [ %1, %then2 ], [ %2, %else ]
615 store float 0.000000e+00, float* %temp, align 4
616 %4 = load float, float* %3, align 4
620 %struct.S = type { i32 }
622 ; Verifies we fixed PR20822. We have a foldable PHI feeding a speculatable PHI
623 ; which requires the rewriting of the speculated PHI to handle insertion
624 ; when the incoming pointer is itself from a PHI node. We would previously
625 ; insert a bitcast instruction *before* a PHI, producing an invalid module;
626 ; make sure we insert *after* the first non-PHI instruction.
627 define void @PR20822() {
628 ; CHECK-LABEL: @PR20822(
630 %f = alloca %struct.S, align 4
631 ; CHECK: %[[alloca:.*]] = alloca
632 br i1 undef, label %if.end, label %for.cond
634 for.cond: ; preds = %for.cond, %entry
637 if.end: ; preds = %for.cond, %entry
638 %f2 = phi %struct.S* [ %f, %entry ], [ %f, %for.cond ]
640 ; CHECK: %[[cast:.*]] = bitcast i32* %[[alloca]] to %struct.S*
641 phi i32 [ undef, %entry ], [ undef, %for.cond ]
642 br i1 undef, label %if.then5, label %if.then2
644 if.then2: ; preds = %if.end
647 if.then5: ; preds = %if.then2, %if.end
648 %f1 = phi %struct.S* [ undef, %if.then2 ], [ %f2, %if.end ]
649 ; CHECK: phi {{.*}} %[[cast]]
650 store %struct.S undef, %struct.S* %f1, align 4
654 define i32 @phi_align(i32* %z) {
655 ; CHECK-LABEL: @phi_align(
657 %a = alloca [8 x i8], align 8
658 ; CHECK: alloca [7 x i8]
660 %a0x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 1
661 %a0 = bitcast i8* %a0x to i32*
662 %a1x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 4
663 %a1 = bitcast i8* %a1x to i32*
664 ; CHECK: store i32 0, {{.*}}, align 1
665 store i32 0, i32* %a0, align 1
666 ; CHECK: store i32 1, {{.*}}, align 1
667 store i32 1, i32* %a1, align 4
668 ; CHECK: load {{.*}}, align 1
669 %v0 = load i32, i32* %a0, align 1
670 ; CHECK: load {{.*}}, align 1
671 %v1 = load i32, i32* %a1, align 4
672 %cond = icmp sle i32 %v0, %v1
673 br i1 %cond, label %then, label %exit
679 ; CHECK: %phi = phi i32* [ {{.*}}, %then ], [ %z, %entry ]
680 ; CHECK-NEXT: %result = load i32, i32* %phi, align 1
681 %phi = phi i32* [ %a1, %then ], [ %z, %entry ]
682 %result = load i32, i32* %phi, align 4
686 ; Don't speculate a load based on an earlier volatile operation.
687 define i8 @volatile_select(i8* %p, i1 %b) {
688 ; CHECK-LABEL: @volatile_select(
689 ; CHECK: select i1 %b, i8* %p, i8* %p2
692 store volatile i8 0, i8* %p
693 %px = select i1 %b, i8* %p, i8* %p2
694 %v2 = load i8, i8* %px