1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -sroa -S | FileCheck %s
3 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"
8 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
9 ; CHECK-NEXT: br i1 [[COND]], label [[THEN:%.*]], label [[EXIT:%.*]]
11 ; CHECK-NEXT: br label [[EXIT]]
13 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ 1, [[THEN]] ], [ 0, [[ENTRY:%.*]] ]
14 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
19 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
20 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
23 %v0 = load i32, i32* %a0
24 %v1 = load i32, i32* %a1
26 %cond = icmp sle i32 %v0, %v1
27 br i1 %cond, label %then, label %exit
33 %phi = phi i32* [ %a1, %then ], [ %a0, %entry ]
35 %result = load i32, i32* %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 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
50 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
53 %v0 = load i32, i32* %a0
54 %v1 = load i32, i32* %a1
56 %cond = icmp sle i32 %v0, %v1
57 %select = select i1 %cond, i32* %a1, i32* %a0
59 %result = load i32, i32* %select
63 define float @test2_bitcast() {
64 ; CHECK-LABEL: @test2_bitcast(
66 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
67 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 1 to float
68 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 0 to float
69 ; CHECK-NEXT: [[RESULT_SROA_SPECULATED:%.*]] = select i1 [[COND]], float [[TMP0]], float [[TMP1]]
70 ; CHECK-NEXT: ret float [[RESULT_SROA_SPECULATED]]
74 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
75 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
78 %v0 = load i32, i32* %a0
79 %v1 = load i32, i32* %a1
80 %cond = icmp sle i32 %v0, %v1
81 %select = select i1 %cond, i32* %a1, i32* %a0
82 %select.bc = bitcast i32* %select to float*
83 %result = load float, float* %select.bc
87 define i32 @test2_addrspacecast() {
88 ; CHECK-LABEL: @test2_addrspacecast(
90 ; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
91 ; CHECK-NEXT: [[A_SROA_3:%.*]] = alloca i32, align 4
92 ; CHECK-NEXT: store i32 0, i32* [[A_SROA_0]], align 4
93 ; CHECK-NEXT: store i32 1, i32* [[A_SROA_3]], align 4
94 ; CHECK-NEXT: [[A_SROA_0_0_A_SROA_0_0_V0:%.*]] = load i32, i32* [[A_SROA_0]], align 4
95 ; CHECK-NEXT: [[A_SROA_3_0_A_SROA_3_4_V1:%.*]] = load i32, i32* [[A_SROA_3]], align 4
96 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 [[A_SROA_0_0_A_SROA_0_0_V0]], [[A_SROA_3_0_A_SROA_3_4_V1]]
97 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i32* [[A_SROA_3]], i32* [[A_SROA_0]]
98 ; CHECK-NEXT: [[SELECT_ASC:%.*]] = addrspacecast i32* [[SELECT]] to i32 addrspace(1)*
99 ; CHECK-NEXT: [[RESULT:%.*]] = load i32, i32 addrspace(1)* [[SELECT_ASC]], align 4
100 ; CHECK-NEXT: ret i32 [[RESULT]]
103 %a = alloca [2 x i32]
104 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
105 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
106 store i32 0, i32* %a0
107 store i32 1, i32* %a1
108 %v0 = load i32, i32* %a0
109 %v1 = load i32, i32* %a1
110 %cond = icmp sle i32 %v0, %v1
111 %select = select i1 %cond, i32* %a1, i32* %a0
112 %select.asc = addrspacecast i32* %select to i32 addrspace(1)*
113 %result = load i32, i32 addrspace(1)* %select.asc
117 define i32 @test3(i32 %x) {
118 ; CHECK-LABEL: @test3(
120 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[BB0:%.*]] [
121 ; CHECK-NEXT: i32 1, label [[BB1:%.*]]
122 ; CHECK-NEXT: i32 2, label [[BB2:%.*]]
123 ; CHECK-NEXT: i32 3, label [[BB3:%.*]]
124 ; CHECK-NEXT: i32 4, label [[BB4:%.*]]
125 ; CHECK-NEXT: i32 5, label [[BB5:%.*]]
126 ; CHECK-NEXT: i32 6, label [[BB6:%.*]]
127 ; CHECK-NEXT: i32 7, label [[BB7:%.*]]
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: br label [[EXIT]]
144 ; CHECK-NEXT: br label [[EXIT]]
146 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ 1, [[BB0]] ], [ 0, [[BB1]] ], [ 0, [[BB2]] ], [ 1, [[BB3]] ], [ 1, [[BB4]] ], [ 0, [[BB5]] ], [ 0, [[BB6]] ], [ 1, [[BB7]] ]
147 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
150 %a = alloca [2 x i32]
152 ; Note that we build redundant GEPs here to ensure that having different GEPs
153 ; into the same alloca partation continues to work with PHI speculation. This
154 ; was the underlying cause of PR13926.
155 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
156 %a0b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
157 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
158 %a1b = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
159 store i32 0, i32* %a0
160 store i32 1, i32* %a1
162 switch i32 %x, label %bb0 [ i32 1, label %bb1
188 %phi = phi i32* [ %a1, %bb0 ], [ %a0, %bb1 ], [ %a0, %bb2 ], [ %a1, %bb3 ],
189 [ %a1b, %bb4 ], [ %a0b, %bb5 ], [ %a0b, %bb6 ], [ %a1b, %bb7 ]
191 %result = load i32, i32* %phi
195 define i32 @test4() {
196 ; CHECK-LABEL: @test4(
198 ; CHECK-NEXT: ret i32 0
201 %a = alloca [2 x i32]
203 %a0 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 0
204 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
205 store i32 0, i32* %a0
206 store i32 1, i32* %a1
207 %v0 = load i32, i32* %a0
208 %v1 = load i32, i32* %a1
210 %cond = icmp sle i32 %v0, %v1
211 %select = select i1 %cond, i32* %a0, i32* %a0
213 %result = load i32, i32* %select
217 define i32 @test5(i32* %b) {
218 ; CHECK-LABEL: @test5(
220 ; CHECK-NEXT: ret i32 1
223 %a = alloca [2 x i32]
225 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
226 store i32 1, i32* %a1
228 %select = select i1 true, i32* %a1, i32* %b
230 %result = load i32, i32* %select
235 declare void @f(i32*, i32*)
237 define i32 @test6(i32* %b) {
238 ; CHECK-LABEL: @test6(
240 ; CHECK-NEXT: [[SELECT2:%.*]] = select i1 false, i32* undef, i32* [[B:%.*]]
241 ; CHECK-NEXT: [[SELECT3:%.*]] = select i1 false, i32* undef, i32* [[B]]
242 ; CHECK-NEXT: call void @f(i32* [[SELECT2]], i32* [[SELECT3]])
243 ; CHECK-NEXT: ret i32 1
246 %a = alloca [2 x i32]
249 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
250 store i32 1, i32* %a1
252 %select = select i1 true, i32* %a1, i32* %b
253 %select2 = select i1 false, i32* %a1, i32* %b
254 %select3 = select i1 false, i32* %c, i32* %b
256 ; Note, this would potentially escape the alloca pointer except for the
257 ; constant folding of the select.
258 call void @f(i32* %select2, i32* %select3)
261 %result = load i32, i32* %select
263 %dead = load i32, i32* %c
268 define i32 @test7() {
269 ; CHECK-LABEL: @test7(
271 ; CHECK-NEXT: br i1 undef, label [[GOOD:%.*]], label [[BAD:%.*]]
273 ; CHECK-NEXT: br label [[EXIT:%.*]]
275 ; CHECK-NEXT: [[P_SROA_SPECULATE_LOAD_BAD:%.*]] = load i32, i32* undef, align 4
276 ; CHECK-NEXT: br label [[EXIT]]
278 ; CHECK-NEXT: [[P_SROA_SPECULATED:%.*]] = phi i32 [ 0, [[GOOD]] ], [ [[P_SROA_SPECULATE_LOAD_BAD]], [[BAD]] ]
279 ; CHECK-NEXT: ret i32 [[P_SROA_SPECULATED]]
284 br i1 undef, label %good, label %bad
287 %Y1 = getelementptr i32, i32* %X, i64 0
288 store i32 0, i32* %Y1
292 %Y2 = getelementptr i32, i32* %X, i64 1
293 store i32 0, i32* %Y2
297 %P = phi i32* [ %Y1, %good ], [ %Y2, %bad ]
298 %Z2 = load i32, i32* %P
302 define i32 @test8(i32 %b, i32* %ptr) {
303 ; Ensure that we rewrite allocas to the used type when that use is hidden by
304 ; a PHI that can be speculated.
305 ; CHECK-LABEL: @test8(
307 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
308 ; CHECK-NEXT: br i1 [[TEST]], label [[THEN:%.*]], label [[ELSE:%.*]]
310 ; CHECK-NEXT: [[PHI_SROA_SPECULATE_LOAD_THEN:%.*]] = load i32, i32* [[PTR:%.*]], align 4
311 ; CHECK-NEXT: br label [[EXIT:%.*]]
313 ; CHECK-NEXT: br label [[EXIT]]
315 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi i32 [ undef, [[ELSE]] ], [ [[PHI_SROA_SPECULATE_LOAD_THEN]], [[THEN]] ]
316 ; CHECK-NEXT: ret i32 [[PHI_SROA_SPECULATED]]
321 %test = icmp ne i32 %b, 0
322 br i1 %test, label %then, label %else
328 %bitcast = bitcast float* %f to i32*
332 %phi = phi i32* [ %bitcast, %else ], [ %ptr, %then ]
333 %loaded = load i32, i32* %phi, align 4
337 define i32 @test9(i32 %b, i32* %ptr) {
338 ; Same as @test8 but for a select rather than a PHI node.
339 ; CHECK-LABEL: @test9(
341 ; CHECK-NEXT: store i32 0, i32* [[PTR:%.*]], align 4
342 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
343 ; CHECK-NEXT: [[LOADED_SROA_SPECULATE_LOAD_FALSE:%.*]] = load i32, i32* [[PTR]], align 4
344 ; CHECK-NEXT: [[LOADED_SROA_SPECULATED:%.*]] = select i1 [[TEST]], i32 undef, i32 [[LOADED_SROA_SPECULATE_LOAD_FALSE]]
345 ; CHECK-NEXT: ret i32 [[LOADED_SROA_SPECULATED]]
350 store i32 0, i32* %ptr
351 %test = icmp ne i32 %b, 0
352 %bitcast = bitcast float* %f to i32*
353 %select = select i1 %test, i32* %bitcast, i32* %ptr
354 %loaded = load i32, i32* %select, align 4
358 define float @test10(i32 %b, float* %ptr) {
359 ; Don't try to promote allocas which are not elligible for it even after
360 ; rewriting due to the necessity of inserting bitcasts when speculating a PHI
362 ; CHECK-LABEL: @test10(
364 ; CHECK-NEXT: [[F:%.*]] = alloca double, align 8
365 ; CHECK-NEXT: store double 0.000000e+00, double* [[F]], align 8
366 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
367 ; CHECK-NEXT: br i1 [[TEST]], label [[THEN:%.*]], label [[ELSE:%.*]]
369 ; CHECK-NEXT: [[PHI_SROA_SPECULATE_LOAD_THEN:%.*]] = load float, float* [[PTR:%.*]], align 4
370 ; CHECK-NEXT: br label [[EXIT:%.*]]
372 ; CHECK-NEXT: [[F_0_F_0_BITCAST_SROA_CAST:%.*]] = bitcast double* [[F]] to float*
373 ; CHECK-NEXT: [[F_0_PHI_SROA_SPECULATE_LOAD_ELSE:%.*]] = load float, float* [[F_0_F_0_BITCAST_SROA_CAST]], align 8
374 ; CHECK-NEXT: br label [[EXIT]]
376 ; CHECK-NEXT: [[PHI_SROA_SPECULATED:%.*]] = phi float [ [[F_0_PHI_SROA_SPECULATE_LOAD_ELSE]], [[ELSE]] ], [ [[PHI_SROA_SPECULATE_LOAD_THEN]], [[THEN]] ]
377 ; CHECK-NEXT: ret float [[PHI_SROA_SPECULATED]]
382 store double 0.0, double* %f
383 %test = icmp ne i32 %b, 0
384 br i1 %test, label %then, label %else
390 %bitcast = bitcast double* %f to float*
394 %phi = phi float* [ %bitcast, %else ], [ %ptr, %then ]
395 %loaded = load float, float* %phi, align 4
399 define float @test11(i32 %b, float* %ptr) {
400 ; Same as @test10 but for a select rather than a PHI node.
401 ; CHECK-LABEL: @test11(
403 ; CHECK-NEXT: [[F:%.*]] = alloca double, align 8
404 ; CHECK-NEXT: store double 0.000000e+00, double* [[F]], align 8
405 ; CHECK-NEXT: store float 0.000000e+00, float* [[PTR:%.*]], align 4
406 ; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
407 ; CHECK-NEXT: [[F_0_F_0_BITCAST_SROA_CAST:%.*]] = bitcast double* [[F]] to float*
408 ; CHECK-NEXT: [[F_0_LOADED_SROA_SPECULATE_LOAD_TRUE:%.*]] = load float, float* [[F_0_F_0_BITCAST_SROA_CAST]], align 8
409 ; CHECK-NEXT: [[LOADED_SROA_SPECULATE_LOAD_FALSE:%.*]] = load float, float* [[PTR]], align 4
410 ; CHECK-NEXT: [[LOADED_SROA_SPECULATED:%.*]] = select i1 [[TEST]], float [[F_0_LOADED_SROA_SPECULATE_LOAD_TRUE]], float [[LOADED_SROA_SPECULATE_LOAD_FALSE]]
411 ; CHECK-NEXT: ret float [[LOADED_SROA_SPECULATED]]
416 store double 0.0, double* %f
417 store float 0.0, float* %ptr
418 %test = icmp ne i32 %b, 0
419 %bitcast = bitcast double* %f to float*
420 %select = select i1 %test, float* %bitcast, float* %ptr
421 %loaded = load float, float* %select, align 4
425 define i32 @test12(i32 %x, i32* %p) {
426 ; Ensure we don't crash or fail to nuke dead selects of allocas if no load is
428 ; CHECK-LABEL: @test12(
430 ; CHECK-NEXT: ret i32 [[X:%.*]]
435 store i32 %x, i32* %a
436 %dead = select i1 undef, i32* %a, i32* %p
437 %load = load i32, i32* %a
441 define i32 @test13(i32 %x, i32* %p) {
442 ; Ensure we don't crash or fail to nuke dead phis of allocas if no load is ever
444 ; CHECK-LABEL: @test13(
446 ; CHECK-NEXT: br label [[LOOP:%.*]]
448 ; CHECK-NEXT: br i1 undef, label [[LOOP]], label [[EXIT:%.*]]
450 ; CHECK-NEXT: ret i32 [[X:%.*]]
455 store i32 %x, i32* %a
459 %phi = phi i32* [ %p, %entry ], [ %a, %loop ]
460 br i1 undef, label %loop, label %exit
463 %load = load i32, i32* %a
467 define i32 @test14(i1 %b1, i1 %b2, i32* %ptr) {
468 ; Check for problems when there are both selects and phis and one is
469 ; speculatable toward promotion but the other is not. That should block all of
471 ; CHECK-LABEL: @test14(
473 ; CHECK-NEXT: [[F:%.*]] = alloca i32, align 4
474 ; CHECK-NEXT: [[G:%.*]] = alloca i32, align 4
475 ; CHECK-NEXT: store i32 0, i32* [[F]], align 4
476 ; CHECK-NEXT: store i32 0, i32* [[G]], align 4
477 ; CHECK-NEXT: [[F_SELECT:%.*]] = select i1 [[B1:%.*]], i32* [[F]], i32* [[PTR:%.*]]
478 ; CHECK-NEXT: br i1 [[B2:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
480 ; CHECK-NEXT: br label [[EXIT:%.*]]
482 ; CHECK-NEXT: br label [[EXIT]]
484 ; CHECK-NEXT: [[F_PHI:%.*]] = phi i32* [ [[F]], [[THEN]] ], [ [[F_SELECT]], [[ELSE]] ]
485 ; CHECK-NEXT: [[G_PHI:%.*]] = phi i32* [ [[G]], [[THEN]] ], [ [[PTR]], [[ELSE]] ]
486 ; CHECK-NEXT: [[F_LOADED:%.*]] = load i32, i32* [[F_PHI]], align 4
487 ; CHECK-NEXT: [[G_SELECT:%.*]] = select i1 [[B1]], i32* [[G]], i32* [[G_PHI]]
488 ; CHECK-NEXT: [[G_LOADED:%.*]] = load i32, i32* [[G_SELECT]], align 4
489 ; CHECK-NEXT: [[RESULT:%.*]] = add i32 [[F_LOADED]], [[G_LOADED]]
490 ; CHECK-NEXT: ret i32 [[RESULT]]
498 %f.select = select i1 %b1, i32* %f, i32* %ptr
499 br i1 %b2, label %then, label %else
508 %f.phi = phi i32* [ %f, %then ], [ %f.select, %else ]
509 %g.phi = phi i32* [ %g, %then ], [ %ptr, %else ]
510 %f.loaded = load i32, i32* %f.phi
511 %g.select = select i1 %b1, i32* %g, i32* %g.phi
512 %g.loaded = load i32, i32* %g.select
513 %result = add i32 %f.loaded, %g.loaded
517 define i32 @PR13905() {
518 ; Check a pattern where we have a chain of dead phi nodes to ensure they are
519 ; deleted and promotion can proceed.
520 ; CHECK-LABEL: @PR13905(
522 ; CHECK-NEXT: br i1 undef, label [[LOOP1:%.*]], label [[EXIT:%.*]]
524 ; CHECK-NEXT: br i1 undef, label [[LOOP1]], label [[LOOP2:%.*]]
526 ; CHECK-NEXT: br i1 undef, label [[LOOP1]], label [[EXIT]]
528 ; CHECK-NEXT: [[PHI2:%.*]] = phi i32* [ undef, [[LOOP2]] ], [ null, [[ENTRY:%.*]] ]
529 ; CHECK-NEXT: ret i32 undef
535 br i1 undef, label %loop1, label %exit
538 %phi1 = phi i32* [ null, %entry ], [ %h, %loop1 ], [ %h, %loop2 ]
539 br i1 undef, label %loop1, label %loop2
542 br i1 undef, label %loop1, label %exit
545 %phi2 = phi i32* [ %phi1, %loop2 ], [ null, %entry ]
549 define i32 @PR13906() {
550 ; Another pattern which can lead to crashes due to failing to clear out dead
551 ; PHI nodes or select nodes. This triggers subtly differently from the above
552 ; cases because the PHI node is (recursively) alive, but the select is dead.
553 ; CHECK-LABEL: @PR13906(
555 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
557 ; CHECK-NEXT: br i1 undef, label [[IF_THEN:%.*]], label [[FOR_COND]]
559 ; CHECK-NEXT: br label [[FOR_COND]]
568 %d.0 = phi i32* [ undef, %entry ], [ %c, %if.then ], [ %d.0, %for.cond ]
569 br i1 undef, label %if.then, label %for.cond
572 %tmpcast.d.0 = select i1 undef, i32* %c, i32* %d.0
576 define i64 @PR14132(i1 %flag) {
577 ; CHECK-LABEL: @PR14132(
579 ; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
581 ; CHECK-NEXT: [[B_0_LOAD_EXT:%.*]] = zext i8 1 to i64
582 ; CHECK-NEXT: br label [[IF_END]]
584 ; CHECK-NEXT: [[PTR_0_SROA_SPECULATED:%.*]] = phi i64 [ [[B_0_LOAD_EXT]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ]
585 ; CHECK-NEXT: ret i64 [[PTR_0_SROA_SPECULATED]]
587 ; Here we form a PHI-node by promoting the pointer alloca first, and then in
588 ; order to promote the other two allocas, we speculate the load of the
589 ; now-phi-node-pointer. In doing so we end up loading a 64-bit value from an i8
590 ; alloca. While this is a bit dubious, we were asserting on trying to
591 ; rewrite it. The trick is that the code using the value may carefully take
592 ; steps to only use the not-undef bits, and so we need to at least loosely
595 %a = alloca i64, align 8
596 %b = alloca i8, align 8
597 %ptr = alloca i64*, align 8
599 %ptr.cast = bitcast i64** %ptr to i8**
600 store i64 0, i64* %a, align 8
601 store i8 1, i8* %b, align 8
602 store i64* %a, i64** %ptr, align 8
603 br i1 %flag, label %if.then, label %if.end
606 store i8* %b, i8** %ptr.cast, align 8
610 %tmp = load i64*, i64** %ptr, align 8
611 %result = load i64, i64* %tmp, align 8
616 define float @PR16687(i64 %x, i1 %flag) {
617 ; CHECK-LABEL: @PR16687(
619 ; CHECK-NEXT: [[A_SROA_0_0_EXTRACT_TRUNC:%.*]] = trunc i64 [[X:%.*]] to i32
620 ; CHECK-NEXT: [[A_SROA_2_0_EXTRACT_SHIFT:%.*]] = lshr i64 [[X]], 32
621 ; CHECK-NEXT: [[A_SROA_2_0_EXTRACT_TRUNC:%.*]] = trunc i64 [[A_SROA_2_0_EXTRACT_SHIFT]] to i32
622 ; CHECK-NEXT: br i1 [[FLAG:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
624 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 [[A_SROA_0_0_EXTRACT_TRUNC]] to float
625 ; CHECK-NEXT: br label [[END:%.*]]
627 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 [[A_SROA_2_0_EXTRACT_TRUNC]] to float
628 ; CHECK-NEXT: br label [[END]]
630 ; CHECK-NEXT: [[A_PHI_F_SROA_SPECULATED:%.*]] = phi float [ [[TMP0]], [[THEN]] ], [ [[TMP1]], [[ELSE]] ]
631 ; CHECK-NEXT: ret float [[A_PHI_F_SROA_SPECULATED]]
633 ; Check that even when we try to speculate the same phi twice (in two slices)
634 ; on an otherwise promotable construct, we don't get ahead of ourselves and try
635 ; to promote one of the slices prior to speculating it.
638 %a = alloca i64, align 8
639 store i64 %x, i64* %a
640 br i1 %flag, label %then, label %else
643 %a.f = bitcast i64* %a to float*
647 %a.raw = bitcast i64* %a to i8*
648 %a.raw.4 = getelementptr i8, i8* %a.raw, i64 4
649 %a.raw.4.f = bitcast i8* %a.raw.4 to float*
653 %a.phi.f = phi float* [ %a.f, %then ], [ %a.raw.4.f, %else ]
654 %f = load float, float* %a.phi.f
658 ; Verifies we fixed PR20425. We should be able to promote all alloca's to
659 ; registers in this test.
663 ; %2 = phi(%0, %1) // == slice
664 define float @simplify_phi_nodes_that_equal_slice(i1 %cond, float* %temp) {
665 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice(
667 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
669 ; CHECK-NEXT: br label [[MERGE:%.*]]
671 ; CHECK-NEXT: br label [[MERGE]]
673 ; CHECK-NEXT: [[ARR_SROA_0_0:%.*]] = phi float [ 1.000000e+00, [[THEN]] ], [ 2.000000e+00, [[ELSE]] ]
674 ; CHECK-NEXT: store float 0.000000e+00, float* [[TEMP:%.*]], align 4
675 ; CHECK-NEXT: ret float [[ARR_SROA_0_0]]
678 %arr = alloca [4 x float], align 4
679 br i1 %cond, label %then, label %else
682 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
683 store float 1.000000e+00, float* %0, align 4
687 %1 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
688 store float 2.000000e+00, float* %1, align 4
692 %2 = phi float* [ %0, %then ], [ %1, %else ]
693 store float 0.000000e+00, float* %temp, align 4
694 %3 = load float, float* %2, align 4
698 ; A slightly complicated example for PR20425.
701 ; %1 = phi(%0) // == slice
703 ; %3 = phi(%1, %2) // == slice
704 define float @simplify_phi_nodes_that_equal_slice_2(i1 %cond, float* %temp) {
705 ; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice_2(
707 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
709 ; CHECK-NEXT: br label [[THEN2:%.*]]
711 ; CHECK-NEXT: br label [[MERGE:%.*]]
713 ; CHECK-NEXT: br label [[MERGE]]
715 ; CHECK-NEXT: [[ARR_SROA_0_0:%.*]] = phi float [ 2.000000e+00, [[THEN2]] ], [ 3.000000e+00, [[ELSE]] ]
716 ; CHECK-NEXT: store float 0.000000e+00, float* [[TEMP:%.*]], align 4
717 ; CHECK-NEXT: ret float [[ARR_SROA_0_0]]
720 %arr = alloca [4 x float], align 4
721 br i1 %cond, label %then, label %else
724 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
725 store float 1.000000e+00, float* %0, align 4
729 %1 = phi float* [ %0, %then ]
730 store float 2.000000e+00, float* %1, align 4
734 %2 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
735 store float 3.000000e+00, float* %2, align 4
739 %3 = phi float* [ %1, %then2 ], [ %2, %else ]
740 store float 0.000000e+00, float* %temp, align 4
741 %4 = load float, float* %3, align 4
745 %struct.S = type { i32 }
747 ; Verifies we fixed PR20822. We have a foldable PHI feeding a speculatable PHI
748 ; which requires the rewriting of the speculated PHI to handle insertion
749 ; when the incoming pointer is itself from a PHI node. We would previously
750 ; insert a bitcast instruction *before* a PHI, producing an invalid module;
751 ; make sure we insert *after* the first non-PHI instruction.
752 define void @PR20822() {
753 ; CHECK-LABEL: @PR20822(
755 ; CHECK-NEXT: [[F_SROA_0:%.*]] = alloca i32, align 4
756 ; CHECK-NEXT: br i1 undef, label [[IF_END:%.*]], label [[FOR_COND:%.*]]
758 ; CHECK-NEXT: br label [[IF_END]]
760 ; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ undef, [[ENTRY:%.*]] ], [ undef, [[FOR_COND]] ]
761 ; CHECK-NEXT: [[F_SROA_0_0_F2_SROA_CAST1:%.*]] = bitcast i32* [[F_SROA_0]] to %struct.S*
762 ; CHECK-NEXT: br i1 undef, label [[IF_THEN5:%.*]], label [[IF_THEN2:%.*]]
764 ; CHECK-NEXT: br label [[IF_THEN5]]
766 ; CHECK-NEXT: [[F1:%.*]] = phi %struct.S* [ undef, [[IF_THEN2]] ], [ [[F_SROA_0_0_F2_SROA_CAST1]], [[IF_END]] ]
767 ; CHECK-NEXT: [[DOTFCA_0_GEP:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], %struct.S* [[F1]], i32 0, i32 0
768 ; CHECK-NEXT: store i32 undef, i32* [[DOTFCA_0_GEP]], align 4
769 ; CHECK-NEXT: ret void
772 %f = alloca %struct.S, align 4
773 br i1 undef, label %if.end, label %for.cond
775 for.cond: ; preds = %for.cond, %entry
778 if.end: ; preds = %for.cond, %entry
779 %f2 = phi %struct.S* [ %f, %entry ], [ %f, %for.cond ]
780 phi i32 [ undef, %entry ], [ undef, %for.cond ]
781 br i1 undef, label %if.then5, label %if.then2
783 if.then2: ; preds = %if.end
786 if.then5: ; preds = %if.then2, %if.end
787 %f1 = phi %struct.S* [ undef, %if.then2 ], [ %f2, %if.end ]
788 store %struct.S undef, %struct.S* %f1, align 4
792 define i32 @phi_align(i32* %z) {
793 ; CHECK-LABEL: @phi_align(
795 ; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca [7 x i8], align 1
796 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_IDX:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* [[A_SROA_0]], i64 0, i64 3
797 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_CAST:%.*]] = bitcast i8* [[A_SROA_0_3_A1_SROA_IDX]] to i32*
798 ; CHECK-NEXT: [[A_SROA_0_0_A0_SROA_CAST:%.*]] = bitcast [7 x i8]* [[A_SROA_0]] to i32*
799 ; CHECK-NEXT: store i32 0, i32* [[A_SROA_0_0_A0_SROA_CAST]], align 1
800 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_IDX7:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* [[A_SROA_0]], i64 0, i64 3
801 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_CAST8:%.*]] = bitcast i8* [[A_SROA_0_3_A1_SROA_IDX7]] to i32*
802 ; CHECK-NEXT: store i32 1, i32* [[A_SROA_0_3_A1_SROA_CAST8]], align 1
803 ; CHECK-NEXT: [[A_SROA_0_0_A0_SROA_CAST6:%.*]] = bitcast [7 x i8]* [[A_SROA_0]] to i32*
804 ; CHECK-NEXT: [[A_SROA_0_0_A_SROA_0_1_V0:%.*]] = load i32, i32* [[A_SROA_0_0_A0_SROA_CAST6]], align 1
805 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_IDX9:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* [[A_SROA_0]], i64 0, i64 3
806 ; CHECK-NEXT: [[A_SROA_0_3_A1_SROA_CAST10:%.*]] = bitcast i8* [[A_SROA_0_3_A1_SROA_IDX9]] to i32*
807 ; CHECK-NEXT: [[A_SROA_0_3_A_SROA_0_4_V1:%.*]] = load i32, i32* [[A_SROA_0_3_A1_SROA_CAST10]], align 1
808 ; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 [[A_SROA_0_0_A_SROA_0_1_V0]], [[A_SROA_0_3_A_SROA_0_4_V1]]
809 ; CHECK-NEXT: br i1 [[COND]], label [[THEN:%.*]], label [[EXIT:%.*]]
811 ; CHECK-NEXT: br label [[EXIT]]
813 ; CHECK-NEXT: [[PHI:%.*]] = phi i32* [ [[A_SROA_0_3_A1_SROA_CAST]], [[THEN]] ], [ [[Z:%.*]], [[ENTRY:%.*]] ]
814 ; CHECK-NEXT: [[RESULT:%.*]] = load i32, i32* [[PHI]], align 1
815 ; CHECK-NEXT: ret i32 [[RESULT]]
818 %a = alloca [8 x i8], align 8
820 %a0x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 1
821 %a0 = bitcast i8* %a0x to i32*
822 %a1x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 4
823 %a1 = bitcast i8* %a1x to i32*
824 store i32 0, i32* %a0, align 1
825 store i32 1, i32* %a1, align 4
826 %v0 = load i32, i32* %a0, align 1
827 %v1 = load i32, i32* %a1, align 4
828 %cond = icmp sle i32 %v0, %v1
829 br i1 %cond, label %then, label %exit
835 %phi = phi i32* [ %a1, %then ], [ %z, %entry ]
836 %result = load i32, i32* %phi, align 4
840 ; Don't speculate a load based on an earlier volatile operation.
841 define i8 @volatile_select(i8* %p, i1 %b) {
842 ; CHECK-LABEL: @volatile_select(
843 ; CHECK-NEXT: [[P2:%.*]] = alloca i8, align 1
844 ; CHECK-NEXT: store i8 0, i8* [[P2]], align 1
845 ; CHECK-NEXT: store volatile i8 0, i8* [[P:%.*]], align 1
846 ; CHECK-NEXT: [[PX:%.*]] = select i1 [[B:%.*]], i8* [[P]], i8* [[P2]]
847 ; CHECK-NEXT: [[V2:%.*]] = load i8, i8* [[PX]], align 1
848 ; CHECK-NEXT: ret i8 [[V2]]
852 store volatile i8 0, i8* %p
853 %px = select i1 %b, i8* %p, i8* %p2
854 %v2 = load i8, i8* %px