1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -basic-aa -gvn -dse -S | FileCheck %s
3target 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
5declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
6
7declare void @external(i32*)
8
9define 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
15;
16  %A = alloca i32
17  call void @external(i32* %A)
18
19  store i32 0, i32* %A
20
21  call void @llvm.memset.p0i8.i32(i8* %P, i8 0, i32 42, i1 false)
22
23  %B = load i32, i32* %A
24  ret i32 %B
25}
26
27define i8 @test1() {
28; CHECK-LABEL: @test1(
29; CHECK-NEXT:    ret i8 2
30;
31  %A = alloca i8
32  %B = alloca i8
33
34  store i8 2, i8* %B  ;; Not written to by memcpy
35
36  call void @llvm.memcpy.p0i8.p0i8.i8(i8* %A, i8* %B, i8 -1, i1 false)
37
38  %C = load i8, i8* %B
39  ret i8 %C
40}
41
42define 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
48;
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)
52  %A = load i8, i8* %P2
53  ret i8 %A
54}
55
56define 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
60;
61  %P2 = getelementptr i8, i8* %P, i32 126
62
63  store i8 1, i8* %P2  ;; Dead, clobbered by memset.
64
65  call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i1 false)
66  %A = load i8, i8* %P2
67  ret i8 %A
68}
69
70define 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
76;
77  %Y = add i8 %X, 1     ;; Dead, because the only use (the store) is dead.
78
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)
82  store i8 2, i8* %P2
83  ret void
84}
85
86define 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
90;
91  %Y = add i8 %X, 1     ;; Dead, because the only use (the store) is dead.
92
93  %P2 = getelementptr i8, i8* %P, i32 2
94  store i8 %Y, i8* %P2
95  call void @llvm.lifetime.end.p0i8(i64 10, i8* %P)
96  ret void
97}
98
99@G1 = external global i32
100@G2 = external global [4000 x i32]
101
102define 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
106;
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
111  ret i32 %sub
112}
113
114; Verify that basicaa is handling variable length memcpy, knowing it doesn't
115; write to G1.
116define 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
120;
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
125  ret i32 %sub
126}
127
128define 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]]
134;
135  %x = load i8, i8* %a
136  %t = va_arg i8* %p, float
137  %y = load i8, i8* %a
138  %z = add i8 %x, %y
139  ret i8 %z
140}
141
142; PR10628
143declare void @test7decl(i32* nocapture %x)
144define i32 @test7() nounwind uwtable ssp {
145; CHECK-LABEL: @test7(
146; CHECK-NEXT:  entry:
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]]
153;
154entry:
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
160  ret i32 %tmp
161}
162
163;; Check that aa correctly handles functions marked with argmemonly
164;; attribute.
165declare i32 @func_argmemonly(i32 * %P) argmemonly
166
167;; Can not remove redundant load, function may write to it.
168
169define 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]]
176;
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
181  ret i32 %Diff
182}
183
184;; In this case load can be removed, function clobbers only %P2.
185define 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
189;
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
194  ret i32 %Diff
195}
196
197;; In this case load can *not* be removed. Function clobers only %P2 but it may
198;; alias with %P.
199define 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]]
206;
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
211  ret i32 %Diff
212}
213
214define 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
218;
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
223  ret i32 %Diff
224
225}
226
227declare i32 @func_argmemonly_two_args(i32* %P, i32* %P2) argmemonly
228
229define 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]]
236;
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
241  ret i32 %Diff
242}
243
244define 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
248;
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
253  ret i32 %Diff
254}
255
256declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i1) nounwind
257declare void @llvm.memset.p0i8.i8(i8* nocapture, i8, i8, i1) nounwind
258declare void @llvm.memcpy.p0i8.p0i8.i8(i8* nocapture, i8* nocapture, i8, i1) nounwind
259declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
260