Fix getCommonType in a different way from the way I fixed it when
[oota-llvm.git] / test / Transforms / SROA / basictest.ll
1 ; RUN: opt < %s -sroa -S | FileCheck %s
2 ; RUN: opt < %s -sroa -force-ssa-updater -S | FileCheck %s
3
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"
5
6 declare void @llvm.lifetime.start(i64, i8* nocapture)
7 declare void @llvm.lifetime.end(i64, i8* nocapture)
8
9 define i32 @test0() {
10 ; CHECK: @test0
11 ; CHECK-NOT: alloca
12 ; CHECK: ret i32
13
14 entry:
15   %a1 = alloca i32
16   %a2 = alloca float
17
18   %a1.i8 = bitcast i32* %a1 to i8*
19   call void @llvm.lifetime.start(i64 4, i8* %a1.i8)
20
21   store i32 0, i32* %a1
22   %v1 = load i32* %a1
23
24   call void @llvm.lifetime.end(i64 4, i8* %a1.i8)
25
26   %a2.i8 = bitcast float* %a2 to i8*
27   call void @llvm.lifetime.start(i64 4, i8* %a2.i8)
28
29   store float 0.0, float* %a2
30   %v2 = load float * %a2
31   %v2.int = bitcast float %v2 to i32
32   %sum1 = add i32 %v1, %v2.int
33
34   call void @llvm.lifetime.end(i64 4, i8* %a2.i8)
35
36   ret i32 %sum1
37 }
38
39 define i32 @test1() {
40 ; CHECK: @test1
41 ; CHECK-NOT: alloca
42 ; CHECK: ret i32 0
43
44 entry:
45   %X = alloca { i32, float }
46   %Y = getelementptr { i32, float }* %X, i64 0, i32 0
47   store i32 0, i32* %Y
48   %Z = load i32* %Y
49   ret i32 %Z
50 }
51
52 define i64 @test2(i64 %X) {
53 ; CHECK: @test2
54 ; CHECK-NOT: alloca
55 ; CHECK: ret i64 %X
56
57 entry:
58   %A = alloca [8 x i8]
59   %B = bitcast [8 x i8]* %A to i64*
60   store i64 %X, i64* %B
61   br label %L2
62
63 L2:
64   %Z = load i64* %B
65   ret i64 %Z
66 }
67
68 define void @test3(i8* %dst, i8* %src) {
69 ; CHECK: @test3
70
71 entry:
72   %a = alloca [300 x i8]
73 ; CHECK-NOT:  alloca
74 ; CHECK:      %[[test3_a1:.*]] = alloca [42 x i8]
75 ; CHECK-NEXT: %[[test3_a2:.*]] = alloca [99 x i8]
76 ; CHECK-NEXT: %[[test3_a3:.*]] = alloca [16 x i8]
77 ; CHECK-NEXT: %[[test3_a4:.*]] = alloca [42 x i8]
78 ; CHECK-NEXT: %[[test3_a5:.*]] = alloca [7 x i8]
79 ; CHECK-NEXT: %[[test3_a6:.*]] = alloca [7 x i8]
80 ; CHECK-NEXT: %[[test3_a7:.*]] = alloca [85 x i8]
81
82   %b = getelementptr [300 x i8]* %a, i64 0, i64 0
83   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %b, i8* %src, i32 300, i32 1, i1 false)
84 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8]* %[[test3_a1]], i64 0, i64 0
85 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %src, i32 42
86 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 42
87 ; CHECK-NEXT: %[[test3_r1:.*]] = load i8* %[[gep]]
88 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 43
89 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [99 x i8]* %[[test3_a2]], i64 0, i64 0
90 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 99
91 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 142
92 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 0
93 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 16
94 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 158
95 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8]* %[[test3_a4]], i64 0, i64 0
96 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 42
97 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 200
98 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 0
99 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
100 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 207
101 ; CHECK-NEXT: %[[test3_r2:.*]] = load i8* %[[gep]]
102 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 208
103 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 0
104 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
105 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 215
106 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [85 x i8]* %[[test3_a7]], i64 0, i64 0
107 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 85
108
109   ; Clobber a single element of the array, this should be promotable.
110   %c = getelementptr [300 x i8]* %a, i64 0, i64 42
111   store i8 0, i8* %c
112
113   ; Make a sequence of overlapping stores to the array. These overlap both in
114   ; forward strides and in shrinking accesses.
115   %overlap.1.i8 = getelementptr [300 x i8]* %a, i64 0, i64 142
116   %overlap.2.i8 = getelementptr [300 x i8]* %a, i64 0, i64 143
117   %overlap.3.i8 = getelementptr [300 x i8]* %a, i64 0, i64 144
118   %overlap.4.i8 = getelementptr [300 x i8]* %a, i64 0, i64 145
119   %overlap.5.i8 = getelementptr [300 x i8]* %a, i64 0, i64 146
120   %overlap.6.i8 = getelementptr [300 x i8]* %a, i64 0, i64 147
121   %overlap.7.i8 = getelementptr [300 x i8]* %a, i64 0, i64 148
122   %overlap.8.i8 = getelementptr [300 x i8]* %a, i64 0, i64 149
123   %overlap.9.i8 = getelementptr [300 x i8]* %a, i64 0, i64 150
124   %overlap.1.i16 = bitcast i8* %overlap.1.i8 to i16*
125   %overlap.1.i32 = bitcast i8* %overlap.1.i8 to i32*
126   %overlap.1.i64 = bitcast i8* %overlap.1.i8 to i64*
127   %overlap.2.i64 = bitcast i8* %overlap.2.i8 to i64*
128   %overlap.3.i64 = bitcast i8* %overlap.3.i8 to i64*
129   %overlap.4.i64 = bitcast i8* %overlap.4.i8 to i64*
130   %overlap.5.i64 = bitcast i8* %overlap.5.i8 to i64*
131   %overlap.6.i64 = bitcast i8* %overlap.6.i8 to i64*
132   %overlap.7.i64 = bitcast i8* %overlap.7.i8 to i64*
133   %overlap.8.i64 = bitcast i8* %overlap.8.i8 to i64*
134   %overlap.9.i64 = bitcast i8* %overlap.9.i8 to i64*
135   store i8 1, i8* %overlap.1.i8
136 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 0
137 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
138   store i16 1, i16* %overlap.1.i16
139 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i16*
140 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
141   store i32 1, i32* %overlap.1.i32
142 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i32*
143 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
144   store i64 1, i64* %overlap.1.i64
145 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i64*
146 ; CHECK-NEXT: store i64 1, i64* %[[bitcast]]
147   store i64 2, i64* %overlap.2.i64
148 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 1
149 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
150 ; CHECK-NEXT: store i64 2, i64* %[[bitcast]]
151   store i64 3, i64* %overlap.3.i64
152 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 2
153 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
154 ; CHECK-NEXT: store i64 3, i64* %[[bitcast]]
155   store i64 4, i64* %overlap.4.i64
156 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 3
157 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
158 ; CHECK-NEXT: store i64 4, i64* %[[bitcast]]
159   store i64 5, i64* %overlap.5.i64
160 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 4
161 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
162 ; CHECK-NEXT: store i64 5, i64* %[[bitcast]]
163   store i64 6, i64* %overlap.6.i64
164 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 5
165 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
166 ; CHECK-NEXT: store i64 6, i64* %[[bitcast]]
167   store i64 7, i64* %overlap.7.i64
168 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 6
169 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
170 ; CHECK-NEXT: store i64 7, i64* %[[bitcast]]
171   store i64 8, i64* %overlap.8.i64
172 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 7
173 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
174 ; CHECK-NEXT: store i64 8, i64* %[[bitcast]]
175   store i64 9, i64* %overlap.9.i64
176 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 8
177 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
178 ; CHECK-NEXT: store i64 9, i64* %[[bitcast]]
179
180   ; Make two sequences of overlapping stores with more gaps and irregularities.
181   %overlap2.1.0.i8 = getelementptr [300 x i8]* %a, i64 0, i64 200
182   %overlap2.1.1.i8 = getelementptr [300 x i8]* %a, i64 0, i64 201
183   %overlap2.1.2.i8 = getelementptr [300 x i8]* %a, i64 0, i64 202
184   %overlap2.1.3.i8 = getelementptr [300 x i8]* %a, i64 0, i64 203
185
186   %overlap2.2.0.i8 = getelementptr [300 x i8]* %a, i64 0, i64 208
187   %overlap2.2.1.i8 = getelementptr [300 x i8]* %a, i64 0, i64 209
188   %overlap2.2.2.i8 = getelementptr [300 x i8]* %a, i64 0, i64 210
189   %overlap2.2.3.i8 = getelementptr [300 x i8]* %a, i64 0, i64 211
190
191   %overlap2.1.0.i16 = bitcast i8* %overlap2.1.0.i8 to i16*
192   %overlap2.1.0.i32 = bitcast i8* %overlap2.1.0.i8 to i32*
193   %overlap2.1.1.i32 = bitcast i8* %overlap2.1.1.i8 to i32*
194   %overlap2.1.2.i32 = bitcast i8* %overlap2.1.2.i8 to i32*
195   %overlap2.1.3.i32 = bitcast i8* %overlap2.1.3.i8 to i32*
196   store i8 1,  i8*  %overlap2.1.0.i8
197 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 0
198 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
199   store i16 1, i16* %overlap2.1.0.i16
200 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a5]] to i16*
201 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
202   store i32 1, i32* %overlap2.1.0.i32
203 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a5]] to i32*
204 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
205   store i32 2, i32* %overlap2.1.1.i32
206 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 1
207 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
208 ; CHECK-NEXT: store i32 2, i32* %[[bitcast]]
209   store i32 3, i32* %overlap2.1.2.i32
210 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 2
211 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
212 ; CHECK-NEXT: store i32 3, i32* %[[bitcast]]
213   store i32 4, i32* %overlap2.1.3.i32
214 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 3
215 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
216 ; CHECK-NEXT: store i32 4, i32* %[[bitcast]]
217
218   %overlap2.2.0.i32 = bitcast i8* %overlap2.2.0.i8 to i32*
219   %overlap2.2.1.i16 = bitcast i8* %overlap2.2.1.i8 to i16*
220   %overlap2.2.1.i32 = bitcast i8* %overlap2.2.1.i8 to i32*
221   %overlap2.2.2.i32 = bitcast i8* %overlap2.2.2.i8 to i32*
222   %overlap2.2.3.i32 = bitcast i8* %overlap2.2.3.i8 to i32*
223   store i32 1, i32* %overlap2.2.0.i32
224 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a6]] to i32*
225 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
226   store i8 1,  i8*  %overlap2.2.1.i8
227 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 1
228 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
229   store i16 1, i16* %overlap2.2.1.i16
230 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 1
231 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
232 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
233   store i32 1, i32* %overlap2.2.1.i32
234 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 1
235 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
236 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
237   store i32 3, i32* %overlap2.2.2.i32
238 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 2
239 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
240 ; CHECK-NEXT: store i32 3, i32* %[[bitcast]]
241   store i32 4, i32* %overlap2.2.3.i32
242 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 3
243 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
244 ; CHECK-NEXT: store i32 4, i32* %[[bitcast]]
245
246   %overlap2.prefix = getelementptr i8* %overlap2.1.1.i8, i64 -4
247   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.prefix, i8* %src, i32 8, i32 1, i1 false)
248 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8]* %[[test3_a4]], i64 0, i64 39
249 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %src, i32 3
250 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 3
251 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 0
252 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 5
253
254   ; Bridge between the overlapping areas
255   call void @llvm.memset.p0i8.i32(i8* %overlap2.1.2.i8, i8 42, i32 8, i32 1, i1 false)
256 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 2
257 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[gep]], i8 42, i32 5
258 ; ...promoted i8 store...
259 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 0
260 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[gep]], i8 42, i32 2
261
262   ; Entirely within the second overlap.
263   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.2.1.i8, i8* %src, i32 5, i32 1, i1 false)
264 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 1
265 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 5
266
267   ; Trailing past the second overlap.
268   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.2.2.i8, i8* %src, i32 8, i32 1, i1 false)
269 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 2
270 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 5
271 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 5
272 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [85 x i8]* %[[test3_a7]], i64 0, i64 0
273 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 3
274
275   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %b, i32 300, i32 1, i1 false)
276 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [42 x i8]* %[[test3_a1]], i64 0, i64 0
277 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[gep]], i32 42
278 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 42
279 ; CHECK-NEXT: store i8 0, i8* %[[gep]]
280 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 43
281 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [99 x i8]* %[[test3_a2]], i64 0, i64 0
282 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 99
283 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 142
284 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [16 x i8]* %[[test3_a3]], i64 0, i64 0
285 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 16
286 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 158
287 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [42 x i8]* %[[test3_a4]], i64 0, i64 0
288 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 42
289 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 200
290 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a5]], i64 0, i64 0
291 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
292 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 207
293 ; CHECK-NEXT: store i8 42, i8* %[[gep]]
294 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 208
295 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test3_a6]], i64 0, i64 0
296 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
297 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 215
298 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [85 x i8]* %[[test3_a7]], i64 0, i64 0
299 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 85
300
301   ret void
302 }
303
304 define void @test4(i8* %dst, i8* %src) {
305 ; CHECK: @test4
306
307 entry:
308   %a = alloca [100 x i8]
309 ; CHECK-NOT:  alloca
310 ; CHECK:      %[[test4_a1:.*]] = alloca [20 x i8]
311 ; CHECK-NEXT: %[[test4_a2:.*]] = alloca [7 x i8]
312 ; CHECK-NEXT: %[[test4_a3:.*]] = alloca [10 x i8]
313 ; CHECK-NEXT: %[[test4_a4:.*]] = alloca [7 x i8]
314 ; CHECK-NEXT: %[[test4_a5:.*]] = alloca [7 x i8]
315 ; CHECK-NEXT: %[[test4_a6:.*]] = alloca [40 x i8]
316
317   %b = getelementptr [100 x i8]* %a, i64 0, i64 0
318   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %b, i8* %src, i32 100, i32 1, i1 false)
319 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [20 x i8]* %[[test4_a1]], i64 0, i64 0
320 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 20
321 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 20
322 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
323 ; CHECK-NEXT: %[[test4_r1:.*]] = load i16* %[[bitcast]]
324 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 22
325 ; CHECK-NEXT: %[[test4_r2:.*]] = load i8* %[[gep]]
326 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 23
327 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a2]], i64 0, i64 0
328 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
329 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 30
330 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [10 x i8]* %[[test4_a3]], i64 0, i64 0
331 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 10
332 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 40
333 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
334 ; CHECK-NEXT: %[[test4_r3:.*]] = load i16* %[[bitcast]]
335 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 42
336 ; CHECK-NEXT: %[[test4_r4:.*]] = load i8* %[[gep]]
337 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 43
338 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a4]], i64 0, i64 0
339 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
340 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 50
341 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
342 ; CHECK-NEXT: %[[test4_r5:.*]] = load i16* %[[bitcast]]
343 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %src, i64 52
344 ; CHECK-NEXT: %[[test4_r6:.*]] = load i8* %[[gep]]
345 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 53
346 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a5]], i64 0, i64 0
347 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
348 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8* %src, i64 60
349 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [40 x i8]* %[[test4_a6]], i64 0, i64 0
350 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 40
351
352   %a.src.1 = getelementptr [100 x i8]* %a, i64 0, i64 20
353   %a.dst.1 = getelementptr [100 x i8]* %a, i64 0, i64 40
354   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a.dst.1, i8* %a.src.1, i32 10, i32 1, i1 false)
355 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a4]], i64 0, i64 0
356 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a2]], i64 0, i64 0
357 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
358
359   ; Clobber a single element of the array, this should be promotable, and be deleted.
360   %c = getelementptr [100 x i8]* %a, i64 0, i64 42
361   store i8 0, i8* %c
362
363   %a.src.2 = getelementptr [100 x i8]* %a, i64 0, i64 50
364   call void @llvm.memmove.p0i8.p0i8.i32(i8* %a.dst.1, i8* %a.src.2, i32 10, i32 1, i1 false)
365 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a4]], i64 0, i64 0
366 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a5]], i64 0, i64 0
367 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
368
369   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %b, i32 100, i32 1, i1 false)
370 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [20 x i8]* %[[test4_a1]], i64 0, i64 0
371 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[gep]], i32 20
372 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 20
373 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
374 ; CHECK-NEXT: store i16 %[[test4_r1]], i16* %[[bitcast]]
375 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 22
376 ; CHECK-NEXT: store i8 %[[test4_r2]], i8* %[[gep]]
377 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 23
378 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a2]], i64 0, i64 0
379 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
380 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 30
381 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [10 x i8]* %[[test4_a3]], i64 0, i64 0
382 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 10
383 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 40
384 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
385 ; CHECK-NEXT: store i16 %[[test4_r5]], i16* %[[bitcast]]
386 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 42
387 ; CHECK-NEXT: store i8 %[[test4_r6]], i8* %[[gep]]
388 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 43
389 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a4]], i64 0, i64 0
390 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
391 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 50
392 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
393 ; CHECK-NEXT: store i16 %[[test4_r5]], i16* %[[bitcast]]
394 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8* %dst, i64 52
395 ; CHECK-NEXT: store i8 %[[test4_r6]], i8* %[[gep]]
396 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 53
397 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8]* %[[test4_a5]], i64 0, i64 0
398 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
399 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8* %dst, i64 60
400 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [40 x i8]* %[[test4_a6]], i64 0, i64 0
401 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 40
402
403   ret void
404 }
405
406 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
407 declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
408 declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1) nounwind
409
410 define i16 @test5() {
411 ; CHECK: @test5
412 ; CHECK: alloca float
413 ; CHECK: ret i16 %
414
415 entry:
416   %a = alloca [4 x i8]
417   %fptr = bitcast [4 x i8]* %a to float*
418   store float 0.0, float* %fptr
419   %ptr = getelementptr [4 x i8]* %a, i32 0, i32 2
420   %iptr = bitcast i8* %ptr to i16*
421   %val = load i16* %iptr
422   ret i16 %val
423 }
424
425 define i32 @test6() {
426 ; CHECK: @test6
427 ; CHECK: alloca i32
428 ; CHECK-NEXT: store volatile i32
429 ; CHECK-NEXT: load i32*
430 ; CHECK-NEXT: ret i32
431
432 entry:
433   %a = alloca [4 x i8]
434   %ptr = getelementptr [4 x i8]* %a, i32 0, i32 0
435   call void @llvm.memset.p0i8.i32(i8* %ptr, i8 42, i32 4, i32 1, i1 true)
436   %iptr = bitcast i8* %ptr to i32*
437   %val = load i32* %iptr
438   ret i32 %val
439 }
440
441 define void @test7(i8* %src, i8* %dst) {
442 ; CHECK: @test7
443 ; CHECK: alloca i32
444 ; CHECK-NEXT: bitcast i8* %src to i32*
445 ; CHECK-NEXT: load volatile i32*
446 ; CHECK-NEXT: store volatile i32
447 ; CHECK-NEXT: bitcast i8* %dst to i32*
448 ; CHECK-NEXT: load volatile i32*
449 ; CHECK-NEXT: store volatile i32
450 ; CHECK-NEXT: ret
451
452 entry:
453   %a = alloca [4 x i8]
454   %ptr = getelementptr [4 x i8]* %a, i32 0, i32 0
455   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 true)
456   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 true)
457   ret void
458 }
459
460
461 %S1 = type { i32, i32, [16 x i8] }
462 %S2 = type { %S1*, %S2* }
463
464 define %S2 @test8(%S2* %s2) {
465 ; CHECK: @test8
466 entry:
467   %new = alloca %S2
468 ; CHECK-NOT: alloca
469
470   %s2.next.ptr = getelementptr %S2* %s2, i64 0, i32 1
471   %s2.next = load %S2** %s2.next.ptr
472 ; CHECK:      %[[gep:.*]] = getelementptr %S2* %s2, i64 0, i32 1
473 ; CHECK-NEXT: %[[next:.*]] = load %S2** %[[gep]]
474
475   %s2.next.s1.ptr = getelementptr %S2* %s2.next, i64 0, i32 0
476   %s2.next.s1 = load %S1** %s2.next.s1.ptr
477   %new.s1.ptr = getelementptr %S2* %new, i64 0, i32 0
478   store %S1* %s2.next.s1, %S1** %new.s1.ptr
479   %s2.next.next.ptr = getelementptr %S2* %s2.next, i64 0, i32 1
480   %s2.next.next = load %S2** %s2.next.next.ptr
481   %new.next.ptr = getelementptr %S2* %new, i64 0, i32 1
482   store %S2* %s2.next.next, %S2** %new.next.ptr
483 ; CHECK-NEXT: %[[gep:.*]] = getelementptr %S2* %[[next]], i64 0, i32 0
484 ; CHECK-NEXT: %[[next_s1:.*]] = load %S1** %[[gep]]
485 ; CHECK-NEXT: %[[gep:.*]] = getelementptr %S2* %[[next]], i64 0, i32 1
486 ; CHECK-NEXT: %[[next_next:.*]] = load %S2** %[[gep]]
487
488   %new.s1 = load %S1** %new.s1.ptr
489   %result1 = insertvalue %S2 undef, %S1* %new.s1, 0
490 ; CHECK-NEXT: %[[result1:.*]] = insertvalue %S2 undef, %S1* %[[next_s1]], 0
491   %new.next = load %S2** %new.next.ptr
492   %result2 = insertvalue %S2 %result1, %S2* %new.next, 1
493 ; CHECK-NEXT: %[[result2:.*]] = insertvalue %S2 %[[result1]], %S2* %[[next_next]], 1
494   ret %S2 %result2
495 ; CHECK-NEXT: ret %S2 %[[result2]]
496 }
497
498 define i64 @test9() {
499 ; Ensure we can handle loads off the end of an alloca even when wrapped in
500 ; weird bit casts and types. The result is undef, but this shouldn't crash
501 ; anything.
502 ; CHECK: @test9
503 ; CHECK-NOT: alloca
504 ; CHECK: ret i64 undef
505
506 entry:
507   %a = alloca { [3 x i8] }
508   %gep1 = getelementptr inbounds { [3 x i8] }* %a, i32 0, i32 0, i32 0
509   store i8 0, i8* %gep1, align 1
510   %gep2 = getelementptr inbounds { [3 x i8] }* %a, i32 0, i32 0, i32 1
511   store i8 0, i8* %gep2, align 1
512   %gep3 = getelementptr inbounds { [3 x i8] }* %a, i32 0, i32 0, i32 2
513   store i8 26, i8* %gep3, align 1
514   %cast = bitcast { [3 x i8] }* %a to { i64 }*
515   %elt = getelementptr inbounds { i64 }* %cast, i32 0, i32 0
516   %result = load i64* %elt
517   ret i64 %result
518 }
519
520 define %S2* @test10() {
521 ; CHECK: @test10
522 ; CHECK-NOT: alloca %S2*
523 ; CHECK: ret %S2* null
524
525 entry:
526   %a = alloca [8 x i8]
527   %ptr = getelementptr [8 x i8]* %a, i32 0, i32 0
528   call void @llvm.memset.p0i8.i32(i8* %ptr, i8 0, i32 8, i32 1, i1 false)
529   %s2ptrptr = bitcast i8* %ptr to %S2**
530   %s2ptr = load %S2** %s2ptrptr
531   ret %S2* %s2ptr
532 }
533
534 define i32 @test11() {
535 ; CHECK: @test11
536 ; CHECK-NOT: alloca
537 ; CHECK: ret i32 0
538
539 entry:
540   %X = alloca i32
541   br i1 undef, label %good, label %bad
542
543 good:
544   %Y = getelementptr i32* %X, i64 0
545   store i32 0, i32* %Y
546   %Z = load i32* %Y
547   ret i32 %Z
548
549 bad:
550   %Y2 = getelementptr i32* %X, i64 1
551   store i32 0, i32* %Y2
552   %Z2 = load i32* %Y2
553   ret i32 %Z2
554 }
555
556 define i32 @test12() {
557 ; CHECK: @test12
558 ; CHECK: alloca i24
559 ;
560 ; FIXME: SROA should promote accesses to this into whole i24 operations instead
561 ; of i8 operations.
562 ; CHECK: store i8 0
563 ; CHECK: store i8 0
564 ; CHECK: store i8 0
565 ;
566 ; CHECK: load i24*
567
568 entry:
569   %a = alloca [3 x i8]
570   %b0ptr = getelementptr [3 x i8]* %a, i64 0, i32 0
571   store i8 0, i8* %b0ptr
572   %b1ptr = getelementptr [3 x i8]* %a, i64 0, i32 1
573   store i8 0, i8* %b1ptr
574   %b2ptr = getelementptr [3 x i8]* %a, i64 0, i32 2
575   store i8 0, i8* %b2ptr
576   %iptr = bitcast [3 x i8]* %a to i24*
577   %i = load i24* %iptr
578   %ret = zext i24 %i to i32
579   ret i32 %ret
580 }
581
582 define i32 @test13() {
583 ; Ensure we don't crash and handle undefined loads that straddle the end of the
584 ; allocation.
585 ; CHECK: @test13
586 ; CHECK: %[[ret:.*]] = zext i16 undef to i32
587 ; CHECK: ret i32 %[[ret]]
588
589 entry:
590   %a = alloca [3 x i8]
591   %b0ptr = getelementptr [3 x i8]* %a, i64 0, i32 0
592   store i8 0, i8* %b0ptr
593   %b1ptr = getelementptr [3 x i8]* %a, i64 0, i32 1
594   store i8 0, i8* %b1ptr
595   %b2ptr = getelementptr [3 x i8]* %a, i64 0, i32 2
596   store i8 0, i8* %b2ptr
597   %iptrcast = bitcast [3 x i8]* %a to i16*
598   %iptrgep = getelementptr i16* %iptrcast, i64 1
599   %i = load i16* %iptrgep
600   %ret = zext i16 %i to i32
601   ret i32 %ret
602 }
603
604 %test14.struct = type { [3 x i32] }
605
606 define void @test14(...) nounwind uwtable {
607 ; This is a strange case where we split allocas into promotable partitions, but
608 ; also gain enough data to prove they must be dead allocas due to GEPs that walk
609 ; across two adjacent allocas. Test that we don't try to promote or otherwise
610 ; do bad things to these dead allocas, they should just be removed.
611 ; CHECK: @test14
612 ; CHECK-NEXT: entry:
613 ; CHECK-NEXT: ret void
614
615 entry:
616   %a = alloca %test14.struct
617   %p = alloca %test14.struct*
618   %0 = bitcast %test14.struct* %a to i8*
619   %1 = getelementptr i8* %0, i64 12
620   %2 = bitcast i8* %1 to %test14.struct*
621   %3 = getelementptr inbounds %test14.struct* %2, i32 0, i32 0
622   %4 = getelementptr inbounds %test14.struct* %a, i32 0, i32 0
623   %5 = bitcast [3 x i32]* %3 to i32*
624   %6 = bitcast [3 x i32]* %4 to i32*
625   %7 = load i32* %6, align 4
626   store i32 %7, i32* %5, align 4
627   %8 = getelementptr inbounds i32* %5, i32 1
628   %9 = getelementptr inbounds i32* %6, i32 1
629   %10 = load i32* %9, align 4
630   store i32 %10, i32* %8, align 4
631   %11 = getelementptr inbounds i32* %5, i32 2
632   %12 = getelementptr inbounds i32* %6, i32 2
633   %13 = load i32* %12, align 4
634   store i32 %13, i32* %11, align 4
635   ret void
636 }
637
638 define i32 @test15(i1 %flag) nounwind uwtable {
639 ; Ensure that when there are dead instructions using an alloca that are not
640 ; loads or stores we still delete them during partitioning and rewriting.
641 ; Otherwise we'll go to promote them while thy still have unpromotable uses.
642 ; CHECK: @test15
643 ; CHECK-NEXT: entry:
644 ; CHECK-NEXT:   br label %loop
645 ; CHECK:      loop:
646 ; CHECK-NEXT:   br label %loop
647
648 entry:
649   %l0 = alloca i64
650   %l1 = alloca i64
651   %l2 = alloca i64
652   %l3 = alloca i64
653   br label %loop
654
655 loop:
656   %dead3 = phi i8* [ %gep3, %loop ], [ null, %entry ]
657
658   store i64 1879048192, i64* %l0, align 8
659   %bc0 = bitcast i64* %l0 to i8*
660   %gep0 = getelementptr i8* %bc0, i64 3
661   %dead0 = bitcast i8* %gep0 to i64*
662
663   store i64 1879048192, i64* %l1, align 8
664   %bc1 = bitcast i64* %l1 to i8*
665   %gep1 = getelementptr i8* %bc1, i64 3
666   %dead1 = getelementptr i8* %gep1, i64 1
667
668   store i64 1879048192, i64* %l2, align 8
669   %bc2 = bitcast i64* %l2 to i8*
670   %gep2.1 = getelementptr i8* %bc2, i64 1
671   %gep2.2 = getelementptr i8* %bc2, i64 3
672   ; Note that this select should get visited multiple times due to using two
673   ; different GEPs off the same alloca. We should only delete it once.
674   %dead2 = select i1 %flag, i8* %gep2.1, i8* %gep2.2
675
676   store i64 1879048192, i64* %l3, align 8
677   %bc3 = bitcast i64* %l3 to i8*
678   %gep3 = getelementptr i8* %bc3, i64 3
679
680   br label %loop
681 }
682
683 define void @test16(i8* %src, i8* %dst) {
684 ; Ensure that we can promote an alloca of [3 x i8] to an i24 SSA value.
685 ; CHECK: @test16
686 ; CHECK-NOT: alloca
687 ; CHECK:      %[[srccast:.*]] = bitcast i8* %src to i24*
688 ; CHECK-NEXT: load i24* %[[srccast]]
689 ; CHECK-NEXT: %[[dstcast:.*]] = bitcast i8* %dst to i24*
690 ; CHECK-NEXT: store i24 0, i24* %[[dstcast]]
691 ; CHECK-NEXT: ret void
692
693 entry:
694   %a = alloca [3 x i8]
695   %ptr = getelementptr [3 x i8]* %a, i32 0, i32 0
696   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 false)
697   %cast = bitcast i8* %ptr to i24*
698   store i24 0, i24* %cast
699   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 false)
700   ret void
701 }
702
703 define void @test17(i8* %src, i8* %dst) {
704 ; Ensure that we can rewrite unpromotable memcpys which extend past the end of
705 ; the alloca.
706 ; CHECK: @test17
707 ; CHECK:      %[[a:.*]] = alloca [3 x i8]
708 ; CHECK-NEXT: %[[ptr:.*]] = getelementptr [3 x i8]* %[[a]], i32 0, i32 0
709 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[ptr]], i8* %src,
710 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[ptr]],
711 ; CHECK-NEXT: ret void
712
713 entry:
714   %a = alloca [3 x i8]
715   %ptr = getelementptr [3 x i8]* %a, i32 0, i32 0
716   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 true)
717   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 true)
718   ret void
719 }
720
721 define void @test18(i8* %src, i8* %dst, i32 %size) {
722 ; Preserve transfer instrinsics with a variable size, even if they overlap with
723 ; fixed size operations. Further, continue to split and promote allocas preceding
724 ; the variable sized intrinsic.
725 ; CHECK: @test18
726 ; CHECK:      %[[a:.*]] = alloca [34 x i8]
727 ; CHECK:      %[[srcgep1:.*]] = getelementptr inbounds i8* %src, i64 4
728 ; CHECK-NEXT: %[[srccast1:.*]] = bitcast i8* %[[srcgep1]] to i32*
729 ; CHECK-NEXT: %[[srcload:.*]] = load i32* %[[srccast1]]
730 ; CHECK-NEXT: %[[agep1:.*]] = getelementptr inbounds [34 x i8]* %[[a]], i64 0, i64 0
731 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[agep1]], i8* %src, i32 %size,
732 ; CHECK-NEXT: %[[agep2:.*]] = getelementptr inbounds [34 x i8]* %[[a]], i64 0, i64 0
733 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[agep2]], i8 42, i32 %size,
734 ; CHECK-NEXT: %[[dstcast1:.*]] = bitcast i8* %dst to i32*
735 ; CHECK-NEXT: store i32 42, i32* %[[dstcast1]]
736 ; CHECK-NEXT: %[[dstgep1:.*]] = getelementptr inbounds i8* %dst, i64 4
737 ; CHECK-NEXT: %[[dstcast2:.*]] = bitcast i8* %[[dstgep1]] to i32*
738 ; CHECK-NEXT: store i32 %[[srcload]], i32* %[[dstcast2]]
739 ; CHECK-NEXT: %[[agep3:.*]] = getelementptr inbounds [34 x i8]* %[[a]], i64 0, i64 0
740 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[agep3]], i32 %size,
741 ; CHECK-NEXT: ret void
742
743 entry:
744   %a = alloca [42 x i8]
745   %ptr = getelementptr [42 x i8]* %a, i32 0, i32 0
746   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 8, i32 1, i1 false)
747   %ptr2 = getelementptr [42 x i8]* %a, i32 0, i32 8
748   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr2, i8* %src, i32 %size, i32 1, i1 false)
749   call void @llvm.memset.p0i8.i32(i8* %ptr2, i8 42, i32 %size, i32 1, i1 false)
750   %cast = bitcast i8* %ptr to i32*
751   store i32 42, i32* %cast
752   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 8, i32 1, i1 false)
753   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr2, i32 %size, i32 1, i1 false)
754   ret void
755 }
756