1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
3 ; RUN: opt < %s -aa-pipeline=basic-aa -passes=dse -S | FileCheck %s
4 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
6 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind
7 declare void @llvm.memset.element.unordered.atomic.p0i8.i64(i8* nocapture, i8, i64, i32) nounwind
8 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) nounwind
9 declare void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32) nounwind
10 declare void @llvm.init.trampoline(i8*, i8*, i8*)
12 define void @test1(i32* %Q, i32* %P) {
13 ; CHECK-LABEL: @test1(
14 ; CHECK-NEXT: store i32 0, i32* [[P:%.*]]
15 ; CHECK-NEXT: ret void
17 %DEAD = load i32, i32* %Q
18 store i32 %DEAD, i32* %P
23 ; PR8576 - Should delete store of 10 even though p/q are may aliases.
24 define void @test2(i32 *%p, i32 *%q) {
25 ; CHECK-LABEL: @test2(
26 ; CHECK-NEXT: store i32 20, i32* [[Q:%.*]], align 4
27 ; CHECK-NEXT: store i32 30, i32* [[P:%.*]], align 4
28 ; CHECK-NEXT: ret void
30 store i32 10, i32* %p, align 4
31 store i32 20, i32* %q, align 4
32 store i32 30, i32* %p, align 4
40 define i32 @test3(i32* %g_addr) nounwind {
41 ; CHECK-LABEL: @test3(
42 ; CHECK-NEXT: [[G_VALUE:%.*]] = load i32, i32* [[G_ADDR:%.*]], align 4
43 ; CHECK-NEXT: store i32 -1, i32* @g, align 4
44 ; CHECK-NEXT: store i32 [[G_VALUE]], i32* [[G_ADDR]], align 4
45 ; CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* @g, align 4
46 ; CHECK-NEXT: ret i32 [[TMP3]]
48 %g_value = load i32, i32* %g_addr, align 4
49 store i32 -1, i32* @g, align 4
50 store i32 %g_value, i32* %g_addr, align 4
51 %tmp3 = load i32, i32* @g, align 4
56 define void @test4(i32* %Q) {
57 ; CHECK-LABEL: @test4(
58 ; CHECK-NEXT: [[A:%.*]] = load i32, i32* [[Q:%.*]]
59 ; CHECK-NEXT: store volatile i32 [[A]], i32* [[Q]]
60 ; CHECK-NEXT: ret void
62 %a = load i32, i32* %Q
63 store volatile i32 %a, i32* %Q
67 define void @test5(i32* %Q) {
68 ; CHECK-LABEL: @test5(
69 ; CHECK-NEXT: [[A:%.*]] = load volatile i32, i32* [[Q:%.*]]
70 ; CHECK-NEXT: ret void
72 %a = load volatile i32, i32* %Q
77 ; Should delete store of 10 even though memset is a may-store to P (P and Q may
79 define void @test6(i32 *%p, i8 *%q) {
80 ; CHECK-LABEL: @test6(
81 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[Q:%.*]], i8 42, i64 900, i1 false)
82 ; CHECK-NEXT: store i32 30, i32* [[P:%.*]], align 4
83 ; CHECK-NEXT: ret void
85 store i32 10, i32* %p, align 4 ;; dead.
86 call void @llvm.memset.p0i8.i64(i8* %q, i8 42, i64 900, i1 false)
87 store i32 30, i32* %p, align 4
91 ; Should delete store of 10 even though memset is a may-store to P (P and Q may
93 define void @test6_atomic(i32* align 4 %p, i8* align 4 %q) {
94 ; CHECK-LABEL: @test6_atomic(
95 ; CHECK-NEXT: call void @llvm.memset.element.unordered.atomic.p0i8.i64(i8* align 4 [[Q:%.*]], i8 42, i64 900, i32 4)
96 ; CHECK-NEXT: store atomic i32 30, i32* [[P:%.*]] unordered, align 4
97 ; CHECK-NEXT: ret void
99 store atomic i32 10, i32* %p unordered, align 4 ;; dead.
100 call void @llvm.memset.element.unordered.atomic.p0i8.i64(i8* align 4 %q, i8 42, i64 900, i32 4)
101 store atomic i32 30, i32* %p unordered, align 4
105 ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
107 define void @test7(i32 *%p, i8 *%q, i8* noalias %r) {
108 ; CHECK-LABEL: @test7(
109 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[Q:%.*]], i8* [[R:%.*]], i64 900, i1 false)
110 ; CHECK-NEXT: store i32 30, i32* [[P:%.*]], align 4
111 ; CHECK-NEXT: ret void
113 store i32 10, i32* %p, align 4 ;; dead.
114 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %r, i64 900, i1 false)
115 store i32 30, i32* %p, align 4
119 ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
121 define void @test7_atomic(i32* align 4 %p, i8* align 4 %q, i8* noalias align 4 %r) {
122 ; CHECK-LABEL: @test7_atomic(
123 ; CHECK-NEXT: call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 4 [[Q:%.*]], i8* align 4 [[R:%.*]], i64 900, i32 4)
124 ; CHECK-NEXT: store atomic i32 30, i32* [[P:%.*]] unordered, align 4
125 ; CHECK-NEXT: ret void
127 store atomic i32 10, i32* %p unordered, align 4 ;; dead.
128 call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 4 %q, i8* align 4 %r, i64 900, i32 4)
129 store atomic i32 30, i32* %p unordered, align 4
133 ; Do not delete stores that are only partially killed.
134 define i32 @test8() {
135 ; CHECK-LABEL: @test8(
136 ; CHECK-NEXT: [[V:%.*]] = alloca i32
137 ; CHECK-NEXT: store i32 1234567, i32* [[V]]
138 ; CHECK-NEXT: [[X:%.*]] = load i32, i32* [[V]]
139 ; CHECK-NEXT: ret i32 [[X]]
142 store i32 1234567, i32* %V
143 %V2 = bitcast i32* %V to i8*
145 %X = load i32, i32* %V
151 ; Test for byval handling.
152 %struct.x = type { i32, i32, i32, i32 }
153 define void @test9(%struct.x* byval %a) nounwind {
154 ; CHECK-LABEL: @test9(
155 ; CHECK-NEXT: ret void
157 %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
158 store i32 1, i32* %tmp2, align 4
162 ; Test for inalloca handling.
163 define void @test9_2(%struct.x* inalloca %a) nounwind {
164 ; CHECK-LABEL: @test9_2(
165 ; CHECK-NEXT: ret void
167 %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
168 store i32 1, i32* %tmp2, align 4
172 ; va_arg has fuzzy dependence, the store shouldn't be zapped.
173 define double @test10(i8* %X) {
174 ; CHECK-LABEL: @test10(
175 ; CHECK-NEXT: [[X_ADDR:%.*]] = alloca i8*
176 ; CHECK-NEXT: store i8* [[X:%.*]], i8** [[X_ADDR]]
177 ; CHECK-NEXT: [[TMP_0:%.*]] = va_arg i8** [[X_ADDR]], double
178 ; CHECK-NEXT: ret double [[TMP_0]]
181 store i8* %X, i8** %X_addr
182 %tmp.0 = va_arg i8** %X_addr, double
187 ; DSE should delete the dead trampoline.
188 declare void @test11f()
189 define void @test11() {
190 ; CHECK-LABEL: @test11(
191 ; CHECK-NEXT: ret void
193 %storage = alloca [10 x i8], align 16 ; <[10 x i8]*> [#uses=1]
194 %cast = getelementptr [10 x i8], [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1]
195 call void @llvm.init.trampoline( i8* %cast, i8* bitcast (void ()* @test11f to i8*), i8* null ) ; <i8*> [#uses=1]
200 ; PR2599 - load -> store to same address.
201 define void @test12({ i32, i32 }* %x) nounwind {
202 ; CHECK-LABEL: @test12(
203 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[X:%.*]], i32 0, i32 1
204 ; CHECK-NEXT: [[TMP8:%.*]] = load i32, i32* [[TMP7]], align 4
205 ; CHECK-NEXT: [[TMP17:%.*]] = sub i32 0, [[TMP8]]
206 ; CHECK-NEXT: store i32 [[TMP17]], i32* [[TMP7]], align 4
207 ; CHECK-NEXT: ret void
209 %tmp4 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 0
210 %tmp5 = load i32, i32* %tmp4, align 4
211 %tmp7 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
212 %tmp8 = load i32, i32* %tmp7, align 4
213 %tmp17 = sub i32 0, %tmp8
214 store i32 %tmp5, i32* %tmp4, align 4
215 store i32 %tmp17, i32* %tmp7, align 4
220 ; %P doesn't escape, the DEAD instructions should be removed.
221 declare void @test13f()
222 define i32* @test13() {
223 ; CHECK-LABEL: @test13(
224 ; CHECK-NEXT: [[PTR:%.*]] = tail call i8* @malloc(i32 4)
225 ; CHECK-NEXT: [[P:%.*]] = bitcast i8* [[PTR]] to i32*
226 ; CHECK-NEXT: call void @test13f()
227 ; CHECK-NEXT: store i32 0, i32* [[P]]
228 ; CHECK-NEXT: ret i32* [[P]]
230 %ptr = tail call i8* @malloc(i32 4)
231 %P = bitcast i8* %ptr to i32*
232 %DEAD = load i32, i32* %P
233 %DEAD2 = add i32 %DEAD, 1
234 store i32 %DEAD2, i32* %P
235 call void @test13f( )
240 define i32 addrspace(1)* @test13_addrspacecast() {
241 ; CHECK-LABEL: @test13_addrspacecast(
242 ; CHECK-NEXT: [[P:%.*]] = tail call i8* @malloc(i32 4)
243 ; CHECK-NEXT: [[P_BC:%.*]] = bitcast i8* [[P]] to i32*
244 ; CHECK-NEXT: [[P:%.*]] = addrspacecast i32* [[P_BC]] to i32 addrspace(1)*
245 ; CHECK-NEXT: call void @test13f()
246 ; CHECK-NEXT: store i32 0, i32 addrspace(1)* [[P]]
247 ; CHECK-NEXT: ret i32 addrspace(1)* [[P]]
249 %p = tail call i8* @malloc(i32 4)
250 %p.bc = bitcast i8* %p to i32*
251 %P = addrspacecast i32* %p.bc to i32 addrspace(1)*
252 %DEAD = load i32, i32 addrspace(1)* %P
253 %DEAD2 = add i32 %DEAD, 1
254 store i32 %DEAD2, i32 addrspace(1)* %P
255 call void @test13f( )
256 store i32 0, i32 addrspace(1)* %P
257 ret i32 addrspace(1)* %P
260 declare noalias i8* @malloc(i32)
261 declare noalias i8* @calloc(i32, i32)
264 define void @test14(i32* %Q) {
265 ; CHECK-LABEL: @test14(
266 ; CHECK-NEXT: ret void
269 %DEAD = load i32, i32* %Q
270 store i32 %DEAD, i32* %P
278 ;; Fully dead overwrite of memcpy.
279 define void @test15(i8* %P, i8* %Q) nounwind ssp {
280 ; CHECK-LABEL: @test15(
281 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
282 ; CHECK-NEXT: ret void
284 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
285 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
289 ;; Fully dead overwrite of memcpy.
290 define void @test15_atomic(i8* %P, i8* %Q) nounwind ssp {
291 ; CHECK-LABEL: @test15_atomic(
292 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
293 ; CHECK-NEXT: ret void
295 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
296 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
300 ;; Fully dead overwrite of memcpy.
301 define void @test15_atomic_weaker(i8* %P, i8* %Q) nounwind ssp {
302 ; CHECK-LABEL: @test15_atomic_weaker(
303 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
304 ; CHECK-NEXT: ret void
306 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i1 false)
307 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
311 ;; Fully dead overwrite of memcpy.
312 define void @test15_atomic_weaker_2(i8* %P, i8* %Q) nounwind ssp {
313 ; CHECK-LABEL: @test15_atomic_weaker_2(
314 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i1 false)
315 ; CHECK-NEXT: ret void
317 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
318 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i1 false)
322 ;; Full overwrite of smaller memcpy.
323 define void @test16(i8* %P, i8* %Q) nounwind ssp {
324 ; CHECK-LABEL: @test16(
325 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
326 ; CHECK-NEXT: ret void
328 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i1 false)
329 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
333 ;; Full overwrite of smaller memcpy.
334 define void @test16_atomic(i8* %P, i8* %Q) nounwind ssp {
335 ; CHECK-LABEL: @test16_atomic(
336 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
337 ; CHECK-NEXT: ret void
339 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 8, i32 1)
340 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
344 ;; Full overwrite of smaller memory where overwrite has stronger atomicity
345 define void @test16_atomic_weaker(i8* %P, i8* %Q) nounwind ssp {
346 ; CHECK-LABEL: @test16_atomic_weaker(
347 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
348 ; CHECK-NEXT: ret void
350 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 8, i1 false)
351 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
355 ;; Full overwrite of smaller memory where overwrite has weaker atomicity.
356 define void @test16_atomic_weaker_2(i8* %P, i8* %Q) nounwind ssp {
357 ; CHECK-LABEL: @test16_atomic_weaker_2(
358 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i1 false)
359 ; CHECK-NEXT: ret void
361 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 8, i32 1)
362 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i1 false)
366 ;; Overwrite of memset by memcpy.
367 define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
368 ; CHECK-LABEL: @test17(
369 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
370 ; CHECK-NEXT: ret void
372 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i1 false)
373 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
377 ;; Overwrite of memset by memcpy.
378 define void @test17_atomic(i8* %P, i8* noalias %Q) nounwind ssp {
379 ; CHECK-LABEL: @test17_atomic(
380 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
381 ; CHECK-NEXT: ret void
383 tail call void @llvm.memset.element.unordered.atomic.p0i8.i64(i8* align 1 %P, i8 42, i64 8, i32 1)
384 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
388 ;; Overwrite of memset by memcpy. Overwrite is stronger atomicity. We can
389 ;; remove the memset.
390 define void @test17_atomic_weaker(i8* %P, i8* noalias %Q) nounwind ssp {
391 ; CHECK-LABEL: @test17_atomic_weaker(
392 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
393 ; CHECK-NEXT: ret void
395 tail call void @llvm.memset.p0i8.i64(i8* align 1 %P, i8 42, i64 8, i1 false)
396 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
400 ;; Overwrite of memset by memcpy. Overwrite is weaker atomicity. We can remove
402 define void @test17_atomic_weaker_2(i8* %P, i8* noalias %Q) nounwind ssp {
403 ; CHECK-LABEL: @test17_atomic_weaker_2(
404 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i1 false)
405 ; CHECK-NEXT: ret void
407 tail call void @llvm.memset.element.unordered.atomic.p0i8.i64(i8* align 1 %P, i8 42, i64 8, i32 1)
408 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i1 false)
412 ; Should not delete the volatile memset.
413 define void @test17v(i8* %P, i8* %Q) nounwind ssp {
414 ; CHECK-LABEL: @test17v(
415 ; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* [[P:%.*]], i8 42, i64 8, i1 true)
416 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P]], i8* [[Q:%.*]], i64 12, i1 false)
417 ; CHECK-NEXT: ret void
419 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i1 true)
420 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
425 ; Do not delete instruction where possible situation is:
429 ; NB! See PR11763 - currently LLVM allows memcpy's source and destination to be
430 ; equal (but not inequal and overlapping).
431 define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
432 ; CHECK-LABEL: @test18(
433 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
434 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P]], i8* [[R:%.*]], i64 12, i1 false)
435 ; CHECK-NEXT: ret void
437 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
438 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i1 false)
442 define void @test18_atomic(i8* %P, i8* %Q, i8* %R) nounwind ssp {
443 ; CHECK-LABEL: @test18_atomic(
444 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
445 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P]], i8* align 1 [[R:%.*]], i64 12, i32 1)
446 ; CHECK-NEXT: ret void
448 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
449 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %R, i64 12, i32 1)
454 ; The store here is not dead because the byval call reads it.
455 declare void @test19f({i32}* byval align 4 %P)
457 define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
458 ; CHECK-LABEL: @test19(
460 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds { i32 }, { i32 }* [[ARG5:%.*]], i32 0, i32 0
461 ; CHECK-NEXT: store i32 912, i32* [[TMP7]]
462 ; CHECK-NEXT: call void @test19f({ i32 }* byval align 4 [[ARG5]])
463 ; CHECK-NEXT: ret void
466 %tmp7 = getelementptr inbounds {i32}, {i32}* %arg5, i32 0, i32 0
467 store i32 912, i32* %tmp7
468 call void @test19f({i32}* byval align 4 %arg5)
473 define void @test20() {
474 ; CHECK-LABEL: @test20(
475 ; CHECK-NEXT: ret void
477 %m = call i8* @malloc(i32 24)
482 define void @test21() {
483 ; CHECK-LABEL: @test21(
484 ; CHECK-NEXT: ret void
486 %m = call i8* @calloc(i32 9, i32 7)
491 define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
492 ; CHECK-LABEL: @test22(
493 ; CHECK-NEXT: ret void
497 %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
498 store i32 0, i32* %k.addr.m.addr, align 4
503 declare noalias i8* @strdup(i8* nocapture) nounwind
504 define noalias i8* @test23() nounwind uwtable ssp {
505 ; CHECK-LABEL: @test23(
506 ; CHECK-NEXT: [[X:%.*]] = alloca [2 x i8], align 1
507 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i8], [2 x i8]* [[X]], i64 0, i64 0
508 ; CHECK-NEXT: store i8 97, i8* [[ARRAYIDX]], align 1
509 ; CHECK-NEXT: [[ARRAYIDX1:%.*]] = getelementptr inbounds [2 x i8], [2 x i8]* [[X]], i64 0, i64 1
510 ; CHECK-NEXT: store i8 0, i8* [[ARRAYIDX1]], align 1
511 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @strdup(i8* [[ARRAYIDX]]) #1
512 ; CHECK-NEXT: ret i8* [[CALL]]
514 %x = alloca [2 x i8], align 1
515 %arrayidx = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 0
516 store i8 97, i8* %arrayidx, align 1
517 %arrayidx1 = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 1
518 store i8 0, i8* %arrayidx1, align 1
519 %call = call i8* @strdup(i8* %arrayidx) nounwind
523 ; Make sure same sized store to later element is deleted
524 define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind {
525 ; CHECK-LABEL: @test24(
526 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [2 x i32], [2 x i32]* [[A:%.*]], i64 0, i64 0
527 ; CHECK-NEXT: store i32 [[B:%.*]], i32* [[TMP1]], align 4
528 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds [2 x i32], [2 x i32]* [[A]], i64 0, i64 1
529 ; CHECK-NEXT: store i32 [[C:%.*]], i32* [[TMP2]], align 4
530 ; CHECK-NEXT: ret void
532 %1 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
533 store i32 0, i32* %1, align 4
534 %2 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
535 store i32 0, i32* %2, align 4
536 %3 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
537 store i32 %b, i32* %3, align 4
538 %4 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
539 store i32 %c, i32* %4, align 4
543 ; Check another case like PR13547 where strdup is not like malloc.
544 define i8* @test25(i8* %p) nounwind {
545 ; CHECK-LABEL: @test25(
546 ; CHECK-NEXT: [[P_4:%.*]] = getelementptr i8, i8* [[P:%.*]], i64 4
547 ; CHECK-NEXT: [[TMP:%.*]] = load i8, i8* [[P_4]], align 1
548 ; CHECK-NEXT: store i8 0, i8* [[P_4]], align 1
549 ; CHECK-NEXT: [[Q:%.*]] = call i8* @strdup(i8* [[P]]) #4
550 ; CHECK-NEXT: store i8 [[TMP]], i8* [[P_4]], align 1
551 ; CHECK-NEXT: ret i8* [[Q]]
553 %p.4 = getelementptr i8, i8* %p, i64 4
554 %tmp = load i8, i8* %p.4, align 1
555 store i8 0, i8* %p.4, align 1
556 %q = call i8* @strdup(i8* %p) nounwind optsize
557 store i8 %tmp, i8* %p.4, align 1
561 ; Remove redundant store if loaded value is in another block.
562 define i32 @test26(i1 %c, i32* %p) {
563 ; CHECK-LABEL: @test26(
565 ; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
567 ; CHECK-NEXT: br label [[BB3:%.*]]
569 ; CHECK-NEXT: br label [[BB3]]
571 ; CHECK-NEXT: ret i32 0
574 %v = load i32, i32* %p, align 4
575 br i1 %c, label %bb1, label %bb2
579 store i32 %v, i32* %p, align 4
585 ; Remove redundant store if loaded value is in another block.
586 define i32 @test27(i1 %c, i32* %p) {
587 ; CHECK-LABEL: @test27(
589 ; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
591 ; CHECK-NEXT: br label [[BB3:%.*]]
593 ; CHECK-NEXT: br label [[BB3]]
595 ; CHECK-NEXT: ret i32 0
598 %v = load i32, i32* %p, align 4
599 br i1 %c, label %bb1, label %bb2
605 store i32 %v, i32* %p, align 4
609 ; Don't remove redundant store because of may-aliased store.
610 define i32 @test28(i1 %c, i32* %p, i32* %p2, i32 %i) {
611 ; CHECK-LABEL: @test28(
613 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4
614 ; CHECK-NEXT: store i32 [[I:%.*]], i32* [[P2:%.*]], align 4
615 ; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
617 ; CHECK-NEXT: br label [[BB3:%.*]]
619 ; CHECK-NEXT: br label [[BB3]]
621 ; CHECK-NEXT: store i32 [[V]], i32* [[P]], align 4
622 ; CHECK-NEXT: ret i32 0
625 %v = load i32, i32* %p, align 4
627 ; Might overwrite value at %p
628 store i32 %i, i32* %p2, align 4
629 br i1 %c, label %bb1, label %bb2
635 store i32 %v, i32* %p, align 4
639 ; Don't remove redundant store because of may-aliased store.
640 define i32 @test29(i1 %c, i32* %p, i32* %p2, i32 %i) {
641 ; CHECK-LABEL: @test29(
643 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4
644 ; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
646 ; CHECK-NEXT: br label [[BB3:%.*]]
648 ; CHECK-NEXT: store i32 [[I:%.*]], i32* [[P2:%.*]], align 4
649 ; CHECK-NEXT: br label [[BB3]]
651 ; CHECK-NEXT: store i32 [[V]], i32* [[P]], align 4
652 ; CHECK-NEXT: ret i32 0
655 %v = load i32, i32* %p, align 4
656 br i1 %c, label %bb1, label %bb2
660 ; Might overwrite value at %p
661 store i32 %i, i32* %p2, align 4
664 store i32 %v, i32* %p, align 4
668 declare void @unknown_func()
670 ; Don't remove redundant store because of unknown call.
671 define i32 @test30(i1 %c, i32* %p, i32 %i) {
672 ; CHECK-LABEL: @test30(
674 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4
675 ; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
677 ; CHECK-NEXT: br label [[BB3:%.*]]
679 ; CHECK-NEXT: call void @unknown_func()
680 ; CHECK-NEXT: br label [[BB3]]
682 ; CHECK-NEXT: store i32 [[V]], i32* [[P]], align 4
683 ; CHECK-NEXT: ret i32 0
686 %v = load i32, i32* %p, align 4
687 br i1 %c, label %bb1, label %bb2
691 ; Might overwrite value at %p
692 call void @unknown_func()
695 store i32 %v, i32* %p, align 4
699 ; Remove redundant store if loaded value is in another block inside a loop.
700 define i32 @test31(i1 %c, i32* %p, i32 %i) {
701 ; CHECK-LABEL: @test31(
703 ; CHECK-NEXT: br label [[BB1:%.*]]
705 ; CHECK-NEXT: br i1 undef, label [[BB1]], label [[BB2:%.*]]
707 ; CHECK-NEXT: ret i32 0
710 %v = load i32, i32* %p, align 4
713 store i32 %v, i32* %p, align 4
714 br i1 undef, label %bb1, label %bb2
719 ; Don't remove redundant store in a loop with a may-alias store.
720 define i32 @test32(i1 %c, i32* %p, i32 %i) {
721 ; CHECK-LABEL: @test32(
723 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4
724 ; CHECK-NEXT: br label [[BB1:%.*]]
726 ; CHECK-NEXT: store i32 [[V]], i32* [[P]], align 4
727 ; CHECK-NEXT: call void @unknown_func()
728 ; CHECK-NEXT: br i1 undef, label [[BB1]], label [[BB2:%.*]]
730 ; CHECK-NEXT: ret i32 0
733 %v = load i32, i32* %p, align 4
736 store i32 %v, i32* %p, align 4
737 ; Might read and overwrite value at %p
738 call void @unknown_func()
739 br i1 undef, label %bb1, label %bb2
744 ; Remove redundant store, which is in the lame loop as the load.
745 define i32 @test33(i1 %c, i32* %p, i32 %i) {
746 ; CHECK-LABEL: @test33(
748 ; CHECK-NEXT: br label [[BB1:%.*]]
750 ; CHECK-NEXT: br label [[BB2:%.*]]
752 ; CHECK-NEXT: call void @unknown_func()
753 ; CHECK-NEXT: br i1 undef, label [[BB1]], label [[BB3:%.*]]
755 ; CHECK-NEXT: ret i32 0
760 %v = load i32, i32* %p, align 4
763 store i32 %v, i32* %p, align 4
764 ; Might read and overwrite value at %p, but doesn't matter.
765 call void @unknown_func()
766 br i1 undef, label %bb1, label %bb3
771 ; Don't remove redundant store: unknown_func could unwind
772 define void @test34(i32* noalias %p) {
773 ; CHECK-LABEL: @test34(
774 ; CHECK-NEXT: store i32 1, i32* [[P:%.*]]
775 ; CHECK-NEXT: call void @unknown_func()
776 ; CHECK-NEXT: store i32 0, i32* [[P]]
777 ; CHECK-NEXT: ret void
780 call void @unknown_func()
785 ; Remove redundant store even with an unwinding function in the same block
786 define void @test35(i32* noalias %p) {
787 ; CHECK-LABEL: @test35(
788 ; CHECK-NEXT: call void @unknown_func()
789 ; CHECK-NEXT: store i32 0, i32* [[P:%.*]]
790 ; CHECK-NEXT: ret void
792 call void @unknown_func()
798 ; We cannot optimize away the first memmove since %P could overlap with %Q.
799 define void @test36(i8* %P, i8* %Q) {
800 ; CHECK-LABEL: @test36(
801 ; CHECK-NEXT: tail call void @llvm.memmove.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
802 ; CHECK-NEXT: tail call void @llvm.memmove.p0i8.p0i8.i64(i8* [[P]], i8* [[Q]], i64 12, i1 false)
803 ; CHECK-NEXT: ret void
806 tail call void @llvm.memmove.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
807 tail call void @llvm.memmove.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
811 define void @test36_atomic(i8* %P, i8* %Q) {
812 ; CHECK-LABEL: @test36_atomic(
813 ; CHECK-NEXT: tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
814 ; CHECK-NEXT: tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P]], i8* align 1 [[Q]], i64 12, i32 1)
815 ; CHECK-NEXT: ret void
818 tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
819 tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
823 define void @test37(i8* %P, i8* %Q, i8* %R) {
824 ; CHECK-LABEL: @test37(
825 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
826 ; CHECK-NEXT: tail call void @llvm.memmove.p0i8.p0i8.i64(i8* [[P]], i8* [[R:%.*]], i64 12, i1 false)
827 ; CHECK-NEXT: ret void
830 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
831 tail call void @llvm.memmove.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i1 false)
835 define void @test37_atomic(i8* %P, i8* %Q, i8* %R) {
836 ; CHECK-LABEL: @test37_atomic(
837 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
838 ; CHECK-NEXT: tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P]], i8* align 1 [[R:%.*]], i64 12, i32 1)
839 ; CHECK-NEXT: ret void
842 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
843 tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %R, i64 12, i32 1)
847 ; Same caveat about memcpy as in @test18 applies here.
848 define void @test38(i8* %P, i8* %Q, i8* %R) {
849 ; CHECK-LABEL: @test38(
850 ; CHECK-NEXT: tail call void @llvm.memmove.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
851 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P]], i8* [[R:%.*]], i64 12, i1 false)
852 ; CHECK-NEXT: ret void
855 tail call void @llvm.memmove.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
856 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i1 false)
860 define void @test38_atomic(i8* %P, i8* %Q, i8* %R) {
861 ; CHECK-LABEL: @test38_atomic(
862 ; CHECK-NEXT: tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
863 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P]], i8* align 1 [[R:%.*]], i64 12, i32 1)
864 ; CHECK-NEXT: ret void
867 tail call void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
868 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %R, i64 12, i32 1)
872 define void @test39(i8* %P, i8* %Q, i8* %R) {
873 ; CHECK-LABEL: @test39(
874 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P:%.*]], i8* [[Q:%.*]], i64 12, i1 false)
875 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[P]], i8* [[R:%.*]], i64 8, i1 false)
876 ; CHECK-NEXT: ret void
879 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i1 false)
880 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 8, i1 false)
884 define void @test39_atomic(i8* %P, i8* %Q, i8* %R) {
885 ; CHECK-LABEL: @test39_atomic(
886 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P:%.*]], i8* align 1 [[Q:%.*]], i64 12, i32 1)
887 ; CHECK-NEXT: tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 [[P]], i8* align 1 [[R:%.*]], i64 8, i32 1)
888 ; CHECK-NEXT: ret void
891 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %Q, i64 12, i32 1)
892 tail call void @llvm.memcpy.element.unordered.atomic.p0i8.p0i8.i64(i8* align 1 %P, i8* align 1 %R, i64 8, i32 1)
896 declare void @llvm.memmove.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i1)
897 declare void @llvm.memmove.element.unordered.atomic.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32)