1 ; RUN: opt < %s -basicaa -gvn -dse -S | FileCheck %s
2 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"
4 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
6 declare void @external(i32*)
8 define i32 @test0(i8* %P) {
10 call void @external(i32* %A)
14 call void @llvm.memset.p0i8.i32(i8* %P, i8 0, i32 42, i1 false)
16 %B = load i32, i32* %A
28 store i8 2, i8* %B ;; Not written to by memcpy
30 call void @llvm.memcpy.p0i8.p0i8.i8(i8* %A, i8* %B, i8 -1, i1 false)
37 define i8 @test2(i8* %P) {
39 %P2 = getelementptr i8, i8* %P, i32 127
40 store i8 1, i8* %P2 ;; Not dead across memset
41 call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i1 false)
47 define i8 @test2a(i8* %P) {
49 %P2 = getelementptr i8, i8* %P, i32 126
51 ;; FIXME: DSE isn't zapping this dead store.
52 store i8 1, i8* %P2 ;; Dead, clobbered by memset.
54 call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i1 false)
61 define void @test3(i8* %P, i8 %X) {
65 %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
67 %P2 = getelementptr i8, i8* %P, i32 2
68 store i8 %Y, i8* %P2 ;; Not read by lifetime.end, should be removed.
69 ; CHECK: store i8 2, i8* %P2
70 call void @llvm.lifetime.end.p0i8(i64 1, i8* %P)
77 define void @test3a(i8* %P, i8 %X) {
78 ; CHECK-LABEL: @test3a
79 %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
81 %P2 = getelementptr i8, i8* %P, i32 2
83 ; CHECK-NEXT: call void @llvm.lifetime.end
84 call void @llvm.lifetime.end.p0i8(i64 10, i8* %P)
86 ; CHECK-NEXT: ret void
89 @G1 = external global i32
90 @G2 = external global [4000 x i32]
92 define i32 @test4(i8* %P) {
93 %tmp = load i32, i32* @G1
94 call void @llvm.memset.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8 0, i32 4000, i1 false)
95 %tmp2 = load i32, i32* @G1
96 %sub = sub i32 %tmp2, %tmp
100 ; CHECK: memset.p0i8.i32
105 ; Verify that basicaa is handling variable length memcpy, knowing it doesn't
107 define i32 @test5(i8* %P, i32 %Len) {
108 %tmp = load i32, i32* @G1
109 call void @llvm.memcpy.p0i8.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8* bitcast (i32* @G1 to i8*), i32 %Len, i1 false)
110 %tmp2 = load i32, i32* @G1
111 %sub = sub i32 %tmp2, %tmp
115 ; CHECK: memcpy.p0i8.p0i8.i32
120 define i8 @test6(i8* %p, i8* noalias %a) {
122 %t = va_arg i8* %p, float
126 ; CHECK-LABEL: @test6
127 ; CHECK: load i8, i8* %a
133 declare void @test7decl(i32* nocapture %x)
134 define i32 @test7() nounwind uwtable ssp {
136 %x = alloca i32, align 4
137 store i32 0, i32* %x, align 4
138 %add.ptr = getelementptr inbounds i32, i32* %x, i64 1
139 call void @test7decl(i32* %add.ptr)
140 %tmp = load i32, i32* %x, align 4
142 ; CHECK-LABEL: @test7(
144 ; CHECK: call void @test7decl
145 ; CHECK: load i32, i32*
148 ;; Check that aa correctly handles functions marked with argmemonly
150 declare i32 @func_argmemonly(i32 * %P) argmemonly
152 ;; Can not remove redundant load, function may write to it.
153 ; CHECK-LABEL: @test8(
154 define i32 @test8(i32 *%P) {
155 %V1 = load i32, i32* %P
156 call i32 @func_argmemonly(i32* %P)
157 %V2 = load i32, i32* %P
158 %Diff = sub i32 %V1, %V2
163 ; CHECK: ret i32 %Diff
166 ;; In this case load can be removed, function clobbers only %P2.
167 ; CHECK-LABEL: @test9(
168 define i32 @test9(i32* %P, i32* noalias %P2) {
169 %V1 = load i32, i32* %P
170 call i32 @func_argmemonly(i32* %P2)
171 %V2 = load i32, i32* %P
172 %Diff = sub i32 %V1, %V2
178 ;; In this case load can *not* be removed. Function clobers only %P2 but it may
180 ; CHECK-LABEL: @test10(
181 define i32 @test10(i32* %P, i32* %P2) {
182 %V1 = load i32, i32* %P
183 call i32 @func_argmemonly(i32* %P2)
184 %V2 = load i32, i32* %P
185 %Diff = sub i32 %V1, %V2
190 ; CHECK: ret i32 %Diff
193 ; CHECK-LABEL: @test11(
194 define i32 @test11(i32* %P, i32* %P2) {
195 %V1 = load i32, i32* %P
196 call i32 @func_argmemonly(i32* readonly %P2)
197 %V2 = load i32, i32* %P
198 %Diff = sub i32 %V1, %V2
204 declare i32 @func_argmemonly_two_args(i32* %P, i32* %P2) argmemonly
206 ; CHECK-LABEL: @test12(
207 define i32 @test12(i32* %P, i32* %P2, i32* %P3) {
208 %V1 = load i32, i32* %P
209 call i32 @func_argmemonly_two_args(i32* readonly %P2, i32* %P3)
210 %V2 = load i32, i32* %P
211 %Diff = sub i32 %V1, %V2
216 ; CHECK: ret i32 %Diff
219 ; CHECK-LABEL: @test13(
220 define i32 @test13(i32* %P, i32* %P2) {
221 %V1 = load i32, i32* %P
222 call i32 @func_argmemonly(i32* readnone %P2)
223 %V2 = load i32, i32* %P
224 %Diff = sub i32 %V1, %V2
230 declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i1) nounwind
231 declare void @llvm.memset.p0i8.i8(i8* nocapture, i8, i8, i1) nounwind
232 declare void @llvm.memcpy.p0i8.p0i8.i8(i8* nocapture, i8* nocapture, i8, i1) nounwind
233 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind