blob: fb76548b1d18787f262236d830b4c5a761ae8e4c [file] [log] [blame]
Chandler Carruth1b398ae2012-09-14 09:22:59 +00001; RUN: opt < %s -sroa -S | FileCheck %s
Chandler Carruth43c8b462012-10-04 10:39:28 +00002target 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"
Chandler Carruth1b398ae2012-09-14 09:22:59 +00003
4define i32 @test1() {
Stephen Linc1c7a132013-07-14 01:42:54 +00005; CHECK-LABEL: @test1(
Chandler Carruth1b398ae2012-09-14 09:22:59 +00006entry:
7 %a = alloca [2 x i32]
8; CHECK-NOT: alloca
9
David Blaikie79e6c742015-02-27 19:29:02 +000010 %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
Chandler Carruth1b398ae2012-09-14 09:22:59 +000012 store i32 0, i32* %a0
13 store i32 1, i32* %a1
David Blaikiea79ac142015-02-27 21:17:42 +000014 %v0 = load i32, i32* %a0
15 %v1 = load i32, i32* %a1
Chandler Carruth1b398ae2012-09-14 09:22:59 +000016; CHECK-NOT: store
17; CHECK-NOT: load
18
19 %cond = icmp sle i32 %v0, %v1
20 br i1 %cond, label %then, label %exit
21
22then:
23 br label %exit
24
25exit:
26 %phi = phi i32* [ %a1, %then ], [ %a0, %entry ]
27; CHECK: phi i32 [ 1, %{{.*}} ], [ 0, %{{.*}} ]
28
David Blaikiea79ac142015-02-27 21:17:42 +000029 %result = load i32, i32* %phi
Chandler Carruth1b398ae2012-09-14 09:22:59 +000030 ret i32 %result
31}
32
33define i32 @test2() {
Stephen Linc1c7a132013-07-14 01:42:54 +000034; CHECK-LABEL: @test2(
Chandler Carruth1b398ae2012-09-14 09:22:59 +000035entry:
36 %a = alloca [2 x i32]
37; CHECK-NOT: alloca
38
David Blaikie79e6c742015-02-27 19:29:02 +000039 %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
Chandler Carruth1b398ae2012-09-14 09:22:59 +000041 store i32 0, i32* %a0
42 store i32 1, i32* %a1
David Blaikiea79ac142015-02-27 21:17:42 +000043 %v0 = load i32, i32* %a0
44 %v1 = load i32, i32* %a1
Chandler Carruth1b398ae2012-09-14 09:22:59 +000045; CHECK-NOT: store
46; CHECK-NOT: load
47
48 %cond = icmp sle i32 %v0, %v1
49 %select = select i1 %cond, i32* %a1, i32* %a0
50; CHECK: select i1 %{{.*}}, i32 1, i32 0
51
David Blaikiea79ac142015-02-27 21:17:42 +000052 %result = load i32, i32* %select
Chandler Carruth1b398ae2012-09-14 09:22:59 +000053 ret i32 %result
54}
55
56define i32 @test3(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000057; CHECK-LABEL: @test3(
Chandler Carruth1b398ae2012-09-14 09:22:59 +000058entry:
59 %a = alloca [2 x i32]
60; CHECK-NOT: alloca
61
Chandler Carruth54e8f0b2012-10-01 01:49:22 +000062 ; 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.
David Blaikie79e6c742015-02-27 19:29:02 +000065 %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
Chandler Carruth1b398ae2012-09-14 09:22:59 +000069 store i32 0, i32* %a0
70 store i32 1, i32* %a1
71; CHECK-NOT: store
72
73 switch i32 %x, label %bb0 [ i32 1, label %bb1
74 i32 2, label %bb2
Chandler Carruth54e8f0b2012-10-01 01:49:22 +000075 i32 3, label %bb3
76 i32 4, label %bb4
77 i32 5, label %bb5
78 i32 6, label %bb6
79 i32 7, label %bb7 ]
Chandler Carruth1b398ae2012-09-14 09:22:59 +000080
81bb0:
82 br label %exit
83bb1:
84 br label %exit
85bb2:
86 br label %exit
87bb3:
88 br label %exit
Chandler Carruth54e8f0b2012-10-01 01:49:22 +000089bb4:
90 br label %exit
91bb5:
92 br label %exit
93bb6:
94 br label %exit
95bb7:
96 br label %exit
Chandler Carruth1b398ae2012-09-14 09:22:59 +000097
98exit:
Chandler Carruth54e8f0b2012-10-01 01:49:22 +000099 %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, %{{.*}} ]
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000102
David Blaikiea79ac142015-02-27 21:17:42 +0000103 %result = load i32, i32* %phi
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000104 ret i32 %result
105}
106
107define i32 @test4() {
Stephen Linc1c7a132013-07-14 01:42:54 +0000108; CHECK-LABEL: @test4(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000109entry:
110 %a = alloca [2 x i32]
111; CHECK-NOT: alloca
112
David Blaikie79e6c742015-02-27 19:29:02 +0000113 %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
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000115 store i32 0, i32* %a0
116 store i32 1, i32* %a1
David Blaikiea79ac142015-02-27 21:17:42 +0000117 %v0 = load i32, i32* %a0
118 %v1 = load i32, i32* %a1
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000119; CHECK-NOT: store
120; CHECK-NOT: load
121
122 %cond = icmp sle i32 %v0, %v1
123 %select = select i1 %cond, i32* %a0, i32* %a0
124; CHECK-NOT: select
125
David Blaikiea79ac142015-02-27 21:17:42 +0000126 %result = load i32, i32* %select
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000127 ret i32 %result
128; CHECK: ret i32 0
129}
130
131define i32 @test5(i32* %b) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000132; CHECK-LABEL: @test5(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000133entry:
134 %a = alloca [2 x i32]
135; CHECK-NOT: alloca
136
David Blaikie79e6c742015-02-27 19:29:02 +0000137 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000138 store i32 1, i32* %a1
139; CHECK-NOT: store
140
141 %select = select i1 true, i32* %a1, i32* %b
142; CHECK-NOT: select
143
David Blaikiea79ac142015-02-27 21:17:42 +0000144 %result = load i32, i32* %select
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000145; CHECK-NOT: load
146
147 ret i32 %result
148; CHECK: ret i32 1
149}
150
Chandler Carruth225d4bd2012-09-21 23:36:40 +0000151declare void @f(i32*, i32*)
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000152
153define i32 @test6(i32* %b) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000154; CHECK-LABEL: @test6(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000155entry:
156 %a = alloca [2 x i32]
Chandler Carruth225d4bd2012-09-21 23:36:40 +0000157 %c = alloca i32
158; CHECK-NOT: alloca
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000159
David Blaikie79e6c742015-02-27 19:29:02 +0000160 %a1 = getelementptr [2 x i32], [2 x i32]* %a, i64 0, i32 1
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000161 store i32 1, i32* %a1
162
163 %select = select i1 true, i32* %a1, i32* %b
164 %select2 = select i1 false, i32* %a1, i32* %b
Chandler Carruth225d4bd2012-09-21 23:36:40 +0000165 %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
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000168
169 ; Note, this would potentially escape the alloca pointer except for the
170 ; constant folding of the select.
Chandler Carruth225d4bd2012-09-21 23:36:40 +0000171 call void @f(i32* %select2, i32* %select3)
172; CHECK: call void @f(i32* %[[select2]], i32* %[[select3]])
173
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000174
David Blaikiea79ac142015-02-27 21:17:42 +0000175 %result = load i32, i32* %select
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000176; CHECK-NOT: load
177
David Blaikiea79ac142015-02-27 21:17:42 +0000178 %dead = load i32, i32* %c
Chandler Carruth225d4bd2012-09-21 23:36:40 +0000179
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000180 ret i32 %result
181; CHECK: ret i32 1
182}
183
184define i32 @test7() {
Stephen Linc1c7a132013-07-14 01:42:54 +0000185; CHECK-LABEL: @test7(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000186; CHECK-NOT: alloca
187
188entry:
189 %X = alloca i32
190 br i1 undef, label %good, label %bad
191
192good:
David Blaikie79e6c742015-02-27 19:29:02 +0000193 %Y1 = getelementptr i32, i32* %X, i64 0
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000194 store i32 0, i32* %Y1
195 br label %exit
196
197bad:
David Blaikie79e6c742015-02-27 19:29:02 +0000198 %Y2 = getelementptr i32, i32* %X, i64 1
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000199 store i32 0, i32* %Y2
200 br label %exit
201
202exit:
203 %P = phi i32* [ %Y1, %good ], [ %Y2, %bad ]
204; CHECK: %[[phi:.*]] = phi i32 [ 0, %good ],
David Blaikiea79ac142015-02-27 21:17:42 +0000205 %Z2 = load i32, i32* %P
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000206 ret i32 %Z2
207; CHECK: ret i32 %[[phi]]
208}
209
210define 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.
Stephen Linc1c7a132013-07-14 01:42:54 +0000213; CHECK-LABEL: @test8(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000214; CHECK-NOT: alloca
215; CHECK-NOT: load
David Blaikiea79ac142015-02-27 21:17:42 +0000216; CHECK: %[[value:.*]] = load i32, i32* %ptr
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000217; CHECK-NOT: load
218; CHECK: %[[result:.*]] = phi i32 [ undef, %else ], [ %[[value]], %then ]
219; CHECK-NEXT: ret i32 %[[result]]
220
221entry:
222 %f = alloca float
223 %test = icmp ne i32 %b, 0
224 br i1 %test, label %then, label %else
225
226then:
227 br label %exit
228
229else:
230 %bitcast = bitcast float* %f to i32*
231 br label %exit
232
233exit:
234 %phi = phi i32* [ %bitcast, %else ], [ %ptr, %then ]
David Blaikiea79ac142015-02-27 21:17:42 +0000235 %loaded = load i32, i32* %phi, align 4
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000236 ret i32 %loaded
237}
238
239define i32 @test9(i32 %b, i32* %ptr) {
240; Same as @test8 but for a select rather than a PHI node.
Stephen Linc1c7a132013-07-14 01:42:54 +0000241; CHECK-LABEL: @test9(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000242; CHECK-NOT: alloca
243; CHECK-NOT: load
David Blaikiea79ac142015-02-27 21:17:42 +0000244; CHECK: %[[value:.*]] = load i32, i32* %ptr
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000245; CHECK-NOT: load
246; CHECK: %[[result:.*]] = select i1 %{{.*}}, i32 undef, i32 %[[value]]
247; CHECK-NEXT: ret i32 %[[result]]
248
249entry:
250 %f = alloca float
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
David Blaikiea79ac142015-02-27 21:17:42 +0000255 %loaded = load i32, i32* %select, align 4
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000256 ret i32 %loaded
257}
258
Chandler Carruth435c4e02012-10-15 08:40:30 +0000259define float @test10(i32 %b, float* %ptr) {
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000260; 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
262; node.
Stephen Linc1c7a132013-07-14 01:42:54 +0000263; CHECK-LABEL: @test10(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000264; CHECK: %[[alloca:.*]] = alloca
David Blaikiea79ac142015-02-27 21:17:42 +0000265; CHECK: %[[argvalue:.*]] = load float, float* %ptr
Chandler Carruth435c4e02012-10-15 08:40:30 +0000266; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
David Blaikiea79ac142015-02-27 21:17:42 +0000267; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
Chandler Carruth435c4e02012-10-15 08:40:30 +0000268; CHECK: %[[result:.*]] = phi float [ %[[allocavalue]], %else ], [ %[[argvalue]], %then ]
269; CHECK-NEXT: ret float %[[result]]
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000270
271entry:
272 %f = alloca double
273 store double 0.0, double* %f
274 %test = icmp ne i32 %b, 0
275 br i1 %test, label %then, label %else
276
277then:
278 br label %exit
279
280else:
Chandler Carruth435c4e02012-10-15 08:40:30 +0000281 %bitcast = bitcast double* %f to float*
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000282 br label %exit
283
284exit:
Chandler Carruth435c4e02012-10-15 08:40:30 +0000285 %phi = phi float* [ %bitcast, %else ], [ %ptr, %then ]
David Blaikiea79ac142015-02-27 21:17:42 +0000286 %loaded = load float, float* %phi, align 4
Chandler Carruth435c4e02012-10-15 08:40:30 +0000287 ret float %loaded
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000288}
289
Chandler Carruth435c4e02012-10-15 08:40:30 +0000290define float @test11(i32 %b, float* %ptr) {
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000291; Same as @test10 but for a select rather than a PHI node.
Stephen Linc1c7a132013-07-14 01:42:54 +0000292; CHECK-LABEL: @test11(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000293; CHECK: %[[alloca:.*]] = alloca
Chandler Carruth435c4e02012-10-15 08:40:30 +0000294; CHECK: %[[cast:.*]] = bitcast double* %[[alloca]] to float*
David Blaikiea79ac142015-02-27 21:17:42 +0000295; CHECK: %[[allocavalue:.*]] = load float, float* %[[cast]]
296; CHECK: %[[argvalue:.*]] = load float, float* %ptr
Chandler Carruth435c4e02012-10-15 08:40:30 +0000297; CHECK: %[[result:.*]] = select i1 %{{.*}}, float %[[allocavalue]], float %[[argvalue]]
298; CHECK-NEXT: ret float %[[result]]
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000299
300entry:
301 %f = alloca double
302 store double 0.0, double* %f
Chandler Carruth435c4e02012-10-15 08:40:30 +0000303 store float 0.0, float* %ptr
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000304 %test = icmp ne i32 %b, 0
Chandler Carruth435c4e02012-10-15 08:40:30 +0000305 %bitcast = bitcast double* %f to float*
306 %select = select i1 %test, float* %bitcast, float* %ptr
David Blaikiea79ac142015-02-27 21:17:42 +0000307 %loaded = load float, float* %select, align 4
Chandler Carruth435c4e02012-10-15 08:40:30 +0000308 ret float %loaded
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000309}
310
311define i32 @test12(i32 %x, i32* %p) {
312; Ensure we don't crash or fail to nuke dead selects of allocas if no load is
313; never found.
Stephen Linc1c7a132013-07-14 01:42:54 +0000314; CHECK-LABEL: @test12(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000315; CHECK-NOT: alloca
316; CHECK-NOT: select
317; CHECK: ret i32 %x
318
319entry:
320 %a = alloca i32
321 store i32 %x, i32* %a
322 %dead = select i1 undef, i32* %a, i32* %p
David Blaikiea79ac142015-02-27 21:17:42 +0000323 %load = load i32, i32* %a
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000324 ret i32 %load
325}
326
327define 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
329; found.
Stephen Linc1c7a132013-07-14 01:42:54 +0000330; CHECK-LABEL: @test13(
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000331; CHECK-NOT: alloca
332; CHECK-NOT: phi
333; CHECK: ret i32 %x
334
335entry:
336 %a = alloca i32
337 store i32 %x, i32* %a
338 br label %loop
339
340loop:
341 %phi = phi i32* [ %p, %entry ], [ %a, %loop ]
342 br i1 undef, label %loop, label %exit
343
344exit:
David Blaikiea79ac142015-02-27 21:17:42 +0000345 %load = load i32, i32* %a
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000346 ret i32 %load
347}
Chandler Carruth8b907e82012-09-25 10:03:40 +0000348
Chandler Carruth58e25d32013-07-24 12:12:17 +0000349define 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
352; the speculation.
353; CHECK-LABEL: @test14(
354; CHECK: alloca
355; CHECK: alloca
356; CHECK: select
357; CHECK: phi
358; CHECK: phi
359; CHECK: select
360; CHECK: ret i32
361
362entry:
363 %f = alloca i32
364 %g = alloca i32
365 store i32 0, i32* %f
366 store i32 0, i32* %g
367 %f.select = select i1 %b1, i32* %f, i32* %ptr
368 br i1 %b2, label %then, label %else
369
370then:
371 br label %exit
372
373else:
374 br label %exit
375
376exit:
377 %f.phi = phi i32* [ %f, %then ], [ %f.select, %else ]
378 %g.phi = phi i32* [ %g, %then ], [ %ptr, %else ]
David Blaikiea79ac142015-02-27 21:17:42 +0000379 %f.loaded = load i32, i32* %f.phi
Chandler Carruth58e25d32013-07-24 12:12:17 +0000380 %g.select = select i1 %b1, i32* %g, i32* %g.phi
David Blaikiea79ac142015-02-27 21:17:42 +0000381 %g.loaded = load i32, i32* %g.select
Chandler Carruth58e25d32013-07-24 12:12:17 +0000382 %result = add i32 %f.loaded, %g.loaded
383 ret i32 %result
384}
385
Chandler Carruth8b907e82012-09-25 10:03:40 +0000386define 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.
Stephen Linc1c7a132013-07-14 01:42:54 +0000389; CHECK-LABEL: @PR13905(
Chandler Carruth8b907e82012-09-25 10:03:40 +0000390; CHECK-NOT: alloca i32
391; CHECK: ret i32 undef
392
393entry:
394 %h = alloca i32
395 store i32 0, i32* %h
396 br i1 undef, label %loop1, label %exit
397
398loop1:
399 %phi1 = phi i32* [ null, %entry ], [ %h, %loop1 ], [ %h, %loop2 ]
400 br i1 undef, label %loop1, label %loop2
401
402loop2:
403 br i1 undef, label %loop1, label %exit
404
405exit:
406 %phi2 = phi i32* [ %phi1, %loop2 ], [ null, %entry ]
407 ret i32 undef
408}
409
410define 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.
Stephen Linc1c7a132013-07-14 01:42:54 +0000414; CHECK-LABEL: @PR13906(
Chandler Carruth8b907e82012-09-25 10:03:40 +0000415; CHECK-NOT: alloca
416
417entry:
418 %c = alloca i32
419 store i32 0, i32* %c
420 br label %for.cond
421
422for.cond:
423 %d.0 = phi i32* [ undef, %entry ], [ %c, %if.then ], [ %d.0, %for.cond ]
424 br i1 undef, label %if.then, label %for.cond
425
426if.then:
427 %tmpcast.d.0 = select i1 undef, i32* %c, i32* %d.0
428 br label %for.cond
429}
Chandler Carruth3e994a22012-11-20 10:02:19 +0000430
431define i64 @PR14132(i1 %flag) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000432; CHECK-LABEL: @PR14132(
Chandler Carruth3e994a22012-11-20 10:02:19 +0000433; 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
Chandler Carrutha1c54bb2013-03-14 11:32:24 +0000436; 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
439; support this..
Chandler Carruth3e994a22012-11-20 10:02:19 +0000440entry:
Chandler Carruthccffdaf2015-07-22 03:32:42 +0000441 %a = alloca i64, align 8
442 %b = alloca i8, align 8
443 %ptr = alloca i64*, align 8
Chandler Carruth3e994a22012-11-20 10:02:19 +0000444; CHECK-NOT: alloca
445
446 %ptr.cast = bitcast i64** %ptr to i8**
Chandler Carruthccffdaf2015-07-22 03:32:42 +0000447 store i64 0, i64* %a, align 8
448 store i8 1, i8* %b, align 8
449 store i64* %a, i64** %ptr, align 8
Chandler Carruth3e994a22012-11-20 10:02:19 +0000450 br i1 %flag, label %if.then, label %if.end
451
452if.then:
Chandler Carruthccffdaf2015-07-22 03:32:42 +0000453 store i8* %b, i8** %ptr.cast, align 8
Chandler Carruth3e994a22012-11-20 10:02:19 +0000454 br label %if.end
Chandler Carrutha1c54bb2013-03-14 11:32:24 +0000455; CHECK-NOT: store
456; CHECK: %[[ext:.*]] = zext i8 1 to i64
Chandler Carruth3e994a22012-11-20 10:02:19 +0000457
458if.end:
Chandler Carruthccffdaf2015-07-22 03:32:42 +0000459 %tmp = load i64*, i64** %ptr, align 8
460 %result = load i64, i64* %tmp, align 8
Chandler Carruth3e994a22012-11-20 10:02:19 +0000461; CHECK-NOT: load
Chandler Carrutha1c54bb2013-03-14 11:32:24 +0000462; CHECK: %[[result:.*]] = phi i64 [ %[[ext]], %if.then ], [ 0, %entry ]
Chandler Carruth3e994a22012-11-20 10:02:19 +0000463
464 ret i64 %result
465; CHECK-NEXT: ret i64 %[[result]]
466}
Chandler Carruth83ea1952013-07-24 09:47:28 +0000467
468define 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.
473
474entry:
475 %a = alloca i64, align 8
476 store i64 %x, i64* %a
477 br i1 %flag, label %then, label %else
478; CHECK-NOT: alloca
479; CHECK-NOT: store
480; CHECK: %[[lo:.*]] = trunc i64 %x to i32
481; CHECK: %[[shift:.*]] = lshr i64 %x, 32
482; CHECK: %[[hi:.*]] = trunc i64 %[[shift]] to i32
483
484then:
485 %a.f = bitcast i64* %a to float*
486 br label %end
487; CHECK: %[[lo_cast:.*]] = bitcast i32 %[[lo]] to float
488
489else:
490 %a.raw = bitcast i64* %a to i8*
David Blaikie79e6c742015-02-27 19:29:02 +0000491 %a.raw.4 = getelementptr i8, i8* %a.raw, i64 4
Chandler Carruth83ea1952013-07-24 09:47:28 +0000492 %a.raw.4.f = bitcast i8* %a.raw.4 to float*
493 br label %end
494; CHECK: %[[hi_cast:.*]] = bitcast i32 %[[hi]] to float
495
496end:
497 %a.phi.f = phi float* [ %a.f, %then ], [ %a.raw.4.f, %else ]
David Blaikiea79ac142015-02-27 21:17:42 +0000498 %f = load float, float* %a.phi.f
Chandler Carruth83ea1952013-07-24 09:47:28 +0000499 ret float %f
500; CHECK: %[[phi:.*]] = phi float [ %[[lo_cast]], %then ], [ %[[hi_cast]], %else ]
501; CHECK-NOT: load
502; CHECK: ret float %[[phi]]
503}
Jingyue Wuec33fa92014-08-22 22:45:57 +0000504
505; Verifies we fixed PR20425. We should be able to promote all alloca's to
506; registers in this test.
507;
508; %0 = slice
509; %1 = slice
510; %2 = phi(%0, %1) // == slice
511define float @simplify_phi_nodes_that_equal_slice(i1 %cond, float* %temp) {
512; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice(
513entry:
514 %arr = alloca [4 x float], align 4
515; CHECK-NOT: alloca
516 br i1 %cond, label %then, label %else
517
518then:
David Blaikie79e6c742015-02-27 19:29:02 +0000519 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
Jingyue Wuec33fa92014-08-22 22:45:57 +0000520 store float 1.000000e+00, float* %0, align 4
521 br label %merge
522
523else:
David Blaikie79e6c742015-02-27 19:29:02 +0000524 %1 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
Jingyue Wuec33fa92014-08-22 22:45:57 +0000525 store float 2.000000e+00, float* %1, align 4
526 br label %merge
527
528merge:
529 %2 = phi float* [ %0, %then ], [ %1, %else ]
530 store float 0.000000e+00, float* %temp, align 4
David Blaikiea79ac142015-02-27 21:17:42 +0000531 %3 = load float, float* %2, align 4
Jingyue Wuec33fa92014-08-22 22:45:57 +0000532 ret float %3
533}
534
535; A slightly complicated example for PR20425.
536;
537; %0 = slice
538; %1 = phi(%0) // == slice
539; %2 = slice
540; %3 = phi(%1, %2) // == slice
541define float @simplify_phi_nodes_that_equal_slice_2(i1 %cond, float* %temp) {
542; CHECK-LABEL: @simplify_phi_nodes_that_equal_slice_2(
543entry:
544 %arr = alloca [4 x float], align 4
545; CHECK-NOT: alloca
546 br i1 %cond, label %then, label %else
547
548then:
David Blaikie79e6c742015-02-27 19:29:02 +0000549 %0 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
Jingyue Wuec33fa92014-08-22 22:45:57 +0000550 store float 1.000000e+00, float* %0, align 4
551 br label %then2
552
553then2:
554 %1 = phi float* [ %0, %then ]
555 store float 2.000000e+00, float* %1, align 4
556 br label %merge
557
558else:
David Blaikie79e6c742015-02-27 19:29:02 +0000559 %2 = getelementptr inbounds [4 x float], [4 x float]* %arr, i64 0, i64 3
Jingyue Wuec33fa92014-08-22 22:45:57 +0000560 store float 3.000000e+00, float* %2, align 4
561 br label %merge
562
563merge:
564 %3 = phi float* [ %1, %then2 ], [ %2, %else ]
565 store float 0.000000e+00, float* %temp, align 4
David Blaikiea79ac142015-02-27 21:17:42 +0000566 %4 = load float, float* %3, align 4
Jingyue Wuec33fa92014-08-22 22:45:57 +0000567 ret float %4
568}
David Majnemerd4cffcf2014-09-01 21:20:14 +0000569
570%struct.S = type { i32 }
571
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.
577define void @PR20822() {
578; CHECK-LABEL: @PR20822(
579entry:
580 %f = alloca %struct.S, align 4
581; CHECK: %[[alloca:.*]] = alloca
582 br i1 undef, label %if.end, label %for.cond
583
584for.cond: ; preds = %for.cond, %entry
585 br label %if.end
586
587if.end: ; preds = %for.cond, %entry
588 %f2 = phi %struct.S* [ %f, %entry ], [ %f, %for.cond ]
589; CHECK: phi i32
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
593
594if.then2: ; preds = %if.end
595 br label %if.then5
596
597if.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
601 ret void
602}