1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes='sroa<preserve-cfg>' -S | FileCheck %s --check-prefixes=CHECK,CHECK-PRESERVE-CFG
3 ; RUN: opt < %s -passes='sroa<modify-cfg>' -S | FileCheck %s --check-prefixes=CHECK,CHECK-MODIFY-CFG
4 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"
9 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
10 ; CHECK-NEXT: br i1 [[COND]], label [[THEN:%.*]], label [[EXIT:%.*]]
12 ; CHECK-NEXT: br label [[EXIT]]
14 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ 1, [[THEN]] ], [ 0, [[ENTRY:%.*]] ]
15 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
20 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
23 %v0 = load i32, ptr %a
24 %v1 = load i32, ptr %a1
26 %cond = icmp sle i32 %v0, %v1
27 br i1 %cond, label %then, label %exit
33 %phi = phi ptr [ %a1, %then ], [ %a, %entry ]
35 %result = load i32, ptr %phi
40 ; CHECK-LABEL: @test2(
42 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
43 ; CHECK-NEXT: [[RESULT_SROA_SPECULATED:%.*]] = select i1 [[COND]], i32 1, i32 0
44 ; CHECK-NEXT: ret i32 [[RESULT_SROA_SPECULATED]]
49 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
52 %v0 = load i32, ptr %a
53 %v1 = load i32, ptr %a1
55 %cond = icmp sle i32 %v0, %v1
56 %select = select i1 %cond, ptr %a1, ptr %a
58 %result = load i32, ptr %select
62 define float @test2_bitcast() {
63 ; CHECK-LABEL: @test2_bitcast(
65 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
66 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 1 to float
67 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 0 to float
68 ; CHECK-NEXT: [[RESULT_SROA_SPECULATED:%.*]] = select i1 [[COND]], float [[TMP0]], float [[TMP1]]
69 ; CHECK-NEXT: ret float [[RESULT_SROA_SPECULATED]]
73 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
76 %v0 = load i32, ptr %a
77 %v1 = load i32, ptr %a1
78 %cond = icmp sle i32 %v0, %v1
79 %select = select i1 %cond, ptr %a1, ptr %a
80 %result = load float, ptr %select
84 define i32 @test2_addrspacecast() {
85 ; CHECK-LABEL: @test2_addrspacecast(
87 ; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
88 ; CHECK-NEXT: [[A_SROA_3:%.*]] = alloca i32, align 4
89 ; CHECK-NEXT: store i32 0, ptr [[A_SROA_0]], align 4
90 ; CHECK-NEXT: store i32 1, ptr [[A_SROA_3]], align 4
91 ; CHECK-NEXT: [[A_SROA_0_0_A_SROA_0_0_V0:%.*]] = load i32, ptr [[A_SROA_0]], align 4
92 ; CHECK-NEXT: [[A_SROA_3_0_A_SROA_3_4_V1:%.*]] = load i32, ptr [[A_SROA_3]], align 4
93 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 [[A_SROA_0_0_A_SROA_0_0_V0]], [[A_SROA_3_0_A_SROA_3_4_V1]]
94 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], ptr [[A_SROA_3]], ptr [[A_SROA_0]]
95 ; CHECK-NEXT: [[SELECT_ASC:%.*]] = addrspacecast ptr [[SELECT]] to ptr addrspace(1)
96 ; CHECK-NEXT: [[RESULT:%.*]] = load i32, ptr addrspace(1) [[SELECT_ASC]], align 4
97 ; CHECK-NEXT: ret i32 [[RESULT]]
100 %a = alloca [2 x i32]
101 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
104 %v0 = load i32, ptr %a
105 %v1 = load i32, ptr %a1
106 %cond = icmp sle i32 %v0, %v1
107 %select = select i1 %cond, ptr %a1, ptr %a
108 %select.asc = addrspacecast ptr %select to ptr addrspace(1)
109 %result = load i32, ptr addrspace(1) %select.asc
113 define i32 @test3(i32 %x) {
114 ; CHECK-LABEL: @test3(
116 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[BB0:%.*]] [
117 ; CHECK-NEXT: i32 1, label [[BB1:%.*]]
118 ; CHECK-NEXT: i32 2, label [[BB2:%.*]]
119 ; CHECK-NEXT: i32 3, label [[BB3:%.*]]
120 ; CHECK-NEXT: i32 4, label [[BB4:%.*]]
121 ; CHECK-NEXT: i32 5, label [[BB5:%.*]]
122 ; CHECK-NEXT: i32 6, label [[BB6:%.*]]
123 ; CHECK-NEXT: i32 7, label [[BB7:%.*]]
126 ; CHECK-NEXT: br label [[EXIT:%.*]]
128 ; CHECK-NEXT: br label [[EXIT]]
130 ; CHECK-NEXT: br label [[EXIT]]
132 ; CHECK-NEXT: br label [[EXIT]]
134 ; CHECK-NEXT: br label [[EXIT]]
136 ; CHECK-NEXT: br label [[EXIT]]
138 ; CHECK-NEXT: br label [[EXIT]]
140 ; CHECK-NEXT: br label [[EXIT]]
142 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ 1, [[BB0]] ], [ 0, [[BB1]] ], [ 0, [[BB2]] ], [ 1, [[BB3]] ], [ 1, [[BB4]] ], [ 0, [[BB5]] ], [ 0, [[BB6]] ], [ 1, [[BB7]] ]
143 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
146 %a = alloca [2 x i32]
148 ; Note that we build redundant GEPs here to ensure that having different GEPs
149 ; into the same alloca partation continues to work with PHI speculation. This
150 ; was the underlying cause of PR13926.
151 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
152 %a1b = getelementptr [2 x i32], ptr %a, i64 0, i32 1
156 switch i32 %x, label %bb0 [ i32 1, label %bb1
182 %phi = phi ptr [ %a1, %bb0 ], [ %a, %bb1 ], [ %a, %bb2 ], [ %a1, %bb3 ],
183 [ %a1b, %bb4 ], [ %a, %bb5 ], [ %a, %bb6 ], [ %a1b, %bb7 ]
185 %result = load i32, ptr %phi
189 define i32 @test4() {
190 ; CHECK-LABEL: @test4(
192 ; CHECK-NEXT: ret i32 0
195 %a = alloca [2 x i32]
197 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
200 %v0 = load i32, ptr %a
201 %v1 = load i32, ptr %a1
203 %cond = icmp sle i32 %v0, %v1
204 %select = select i1 %cond, ptr %a, ptr %a
206 %result = load i32, ptr %select
210 define i32 @test5(ptr %b) {
211 ; CHECK-LABEL: @test5(
213 ; CHECK-NEXT: ret i32 1
216 %a = alloca [2 x i32]
218 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
221 %select = select i1 true, ptr %a1, ptr %b
223 %result = load i32, ptr %select
228 declare void @f(ptr, ptr)
230 define i32 @test6(ptr %b) {
231 ; CHECK-LABEL: @test6(
233 ; CHECK-NEXT: [[SELECT2:%.*]] = select i1 false, ptr poison, ptr [[B:%.*]]
234 ; CHECK-NEXT: [[SELECT3:%.*]] = select i1 false, ptr poison, ptr [[B]]
235 ; CHECK-NEXT: call void @f(ptr [[SELECT2]], ptr [[SELECT3]])
236 ; CHECK-NEXT: ret i32 1
239 %a = alloca [2 x i32]
242 %a1 = getelementptr [2 x i32], ptr %a, i64 0, i32 1
245 %select = select i1 true, ptr %a1, ptr %b
246 %select2 = select i1 false, ptr %a1, ptr %b
247 %select3 = select i1 false, ptr %c, ptr %b
249 ; Note, this would potentially escape the alloca pointer except for the
250 ; constant folding of the select.
251 call void @f(ptr %select2, ptr %select3)
254 %result = load i32, ptr %select
256 %dead = load i32, ptr %c
261 define i32 @test7(i1 %c1) {
262 ; CHECK-LABEL: @test7(
264 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[GOOD:%.*]], label [[BAD:%.*]]
266 ; CHECK-NEXT: br label [[EXIT:%.*]]
268 ; CHECK-NEXT: [[P_SROA_SPECULATE_LOAD_BAD:%.*]] = load i32, ptr poison, align 4
269 ; CHECK-NEXT: br label [[EXIT]]
271 ; CHECK-NEXT: [[P_SROA_SPECULATED:%.*]] = phi i32 [ 0, [[GOOD]] ], [ [[P_SROA_SPECULATE_LOAD_BAD]], [[BAD]] ]
272 ; CHECK-NEXT: ret i32 [[P_SROA_SPECULATED]]
276 br i1 %c1, label %good, label %bad
283 %Y2 = getelementptr i32, ptr %X, i64 1
288 %P = phi ptr [ %X, %good ], [ %Y2, %bad ]
289 %Z2 = load i32, ptr %P
293 define i32 @test8(i32 %b, ptr %ptr) {
294 ; Ensure that we rewrite allocas to the used type when that use is hidden by
295 ; a PHI that can be speculated.
297 ; CHECK-LABEL: @test8(
299 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
300 ; CHECK-NEXT: br i1 [[TEST]], label [[THEN:%.*]], label [[ELSE:%.*]]
302 ; CHECK-NEXT: [[PHI_SROA_SPECULATE_LOAD_THEN:%.*]] = load i32, ptr [[PTR:%.*]], align 4
303 ; CHECK-NEXT: br label [[EXIT:%.*]]
305 ; CHECK-NEXT: br label [[EXIT]]
307 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ undef, [[ELSE]] ], [ [[PHI_SROA_SPECULATE_LOAD_THEN]], [[THEN]] ]
308 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
312 %test = icmp ne i32 %b, 0
313 br i1 %test, label %then, label %else
322 %phi = phi ptr [ %f, %else ], [ %ptr, %then ]
323 %loaded = load i32, ptr %phi, align 4
327 define i32 @test9(i32 %b, ptr %ptr) {
328 ; Same as @test8 but for a select rather than a PHI node.
330 ; CHECK-LABEL: @test9(
332 ; CHECK-NEXT: store i32 0, ptr [[PTR:%.*]], align 4
333 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
334 ; CHECK-NEXT: [[LOADED_SROA_SPECULATE_LOAD_FALSE:%.*]] = load i32, ptr [[PTR]], align 4
335 ; CHECK-NEXT: [[LOADED_SROA_SPECULATED:%.*]] = select i1 [[TEST]], i32 undef, i32 [[LOADED_SROA_SPECULATE_LOAD_FALSE]]
336 ; CHECK-NEXT: ret i32 [[LOADED_SROA_SPECULATED]]
340 store i32 0, ptr %ptr
341 %test = icmp ne i32 %b, 0
342 %select = select i1 %test, ptr %f, ptr %ptr
343 %loaded = load i32, ptr %select, align 4
347 ; We should not unconditionally load with sanitizers.
348 define i32 @test9_asan(i32 %b, ptr %ptr) sanitize_address {
349 ; Same as @test8 but for a select rather than a PHI node.
351 ; CHECK-PRESERVE-CFG-LABEL: @test9_asan(
352 ; CHECK-PRESERVE-CFG-NEXT: entry:
353 ; CHECK-PRESERVE-CFG-NEXT: [[F:%.*]] = alloca float, align 4
354 ; CHECK-PRESERVE-CFG-NEXT: store i32 0, ptr [[PTR:%.*]], align 4
355 ; CHECK-PRESERVE-CFG-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
356 ; CHECK-PRESERVE-CFG-NEXT: [[SELECT:%.*]] = select i1 [[TEST]], ptr [[F]], ptr [[PTR]]
357 ; CHECK-PRESERVE-CFG-NEXT: [[LOADED:%.*]] = load i32, ptr [[SELECT]], align 4
358 ; CHECK-PRESERVE-CFG-NEXT: ret i32 [[LOADED]]
360 ; CHECK-MODIFY-CFG-LABEL: @test9_asan(
361 ; CHECK-MODIFY-CFG-NEXT: entry:
362 ; CHECK-MODIFY-CFG-NEXT: store i32 0, ptr [[PTR:%.*]], align 4
363 ; CHECK-MODIFY-CFG-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
364 ; CHECK-MODIFY-CFG-NEXT: [[LOADED_ELSE_VAL:%.*]] = load i32, ptr [[PTR]], align 4
365 ; CHECK-MODIFY-CFG-NEXT: br i1 [[TEST]], label [[ENTRY_THEN:%.*]], label [[ENTRY_CONT:%.*]]
366 ; CHECK-MODIFY-CFG: entry.then:
367 ; CHECK-MODIFY-CFG-NEXT: br label [[ENTRY_CONT]]
368 ; CHECK-MODIFY-CFG: entry.cont:
369 ; CHECK-MODIFY-CFG-NEXT: [[LOADED:%.*]] = phi i32 [ undef, [[ENTRY_THEN]] ], [ [[LOADED_ELSE_VAL]], [[ENTRY:%.*]] ]
370 ; CHECK-MODIFY-CFG-NEXT: ret i32 [[LOADED]]
374 store i32 0, ptr %ptr
375 %test = icmp ne i32 %b, 0
376 %select = select i1 %test, ptr %f, ptr %ptr
377 %loaded = load i32, ptr %select, align 4
381 define float @test10(i32 %b, ptr %ptr) {
382 ; Don't try to promote allocas which are not elligible for it even after
383 ; rewriting due to the necessity of inserting bitcasts when speculating a PHI
386 ; CHECK-LABEL: @test10(
388 ; CHECK-NEXT: [[F:%.*]] = alloca double, align 8
389 ; CHECK-NEXT: store double 0.000000e+00, ptr [[F]], align 8
390 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
391 ; CHECK-NEXT: br i1 [[TEST]], label [[THEN:%.*]], label [[ELSE:%.*]]
393 ; CHECK-NEXT: [[PHI_SROA_SPECULATE_LOAD_THEN:%.*]] = load float, ptr [[PTR:%.*]], align 4
394 ; CHECK-NEXT: br label [[EXIT:%.*]]
396 ; CHECK-NEXT: [[F_0_PHI_SROA_SPECULATE_LOAD_ELSE:%.*]] = load float, ptr [[F]], align 8
397 ; CHECK-NEXT: br label [[EXIT]]
399 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi float [ [[F_0_PHI_SROA_SPECULATE_LOAD_ELSE]], [[ELSE]] ], [ [[PHI_SROA_SPECULATE_LOAD_THEN]], [[THEN]] ]
400 ; CHECK-NEXT: ret float [[PHI_SROA_SPECULATED]]
404 store double 0.0, ptr %f
405 %test = icmp ne i32 %b, 0
406 br i1 %test, label %then, label %else
415 %phi = phi ptr [ %f, %else ], [ %ptr, %then ]
416 %loaded = load float, ptr %phi, align 4
420 define float @test11(i32 %b, ptr %ptr) {
421 ; Same as @test10 but for a select rather than a PHI node.
423 ; CHECK-LABEL: @test11(
425 ; CHECK-NEXT: [[F:%.*]] = alloca double, align 8
426 ; CHECK-NEXT: store double 0.000000e+00, ptr [[F]], align 8
427 ; CHECK-NEXT: store float 0.000000e+00, ptr [[PTR:%.*]], align 4
428 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
429 ; CHECK-NEXT: [[F_0_LOADED_SROA_SPECULATE_LOAD_TRUE:%.*]] = load float, ptr [[F]], align 8
430 ; CHECK-NEXT: [[LOADED_SROA_SPECULATE_LOAD_FALSE:%.*]] = load float, ptr [[PTR]], align 4
431 ; CHECK-NEXT: [[LOADED_SROA_SPECULATED:%.*]] = select i1 [[TEST]], float [[F_0_LOADED_SROA_SPECULATE_LOAD_TRUE]], float [[LOADED_SROA_SPECULATE_LOAD_FALSE]]
432 ; CHECK-NEXT: ret float [[LOADED_SROA_SPECULATED]]
436 store double 0.0, ptr %f
437 store float 0.0, ptr %ptr
438 %test = icmp ne i32 %b, 0
439 %select = select i1 %test, ptr %f, ptr %ptr
440 %loaded = load float, ptr %select, align 4
444 define i32 @test12(i32 %x, ptr %p, i1 %c1) {
445 ; Ensure we don't crash or fail to nuke dead selects of allocas if no load is
448 ; CHECK-LABEL: @test12(
450 ; CHECK-NEXT: ret i32 [[X:%.*]]
455 %dead = select i1 %c1, ptr %a, ptr %p
456 %load = load i32, ptr %a
460 define i32 @test13(i32 %x, ptr %p, i1 %c1) {
461 ; Ensure we don't crash or fail to nuke dead phis of allocas if no load is ever
464 ; CHECK-LABEL: @test13(
466 ; CHECK-NEXT: br label [[LOOP:%.*]]
468 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOOP]], label [[EXIT:%.*]]
470 ; CHECK-NEXT: ret i32 [[X:%.*]]
478 %phi = phi ptr [ %p, %entry ], [ %a, %loop ]
479 br i1 %c1, label %loop, label %exit
482 %load = load i32, ptr %a
486 define i32 @test14(i1 %b1, i1 %b2, ptr %ptr) {
487 ; Check for problems when there are both selects and phis and one is
488 ; speculatable toward promotion but the other is not. That should block all of
491 ; CHECK-LABEL: @test14(
493 ; CHECK-NEXT: [[F:%.*]] = alloca i32, align 4
494 ; CHECK-NEXT: [[G:%.*]] = alloca i32, align 4
495 ; CHECK-NEXT: store i32 0, ptr [[F]], align 4
496 ; CHECK-NEXT: store i32 0, ptr [[G]], align 4
497 ; CHECK-NEXT: [[F_SELECT:%.*]] = select i1 [[B1:%.*]], ptr [[F]], ptr [[PTR:%.*]]
498 ; CHECK-NEXT: br i1 [[B2:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
500 ; CHECK-NEXT: br label [[EXIT:%.*]]
502 ; CHECK-NEXT: br label [[EXIT]]
504 ; CHECK-NEXT: [[F_PHI:%.*]] = phi ptr [ [[F]], [[THEN]] ], [ [[F_SELECT]], [[ELSE]] ]
505 ; CHECK-NEXT: [[G_PHI:%.*]] = phi ptr [ [[G]], [[THEN]] ], [ [[PTR]], [[ELSE]] ]
506 ; CHECK-NEXT: [[F_LOADED:%.*]] = load i32, ptr [[F_PHI]], align 4
507 ; CHECK-NEXT: [[G_SELECT:%.*]] = select i1 [[B1]], ptr [[G]], ptr [[G_PHI]]
508 ; CHECK-NEXT: [[G_LOADED:%.*]] = load i32, ptr [[G_SELECT]], align 4
509 ; CHECK-NEXT: [[RESULT:%.*]] = add i32 [[F_LOADED]], [[G_LOADED]]
510 ; CHECK-NEXT: ret i32 [[RESULT]]
517 %f.select = select i1 %b1, ptr %f, ptr %ptr
518 br i1 %b2, label %then, label %else
527 %f.phi = phi ptr [ %f, %then ], [ %f.select, %else ]
528 %g.phi = phi ptr [ %g, %then ], [ %ptr, %else ]
529 %f.loaded = load i32, ptr %f.phi
530 %g.select = select i1 %b1, ptr %g, ptr %g.phi
531 %g.loaded = load i32, ptr %g.select
532 %result = add i32 %f.loaded, %g.loaded
536 define void @PR13905(i1 %c1, i1 %c2, i1 %c3) {
537 ; Check a pattern where we have a chain of dead phi nodes to ensure they are
538 ; deleted and promotion can proceed.
540 ; CHECK-LABEL: @PR13905(
542 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOOP1:%.*]], label [[EXIT:%.*]]
544 ; CHECK-NEXT: br i1 [[C2:%.*]], label [[LOOP1]], label [[LOOP2:%.*]]
546 ; CHECK-NEXT: br i1 [[C3:%.*]], label [[LOOP1]], label [[EXIT]]
548 ; CHECK-NEXT: [[PHI2:%.*]] = phi ptr [ poison, [[LOOP2]] ], [ null, [[ENTRY:%.*]] ]
549 ; CHECK-NEXT: ret void
554 br i1 %c1, label %loop1, label %exit
557 %phi1 = phi ptr [ null, %entry ], [ %h, %loop1 ], [ %h, %loop2 ]
558 br i1 %c2, label %loop1, label %loop2
561 br i1 %c3, label %loop1, label %exit
564 %phi2 = phi ptr [ %phi1, %loop2 ], [ null, %entry ]
568 define i32 @PR13906(i1 %c1, i1 %c2) {
569 ; Another pattern which can lead to crashes due to failing to clear out dead
570 ; PHI nodes or select nodes. This triggers subtly differently from the above
571 ; cases because the PHI node is (recursively) alive, but the select is dead.
573 ; CHECK-LABEL: @PR13906(
575 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
577 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[IF_THEN:%.*]], label [[FOR_COND]]
579 ; CHECK-NEXT: br label [[FOR_COND]]
587 %d.0 = phi ptr [ poison, %entry ], [ %c, %if.then ], [ %d.0, %for.cond ]
588 br i1 %c1, label %if.then, label %for.cond
591 %tmpcast.d.0 = select i1 %c2, ptr %c, ptr %d.0
595 define i64 @PR14132(i1 %flag) {
596 ; Here we form a PHI-node by promoting the pointer alloca first, and then in
597 ; order to promote the other two allocas, we speculate the load of the
598 ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
599 ; alloca. While this is a bit dubious, we were asserting on trying to
600 ; rewrite it. The trick is that the code using the value may carefully take
601 ; steps to only use the not-undef bits, and so we need to at least loosely
603 ; CHECK-LABEL: @PR14132(
605 ; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
607 ; CHECK-NEXT: [[B_0_LOAD_EXT:%.*]] = zext i8 1 to i64
608 ; CHECK-NEXT: br label [[IF_END]]
610 ; CHECK-NEXT: [[PTR_0_SROA_SPECULATED:%.*]] = phi i64 [ [[B_0_LOAD_EXT]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ]
611 ; CHECK-NEXT: ret i64 [[PTR_0_SROA_SPECULATED]]
614 %a = alloca i64, align 8
615 %b = alloca i8, align 8
616 %ptr = alloca ptr, align 8
618 store i64 0, ptr %a, align 8
619 store i8 1, ptr %b, align 8
620 store ptr %a, ptr %ptr, align 8
621 br i1 %flag, label %if.then, label %if.end
624 store ptr %b, ptr %ptr, align 8
628 %tmp = load ptr, ptr %ptr, align 8
629 %result = load i64, ptr %tmp, align 8
634 define float @PR16687(i64 %x, i1 %flag) {
635 ; Check that even when we try to speculate the same phi twice (in two slices)
636 ; on an otherwise promotable construct, we don't get ahead of ourselves and try
637 ; to promote one of the slices prior to speculating it.
638 ; CHECK-LABEL: @PR16687(
640 ; CHECK-NEXT: [[A_SROA_0_0_EXTRACT_TRUNC:%.*]] = trunc i64 [[X:%.*]] to i32
641 ; CHECK-NEXT: [[A_SROA_2_0_EXTRACT_SHIFT:%.*]] = lshr i64 [[X]], 32
642 ; CHECK-NEXT: [[A_SROA_2_0_EXTRACT_TRUNC:%.*]] = trunc i64 [[A_SROA_2_0_EXTRACT_SHIFT]] to i32
643 ; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
645 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 [[A_SROA_0_0_EXTRACT_TRUNC]] to float
646 ; CHECK-NEXT: br label [[END:%.*]]
648 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 [[A_SROA_2_0_EXTRACT_TRUNC]] to float
649 ; CHECK-NEXT: br label [[END]]
651 ; CHECK-NEXT: [[A_PHI_F_SROA_SPECULATED:%.*]] = phi float [ [[TMP0]], [[THEN]] ], [ [[TMP1]], [[ELSE]] ]
652 ; CHECK-NEXT: ret float [[A_PHI_F_SROA_SPECULATED]]
655 %a = alloca i64, align 8
657 br i1 %flag, label %then, label %else
663 %a.raw.4 = getelementptr i8, ptr %a, i64 4
667 %a.phi.f = phi ptr [ %a, %then ], [ %a.raw.4, %else ]
668 %f = load float, ptr %a.phi.f
672 ; Verifies we fixed PR20425. We should be able to promote all alloca's to
673 ; registers in this test.
677 ; %2 = phi(%0, %1) // == slice
678 define float @simplify_phi_nodes_that_equal_slice(i1 %cond, ptr %temp) {
679 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice(
681 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
683 ; CHECK-NEXT: br label [[MERGE:%.*]]
685 ; CHECK-NEXT: br label [[MERGE]]
687 ; CHECK-NEXT: [[ARR_SROA_0_0:%.*]] = phi float [ 1.000000e+00, [[THEN]] ], [ 2.000000e+00, [[ELSE]] ]
688 ; CHECK-NEXT: store float 0.000000e+00, ptr [[TEMP:%.*]], align 4
689 ; CHECK-NEXT: ret float [[ARR_SROA_0_0]]
692 %arr = alloca [4 x float], align 4
693 br i1 %cond, label %then, label %else
696 %0 = getelementptr inbounds [4 x float], ptr %arr, i64 0, i64 3
697 store float 1.000000e+00, ptr %0, align 4
701 %1 = getelementptr inbounds [4 x float], ptr %arr, i64 0, i64 3
702 store float 2.000000e+00, ptr %1, align 4
706 %2 = phi ptr [ %0, %then ], [ %1, %else ]
707 store float 0.000000e+00, ptr %temp, align 4
708 %3 = load float, ptr %2, align 4
712 ; A slightly complicated example for PR20425.
715 ; %1 = phi(%0) // == slice
717 ; %3 = phi(%1, %2) // == slice
718 define float @simplify_phi_nodes_that_equal_slice_2(i1 %cond, ptr %temp) {
719 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice_2(
721 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
723 ; CHECK-NEXT: br label [[THEN2:%.*]]
725 ; CHECK-NEXT: br label [[MERGE:%.*]]
727 ; CHECK-NEXT: br label [[MERGE]]
729 ; CHECK-NEXT: [[ARR_SROA_0_0:%.*]] = phi float [ 2.000000e+00, [[THEN2]] ], [ 3.000000e+00, [[ELSE]] ]
730 ; CHECK-NEXT: store float 0.000000e+00, ptr [[TEMP:%.*]], align 4
731 ; CHECK-NEXT: ret float [[ARR_SROA_0_0]]
734 %arr = alloca [4 x float], align 4
735 br i1 %cond, label %then, label %else
738 %0 = getelementptr inbounds [4 x float], ptr %arr, i64 0, i64 3
739 store float 1.000000e+00, ptr %0, align 4
743 %1 = phi ptr [ %0, %then ]
744 store float 2.000000e+00, ptr %1, align 4
748 %2 = getelementptr inbounds [4 x float], ptr %arr, i64 0, i64 3
749 store float 3.000000e+00, ptr %2, align 4
753 %3 = phi ptr [ %1, %then2 ], [ %2, %else ]
754 store float 0.000000e+00, ptr %temp, align 4
755 %4 = load float, ptr %3, align 4
759 %struct.S = type { i32 }
761 ; Verifies we fixed PR20822. We have a foldable PHI feeding a speculatable PHI
762 ; which requires the rewriting of the speculated PHI to handle insertion
763 ; when the incoming pointer is itself from a PHI node. We would previously
764 ; insert a bitcast instruction *before* a PHI, producing an invalid module;
765 ; make sure we insert *after* the first non-PHI instruction.
766 define void @PR20822(i1 %c1, i1 %c2, ptr %ptr) {
767 ; CHECK-LABEL: @PR20822(
769 ; CHECK-NEXT: [[F_SROA_0:%.*]] = alloca i32, align 4
770 ; CHECK-NEXT: [[F1_SROA_GEP:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[PTR:%.*]], i32 0, i32 0
771 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[IF_END:%.*]], label [[FOR_COND:%.*]]
773 ; CHECK-NEXT: br label [[IF_END]]
775 ; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ poison, [[ENTRY:%.*]] ], [ poison, [[FOR_COND]] ]
776 ; CHECK-NEXT: br i1 [[C2:%.*]], label [[IF_THEN5:%.*]], label [[IF_THEN2:%.*]]
778 ; CHECK-NEXT: br label [[IF_THEN5]]
780 ; CHECK-NEXT: [[F1_SROA_PHI:%.*]] = phi ptr [ [[F1_SROA_GEP]], [[IF_THEN2]] ], [ [[F_SROA_0]], [[IF_END]] ]
781 ; CHECK-NEXT: store i32 0, ptr [[F1_SROA_PHI]], align 4
782 ; CHECK-NEXT: ret void
785 %f = alloca %struct.S, align 4
786 br i1 %c1, label %if.end, label %for.cond
788 for.cond: ; preds = %for.cond, %entry
791 if.end: ; preds = %for.cond, %entry
792 %f2 = phi ptr [ %f, %entry ], [ %f, %for.cond ]
793 phi i32 [ poison, %entry ], [ poison, %for.cond ]
794 br i1 %c2, label %if.then5, label %if.then2
796 if.then2: ; preds = %if.end
799 if.then5: ; preds = %if.then2, %if.end
800 %f1 = phi ptr [ %ptr, %if.then2 ], [ %f2, %if.end ]
801 store %struct.S zeroinitializer, ptr %f1, align 4
805 define i32 @phi_align(ptr %z) {
806 ; CHECK-LABEL: @phi_align(
808 ; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca [7 x i8], align 1
809 ; CHECK-NEXT: [[A_SROA_0_3_A1X_SROA_IDX:%.*]] = getelementptr inbounds i8, ptr [[A_SROA_0]], i64 3
810 ; CHECK-NEXT: store i32 0, ptr [[A_SROA_0]], align 1
811 ; CHECK-NEXT: [[A_SROA_0_3_A1X_SROA_IDX3:%.*]] = getelementptr inbounds i8, ptr [[A_SROA_0]], i64 3
812 ; CHECK-NEXT: store i32 1, ptr [[A_SROA_0_3_A1X_SROA_IDX3]], align 1
813 ; CHECK-NEXT: [[A_SROA_0_0_A_SROA_0_1_V0:%.*]] = load i32, ptr [[A_SROA_0]], align 1
814 ; CHECK-NEXT: [[A_SROA_0_3_A1X_SROA_IDX4:%.*]] = getelementptr inbounds i8, ptr [[A_SROA_0]], i64 3
815 ; CHECK-NEXT: [[A_SROA_0_3_A_SROA_0_4_V1:%.*]] = load i32, ptr [[A_SROA_0_3_A1X_SROA_IDX4]], align 1
816 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 [[A_SROA_0_0_A_SROA_0_1_V0]], [[A_SROA_0_3_A_SROA_0_4_V1]]
817 ; CHECK-NEXT: br i1 [[COND]], label [[THEN:%.*]], label [[EXIT:%.*]]
819 ; CHECK-NEXT: br label [[EXIT]]
821 ; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[A_SROA_0_3_A1X_SROA_IDX]], [[THEN]] ], [ [[Z:%.*]], [[ENTRY:%.*]] ]
822 ; CHECK-NEXT: [[RESULT:%.*]] = load i32, ptr [[PHI]], align 1
823 ; CHECK-NEXT: ret i32 [[RESULT]]
826 %a = alloca [8 x i8], align 8
828 %a0x = getelementptr [8 x i8], ptr %a, i64 0, i32 1
829 %a1x = getelementptr [8 x i8], ptr %a, i64 0, i32 4
830 store i32 0, ptr %a0x, align 1
831 store i32 1, ptr %a1x, align 4
832 %v0 = load i32, ptr %a0x, align 1
833 %v1 = load i32, ptr %a1x, align 4
834 %cond = icmp sle i32 %v0, %v1
835 br i1 %cond, label %then, label %exit
841 %phi = phi ptr [ %a1x, %then ], [ %z, %entry ]
842 %result = load i32, ptr %phi, align 4
846 ; Don't speculate a load based on an earlier volatile operation.
847 define i8 @volatile_select(ptr %p, i1 %b) {
848 ; CHECK-PRESERVE-CFG-LABEL: @volatile_select(
849 ; CHECK-PRESERVE-CFG-NEXT: [[P2:%.*]] = alloca i8, align 1
850 ; CHECK-PRESERVE-CFG-NEXT: store i8 0, ptr [[P2]], align 1
851 ; CHECK-PRESERVE-CFG-NEXT: store volatile i8 0, ptr [[P:%.*]], align 1
852 ; CHECK-PRESERVE-CFG-NEXT: [[PX:%.*]] = select i1 [[B:%.*]], ptr [[P]], ptr [[P2]]
853 ; CHECK-PRESERVE-CFG-NEXT: [[V2:%.*]] = load i8, ptr [[PX]], align 1
854 ; CHECK-PRESERVE-CFG-NEXT: ret i8 [[V2]]
856 ; CHECK-MODIFY-CFG-LABEL: @volatile_select(
857 ; CHECK-MODIFY-CFG-NEXT: store volatile i8 0, ptr [[P:%.*]], align 1
858 ; CHECK-MODIFY-CFG-NEXT: br i1 [[B:%.*]], label [[DOTTHEN:%.*]], label [[DOTCONT:%.*]]
859 ; CHECK-MODIFY-CFG: .then:
860 ; CHECK-MODIFY-CFG-NEXT: [[V2_THEN_VAL:%.*]] = load i8, ptr [[P]], align 1
861 ; CHECK-MODIFY-CFG-NEXT: br label [[DOTCONT]]
862 ; CHECK-MODIFY-CFG: .cont:
863 ; CHECK-MODIFY-CFG-NEXT: [[V2:%.*]] = phi i8 [ [[V2_THEN_VAL]], [[DOTTHEN]] ], [ 0, [[TMP0:%.*]] ]
864 ; CHECK-MODIFY-CFG-NEXT: ret i8 [[V2]]
868 store volatile i8 0, ptr %p
869 %px = select i1 %b, ptr %p, ptr %p2
870 %v2 = load i8, ptr %px