1 ; RUN: opt < %s -sroa -S | FileCheck %s
3 target datalayout = "e-p:64:64:64-p1:16:16:16-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 declare void @llvm.lifetime.start(i64, i8* nocapture)
6 declare void @llvm.lifetime.end(i64, i8* nocapture)
17 %a1.i8 = bitcast i32* %a1 to i8*
18 call void @llvm.lifetime.start(i64 4, i8* %a1.i8)
21 %v1 = load i32, i32* %a1
23 call void @llvm.lifetime.end(i64 4, i8* %a1.i8)
25 %a2.i8 = bitcast float* %a2 to i8*
26 call void @llvm.lifetime.start(i64 4, i8* %a2.i8)
28 store float 0.0, float* %a2
29 %v2 = load float , float * %a2
30 %v2.int = bitcast float %v2 to i32
31 %sum1 = add i32 %v1, %v2.int
33 call void @llvm.lifetime.end(i64 4, i8* %a2.i8)
39 ; CHECK-LABEL: @test1(
44 %X = alloca { i32, float }
45 %Y = getelementptr { i32, float }, { i32, float }* %X, i64 0, i32 0
47 %Z = load i32, i32* %Y
51 define i64 @test2(i64 %X) {
52 ; CHECK-LABEL: @test2(
58 %B = bitcast [8 x i8]* %A to i64*
63 %Z = load i64, i64* %B
67 define void @test3(i8* %dst, i8* %src) {
68 ; CHECK-LABEL: @test3(
71 %a = alloca [300 x i8]
73 ; CHECK: %[[test3_a1:.*]] = alloca [42 x i8]
74 ; CHECK-NEXT: %[[test3_a2:.*]] = alloca [99 x i8]
75 ; CHECK-NEXT: %[[test3_a3:.*]] = alloca [16 x i8]
76 ; CHECK-NEXT: %[[test3_a4:.*]] = alloca [42 x i8]
77 ; CHECK-NEXT: %[[test3_a5:.*]] = alloca [7 x i8]
78 ; CHECK-NEXT: %[[test3_a6:.*]] = alloca [7 x i8]
79 ; CHECK-NEXT: %[[test3_a7:.*]] = alloca [85 x i8]
81 %b = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 0
82 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %b, i8* %src, i32 300, i32 1, i1 false)
83 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8], [42 x i8]* %[[test3_a1]], i64 0, i64 0
84 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %src, i32 42
85 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 42
86 ; CHECK-NEXT: %[[test3_r1:.*]] = load i8, i8* %[[gep]]
87 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 43
88 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [99 x i8], [99 x i8]* %[[test3_a2]], i64 0, i64 0
89 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 99
90 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 142
91 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 0
92 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 16
93 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 158
94 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8], [42 x i8]* %[[test3_a4]], i64 0, i64 0
95 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 42
96 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 200
97 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 0
98 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
99 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 207
100 ; CHECK-NEXT: %[[test3_r2:.*]] = load i8, i8* %[[gep]]
101 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 208
102 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 0
103 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
104 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 215
105 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [85 x i8], [85 x i8]* %[[test3_a7]], i64 0, i64 0
106 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 85
108 ; Clobber a single element of the array, this should be promotable.
109 %c = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 42
112 ; Make a sequence of overlapping stores to the array. These overlap both in
113 ; forward strides and in shrinking accesses.
114 %overlap.1.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 142
115 %overlap.2.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 143
116 %overlap.3.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 144
117 %overlap.4.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 145
118 %overlap.5.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 146
119 %overlap.6.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 147
120 %overlap.7.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 148
121 %overlap.8.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 149
122 %overlap.9.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 150
123 %overlap.1.i16 = bitcast i8* %overlap.1.i8 to i16*
124 %overlap.1.i32 = bitcast i8* %overlap.1.i8 to i32*
125 %overlap.1.i64 = bitcast i8* %overlap.1.i8 to i64*
126 %overlap.2.i64 = bitcast i8* %overlap.2.i8 to i64*
127 %overlap.3.i64 = bitcast i8* %overlap.3.i8 to i64*
128 %overlap.4.i64 = bitcast i8* %overlap.4.i8 to i64*
129 %overlap.5.i64 = bitcast i8* %overlap.5.i8 to i64*
130 %overlap.6.i64 = bitcast i8* %overlap.6.i8 to i64*
131 %overlap.7.i64 = bitcast i8* %overlap.7.i8 to i64*
132 %overlap.8.i64 = bitcast i8* %overlap.8.i8 to i64*
133 %overlap.9.i64 = bitcast i8* %overlap.9.i8 to i64*
134 store i8 1, i8* %overlap.1.i8
135 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 0
136 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
137 store i16 1, i16* %overlap.1.i16
138 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i16*
139 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
140 store i32 1, i32* %overlap.1.i32
141 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i32*
142 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
143 store i64 1, i64* %overlap.1.i64
144 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [16 x i8]* %[[test3_a3]] to i64*
145 ; CHECK-NEXT: store i64 1, i64* %[[bitcast]]
146 store i64 2, i64* %overlap.2.i64
147 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 1
148 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
149 ; CHECK-NEXT: store i64 2, i64* %[[bitcast]]
150 store i64 3, i64* %overlap.3.i64
151 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 2
152 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
153 ; CHECK-NEXT: store i64 3, i64* %[[bitcast]]
154 store i64 4, i64* %overlap.4.i64
155 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 3
156 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
157 ; CHECK-NEXT: store i64 4, i64* %[[bitcast]]
158 store i64 5, i64* %overlap.5.i64
159 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 4
160 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
161 ; CHECK-NEXT: store i64 5, i64* %[[bitcast]]
162 store i64 6, i64* %overlap.6.i64
163 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 5
164 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
165 ; CHECK-NEXT: store i64 6, i64* %[[bitcast]]
166 store i64 7, i64* %overlap.7.i64
167 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 6
168 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
169 ; CHECK-NEXT: store i64 7, i64* %[[bitcast]]
170 store i64 8, i64* %overlap.8.i64
171 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 7
172 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
173 ; CHECK-NEXT: store i64 8, i64* %[[bitcast]]
174 store i64 9, i64* %overlap.9.i64
175 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 8
176 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i64*
177 ; CHECK-NEXT: store i64 9, i64* %[[bitcast]]
179 ; Make two sequences of overlapping stores with more gaps and irregularities.
180 %overlap2.1.0.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 200
181 %overlap2.1.1.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 201
182 %overlap2.1.2.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 202
183 %overlap2.1.3.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 203
185 %overlap2.2.0.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 208
186 %overlap2.2.1.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 209
187 %overlap2.2.2.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 210
188 %overlap2.2.3.i8 = getelementptr [300 x i8], [300 x i8]* %a, i64 0, i64 211
190 %overlap2.1.0.i16 = bitcast i8* %overlap2.1.0.i8 to i16*
191 %overlap2.1.0.i32 = bitcast i8* %overlap2.1.0.i8 to i32*
192 %overlap2.1.1.i32 = bitcast i8* %overlap2.1.1.i8 to i32*
193 %overlap2.1.2.i32 = bitcast i8* %overlap2.1.2.i8 to i32*
194 %overlap2.1.3.i32 = bitcast i8* %overlap2.1.3.i8 to i32*
195 store i8 1, i8* %overlap2.1.0.i8
196 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 0
197 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
198 store i16 1, i16* %overlap2.1.0.i16
199 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a5]] to i16*
200 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
201 store i32 1, i32* %overlap2.1.0.i32
202 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a5]] to i32*
203 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
204 store i32 2, i32* %overlap2.1.1.i32
205 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 1
206 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
207 ; CHECK-NEXT: store i32 2, i32* %[[bitcast]]
208 store i32 3, i32* %overlap2.1.2.i32
209 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 2
210 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
211 ; CHECK-NEXT: store i32 3, i32* %[[bitcast]]
212 store i32 4, i32* %overlap2.1.3.i32
213 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 3
214 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
215 ; CHECK-NEXT: store i32 4, i32* %[[bitcast]]
217 %overlap2.2.0.i32 = bitcast i8* %overlap2.2.0.i8 to i32*
218 %overlap2.2.1.i16 = bitcast i8* %overlap2.2.1.i8 to i16*
219 %overlap2.2.1.i32 = bitcast i8* %overlap2.2.1.i8 to i32*
220 %overlap2.2.2.i32 = bitcast i8* %overlap2.2.2.i8 to i32*
221 %overlap2.2.3.i32 = bitcast i8* %overlap2.2.3.i8 to i32*
222 store i32 1, i32* %overlap2.2.0.i32
223 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast [7 x i8]* %[[test3_a6]] to i32*
224 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
225 store i8 1, i8* %overlap2.2.1.i8
226 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 1
227 ; CHECK-NEXT: store i8 1, i8* %[[gep]]
228 store i16 1, i16* %overlap2.2.1.i16
229 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 1
230 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
231 ; CHECK-NEXT: store i16 1, i16* %[[bitcast]]
232 store i32 1, i32* %overlap2.2.1.i32
233 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 1
234 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
235 ; CHECK-NEXT: store i32 1, i32* %[[bitcast]]
236 store i32 3, i32* %overlap2.2.2.i32
237 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 2
238 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
239 ; CHECK-NEXT: store i32 3, i32* %[[bitcast]]
240 store i32 4, i32* %overlap2.2.3.i32
241 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 3
242 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i32*
243 ; CHECK-NEXT: store i32 4, i32* %[[bitcast]]
245 %overlap2.prefix = getelementptr i8, i8* %overlap2.1.1.i8, i64 -4
246 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.prefix, i8* %src, i32 8, i32 1, i1 false)
247 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [42 x i8], [42 x i8]* %[[test3_a4]], i64 0, i64 39
248 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %src, i32 3
249 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 3
250 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 0
251 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 5
253 ; Bridge between the overlapping areas
254 call void @llvm.memset.p0i8.i32(i8* %overlap2.1.2.i8, i8 42, i32 8, i32 1, i1 false)
255 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 2
256 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[gep]], i8 42, i32 5
257 ; ...promoted i8 store...
258 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 0
259 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[gep]], i8 42, i32 2
261 ; Entirely within the second overlap.
262 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.2.1.i8, i8* %src, i32 5, i32 1, i1 false)
263 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 1
264 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 5
266 ; Trailing past the second overlap.
267 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %overlap2.2.2.i8, i8* %src, i32 8, i32 1, i1 false)
268 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 2
269 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 5
270 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 5
271 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [85 x i8], [85 x i8]* %[[test3_a7]], i64 0, i64 0
272 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 3
274 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %b, i32 300, i32 1, i1 false)
275 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [42 x i8], [42 x i8]* %[[test3_a1]], i64 0, i64 0
276 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[gep]], i32 42
277 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 42
278 ; CHECK-NEXT: store i8 0, i8* %[[gep]]
279 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 43
280 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [99 x i8], [99 x i8]* %[[test3_a2]], i64 0, i64 0
281 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 99
282 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 142
283 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[test3_a3]], i64 0, i64 0
284 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 16
285 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 158
286 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [42 x i8], [42 x i8]* %[[test3_a4]], i64 0, i64 0
287 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 42
288 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 200
289 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a5]], i64 0, i64 0
290 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
291 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 207
292 ; CHECK-NEXT: store i8 42, i8* %[[gep]]
293 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 208
294 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test3_a6]], i64 0, i64 0
295 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
296 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 215
297 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [85 x i8], [85 x i8]* %[[test3_a7]], i64 0, i64 0
298 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 85
303 define void @test4(i8* %dst, i8* %src) {
304 ; CHECK-LABEL: @test4(
307 %a = alloca [100 x i8]
309 ; CHECK: %[[test4_a1:.*]] = alloca [20 x i8]
310 ; CHECK-NEXT: %[[test4_a2:.*]] = alloca [7 x i8]
311 ; CHECK-NEXT: %[[test4_a3:.*]] = alloca [10 x i8]
312 ; CHECK-NEXT: %[[test4_a4:.*]] = alloca [7 x i8]
313 ; CHECK-NEXT: %[[test4_a5:.*]] = alloca [7 x i8]
314 ; CHECK-NEXT: %[[test4_a6:.*]] = alloca [40 x i8]
316 %b = getelementptr [100 x i8], [100 x i8]* %a, i64 0, i64 0
317 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %b, i8* %src, i32 100, i32 1, i1 false)
318 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [20 x i8], [20 x i8]* %[[test4_a1]], i64 0, i64 0
319 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep]], i8* %src, i32 20
320 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 20
321 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
322 ; CHECK-NEXT: %[[test4_r1:.*]] = load i16, i16* %[[bitcast]]
323 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 22
324 ; CHECK-NEXT: %[[test4_r2:.*]] = load i8, i8* %[[gep]]
325 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 23
326 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a2]], i64 0, i64 0
327 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
328 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 30
329 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [10 x i8], [10 x i8]* %[[test4_a3]], i64 0, i64 0
330 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 10
331 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 40
332 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
333 ; CHECK-NEXT: %[[test4_r3:.*]] = load i16, i16* %[[bitcast]]
334 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 42
335 ; CHECK-NEXT: %[[test4_r4:.*]] = load i8, i8* %[[gep]]
336 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 43
337 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a4]], i64 0, i64 0
338 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
339 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 50
340 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
341 ; CHECK-NEXT: %[[test4_r5:.*]] = load i16, i16* %[[bitcast]]
342 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %src, i64 52
343 ; CHECK-NEXT: %[[test4_r6:.*]] = load i8, i8* %[[gep]]
344 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 53
345 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a5]], i64 0, i64 0
346 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
347 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds i8, i8* %src, i64 60
348 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [40 x i8], [40 x i8]* %[[test4_a6]], i64 0, i64 0
349 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 40
351 %a.src.1 = getelementptr [100 x i8], [100 x i8]* %a, i64 0, i64 20
352 %a.dst.1 = getelementptr [100 x i8], [100 x i8]* %a, i64 0, i64 40
353 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a.dst.1, i8* %a.src.1, i32 10, i32 1, i1 false)
354 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a4]], i64 0, i64 0
355 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a2]], i64 0, i64 0
356 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
358 ; Clobber a single element of the array, this should be promotable, and be deleted.
359 %c = getelementptr [100 x i8], [100 x i8]* %a, i64 0, i64 42
362 %a.src.2 = getelementptr [100 x i8], [100 x i8]* %a, i64 0, i64 50
363 call void @llvm.memmove.p0i8.p0i8.i32(i8* %a.dst.1, i8* %a.src.2, i32 10, i32 1, i1 false)
364 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a4]], i64 0, i64 0
365 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a5]], i64 0, i64 0
366 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
368 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %b, i32 100, i32 1, i1 false)
369 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds [20 x i8], [20 x i8]* %[[test4_a1]], i64 0, i64 0
370 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[gep]], i32 20
371 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 20
372 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
373 ; CHECK-NEXT: store i16 %[[test4_r1]], i16* %[[bitcast]]
374 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 22
375 ; CHECK-NEXT: store i8 %[[test4_r2]], i8* %[[gep]]
376 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 23
377 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a2]], i64 0, i64 0
378 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
379 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 30
380 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [10 x i8], [10 x i8]* %[[test4_a3]], i64 0, i64 0
381 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 10
382 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 40
383 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
384 ; CHECK-NEXT: store i16 %[[test4_r5]], i16* %[[bitcast]]
385 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 42
386 ; CHECK-NEXT: store i8 %[[test4_r6]], i8* %[[gep]]
387 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 43
388 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a4]], i64 0, i64 0
389 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
390 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 50
391 ; CHECK-NEXT: %[[bitcast:.*]] = bitcast i8* %[[gep]] to i16*
392 ; CHECK-NEXT: store i16 %[[test4_r5]], i16* %[[bitcast]]
393 ; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i8, i8* %dst, i64 52
394 ; CHECK-NEXT: store i8 %[[test4_r6]], i8* %[[gep]]
395 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 53
396 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [7 x i8], [7 x i8]* %[[test4_a5]], i64 0, i64 0
397 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 7
398 ; CHECK-NEXT: %[[gep_dst:.*]] = getelementptr inbounds i8, i8* %dst, i64 60
399 ; CHECK-NEXT: %[[gep_src:.*]] = getelementptr inbounds [40 x i8], [40 x i8]* %[[test4_a6]], i64 0, i64 0
400 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[gep_dst]], i8* %[[gep_src]], i32 40
405 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
406 declare void @llvm.memcpy.p1i8.p0i8.i32(i8 addrspace(1)* 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
410 define i16 @test5() {
411 ; CHECK-LABEL: @test5(
412 ; CHECK-NOT: alloca float
413 ; CHECK: %[[cast:.*]] = bitcast float 0.0{{.*}} to i32
414 ; CHECK-NEXT: %[[shr:.*]] = lshr i32 %[[cast]], 16
415 ; CHECK-NEXT: %[[trunc:.*]] = trunc i32 %[[shr]] to i16
416 ; CHECK-NEXT: ret i16 %[[trunc]]
420 %fptr = bitcast [4 x i8]* %a to float*
421 store float 0.0, float* %fptr
422 %ptr = getelementptr [4 x i8], [4 x i8]* %a, i32 0, i32 2
423 %iptr = bitcast i8* %ptr to i16*
424 %val = load i16, i16* %iptr
428 define i32 @test6() {
429 ; CHECK-LABEL: @test6(
431 ; CHECK-NEXT: store volatile i32
432 ; CHECK-NEXT: load i32, i32*
433 ; CHECK-NEXT: ret i32
437 %ptr = getelementptr [4 x i8], [4 x i8]* %a, i32 0, i32 0
438 call void @llvm.memset.p0i8.i32(i8* %ptr, i8 42, i32 4, i32 1, i1 true)
439 %iptr = bitcast i8* %ptr to i32*
440 %val = load i32, i32* %iptr
444 define void @test7(i8* %src, i8* %dst) {
445 ; CHECK-LABEL: @test7(
447 ; CHECK-NEXT: bitcast i8* %src to i32*
448 ; CHECK-NEXT: load volatile i32, i32*
449 ; CHECK-NEXT: store volatile i32
450 ; CHECK-NEXT: bitcast i8* %dst to i32*
451 ; CHECK-NEXT: load volatile i32, i32*
452 ; CHECK-NEXT: store volatile i32
457 %ptr = getelementptr [4 x i8], [4 x i8]* %a, i32 0, i32 0
458 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 true)
459 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 true)
464 %S1 = type { i32, i32, [16 x i8] }
465 %S2 = type { %S1*, %S2* }
467 define %S2 @test8(%S2* %s2) {
468 ; CHECK-LABEL: @test8(
473 %s2.next.ptr = getelementptr %S2, %S2* %s2, i64 0, i32 1
474 %s2.next = load %S2*, %S2** %s2.next.ptr
475 ; CHECK: %[[gep:.*]] = getelementptr %S2, %S2* %s2, i64 0, i32 1
476 ; CHECK-NEXT: %[[next:.*]] = load %S2*, %S2** %[[gep]]
478 %s2.next.s1.ptr = getelementptr %S2, %S2* %s2.next, i64 0, i32 0
479 %s2.next.s1 = load %S1*, %S1** %s2.next.s1.ptr
480 %new.s1.ptr = getelementptr %S2, %S2* %new, i64 0, i32 0
481 store %S1* %s2.next.s1, %S1** %new.s1.ptr
482 %s2.next.next.ptr = getelementptr %S2, %S2* %s2.next, i64 0, i32 1
483 %s2.next.next = load %S2*, %S2** %s2.next.next.ptr
484 %new.next.ptr = getelementptr %S2, %S2* %new, i64 0, i32 1
485 store %S2* %s2.next.next, %S2** %new.next.ptr
486 ; CHECK-NEXT: %[[gep:.*]] = getelementptr %S2, %S2* %[[next]], i64 0, i32 0
487 ; CHECK-NEXT: %[[next_s1:.*]] = load %S1*, %S1** %[[gep]]
488 ; CHECK-NEXT: %[[gep:.*]] = getelementptr %S2, %S2* %[[next]], i64 0, i32 1
489 ; CHECK-NEXT: %[[next_next:.*]] = load %S2*, %S2** %[[gep]]
491 %new.s1 = load %S1*, %S1** %new.s1.ptr
492 %result1 = insertvalue %S2 undef, %S1* %new.s1, 0
493 ; CHECK-NEXT: %[[result1:.*]] = insertvalue %S2 undef, %S1* %[[next_s1]], 0
494 %new.next = load %S2*, %S2** %new.next.ptr
495 %result2 = insertvalue %S2 %result1, %S2* %new.next, 1
496 ; CHECK-NEXT: %[[result2:.*]] = insertvalue %S2 %[[result1]], %S2* %[[next_next]], 1
498 ; CHECK-NEXT: ret %S2 %[[result2]]
501 define i64 @test9() {
502 ; Ensure we can handle loads off the end of an alloca even when wrapped in
503 ; weird bit casts and types. This is valid IR due to the alignment and masking
504 ; off the bits past the end of the alloca.
506 ; CHECK-LABEL: @test9(
508 ; CHECK: %[[b2:.*]] = zext i8 26 to i64
509 ; CHECK-NEXT: %[[s2:.*]] = shl i64 %[[b2]], 16
510 ; CHECK-NEXT: %[[m2:.*]] = and i64 undef, -16711681
511 ; CHECK-NEXT: %[[i2:.*]] = or i64 %[[m2]], %[[s2]]
512 ; CHECK-NEXT: %[[b1:.*]] = zext i8 0 to i64
513 ; CHECK-NEXT: %[[s1:.*]] = shl i64 %[[b1]], 8
514 ; CHECK-NEXT: %[[m1:.*]] = and i64 %[[i2]], -65281
515 ; CHECK-NEXT: %[[i1:.*]] = or i64 %[[m1]], %[[s1]]
516 ; CHECK-NEXT: %[[b0:.*]] = zext i8 0 to i64
517 ; CHECK-NEXT: %[[m0:.*]] = and i64 %[[i1]], -256
518 ; CHECK-NEXT: %[[i0:.*]] = or i64 %[[m0]], %[[b0]]
519 ; CHECK-NEXT: %[[result:.*]] = and i64 %[[i0]], 16777215
520 ; CHECK-NEXT: ret i64 %[[result]]
523 %a = alloca { [3 x i8] }, align 8
524 %gep1 = getelementptr inbounds { [3 x i8] }, { [3 x i8] }* %a, i32 0, i32 0, i32 0
525 store i8 0, i8* %gep1, align 1
526 %gep2 = getelementptr inbounds { [3 x i8] }, { [3 x i8] }* %a, i32 0, i32 0, i32 1
527 store i8 0, i8* %gep2, align 1
528 %gep3 = getelementptr inbounds { [3 x i8] }, { [3 x i8] }* %a, i32 0, i32 0, i32 2
529 store i8 26, i8* %gep3, align 1
530 %cast = bitcast { [3 x i8] }* %a to { i64 }*
531 %elt = getelementptr inbounds { i64 }, { i64 }* %cast, i32 0, i32 0
532 %load = load i64, i64* %elt
533 %result = and i64 %load, 16777215
537 define %S2* @test10() {
538 ; CHECK-LABEL: @test10(
539 ; CHECK-NOT: alloca %S2*
540 ; CHECK: ret %S2* null
544 %ptr = getelementptr [8 x i8], [8 x i8]* %a, i32 0, i32 0
545 call void @llvm.memset.p0i8.i32(i8* %ptr, i8 0, i32 8, i32 1, i1 false)
546 %s2ptrptr = bitcast i8* %ptr to %S2**
547 %s2ptr = load %S2*, %S2** %s2ptrptr
551 define i32 @test11() {
552 ; CHECK-LABEL: @test11(
558 br i1 undef, label %good, label %bad
561 %Y = getelementptr i32, i32* %X, i64 0
563 %Z = load i32, i32* %Y
567 %Y2 = getelementptr i32, i32* %X, i64 1
568 store i32 0, i32* %Y2
569 %Z2 = load i32, i32* %Y2
573 define i8 @test12() {
574 ; We fully promote these to the i24 load or store size, resulting in just masks
575 ; and other operations that instcombine will fold, but no alloca.
577 ; CHECK-LABEL: @test12(
584 %a0ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 0
585 store i8 0, i8* %a0ptr
586 %a1ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 1
587 store i8 0, i8* %a1ptr
588 %a2ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 2
589 store i8 0, i8* %a2ptr
590 %aiptr = bitcast [3 x i8]* %a to i24*
591 %ai = load i24, i24* %aiptr
594 ; CHECK: %[[ext2:.*]] = zext i8 0 to i24
595 ; CHECK-NEXT: %[[shift2:.*]] = shl i24 %[[ext2]], 16
596 ; CHECK-NEXT: %[[mask2:.*]] = and i24 undef, 65535
597 ; CHECK-NEXT: %[[insert2:.*]] = or i24 %[[mask2]], %[[shift2]]
598 ; CHECK-NEXT: %[[ext1:.*]] = zext i8 0 to i24
599 ; CHECK-NEXT: %[[shift1:.*]] = shl i24 %[[ext1]], 8
600 ; CHECK-NEXT: %[[mask1:.*]] = and i24 %[[insert2]], -65281
601 ; CHECK-NEXT: %[[insert1:.*]] = or i24 %[[mask1]], %[[shift1]]
602 ; CHECK-NEXT: %[[ext0:.*]] = zext i8 0 to i24
603 ; CHECK-NEXT: %[[mask0:.*]] = and i24 %[[insert1]], -256
604 ; CHECK-NEXT: %[[insert0:.*]] = or i24 %[[mask0]], %[[ext0]]
606 %biptr = bitcast [3 x i8]* %b to i24*
607 store i24 %ai, i24* %biptr
608 %b0ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 0
609 %b0 = load i8, i8* %b0ptr
610 %b1ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 1
611 %b1 = load i8, i8* %b1ptr
612 %b2ptr = getelementptr [3 x i8], [3 x i8]* %b, i64 0, i32 2
613 %b2 = load i8, i8* %b2ptr
616 ; CHECK: %[[trunc0:.*]] = trunc i24 %[[insert0]] to i8
617 ; CHECK-NEXT: %[[shift1:.*]] = lshr i24 %[[insert0]], 8
618 ; CHECK-NEXT: %[[trunc1:.*]] = trunc i24 %[[shift1]] to i8
619 ; CHECK-NEXT: %[[shift2:.*]] = lshr i24 %[[insert0]], 16
620 ; CHECK-NEXT: %[[trunc2:.*]] = trunc i24 %[[shift2]] to i8
622 %bsum0 = add i8 %b0, %b1
623 %bsum1 = add i8 %bsum0, %b2
625 ; CHECK: %[[sum0:.*]] = add i8 %[[trunc0]], %[[trunc1]]
626 ; CHECK-NEXT: %[[sum1:.*]] = add i8 %[[sum0]], %[[trunc2]]
627 ; CHECK-NEXT: ret i8 %[[sum1]]
630 define i32 @test13() {
631 ; Ensure we don't crash and handle undefined loads that straddle the end of the
633 ; CHECK-LABEL: @test13(
634 ; CHECK: %[[value:.*]] = zext i8 0 to i16
635 ; CHECK-NEXT: %[[ret:.*]] = zext i16 %[[value]] to i32
636 ; CHECK-NEXT: ret i32 %[[ret]]
639 %a = alloca [3 x i8], align 2
640 %b0ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 0
641 store i8 0, i8* %b0ptr
642 %b1ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 1
643 store i8 0, i8* %b1ptr
644 %b2ptr = getelementptr [3 x i8], [3 x i8]* %a, i64 0, i32 2
645 store i8 0, i8* %b2ptr
646 %iptrcast = bitcast [3 x i8]* %a to i16*
647 %iptrgep = getelementptr i16, i16* %iptrcast, i64 1
648 %i = load i16, i16* %iptrgep
649 %ret = zext i16 %i to i32
653 %test14.struct = type { [3 x i32] }
655 define void @test14(...) nounwind uwtable {
656 ; This is a strange case where we split allocas into promotable partitions, but
657 ; also gain enough data to prove they must be dead allocas due to GEPs that walk
658 ; across two adjacent allocas. Test that we don't try to promote or otherwise
659 ; do bad things to these dead allocas, they should just be removed.
660 ; CHECK-LABEL: @test14(
662 ; CHECK-NEXT: ret void
665 %a = alloca %test14.struct
666 %p = alloca %test14.struct*
667 %0 = bitcast %test14.struct* %a to i8*
668 %1 = getelementptr i8, i8* %0, i64 12
669 %2 = bitcast i8* %1 to %test14.struct*
670 %3 = getelementptr inbounds %test14.struct, %test14.struct* %2, i32 0, i32 0
671 %4 = getelementptr inbounds %test14.struct, %test14.struct* %a, i32 0, i32 0
672 %5 = bitcast [3 x i32]* %3 to i32*
673 %6 = bitcast [3 x i32]* %4 to i32*
674 %7 = load i32, i32* %6, align 4
675 store i32 %7, i32* %5, align 4
676 %8 = getelementptr inbounds i32, i32* %5, i32 1
677 %9 = getelementptr inbounds i32, i32* %6, i32 1
678 %10 = load i32, i32* %9, align 4
679 store i32 %10, i32* %8, align 4
680 %11 = getelementptr inbounds i32, i32* %5, i32 2
681 %12 = getelementptr inbounds i32, i32* %6, i32 2
682 %13 = load i32, i32* %12, align 4
683 store i32 %13, i32* %11, align 4
687 define i32 @test15(i1 %flag) nounwind uwtable {
688 ; Ensure that when there are dead instructions using an alloca that are not
689 ; loads or stores we still delete them during partitioning and rewriting.
690 ; Otherwise we'll go to promote them while thy still have unpromotable uses.
691 ; CHECK-LABEL: @test15(
693 ; CHECK-NEXT: br label %loop
695 ; CHECK-NEXT: br label %loop
705 %dead3 = phi i8* [ %gep3, %loop ], [ null, %entry ]
707 store i64 1879048192, i64* %l0, align 8
708 %bc0 = bitcast i64* %l0 to i8*
709 %gep0 = getelementptr i8, i8* %bc0, i64 3
710 %dead0 = bitcast i8* %gep0 to i64*
712 store i64 1879048192, i64* %l1, align 8
713 %bc1 = bitcast i64* %l1 to i8*
714 %gep1 = getelementptr i8, i8* %bc1, i64 3
715 %dead1 = getelementptr i8, i8* %gep1, i64 1
717 store i64 1879048192, i64* %l2, align 8
718 %bc2 = bitcast i64* %l2 to i8*
719 %gep2.1 = getelementptr i8, i8* %bc2, i64 1
720 %gep2.2 = getelementptr i8, i8* %bc2, i64 3
721 ; Note that this select should get visited multiple times due to using two
722 ; different GEPs off the same alloca. We should only delete it once.
723 %dead2 = select i1 %flag, i8* %gep2.1, i8* %gep2.2
725 store i64 1879048192, i64* %l3, align 8
726 %bc3 = bitcast i64* %l3 to i8*
727 %gep3 = getelementptr i8, i8* %bc3, i64 3
732 define void @test16(i8* %src, i8* %dst) {
733 ; Ensure that we can promote an alloca of [3 x i8] to an i24 SSA value.
734 ; CHECK-LABEL: @test16(
736 ; CHECK: %[[srccast:.*]] = bitcast i8* %src to i24*
737 ; CHECK-NEXT: load i24, i24* %[[srccast]]
738 ; CHECK-NEXT: %[[dstcast:.*]] = bitcast i8* %dst to i24*
739 ; CHECK-NEXT: store i24 0, i24* %[[dstcast]]
740 ; CHECK-NEXT: ret void
744 %ptr = getelementptr [3 x i8], [3 x i8]* %a, i32 0, i32 0
745 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 false)
746 %cast = bitcast i8* %ptr to i24*
747 store i24 0, i24* %cast
748 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 false)
752 define void @test17(i8* %src, i8* %dst) {
753 ; Ensure that we can rewrite unpromotable memcpys which extend past the end of
755 ; CHECK-LABEL: @test17(
756 ; CHECK: %[[a:.*]] = alloca [3 x i8]
757 ; CHECK-NEXT: %[[ptr:.*]] = getelementptr [3 x i8], [3 x i8]* %[[a]], i32 0, i32 0
758 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[ptr]], i8* %src,
759 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[ptr]],
760 ; CHECK-NEXT: ret void
764 %ptr = getelementptr [3 x i8], [3 x i8]* %a, i32 0, i32 0
765 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 4, i32 1, i1 true)
766 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 4, i32 1, i1 true)
770 define void @test18(i8* %src, i8* %dst, i32 %size) {
771 ; Preserve transfer instrinsics with a variable size, even if they overlap with
772 ; fixed size operations. Further, continue to split and promote allocas preceding
773 ; the variable sized intrinsic.
774 ; CHECK-LABEL: @test18(
775 ; CHECK: %[[a:.*]] = alloca [34 x i8]
776 ; CHECK: %[[srcgep1:.*]] = getelementptr inbounds i8, i8* %src, i64 4
777 ; CHECK-NEXT: %[[srccast1:.*]] = bitcast i8* %[[srcgep1]] to i32*
778 ; CHECK-NEXT: %[[srcload:.*]] = load i32, i32* %[[srccast1]]
779 ; CHECK-NEXT: %[[agep1:.*]] = getelementptr inbounds [34 x i8], [34 x i8]* %[[a]], i64 0, i64 0
780 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[agep1]], i8* %src, i32 %size,
781 ; CHECK-NEXT: %[[agep2:.*]] = getelementptr inbounds [34 x i8], [34 x i8]* %[[a]], i64 0, i64 0
782 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* %[[agep2]], i8 42, i32 %size,
783 ; CHECK-NEXT: %[[dstcast1:.*]] = bitcast i8* %dst to i32*
784 ; CHECK-NEXT: store i32 42, i32* %[[dstcast1]]
785 ; CHECK-NEXT: %[[dstgep1:.*]] = getelementptr inbounds i8, i8* %dst, i64 4
786 ; CHECK-NEXT: %[[dstcast2:.*]] = bitcast i8* %[[dstgep1]] to i32*
787 ; CHECK-NEXT: store i32 %[[srcload]], i32* %[[dstcast2]]
788 ; CHECK-NEXT: %[[agep3:.*]] = getelementptr inbounds [34 x i8], [34 x i8]* %[[a]], i64 0, i64 0
789 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %[[agep3]], i32 %size,
790 ; CHECK-NEXT: ret void
793 %a = alloca [42 x i8]
794 %ptr = getelementptr [42 x i8], [42 x i8]* %a, i32 0, i32 0
795 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 8, i32 1, i1 false)
796 %ptr2 = getelementptr [42 x i8], [42 x i8]* %a, i32 0, i32 8
797 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr2, i8* %src, i32 %size, i32 1, i1 false)
798 call void @llvm.memset.p0i8.i32(i8* %ptr2, i8 42, i32 %size, i32 1, i1 false)
799 %cast = bitcast i8* %ptr to i32*
800 store i32 42, i32* %cast
801 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 8, i32 1, i1 false)
802 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr2, i32 %size, i32 1, i1 false)
806 %opaque = type opaque
808 define i32 @test19(%opaque* %x) {
809 ; This input will cause us to try to compute a natural GEP when rewriting
810 ; pointers in such a way that we try to GEP through the opaque type. Previously,
811 ; a check for an unsized type was missing and this crashed. Ensure it behaves
813 ; CHECK-LABEL: @test19(
815 ; CHECK: ret i32 undef
818 %a = alloca { i64, i8* }
819 %cast1 = bitcast %opaque* %x to i8*
820 %cast2 = bitcast { i64, i8* }* %a to i8*
821 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %cast2, i8* %cast1, i32 16, i32 1, i1 false)
822 %gep = getelementptr inbounds { i64, i8* }, { i64, i8* }* %a, i32 0, i32 0
823 %val = load i64, i64* %gep
827 define i32 @test20() {
828 ; Ensure we can track negative offsets (before the beginning of the alloca) and
829 ; negative relative offsets from offsets starting past the end of the alloca.
830 ; CHECK-LABEL: @test20(
832 ; CHECK: %[[sum1:.*]] = add i32 1, 2
833 ; CHECK: %[[sum2:.*]] = add i32 %[[sum1]], 3
834 ; CHECK: ret i32 %[[sum2]]
837 %a = alloca [3 x i32]
838 %gep1 = getelementptr [3 x i32], [3 x i32]* %a, i32 0, i32 0
839 store i32 1, i32* %gep1
840 %gep2.1 = getelementptr [3 x i32], [3 x i32]* %a, i32 0, i32 -2
841 %gep2.2 = getelementptr i32, i32* %gep2.1, i32 3
842 store i32 2, i32* %gep2.2
843 %gep3.1 = getelementptr [3 x i32], [3 x i32]* %a, i32 0, i32 14
844 %gep3.2 = getelementptr i32, i32* %gep3.1, i32 -12
845 store i32 3, i32* %gep3.2
847 %load1 = load i32, i32* %gep1
848 %load2 = load i32, i32* %gep2.2
849 %load3 = load i32, i32* %gep3.2
850 %sum1 = add i32 %load1, %load2
851 %sum2 = add i32 %sum1, %load3
855 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
857 define i8 @test21() {
858 ; Test allocations and offsets which border on overflow of the int64_t used
859 ; internally. This is really awkward to really test as LLVM doesn't really
860 ; support such extreme constructs cleanly.
861 ; CHECK-LABEL: @test21(
863 ; CHECK: or i8 -1, -1
866 %a = alloca [2305843009213693951 x i8]
867 %gep0 = getelementptr [2305843009213693951 x i8], [2305843009213693951 x i8]* %a, i64 0, i64 2305843009213693949
868 store i8 255, i8* %gep0
869 %gep1 = getelementptr [2305843009213693951 x i8], [2305843009213693951 x i8]* %a, i64 0, i64 -9223372036854775807
870 %gep2 = getelementptr i8, i8* %gep1, i64 -1
871 call void @llvm.memset.p0i8.i64(i8* %gep2, i8 0, i64 18446744073709551615, i32 1, i1 false)
872 %gep3 = getelementptr i8, i8* %gep1, i64 9223372036854775807
873 %gep4 = getelementptr i8, i8* %gep3, i64 9223372036854775807
874 %gep5 = getelementptr i8, i8* %gep4, i64 -6917529027641081857
875 store i8 255, i8* %gep5
876 %cast1 = bitcast i8* %gep4 to i32*
877 store i32 0, i32* %cast1
878 %load = load i8, i8* %gep0
879 %gep6 = getelementptr i8, i8* %gep0, i32 1
880 %load2 = load i8, i8* %gep6
881 %result = or i8 %load, %load2
885 %PR13916.struct = type { i8 }
887 define void @PR13916.1() {
888 ; Ensure that we handle overlapping memcpy intrinsics correctly, especially in
889 ; the case where there is a directly identical value for both source and dest.
896 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %a, i32 1, i32 1, i1 false)
897 %tmp2 = load i8, i8* %a
901 define void @PR13916.2() {
902 ; Check whether we continue to handle them correctly when they start off with
903 ; different pointer value chains, but during rewriting we coalesce them into the
910 %a = alloca %PR13916.struct, align 1
911 br i1 undef, label %if.then, label %if.end
914 %tmp0 = bitcast %PR13916.struct* %a to i8*
915 %tmp1 = bitcast %PR13916.struct* %a to i8*
916 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %tmp0, i8* %tmp1, i32 1, i32 1, i1 false)
920 %gep = getelementptr %PR13916.struct, %PR13916.struct* %a, i32 0, i32 0
921 %tmp2 = load i8, i8* %gep
925 define void @PR13990() {
926 ; Ensure we can handle cases where processing one alloca causes the other
927 ; alloca to become dead and get deleted. This might crash or fail under
928 ; Valgrind if we regress.
929 ; CHECK-LABEL: @PR13990(
937 br i1 undef, label %bb1, label %bb2
940 store i8* undef, i8** %tmp2
941 br i1 undef, label %bb2, label %bb3
944 %tmp50 = select i1 undef, i8** %tmp2, i8** %tmp1
945 br i1 undef, label %bb3, label %bb4
954 define double @PR13969(double %x) {
955 ; Check that we detect when promotion will un-escape an alloca and iterate to
956 ; re-try running SROA over that alloca. Without that, the two allocas that are
957 ; stored into a dead alloca don't get rewritten and promoted.
958 ; CHECK-LABEL: @PR13969(
966 store double %x, double* %a
967 store double* %c, double** %b
968 store double* %a, double** %b
969 store double %x, double* %c
970 %ret = load double, double* %a
975 ; CHECK: ret double %x
978 %PR14034.struct = type { { {} }, i32, %PR14034.list }
979 %PR14034.list = type { %PR14034.list*, %PR14034.list* }
981 define void @PR14034() {
982 ; This test case tries to form GEPs into the empty leading struct members, and
983 ; subsequently crashed (under valgrind) before we fixed the PR. The important
984 ; thing is to handle empty structs gracefully.
985 ; CHECK-LABEL: @PR14034(
988 %a = alloca %PR14034.struct
989 %list = getelementptr %PR14034.struct, %PR14034.struct* %a, i32 0, i32 2
990 %prev = getelementptr %PR14034.list, %PR14034.list* %list, i32 0, i32 1
991 store %PR14034.list* undef, %PR14034.list** %prev
992 %cast0 = bitcast %PR14034.struct* undef to i8*
993 %cast1 = bitcast %PR14034.struct* %a to i8*
994 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %cast0, i8* %cast1, i32 12, i32 0, i1 false)
998 define i32 @test22(i32 %x) {
999 ; Test that SROA and promotion is not confused by a grab bax mixture of pointer
1000 ; types involving wrapper aggregates and zero-length aggregate members.
1001 ; CHECK-LABEL: @test22(
1004 %a1 = alloca { { [1 x { i32 }] } }
1005 %a2 = alloca { {}, { float }, [0 x i8] }
1006 %a3 = alloca { [0 x i8], { [0 x double], [1 x [1 x <4 x i8>]], {} }, { { {} } } }
1009 %wrap1 = insertvalue [1 x { i32 }] undef, i32 %x, 0, 0
1010 %gep1 = getelementptr { { [1 x { i32 }] } }, { { [1 x { i32 }] } }* %a1, i32 0, i32 0, i32 0
1011 store [1 x { i32 }] %wrap1, [1 x { i32 }]* %gep1
1013 %gep2 = getelementptr { { [1 x { i32 }] } }, { { [1 x { i32 }] } }* %a1, i32 0, i32 0
1014 %ptrcast1 = bitcast { [1 x { i32 }] }* %gep2 to { [1 x { float }] }*
1015 %load1 = load { [1 x { float }] }, { [1 x { float }] }* %ptrcast1
1016 %unwrap1 = extractvalue { [1 x { float }] } %load1, 0, 0
1018 %wrap2 = insertvalue { {}, { float }, [0 x i8] } undef, { float } %unwrap1, 1
1019 store { {}, { float }, [0 x i8] } %wrap2, { {}, { float }, [0 x i8] }* %a2
1021 %gep3 = getelementptr { {}, { float }, [0 x i8] }, { {}, { float }, [0 x i8] }* %a2, i32 0, i32 1, i32 0
1022 %ptrcast2 = bitcast float* %gep3 to <4 x i8>*
1023 %load3 = load <4 x i8>, <4 x i8>* %ptrcast2
1024 %valcast1 = bitcast <4 x i8> %load3 to i32
1026 %wrap3 = insertvalue [1 x [1 x i32]] undef, i32 %valcast1, 0, 0
1027 %wrap4 = insertvalue { [1 x [1 x i32]], {} } undef, [1 x [1 x i32]] %wrap3, 0
1028 %gep4 = getelementptr { [0 x i8], { [0 x double], [1 x [1 x <4 x i8>]], {} }, { { {} } } }, { [0 x i8], { [0 x double], [1 x [1 x <4 x i8>]], {} }, { { {} } } }* %a3, i32 0, i32 1
1029 %ptrcast3 = bitcast { [0 x double], [1 x [1 x <4 x i8>]], {} }* %gep4 to { [1 x [1 x i32]], {} }*
1030 store { [1 x [1 x i32]], {} } %wrap4, { [1 x [1 x i32]], {} }* %ptrcast3
1032 %gep5 = getelementptr { [0 x i8], { [0 x double], [1 x [1 x <4 x i8>]], {} }, { { {} } } }, { [0 x i8], { [0 x double], [1 x [1 x <4 x i8>]], {} }, { { {} } } }* %a3, i32 0, i32 1, i32 1, i32 0
1033 %ptrcast4 = bitcast [1 x <4 x i8>]* %gep5 to { {}, float, {} }*
1034 %load4 = load { {}, float, {} }, { {}, float, {} }* %ptrcast4
1035 %unwrap2 = extractvalue { {}, float, {} } %load4, 1
1036 %valcast2 = bitcast float %unwrap2 to i32
1042 define void @PR14059.1(double* %d) {
1043 ; In PR14059 a peculiar construct was identified as something that is used
1044 ; pervasively in ARM's ABI-calling-convention lowering: the passing of a struct
1045 ; of doubles via an array of i32 in order to place the data into integer
1046 ; registers. This in turn was missed as an optimization by SROA due to the
1047 ; partial loads and stores of integers to the double alloca we were trying to
1048 ; form and promote. The solution is to widen the integer operations to be
1049 ; whole-alloca operations, and perform the appropriate bitcasting on the
1050 ; *values* rather than the pointers. When this works, partial reads and writes
1051 ; via integers can be promoted away.
1057 %X.sroa.0.i = alloca double, align 8
1058 %0 = bitcast double* %X.sroa.0.i to i8*
1059 call void @llvm.lifetime.start(i64 -1, i8* %0)
1061 ; Store to the low 32-bits...
1062 %X.sroa.0.0.cast2.i = bitcast double* %X.sroa.0.i to i32*
1063 store i32 0, i32* %X.sroa.0.0.cast2.i, align 8
1065 ; Also use a memset to the middle 32-bits for fun.
1066 %X.sroa.0.2.raw_idx2.i = getelementptr inbounds i8, i8* %0, i32 2
1067 call void @llvm.memset.p0i8.i64(i8* %X.sroa.0.2.raw_idx2.i, i8 0, i64 4, i32 1, i1 false)
1069 ; Or a memset of the whole thing.
1070 call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 8, i32 1, i1 false)
1072 ; Write to the high 32-bits with a memcpy.
1073 %X.sroa.0.4.raw_idx4.i = getelementptr inbounds i8, i8* %0, i32 4
1074 %d.raw = bitcast double* %d to i8*
1075 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %X.sroa.0.4.raw_idx4.i, i8* %d.raw, i32 4, i32 1, i1 false)
1077 ; Store to the high 32-bits...
1078 %X.sroa.0.4.cast5.i = bitcast i8* %X.sroa.0.4.raw_idx4.i to i32*
1079 store i32 1072693248, i32* %X.sroa.0.4.cast5.i, align 4
1081 ; Do the actual math...
1082 %X.sroa.0.0.load1.i = load double, double* %X.sroa.0.i, align 8
1083 %accum.real.i = load double, double* %d, align 8
1084 %add.r.i = fadd double %accum.real.i, %X.sroa.0.0.load1.i
1085 store double %add.r.i, double* %d, align 8
1086 call void @llvm.lifetime.end(i64 -1, i8* %0)
1090 define i64 @PR14059.2({ float, float }* %phi) {
1091 ; Check that SROA can split up alloca-wide integer loads and stores where the
1092 ; underlying alloca has smaller components that are accessed independently. This
1093 ; shows up particularly with ABI lowering patterns coming out of Clang that rely
1094 ; on the particular register placement of a single large integer return value.
1098 %retval = alloca { float, float }, align 4
1101 %0 = bitcast { float, float }* %retval to i64*
1102 store i64 0, i64* %0
1105 %phi.realp = getelementptr inbounds { float, float }, { float, float }* %phi, i32 0, i32 0
1106 %phi.real = load float, float* %phi.realp
1107 %phi.imagp = getelementptr inbounds { float, float }, { float, float }* %phi, i32 0, i32 1
1108 %phi.imag = load float, float* %phi.imagp
1109 ; CHECK: %[[realp:.*]] = getelementptr inbounds { float, float }, { float, float }* %phi, i32 0, i32 0
1110 ; CHECK-NEXT: %[[real:.*]] = load float, float* %[[realp]]
1111 ; CHECK-NEXT: %[[imagp:.*]] = getelementptr inbounds { float, float }, { float, float }* %phi, i32 0, i32 1
1112 ; CHECK-NEXT: %[[imag:.*]] = load float, float* %[[imagp]]
1114 %real = getelementptr inbounds { float, float }, { float, float }* %retval, i32 0, i32 0
1115 %imag = getelementptr inbounds { float, float }, { float, float }* %retval, i32 0, i32 1
1116 store float %phi.real, float* %real
1117 store float %phi.imag, float* %imag
1118 ; CHECK-NEXT: %[[real_convert:.*]] = bitcast float %[[real]] to i32
1119 ; CHECK-NEXT: %[[imag_convert:.*]] = bitcast float %[[imag]] to i32
1120 ; CHECK-NEXT: %[[imag_ext:.*]] = zext i32 %[[imag_convert]] to i64
1121 ; CHECK-NEXT: %[[imag_shift:.*]] = shl i64 %[[imag_ext]], 32
1122 ; CHECK-NEXT: %[[imag_mask:.*]] = and i64 undef, 4294967295
1123 ; CHECK-NEXT: %[[imag_insert:.*]] = or i64 %[[imag_mask]], %[[imag_shift]]
1124 ; CHECK-NEXT: %[[real_ext:.*]] = zext i32 %[[real_convert]] to i64
1125 ; CHECK-NEXT: %[[real_mask:.*]] = and i64 %[[imag_insert]], -4294967296
1126 ; CHECK-NEXT: %[[real_insert:.*]] = or i64 %[[real_mask]], %[[real_ext]]
1128 %1 = load i64, i64* %0, align 1
1130 ; CHECK-NEXT: ret i64 %[[real_insert]]
1133 define void @PR14105({ [16 x i8] }* %ptr) {
1134 ; Ensure that when rewriting the GEP index '-1' for this alloca we preserve is
1135 ; sign as negative. We use a volatile memcpy to ensure promotion never actually
1137 ; CHECK-LABEL: @PR14105(
1140 %a = alloca { [16 x i8] }, align 8
1141 ; CHECK: alloca [16 x i8], align 8
1143 %gep = getelementptr inbounds { [16 x i8] }, { [16 x i8] }* %ptr, i64 -1
1144 ; CHECK-NEXT: getelementptr inbounds { [16 x i8] }, { [16 x i8] }* %ptr, i64 -1, i32 0, i64 0
1146 %cast1 = bitcast { [16 x i8 ] }* %gep to i8*
1147 %cast2 = bitcast { [16 x i8 ] }* %a to i8*
1148 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %cast1, i8* %cast2, i32 16, i32 8, i1 true)
1153 define void @PR14105_as1({ [16 x i8] } addrspace(1)* %ptr) {
1154 ; Make sure this the right address space pointer is used for type check.
1155 ; CHECK-LABEL: @PR14105_as1(
1158 %a = alloca { [16 x i8] }, align 8
1159 ; CHECK: alloca [16 x i8], align 8
1161 %gep = getelementptr inbounds { [16 x i8] }, { [16 x i8] } addrspace(1)* %ptr, i64 -1
1162 ; CHECK-NEXT: getelementptr inbounds { [16 x i8] }, { [16 x i8] } addrspace(1)* %ptr, i16 -1, i32 0, i16 0
1164 %cast1 = bitcast { [16 x i8 ] } addrspace(1)* %gep to i8 addrspace(1)*
1165 %cast2 = bitcast { [16 x i8 ] }* %a to i8*
1166 call void @llvm.memcpy.p1i8.p0i8.i32(i8 addrspace(1)* %cast1, i8* %cast2, i32 16, i32 8, i1 true)
1171 define void @PR14465() {
1172 ; Ensure that we don't crash when analyzing a alloca larger than the maximum
1173 ; integer type width (MAX_INT_BITS) supported by llvm (1048576*32 > (1<<23)-1).
1174 ; CHECK-LABEL: @PR14465(
1176 %stack = alloca [1048576 x i32], align 16
1177 ; CHECK: alloca [1048576 x i32]
1178 %cast = bitcast [1048576 x i32]* %stack to i8*
1179 call void @llvm.memset.p0i8.i64(i8* %cast, i8 -2, i64 4194304, i32 16, i1 false)
1184 define void @PR14548(i1 %x) {
1185 ; Handle a mixture of i1 and i8 loads and stores to allocas. This particular
1186 ; pattern caused crashes and invalid output in the PR, and its nature will
1187 ; trigger a mixture in several permutations as we resolve each alloca
1189 ; Note that we don't do a particularly good *job* of handling these mixtures,
1190 ; but the hope is that this is very rare.
1191 ; CHECK-LABEL: @PR14548(
1194 %a = alloca <{ i1 }>, align 8
1195 %b = alloca <{ i1 }>, align 8
1196 ; CHECK: %[[a:.*]] = alloca i8, align 8
1197 ; CHECK-NEXT: %[[b:.*]] = alloca i8, align 8
1199 %b.i1 = bitcast <{ i1 }>* %b to i1*
1200 store i1 %x, i1* %b.i1, align 8
1201 %b.i8 = bitcast <{ i1 }>* %b to i8*
1202 %foo = load i8, i8* %b.i8, align 1
1203 ; CHECK-NEXT: %[[b_cast:.*]] = bitcast i8* %[[b]] to i1*
1204 ; CHECK-NEXT: store i1 %x, i1* %[[b_cast]], align 8
1205 ; CHECK-NEXT: {{.*}} = load i8, i8* %[[b]], align 8
1207 %a.i8 = bitcast <{ i1 }>* %a to i8*
1208 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a.i8, i8* %b.i8, i32 1, i32 1, i1 false) nounwind
1209 %bar = load i8, i8* %a.i8, align 1
1210 %a.i1 = getelementptr inbounds <{ i1 }>, <{ i1 }>* %a, i32 0, i32 0
1211 %baz = load i1, i1* %a.i1, align 1
1212 ; CHECK-NEXT: %[[copy:.*]] = load i8, i8* %[[b]], align 8
1213 ; CHECK-NEXT: store i8 %[[copy]], i8* %[[a]], align 8
1214 ; CHECK-NEXT: {{.*}} = load i8, i8* %[[a]], align 8
1215 ; CHECK-NEXT: %[[a_cast:.*]] = bitcast i8* %[[a]] to i1*
1216 ; CHECK-NEXT: {{.*}} = load i1, i1* %[[a_cast]], align 8
1221 define <3 x i8> @PR14572.1(i32 %x) {
1222 ; Ensure that a split integer store which is wider than the type size of the
1223 ; alloca (relying on the alloc size padding) doesn't trigger an assert.
1227 %a = alloca <3 x i8>, align 4
1230 %cast = bitcast <3 x i8>* %a to i32*
1231 store i32 %x, i32* %cast, align 1
1232 %y = load <3 x i8>, <3 x i8>* %a, align 4
1234 ; CHECK: ret <3 x i8>
1237 define i32 @PR14572.2(<3 x i8> %x) {
1238 ; Ensure that a split integer load which is wider than the type size of the
1239 ; alloca (relying on the alloc size padding) doesn't trigger an assert.
1243 %a = alloca <3 x i8>, align 4
1246 store <3 x i8> %x, <3 x i8>* %a, align 1
1247 %cast = bitcast <3 x i8>* %a to i32*
1248 %y = load i32, i32* %cast, align 4
1253 define i32 @PR14601(i32 %x) {
1254 ; Don't try to form a promotable integer alloca when there is a variable length
1256 ; CHECK-LABEL: @PR14601(
1262 %a.i8 = bitcast i32* %a to i8*
1263 call void @llvm.memset.p0i8.i32(i8* %a.i8, i8 0, i32 %x, i32 1, i1 false)
1264 %v = load i32, i32* %a
1268 define void @PR15674(i8* %data, i8* %src, i32 %size) {
1269 ; Arrange (via control flow) to have unmerged stores of a particular width to
1270 ; an alloca where we incrementally store from the end of the array toward the
1271 ; beginning of the array. Ensure that the final integer store, despite being
1272 ; convertable to the integer type that we end up promoting this alloca toward,
1273 ; doesn't get widened to a full alloca store.
1274 ; CHECK-LABEL: @PR15674(
1277 %tmp = alloca [4 x i8], align 1
1280 switch i32 %size, label %end [
1288 %src.gep3 = getelementptr inbounds i8, i8* %src, i32 3
1289 %src.3 = load i8, i8* %src.gep3
1290 %tmp.gep3 = getelementptr inbounds [4 x i8], [4 x i8]* %tmp, i32 0, i32 3
1291 store i8 %src.3, i8* %tmp.gep3
1297 %src.gep2 = getelementptr inbounds i8, i8* %src, i32 2
1298 %src.2 = load i8, i8* %src.gep2
1299 %tmp.gep2 = getelementptr inbounds [4 x i8], [4 x i8]* %tmp, i32 0, i32 2
1300 store i8 %src.2, i8* %tmp.gep2
1306 %src.gep1 = getelementptr inbounds i8, i8* %src, i32 1
1307 %src.1 = load i8, i8* %src.gep1
1308 %tmp.gep1 = getelementptr inbounds [4 x i8], [4 x i8]* %tmp, i32 0, i32 1
1309 store i8 %src.1, i8* %tmp.gep1
1315 %src.gep0 = getelementptr inbounds i8, i8* %src, i32 0
1316 %src.0 = load i8, i8* %src.gep0
1317 %tmp.gep0 = getelementptr inbounds [4 x i8], [4 x i8]* %tmp, i32 0, i32 0
1318 store i8 %src.0, i8* %tmp.gep0
1324 %tmp.raw = bitcast [4 x i8]* %tmp to i8*
1325 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %data, i8* %tmp.raw, i32 %size, i32 1, i1 false)
1330 define void @PR15805(i1 %a, i1 %b) {
1331 ; CHECK-LABEL: @PR15805(
1335 %c = alloca i64, align 8
1336 %p.0.c = select i1 undef, i64* %c, i64* %c
1337 %cond.in = select i1 undef, i64* %p.0.c, i64* %c
1338 %cond = load i64, i64* %cond.in, align 8
1342 define void @PR15805.1(i1 %a, i1 %b) {
1343 ; Same as the normal PR15805, but rigged to place the use before the def inside
1344 ; of looping unreachable code. This helps ensure that we aren't sensitive to the
1345 ; order in which the uses of the alloca are visited.
1347 ; CHECK-LABEL: @PR15805.1(
1351 %c = alloca i64, align 8
1355 %cond.in = select i1 undef, i64* %c, i64* %p.0.c
1356 %p.0.c = select i1 undef, i64* %c, i64* %c
1357 %cond = load i64, i64* %cond.in, align 8
1358 br i1 undef, label %loop, label %exit
1364 define void @PR16651.1(i8* %a) {
1365 ; This test case caused a crash due to the volatile memcpy in combination with
1366 ; lowering to integer loads and stores of a width other than that of the original
1369 ; CHECK-LABEL: @PR16651.1(
1373 ; CHECK: unreachable
1376 %b = alloca i32, align 4
1377 %b.cast = bitcast i32* %b to i8*
1378 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %b.cast, i8* %a, i32 4, i32 4, i1 true)
1379 %b.gep = getelementptr inbounds i8, i8* %b.cast, i32 2
1380 load i8, i8* %b.gep, align 2
1384 define void @PR16651.2() {
1385 ; This test case caused a crash due to failing to promote given a select that
1386 ; can't be speculated. It shouldn't be promoted, but we missed that fact when
1387 ; analyzing whether we could form a vector promotion because that code didn't
1388 ; bail on select instructions.
1390 ; CHECK-LABEL: @PR16651.2(
1391 ; CHECK: alloca <2 x float>
1395 %tv1 = alloca { <2 x float>, <2 x float> }, align 8
1396 %0 = getelementptr { <2 x float>, <2 x float> }, { <2 x float>, <2 x float> }* %tv1, i64 0, i32 1
1397 store <2 x float> undef, <2 x float>* %0, align 8
1398 %1 = getelementptr inbounds { <2 x float>, <2 x float> }, { <2 x float>, <2 x float> }* %tv1, i64 0, i32 1, i64 0
1399 %cond105.in.i.i = select i1 undef, float* null, float* %1
1400 %cond105.i.i = load float, float* %cond105.in.i.i, align 8
1404 define void @test23(i32 %x) {
1405 ; CHECK-LABEL: @test23(
1409 %a = alloca i32, align 4
1410 store i32 %x, i32* %a, align 4
1411 %gep1 = getelementptr inbounds i32, i32* %a, i32 1
1412 %gep0 = getelementptr inbounds i32, i32* %a, i32 0
1413 %cast1 = bitcast i32* %gep1 to i8*
1414 %cast0 = bitcast i32* %gep0 to i8*
1415 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %cast1, i8* %cast0, i32 4, i32 1, i1 false)
1419 define void @PR18615() {
1420 ; CHECK-LABEL: @PR18615(
1425 %gep = getelementptr i8, i8* %f, i64 -1
1426 call void @llvm.memcpy.p0i8.p0i8.i32(i8* undef, i8* %gep, i32 1, i32 1, i1 false)
1430 define void @test24(i8* %src, i8* %dst) {
1431 ; CHECK-LABEL: @test24(
1432 ; CHECK: alloca i64, align 16
1433 ; CHECK: load volatile i64, i64* %{{[^,]*}}, align 1
1434 ; CHECK: store volatile i64 %{{[^,]*}}, i64* %{{[^,]*}}, align 16
1435 ; CHECK: load volatile i64, i64* %{{[^,]*}}, align 16
1436 ; CHECK: store volatile i64 %{{[^,]*}}, i64* %{{[^,]*}}, align 1
1439 %a = alloca i64, align 16
1440 %ptr = bitcast i64* %a to i8*
1441 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %src, i32 8, i32 1, i1 true)
1442 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %ptr, i32 8, i32 1, i1 true)
1446 define float @test25() {
1447 ; Check that we split up stores in order to promote the smaller SSA values.. These types
1448 ; of patterns can arise because LLVM maps small memcpy's to integer load and
1449 ; stores. If we get a memcpy of an aggregate (such as C and C++ frontends would
1450 ; produce, but so might any language frontend), this will in many cases turn into
1451 ; an integer load and store. SROA needs to be extremely powerful to correctly
1452 ; handle these cases and form splitable and promotable SSA values.
1454 ; CHECK-LABEL: @test25(
1456 ; CHECK: %[[F1:.*]] = bitcast i32 0 to float
1457 ; CHECK: %[[F2:.*]] = bitcast i32 1065353216 to float
1458 ; CHECK: %[[SUM:.*]] = fadd float %[[F1]], %[[F2]]
1459 ; CHECK: ret float %[[SUM]]
1464 %a.cast = bitcast i64* %a to [2 x float]*
1465 %a.gep1 = getelementptr [2 x float], [2 x float]* %a.cast, i32 0, i32 0
1466 %a.gep2 = getelementptr [2 x float], [2 x float]* %a.cast, i32 0, i32 1
1467 %b.cast = bitcast i64* %b to [2 x float]*
1468 %b.gep1 = getelementptr [2 x float], [2 x float]* %b.cast, i32 0, i32 0
1469 %b.gep2 = getelementptr [2 x float], [2 x float]* %b.cast, i32 0, i32 1
1470 store float 0.0, float* %a.gep1
1471 store float 1.0, float* %a.gep2
1472 %v = load i64, i64* %a
1473 store i64 %v, i64* %b
1474 %f1 = load float, float* %b.gep1
1475 %f2 = load float, float* %b.gep2
1476 %ret = fadd float %f1, %f2
1480 @complex1 = external global [2 x float]
1481 @complex2 = external global [2 x float]
1483 define void @test26() {
1484 ; Test a case of splitting up loads and stores against a globals.
1486 ; CHECK-LABEL: @test26(
1488 ; CHECK: %[[L1:.*]] = load i32, i32* bitcast
1489 ; CHECK: %[[L2:.*]] = load i32, i32* bitcast
1490 ; CHECK: %[[F1:.*]] = bitcast i32 %[[L1]] to float
1491 ; CHECK: %[[F2:.*]] = bitcast i32 %[[L2]] to float
1492 ; CHECK: %[[SUM:.*]] = fadd float %[[F1]], %[[F2]]
1493 ; CHECK: %[[C1:.*]] = bitcast float %[[SUM]] to i32
1494 ; CHECK: %[[C2:.*]] = bitcast float %[[SUM]] to i32
1495 ; CHECK: store i32 %[[C1]], i32* bitcast
1496 ; CHECK: store i32 %[[C2]], i32* bitcast
1501 %a.cast = bitcast i64* %a to [2 x float]*
1502 %a.gep1 = getelementptr [2 x float], [2 x float]* %a.cast, i32 0, i32 0
1503 %a.gep2 = getelementptr [2 x float], [2 x float]* %a.cast, i32 0, i32 1
1504 %v1 = load i64, i64* bitcast ([2 x float]* @complex1 to i64*)
1505 store i64 %v1, i64* %a
1506 %f1 = load float, float* %a.gep1
1507 %f2 = load float, float* %a.gep2
1508 %sum = fadd float %f1, %f2
1509 store float %sum, float* %a.gep1
1510 store float %sum, float* %a.gep2
1511 %v2 = load i64, i64* %a
1512 store i64 %v2, i64* bitcast ([2 x float]* @complex2 to i64*)
1516 define float @test27() {
1517 ; Another, more complex case of splittable i64 loads and stores. This example
1518 ; is a particularly challenging one because the load and store both point into
1519 ; the alloca SROA is processing, and they overlap but at an offset.
1521 ; CHECK-LABEL: @test27(
1523 ; CHECK: %[[F1:.*]] = bitcast i32 0 to float
1524 ; CHECK: %[[F2:.*]] = bitcast i32 1065353216 to float
1525 ; CHECK: %[[SUM:.*]] = fadd float %[[F1]], %[[F2]]
1526 ; CHECK: ret float %[[SUM]]
1529 %a = alloca [12 x i8]
1530 %gep1 = getelementptr [12 x i8], [12 x i8]* %a, i32 0, i32 0
1531 %gep2 = getelementptr [12 x i8], [12 x i8]* %a, i32 0, i32 4
1532 %gep3 = getelementptr [12 x i8], [12 x i8]* %a, i32 0, i32 8
1533 %iptr1 = bitcast i8* %gep1 to i64*
1534 %iptr2 = bitcast i8* %gep2 to i64*
1535 %fptr1 = bitcast i8* %gep1 to float*
1536 %fptr2 = bitcast i8* %gep2 to float*
1537 %fptr3 = bitcast i8* %gep3 to float*
1538 store float 0.0, float* %fptr1
1539 store float 1.0, float* %fptr2
1540 %v = load i64, i64* %iptr1
1541 store i64 %v, i64* %iptr2
1542 %f1 = load float, float* %fptr2
1543 %f2 = load float, float* %fptr3
1544 %ret = fadd float %f1, %f2
1548 define i32 @PR22093() {
1549 ; Test that we don't try to pre-split a splittable store of a splittable but
1550 ; not pre-splittable load over the same alloca. We "handle" this case when the
1551 ; load is unsplittable but unrelated to this alloca by just generating extra
1552 ; loads without touching the original, but when the original load was out of
1553 ; this alloca we need to handle it specially to ensure the splits line up
1554 ; properly for rewriting.
1556 ; CHECK-LABEL: @PR22093(
1560 ; CHECK: store volatile i16
1564 %a.cast = bitcast i32* %a to i16*
1565 store volatile i16 42, i16* %a.cast
1566 %load = load i32, i32* %a
1567 store i32 %load, i32* %a
1571 define void @PR22093.2() {
1572 ; Another way that we end up being unable to split a particular set of loads
1573 ; and stores can even have ordering importance. Here we have a load which is
1574 ; pre-splittable by itself, and the first store is also compatible. But the
1575 ; second store of the load makes the load unsplittable because of a mismatch of
1576 ; splits. Because this makes the load unsplittable, we also have to go back and
1577 ; remove the first store from the presplit candidates as its load won't be
1580 ; CHECK-LABEL: @PR22093.2(
1583 ; CHECK-NEXT: alloca i8
1585 ; CHECK: store volatile i16
1586 ; CHECK: store volatile i8
1590 %a.cast1 = bitcast i64* %a to i32*
1591 %a.cast2 = bitcast i64* %a to i16*
1592 store volatile i16 42, i16* %a.cast2
1593 %load = load i32, i32* %a.cast1
1594 store i32 %load, i32* %a.cast1
1595 %a.gep1 = getelementptr i32, i32* %a.cast1, i32 1
1596 %a.cast3 = bitcast i32* %a.gep1 to i8*
1597 store volatile i8 13, i8* %a.cast3
1598 store i32 %load, i32* %a.gep1
1602 define void @PR23737() {
1603 ; CHECK-LABEL: @PR23737(
1604 ; CHECK: store atomic volatile {{.*}} seq_cst
1605 ; CHECK: load atomic volatile {{.*}} seq_cst
1607 %ptr = alloca i64, align 8
1608 store atomic volatile i64 0, i64* %ptr seq_cst, align 8
1609 %load = load atomic volatile i64, i64* %ptr seq_cst, align 8
1613 define i16 @PR24463() {
1614 ; Ensure we can handle a very interesting case where there is an integer-based
1615 ; rewrite of the uses of the alloca, but where one of the integers in that is
1616 ; a sub-integer that requires extraction *and* extends past the end of the
1617 ; alloca. In this case, we should extract the i8 and then zext it to i16.
1619 ; CHECK-LABEL: @PR24463(
1621 ; CHECK: %[[SHIFT:.*]] = lshr i16 0, 8
1622 ; CHECK: %[[TRUNC:.*]] = trunc i16 %[[SHIFT]] to i8
1623 ; CHECK: %[[ZEXT:.*]] = zext i8 %[[TRUNC]] to i16
1624 ; CHECK: ret i16 %[[ZEXT]]
1626 %alloca = alloca [3 x i8]
1627 %gep1 = getelementptr inbounds [3 x i8], [3 x i8]* %alloca, i64 0, i64 1
1628 %bc1 = bitcast i8* %gep1 to i16*
1629 store i16 0, i16* %bc1
1630 %gep2 = getelementptr inbounds [3 x i8], [3 x i8]* %alloca, i64 0, i64 2
1631 %bc2 = bitcast i8* %gep2 to i16*
1632 %load = load i16, i16* %bc2