1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -basic-aa -gvn -dse -S | FileCheck %s
3 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"
5 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
7 declare void @external(i32*)
9 define i32 @test0(i8* %P) {
10 ; CHECK-LABEL: @test0(
11 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
12 ; CHECK-NEXT: call void @external(i32* [[A]])
13 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* [[P:%.*]], i8 0, i32 42, i1 false)
14 ; CHECK-NEXT: ret i32 0
17 call void @external(i32* %A)
21 call void @llvm.memset.p0i8.i32(i8* %P, i8 0, i32 42, i1 false)
23 %B = load i32, i32* %A
28 ; CHECK-LABEL: @test1(
29 ; CHECK-NEXT: ret i8 2
34 store i8 2, i8* %B ;; Not written to by memcpy
36 call void @llvm.memcpy.p0i8.p0i8.i8(i8* %A, i8* %B, i8 -1, i1 false)
42 define i8 @test2(i8* %P) {
43 ; CHECK-LABEL: @test2(
44 ; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, i8* [[P:%.*]], i32 127
45 ; CHECK-NEXT: store i8 1, i8* [[P2]], align 1
46 ; CHECK-NEXT: call void @llvm.memset.p0i8.i8(i8* [[P]], i8 2, i8 127, i1 false)
47 ; CHECK-NEXT: ret i8 1
49 %P2 = getelementptr i8, i8* %P, i32 127
50 store i8 1, i8* %P2 ;; Not dead across memset
51 call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i1 false)
56 define i8 @test2a(i8* %P) {
57 ; CHECK-LABEL: @test2a(
58 ; CHECK-NEXT: call void @llvm.memset.p0i8.i8(i8* [[P:%.*]], i8 2, i8 127, i1 false)
59 ; CHECK-NEXT: ret i8 2
61 %P2 = getelementptr i8, i8* %P, i32 126
63 store i8 1, i8* %P2 ;; Dead, clobbered by memset.
65 call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i1 false)
70 define void @test3(i8* %P, i8 %X) {
71 ; CHECK-LABEL: @test3(
72 ; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, i8* [[P:%.*]], i32 2
73 ; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* [[P]])
74 ; CHECK-NEXT: store i8 2, i8* [[P2]], align 1
75 ; CHECK-NEXT: ret void
77 %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
79 %P2 = getelementptr i8, i8* %P, i32 2
80 store i8 %Y, i8* %P2 ;; Not read by lifetime.end, should be removed.
81 call void @llvm.lifetime.end.p0i8(i64 1, i8* %P)
86 define void @test3a(i8* %P, i8 %X) {
87 ; CHECK-LABEL: @test3a(
88 ; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 10, i8* [[P:%.*]])
89 ; CHECK-NEXT: ret void
91 %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
93 %P2 = getelementptr i8, i8* %P, i32 2
95 call void @llvm.lifetime.end.p0i8(i64 10, i8* %P)
99 @G1 = external global i32
100 @G2 = external global [4000 x i32]
102 define i32 @test4(i8* %P) {
103 ; CHECK-LABEL: @test4(
104 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8 0, i32 4000, i1 false)
105 ; CHECK-NEXT: ret i32 0
107 %tmp = load i32, i32* @G1
108 call void @llvm.memset.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8 0, i32 4000, i1 false)
109 %tmp2 = load i32, i32* @G1
110 %sub = sub i32 %tmp2, %tmp
114 ; Verify that basicaa is handling variable length memcpy, knowing it doesn't
116 define i32 @test5(i8* %P, i32 %Len) {
117 ; CHECK-LABEL: @test5(
118 ; CHECK-NEXT: 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)
119 ; CHECK-NEXT: ret i32 0
121 %tmp = load i32, i32* @G1
122 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)
123 %tmp2 = load i32, i32* @G1
124 %sub = sub i32 %tmp2, %tmp
128 define i8 @test6(i8* %p, i8* noalias %a) {
129 ; CHECK-LABEL: @test6(
130 ; CHECK-NEXT: [[X:%.*]] = load i8, i8* [[A:%.*]], align 1
131 ; CHECK-NEXT: [[T:%.*]] = va_arg i8* [[P:%.*]], float
132 ; CHECK-NEXT: [[Z:%.*]] = add i8 [[X]], [[X]]
133 ; CHECK-NEXT: ret i8 [[Z]]
136 %t = va_arg i8* %p, float
143 declare void @test7decl(i32* nocapture %x)
144 define i32 @test7() nounwind uwtable ssp {
145 ; CHECK-LABEL: @test7(
147 ; CHECK-NEXT: [[X:%.*]] = alloca i32, align 4
148 ; CHECK-NEXT: store i32 0, i32* [[X]], align 4
149 ; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i32, i32* [[X]], i64 1
150 ; CHECK-NEXT: call void @test7decl(i32* [[ADD_PTR]])
151 ; CHECK-NEXT: [[TMP:%.*]] = load i32, i32* [[X]], align 4
152 ; CHECK-NEXT: ret i32 [[TMP]]
155 %x = alloca i32, align 4
156 store i32 0, i32* %x, align 4
157 %add.ptr = getelementptr inbounds i32, i32* %x, i64 1
158 call void @test7decl(i32* %add.ptr)
159 %tmp = load i32, i32* %x, align 4
163 ;; Check that aa correctly handles functions marked with argmemonly
165 declare i32 @func_argmemonly(i32 * %P) argmemonly
167 ;; Can not remove redundant load, function may write to it.
169 define i32 @test8(i32 *%P) {
170 ; CHECK-LABEL: @test8(
171 ; CHECK-NEXT: [[V1:%.*]] = load i32, i32* [[P:%.*]], align 4
172 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly(i32* [[P]])
173 ; CHECK-NEXT: [[V2:%.*]] = load i32, i32* [[P]], align 4
174 ; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[V1]], [[V2]]
175 ; CHECK-NEXT: ret i32 [[DIFF]]
177 %V1 = load i32, i32* %P
178 call i32 @func_argmemonly(i32* %P)
179 %V2 = load i32, i32* %P
180 %Diff = sub i32 %V1, %V2
184 ;; In this case load can be removed, function clobbers only %P2.
185 define i32 @test9(i32* %P, i32* noalias %P2) {
186 ; CHECK-LABEL: @test9(
187 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly(i32* [[P2:%.*]])
188 ; CHECK-NEXT: ret i32 0
190 %V1 = load i32, i32* %P
191 call i32 @func_argmemonly(i32* %P2)
192 %V2 = load i32, i32* %P
193 %Diff = sub i32 %V1, %V2
197 ;; In this case load can *not* be removed. Function clobers only %P2 but it may
199 define i32 @test10(i32* %P, i32* %P2) {
200 ; CHECK-LABEL: @test10(
201 ; CHECK-NEXT: [[V1:%.*]] = load i32, i32* [[P:%.*]], align 4
202 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly(i32* [[P2:%.*]])
203 ; CHECK-NEXT: [[V2:%.*]] = load i32, i32* [[P]], align 4
204 ; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[V1]], [[V2]]
205 ; CHECK-NEXT: ret i32 [[DIFF]]
207 %V1 = load i32, i32* %P
208 call i32 @func_argmemonly(i32* %P2)
209 %V2 = load i32, i32* %P
210 %Diff = sub i32 %V1, %V2
214 define i32 @test11(i32* %P, i32* %P2) {
215 ; CHECK-LABEL: @test11(
216 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly(i32* readonly [[P2:%.*]])
217 ; CHECK-NEXT: ret i32 0
219 %V1 = load i32, i32* %P
220 call i32 @func_argmemonly(i32* readonly %P2)
221 %V2 = load i32, i32* %P
222 %Diff = sub i32 %V1, %V2
227 declare i32 @func_argmemonly_two_args(i32* %P, i32* %P2) argmemonly
229 define i32 @test12(i32* %P, i32* %P2, i32* %P3) {
230 ; CHECK-LABEL: @test12(
231 ; CHECK-NEXT: [[V1:%.*]] = load i32, i32* [[P:%.*]], align 4
232 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly_two_args(i32* readonly [[P2:%.*]], i32* [[P3:%.*]])
233 ; CHECK-NEXT: [[V2:%.*]] = load i32, i32* [[P]], align 4
234 ; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[V1]], [[V2]]
235 ; CHECK-NEXT: ret i32 [[DIFF]]
237 %V1 = load i32, i32* %P
238 call i32 @func_argmemonly_two_args(i32* readonly %P2, i32* %P3)
239 %V2 = load i32, i32* %P
240 %Diff = sub i32 %V1, %V2
244 define i32 @test13(i32* %P, i32* %P2) {
245 ; CHECK-LABEL: @test13(
246 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @func_argmemonly(i32* readnone [[P2:%.*]])
247 ; CHECK-NEXT: ret i32 0
249 %V1 = load i32, i32* %P
250 call i32 @func_argmemonly(i32* readnone %P2)
251 %V2 = load i32, i32* %P
252 %Diff = sub i32 %V1, %V2
256 declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i1) nounwind
257 declare void @llvm.memset.p0i8.i8(i8* nocapture, i8, i8, i1) nounwind
258 declare void @llvm.memcpy.p0i8.p0i8.i8(i8* nocapture, i8* nocapture, i8, i1) nounwind
259 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind