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 define i32 @test3(i32 %x) {
57 ; CHECK-LABEL: @test3(
62 ; Note that we build redundant GEPs here to ensure that having different GEPs
63 ; into the same alloca partation continues to work with PHI speculation. This
64 ; was the underlying cause of PR13926.
65 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
66 %a0b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
67 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
68 %a1b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
73 switch i32 %x, label %bb0 [ i32 1, label %bb1
99 %phi = phi i32* [ %a1, %bb0 ], [ %a0, %bb1 ], [ %a0, %bb2 ], [ %a1, %bb3 ],
100 [ %a1b, %bb4 ], [ %a0b, %bb5 ], [ %a0b, %bb6 ], [ %a1b, %bb7 ]
101 ; CHECK: phi i32 [ 1, %{{.*}} ], [ 0, %{{.*}} ], [ 0, %{{.*}} ], [ 1, %{{.*}} ], [ 1, %{{.*}} ], [ 0, %{{.*}} ], [ 0, %{{.*}} ], [ 1, %{{.*}} ]
103 %result = load i32, i32* %phi
107 define i32 @test4() {
108 ; CHECK-LABEL: @test4(
110 %a = alloca [2 x i32]
113 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
114 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
115 store i32 0, i32* %a0
116 store i32 1, i32* %a1
117 %v0 = load i32, i32* %a0
118 %v1 = load i32, i32* %a1
122 %cond = icmp sle i32 %v0, %v1
123 %select = select i1 %cond, i32* %a0, i32* %a0
126 %result = load i32, i32* %select
131 define i32 @test5(i32* %b) {
132 ; CHECK-LABEL: @test5(
134 %a = alloca [2 x i32]
137 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
138 store i32 1, i32* %a1
141 %select = select i1 true, i32* %a1, i32* %b
144 %result = load i32, i32* %select
151 declare void @f(i32*, i32*)
153 define i32 @test6(i32* %b) {
154 ; CHECK-LABEL: @test6(
156 %a = alloca [2 x i32]
160 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
161 store i32 1, i32* %a1
163 %select = select i1 true, i32* %a1, i32* %b
164 %select2 = select i1 false, i32* %a1, i32* %b
165 %select3 = select i1 false, i32* %c, i32* %b
166 ; CHECK: %[[select2:.*]] = select i1 false, i32* undef, i32* %b
167 ; CHECK: %[[select3:.*]] = select i1 false, i32* undef, i32* %b
169 ; Note, this would potentially escape the alloca pointer except for the
170 ; constant folding of the select.
171 call void @f(i32* %select2, i32* %select3)
172 ; CHECK: call void @f(i32* %[[select2]], i32* %[[select3]])
175 %result = load i32, i32* %select
178 %dead = load i32, i32* %c
184 define i32 @test7() {
185 ; CHECK-LABEL: @test7(
190 br i1 undef, label %good, label %bad
193 %Y1 = getelementptr i32, i32* %X, i64 0
194 store i32 0, i32* %Y1
198 %Y2 = getelementptr i32, i32* %X, i64 1
199 store i32 0, i32* %Y2
203 %P = phi i32* [ %Y1, %good ], [ %Y2, %bad ]
204 ; CHECK: %[[phi:.*]] = phi i32 [ 0, %good ],
205 %Z2 = load i32, i32* %P
207 ; CHECK: ret i32 %[[phi]]
210 define i32 @test8(i32 %b, i32* %ptr) {
211 ; Ensure that we rewrite allocas to the used type when that use is hidden by
212 ; a PHI that can be speculated.
213 ; CHECK-LABEL: @test8(
216 ; CHECK: %[[value:.*]] = load i32, i32* %ptr
218 ; CHECK: %[[result:.*]] = phi i32 [ undef, %else ], [ %[[value]], %then ]
219 ; CHECK-NEXT: ret i32 %[[result]]
223 %test = icmp ne i32 %b, 0
224 br i1 %test, label %then, label %else
230 %bitcast = bitcast float* %f to i32*
234 %phi = phi i32* [ %bitcast, %else ], [ %ptr, %then ]
235 %loaded = load i32, i32* %phi, align 4
239 define i32 @test9(i32 %b, i32* %ptr) {
240 ; Same as @test8 but for a select rather than a PHI node.
241 ; CHECK-LABEL: @test9(
244 ; CHECK: %[[value:.*]] = load i32, i32* %ptr
246 ; CHECK: %[[result:.*]] = select i1 %{{.*}}, i32 undef, i32 %[[value]]
247 ; CHECK-NEXT: ret i32 %[[result]]
251 store i32 0, i32* %ptr
252 %test = icmp ne i32 %b, 0
253 %bitcast = bitcast float* %f to i32*
254 %select = select i1 %test, i32* %bitcast, i32* %ptr
255 %loaded = load i32, i32* %select, align 4
259 define float @test10(i32 %b, float* %ptr) {
260 ; Don't try to promote allocas which are not elligible for it even after
261 ; rewriting due to the necessity of inserting bitcasts when speculating a PHI
263 ; CHECK-LABEL: @test10(
264 ; CHECK: %[[alloca:.*]] = alloca
265 ; CHECK: %[[argvalue:.*]] = load float, float* %ptr
266 ; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
267 ; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
268 ; CHECK: %[[result:.*]] = phi float [ %[[allocavalue]], %else ], [ %[[argvalue]], %then ]
269 ; CHECK-NEXT: ret float %[[result]]
273 store double 0.0, double* %f
274 %test = icmp ne i32 %b, 0
275 br i1 %test, label %then, label %else
281 %bitcast = bitcast double* %f to float*
285 %phi = phi float* [ %bitcast, %else ], [ %ptr, %then ]
286 %loaded = load float, float* %phi, align 4
290 define float @test11(i32 %b, float* %ptr) {
291 ; Same as @test10 but for a select rather than a PHI node.
292 ; CHECK-LABEL: @test11(
293 ; CHECK: %[[alloca:.*]] = alloca
294 ; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
295 ; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
296 ; CHECK: %[[argvalue:.*]] = load float, float* %ptr
297 ; CHECK: %[[result:.*]] = select i1 %{{.*}}, float %[[allocavalue]], float %[[argvalue]]
298 ; CHECK-NEXT: ret float %[[result]]
302 store double 0.0, double* %f
303 store float 0.0, float* %ptr
304 %test = icmp ne i32 %b, 0
305 %bitcast = bitcast double* %f to float*
306 %select = select i1 %test, float* %bitcast, float* %ptr
307 %loaded = load float, float* %select, align 4
311 define i32 @test12(i32 %x, i32* %p) {
312 ; Ensure we don't crash or fail to nuke dead selects of allocas if no load is
314 ; CHECK-LABEL: @test12(
321 store i32 %x, i32* %a
322 %dead = select i1 undef, i32* %a, i32* %p
323 %load = load i32, i32* %a
327 define i32 @test13(i32 %x, i32* %p) {
328 ; Ensure we don't crash or fail to nuke dead phis of allocas if no load is ever
330 ; CHECK-LABEL: @test13(
337 store i32 %x, i32* %a
341 %phi = phi i32* [ %p, %entry ], [ %a, %loop ]
342 br i1 undef, label %loop, label %exit
345 %load = load i32, i32* %a
349 define i32 @test14(i1 %b1, i1 %b2, i32* %ptr) {
350 ; Check for problems when there are both selects and phis and one is
351 ; speculatable toward promotion but the other is not. That should block all of
353 ; CHECK-LABEL: @test14(
367 %f.select = select i1 %b1, i32* %f, i32* %ptr
368 br i1 %b2, label %then, label %else
377 %f.phi = phi i32* [ %f, %then ], [ %f.select, %else ]
378 %g.phi = phi i32* [ %g, %then ], [ %ptr, %else ]
379 %f.loaded = load i32, i32* %f.phi
380 %g.select = select i1 %b1, i32* %g, i32* %g.phi
381 %g.loaded = load i32, i32* %g.select
382 %result = add i32 %f.loaded, %g.loaded
386 define i32 @PR13905() {
387 ; Check a pattern where we have a chain of dead phi nodes to ensure they are
388 ; deleted and promotion can proceed.
389 ; CHECK-LABEL: @PR13905(
390 ; CHECK-NOT: alloca i32
391 ; CHECK: ret i32 undef
396 br i1 undef, label %loop1, label %exit
399 %phi1 = phi i32* [ null, %entry ], [ %h, %loop1 ], [ %h, %loop2 ]
400 br i1 undef, label %loop1, label %loop2
403 br i1 undef, label %loop1, label %exit
406 %phi2 = phi i32* [ %phi1, %loop2 ], [ null, %entry ]
410 define i32 @PR13906() {
411 ; Another pattern which can lead to crashes due to failing to clear out dead
412 ; PHI nodes or select nodes. This triggers subtly differently from the above
413 ; cases because the PHI node is (recursively) alive, but the select is dead.
414 ; CHECK-LABEL: @PR13906(
423 %d.0 = phi i32* [ undef, %entry ], [ %c, %if.then ], [ %d.0, %for.cond ]
424 br i1 undef, label %if.then, label %for.cond
427 %tmpcast.d.0 = select i1 undef, i32* %c, i32* %d.0
431 define i64 @PR14132(i1 %flag) {
432 ; CHECK-LABEL: @PR14132(
433 ; Here we form a PHI-node by promoting the pointer alloca first, and then in
434 ; order to promote the other two allocas, we speculate the load of the
435 ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
436 ; alloca. While this is a bit dubious, we were asserting on trying to
437 ; rewrite it. The trick is that the code using the value may carefully take
438 ; steps to only use the not-undef bits, and so we need to at least loosely
441 %a = alloca i64, align 8
442 %b = alloca i8, align 8
443 %ptr = alloca i64*, align 8
446 %ptr.cast = bitcast i64** %ptr to i8**
447 store i64 0, i64* %a, align 8
448 store i8 1, i8* %b, align 8
449 store i64* %a, i64** %ptr, align 8
450 br i1 %flag, label %if.then, label %if.end
453 store i8* %b, i8** %ptr.cast, align 8
456 ; CHECK: %[[ext:.*]] = zext i8 1 to i64
459 %tmp = load i64*, i64** %ptr, align 8
460 %result = load i64, i64* %tmp, align 8
462 ; CHECK: %[[result:.*]] = phi i64 [ %[[ext]], %if.then ], [ 0, %entry ]
465 ; CHECK-NEXT: ret i64 %[[result]]
468 define float @PR16687(i64 %x, i1 %flag) {
469 ; CHECK-LABEL: @PR16687(
470 ; Check that even when we try to speculate the same phi twice (in two slices)
471 ; on an otherwise promotable construct, we don't get ahead of ourselves and try
472 ; to promote one of the slices prior to speculating it.
475 %a = alloca i64, align 8
476 store i64 %x, i64* %a
477 br i1 %flag, label %then, label %else
480 ; CHECK: %[[lo:.*]] = trunc i64 %x to i32
481 ; CHECK: %[[shift:.*]] = lshr i64 %x, 32
482 ; CHECK: %[[hi:.*]] = trunc i64 %[[shift]] to i32
485 %a.f = bitcast i64* %a to float*
487 ; CHECK: %[[lo_cast:.*]] = bitcast i32 %[[lo]] to float
490 %a.raw = bitcast i64* %a to i8*
491 %a.raw.4 = getelementptr i8, i8* %a.raw, i64 4
492 %a.raw.4.f = bitcast i8* %a.raw.4 to float*
494 ; CHECK: %[[hi_cast:.*]] = bitcast i32 %[[hi]] to float
497 %a.phi.f = phi float* [ %a.f, %then ], [ %a.raw.4.f, %else ]
498 %f = load float, float* %a.phi.f
500 ; CHECK: %[[phi:.*]] = phi float [ %[[lo_cast]], %then ], [ %[[hi_cast]], %else ]
502 ; CHECK: ret float %[[phi]]
505 ; Verifies we fixed PR20425. We should be able to promote all alloca's to
506 ; registers in this test.
510 ; %2 = phi(%0, %1) // == slice
511 define float @simplify_phi_nodes_that_equal_slice(i1 %cond, float* %temp) {
512 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice(
514 %arr = alloca [4 x float], align 4
516 br i1 %cond, label %then, label %else
519 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
520 store float 1.000000e+00, float* %0, align 4
524 %1 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
525 store float 2.000000e+00, float* %1, align 4
529 %2 = phi float* [ %0, %then ], [ %1, %else ]
530 store float 0.000000e+00, float* %temp, align 4
531 %3 = load float, float* %2, align 4
535 ; A slightly complicated example for PR20425.
538 ; %1 = phi(%0) // == slice
540 ; %3 = phi(%1, %2) // == slice
541 define float @simplify_phi_nodes_that_equal_slice_2(i1 %cond, float* %temp) {
542 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice_2(
544 %arr = alloca [4 x float], align 4
546 br i1 %cond, label %then, label %else
549 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
550 store float 1.000000e+00, float* %0, align 4
554 %1 = phi float* [ %0, %then ]
555 store float 2.000000e+00, float* %1, align 4
559 %2 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
560 store float 3.000000e+00, float* %2, align 4
564 %3 = phi float* [ %1, %then2 ], [ %2, %else ]
565 store float 0.000000e+00, float* %temp, align 4
566 %4 = load float, float* %3, align 4
570 %struct.S = type { i32 }
572 ; Verifies we fixed PR20822. We have a foldable PHI feeding a speculatable PHI
573 ; which requires the rewriting of the speculated PHI to handle insertion
574 ; when the incoming pointer is itself from a PHI node. We would previously
575 ; insert a bitcast instruction *before* a PHI, producing an invalid module;
576 ; make sure we insert *after* the first non-PHI instruction.
577 define void @PR20822() {
578 ; CHECK-LABEL: @PR20822(
580 %f = alloca %struct.S, align 4
581 ; CHECK: %[[alloca:.*]] = alloca
582 br i1 undef, label %if.end, label %for.cond
584 for.cond: ; preds = %for.cond, %entry
587 if.end: ; preds = %for.cond, %entry
588 %f2 = phi %struct.S* [ %f, %entry ], [ %f, %for.cond ]
590 ; CHECK: %[[cast:.*]] = bitcast i32* %[[alloca]] to %struct.S*
591 phi i32 [ undef, %entry ], [ undef, %for.cond ]
592 br i1 undef, label %if.then5, label %if.then2
594 if.then2: ; preds = %if.end
597 if.then5: ; preds = %if.then2, %if.end
598 %f1 = phi %struct.S* [ undef, %if.then2 ], [ %f2, %if.end ]
599 ; CHECK: phi {{.*}} %[[cast]]
600 store %struct.S undef, %struct.S* %f1, align 4