1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=dse -S < %s | FileCheck %s
4 declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
5 declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
7 declare void @unknown()
9 declare void @f2(ptr, ptr)
10 declare ptr @f3(ptr, ptr)
12 ; Basic case for DSEing a trivially dead writing call
13 define void @test_dead() {
14 ; CHECK-LABEL: @test_dead(
15 ; CHECK-NEXT: ret void
17 %a = alloca i32, align 4
18 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
22 ; Add in canonical lifetime intrinsics
23 define void @test_lifetime() {
24 ; CHECK-LABEL: @test_lifetime(
25 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
26 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[A]])
27 ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr [[A]])
28 ; CHECK-NEXT: ret void
30 %a = alloca i32, align 4
31 call void @llvm.lifetime.start.p0(i64 4, ptr %a)
32 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
33 call void @llvm.lifetime.end.p0(i64 4, ptr %a)
37 ; Add some unknown calls just to point out that this is use based, not
38 ; instruction order sensitive
39 define void @test_lifetime2() {
40 ; CHECK-LABEL: @test_lifetime2(
41 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
42 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[A]])
43 ; CHECK-NEXT: call void @unknown()
44 ; CHECK-NEXT: call void @unknown()
45 ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr [[A]])
46 ; CHECK-NEXT: ret void
48 %a = alloca i32, align 4
49 call void @llvm.lifetime.start.p0(i64 4, ptr %a)
51 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
53 call void @llvm.lifetime.end.p0(i64 4, ptr %a)
57 ; As long as the result is unused, we can even remove reads of the alloca
58 ; itself since the write will be dropped.
59 define void @test_dead_readwrite() {
60 ; CHECK-LABEL: @test_dead_readwrite(
61 ; CHECK-NEXT: ret void
63 %a = alloca i32, align 4
64 call void @f(ptr nocapture %a) argmemonly nounwind willreturn
68 define i32 @test_neg_read_after() {
69 ; CHECK-LABEL: @test_neg_read_after(
70 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
71 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR1:[0-9]+]]
72 ; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[A]], align 4
73 ; CHECK-NEXT: ret i32 [[RES]]
75 %a = alloca i32, align 4
76 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
77 %res = load i32, ptr %a
82 define void @test_neg_infinite_loop() {
83 ; CHECK-LABEL: @test_neg_infinite_loop(
84 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
85 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR2:[0-9]+]]
86 ; CHECK-NEXT: ret void
88 %a = alloca i32, align 4
89 call void @f(ptr writeonly nocapture %a) argmemonly nounwind
93 define void @test_neg_throw() {
94 ; CHECK-LABEL: @test_neg_throw(
95 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
96 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR3:[0-9]+]]
97 ; CHECK-NEXT: ret void
99 %a = alloca i32, align 4
100 call void @f(ptr writeonly nocapture %a) argmemonly willreturn
104 define void @test_neg_extra_write() {
105 ; CHECK-LABEL: @test_neg_extra_write(
106 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
107 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR4:[0-9]+]]
108 ; CHECK-NEXT: ret void
110 %a = alloca i32, align 4
111 call void @f(ptr writeonly nocapture %a) nounwind willreturn
115 ; In this case, we can't remove a1 because we need to preserve the write to
116 ; a2, and if we leave the call around, we need memory to pass to the first arg.
117 define void @test_neg_unmodeled_write() {
118 ; CHECK-LABEL: @test_neg_unmodeled_write(
119 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
120 ; CHECK-NEXT: [[A2:%.*]] = alloca i32, align 4
121 ; CHECK-NEXT: call void @f2(ptr nocapture writeonly [[A]], ptr [[A2]]) #[[ATTR1]]
122 ; CHECK-NEXT: ret void
124 %a = alloca i32, align 4
125 %a2 = alloca i32, align 4
126 call void @f2(ptr nocapture writeonly %a, ptr %a2) argmemonly nounwind willreturn
130 define i32 @test_neg_captured_by_call() {
131 ; CHECK-LABEL: @test_neg_captured_by_call(
132 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
133 ; CHECK-NEXT: [[A2:%.*]] = alloca ptr, align 4
134 ; CHECK-NEXT: call void @f2(ptr writeonly [[A]], ptr [[A2]]) #[[ATTR1]]
135 ; CHECK-NEXT: [[A_COPY_CAST:%.*]] = load ptr, ptr [[A2]], align 8
136 ; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[A_COPY_CAST]], align 4
137 ; CHECK-NEXT: ret i32 [[RES]]
139 %a = alloca i32, align 4
140 %a2 = alloca ptr, align 4
141 call void @f2(ptr writeonly %a, ptr %a2) argmemonly nounwind willreturn
142 %a_copy_cast = load ptr, ptr %a2
143 %res = load i32, ptr %a_copy_cast
147 define i32 @test_neg_captured_before() {
148 ; CHECK-LABEL: @test_neg_captured_before(
149 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
150 ; CHECK-NEXT: [[A2:%.*]] = alloca ptr, align 4
151 ; CHECK-NEXT: store ptr [[A]], ptr [[A2]], align 8
152 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR1]]
153 ; CHECK-NEXT: [[A_COPY_CAST:%.*]] = load ptr, ptr [[A2]], align 8
154 ; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[A_COPY_CAST]], align 4
155 ; CHECK-NEXT: ret i32 [[RES]]
157 %a = alloca i32, align 4
158 %a2 = alloca ptr, align 4
159 store ptr %a, ptr %a2
160 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
161 %a_copy_cast = load ptr, ptr %a2
162 %res = load i32, ptr %a_copy_cast
166 ; Callee might be dead, but op bundle has unknown semantics and thus isn't.
167 define void @test_new_op_bundle() {
168 ; CHECK-LABEL: @test_new_op_bundle(
169 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
170 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR1]] [ "unknown"(ptr [[A]]) ]
171 ; CHECK-NEXT: ret void
173 %a = alloca i32, align 4
174 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn ["unknown" (ptr %a)]
178 ; Show that reading from unrelated memory is okay
179 define void @test_unreleated_read() {
180 ; CHECK-LABEL: @test_unreleated_read(
181 ; CHECK-NEXT: ret void
183 %a = alloca i32, align 4
184 %a2 = alloca i32, align 4
185 call void @f2(ptr nocapture writeonly %a, ptr nocapture readonly %a2) argmemonly nounwind willreturn
189 ; Removing a capture is also okay. The capture can only be in the return value
190 ; (which is unused) or written into the dead out parameter.
191 define void @test_unrelated_capture() {
192 ; CHECK-LABEL: @test_unrelated_capture(
193 ; CHECK-NEXT: ret void
195 %a = alloca i32, align 4
196 %a2 = alloca i32, align 4
197 call ptr @f3(ptr nocapture writeonly %a, ptr readonly %a2) argmemonly nounwind willreturn
201 ; Cannot remove call, as %a2 is captured via the return value.
202 define i8 @test_neg_unrelated_capture_used_via_return() {
203 ; CHECK-LABEL: @test_neg_unrelated_capture_used_via_return(
204 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
205 ; CHECK-NEXT: [[A2:%.*]] = alloca i32, align 4
206 ; CHECK-NEXT: [[CAPTURE:%.*]] = call ptr @f3(ptr nocapture writeonly [[A]], ptr readonly [[A2]]) #[[ATTR1]]
207 ; CHECK-NEXT: [[V:%.*]] = load i8, ptr [[CAPTURE]], align 1
208 ; CHECK-NEXT: ret i8 [[V]]
210 %a = alloca i32, align 4
211 %a2 = alloca i32, align 4
212 %capture = call ptr @f3(ptr nocapture writeonly %a, ptr readonly %a2) argmemonly nounwind willreturn
213 %v = load i8, ptr %capture
217 ; As long as the result is unused, we can even remove reads of the alloca
218 ; itself since the write will be dropped.
219 define void @test_self_read() {
220 ; CHECK-LABEL: @test_self_read(
221 ; CHECK-NEXT: ret void
223 %a = alloca i32, align 4
224 call void @f2(ptr nocapture writeonly %a, ptr nocapture readonly %a) argmemonly nounwind willreturn
228 ; We can remove the call because while we don't know the size of the write done
229 ; by the call, we do know the following store writes to the entire contents of
231 define i32 @test_dse_overwrite() {
232 ; CHECK-LABEL: @test_dse_overwrite(
233 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
234 ; CHECK-NEXT: store i32 0, ptr [[A]], align 4
235 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[A]], align 4
236 ; CHECK-NEXT: ret i32 [[V]]
238 %a = alloca i32, align 4
239 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
241 %v = load i32, ptr %a
245 ; Negative case where we can read part of the value written by @f.
246 define i32 @test_neg_dse_partial_overwrite() {
247 ; CHECK-LABEL: @test_neg_dse_partial_overwrite(
248 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
249 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A]]) #[[ATTR1]]
250 ; CHECK-NEXT: store i8 0, ptr [[A]], align 1
251 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[A]], align 4
252 ; CHECK-NEXT: ret i32 [[V]]
254 %a = alloca i32, align 4
255 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
257 %v = load i32, ptr %a
261 ; Negative case where we don't know the size of a, and thus can't use the
262 ; full overwrite reasoning
263 define i32 @test_neg_dse_unsized(ptr %a) {
264 ; CHECK-LABEL: @test_neg_dse_unsized(
265 ; CHECK-NEXT: call void @f(ptr nocapture writeonly [[A:%.*]]) #[[ATTR1]]
266 ; CHECK-NEXT: store i32 0, ptr [[A]], align 4
267 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[A]], align 4
268 ; CHECK-NEXT: ret i32 [[V]]
270 call void @f(ptr writeonly nocapture %a) argmemonly nounwind willreturn
272 %v = load i32, ptr %a
278 ; Same as test_dse_overwrite, but with a non-alloca object.
279 define void @test_dse_non_alloca() {
280 ; CHECK-LABEL: @test_dse_non_alloca(
281 ; CHECK-NEXT: store i8 0, ptr @G, align 1
282 ; CHECK-NEXT: ret void
284 call void @f(ptr writeonly nocapture @G) argmemonly nounwind willreturn