1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -early-cse -earlycse-debug-hash | FileCheck %s
3 ; RUN: opt < %s -S -basic-aa -early-cse-memssa | FileCheck %s
4 ; RUN: opt < %s -S -passes=early-cse | FileCheck %s
6 declare void @llvm.assume(i1) nounwind
8 define void @test1(i8 %V, ptr%P) {
10 ; CHECK-NEXT: store i32 23, ptr [[P:%.*]], align 4
11 ; CHECK-NEXT: [[C:%.*]] = zext i8 [[V:%.*]] to i32
12 ; CHECK-NEXT: store volatile i32 [[C]], ptr [[P]], align 4
13 ; CHECK-NEXT: store volatile i32 [[C]], ptr [[P]], align 4
14 ; CHECK-NEXT: [[E:%.*]] = add i32 [[C]], [[C]]
15 ; CHECK-NEXT: store volatile i32 [[E]], ptr [[P]], align 4
16 ; CHECK-NEXT: store volatile i32 [[E]], ptr [[P]], align 4
17 ; CHECK-NEXT: store volatile i32 [[E]], ptr [[P]], align 4
18 ; CHECK-NEXT: ret void
20 %A = bitcast i64 42 to double ;; dead
21 %B = add i32 4, 19 ;; constant folds
24 %C = zext i8 %V to i32
25 %D = zext i8 %V to i32 ;; CSE
26 store volatile i32 %C, ptr %P
27 store volatile i32 %D, ptr %P
31 store volatile i32 %E, ptr %P
32 store volatile i32 %F, ptr %P
34 %G = add nuw i32 %C, %C
35 store volatile i32 %G, ptr %P
40 ;; Simple load value numbering.
41 define i32 @test2(ptr%P) {
42 ; CHECK-LABEL: @test2(
43 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
44 ; CHECK-NEXT: ret i32 0
46 %V1 = load i32, ptr %P
47 %V2 = load i32, ptr %P
48 %Diff = sub i32 %V1, %V2
52 define i32 @test2a(ptr%P, i1 %b) {
53 ; CHECK-LABEL: @test2a(
54 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
55 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[B:%.*]])
56 ; CHECK-NEXT: ret i32 0
58 %V1 = load i32, ptr %P
59 tail call void @llvm.assume(i1 %b)
60 %V2 = load i32, ptr %P
61 %Diff = sub i32 %V1, %V2
65 ;; Cross block load value numbering.
66 define i32 @test3(ptr%P, i1 %Cond) {
67 ; CHECK-LABEL: @test3(
68 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
69 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[T:%.*]], label [[F:%.*]]
71 ; CHECK-NEXT: store i32 4, ptr [[P]], align 4
72 ; CHECK-NEXT: ret i32 42
74 ; CHECK-NEXT: ret i32 0
76 %V1 = load i32, ptr %P
77 br i1 %Cond, label %T, label %F
82 %V2 = load i32, ptr %P
83 %Diff = sub i32 %V1, %V2
87 define i32 @test3a(ptr%P, i1 %Cond, i1 %b) {
88 ; CHECK-LABEL: @test3a(
89 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
90 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[T:%.*]], label [[F:%.*]]
92 ; CHECK-NEXT: store i32 4, ptr [[P]], align 4
93 ; CHECK-NEXT: ret i32 42
95 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[B:%.*]])
96 ; CHECK-NEXT: ret i32 0
98 %V1 = load i32, ptr %P
99 br i1 %Cond, label %T, label %F
104 tail call void @llvm.assume(i1 %b)
105 %V2 = load i32, ptr %P
106 %Diff = sub i32 %V1, %V2
110 ;; Cross block load value numbering stops when stores happen.
111 define i32 @test4(ptr%P, i1 %Cond) {
112 ; CHECK-LABEL: @test4(
113 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
114 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[T:%.*]], label [[F:%.*]]
116 ; CHECK-NEXT: ret i32 42
118 ; CHECK-NEXT: store i32 42, ptr [[P]], align 4
119 ; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[V1]], 42
120 ; CHECK-NEXT: ret i32 [[DIFF]]
122 %V1 = load i32, ptr %P
123 br i1 %Cond, label %T, label %F
130 %V2 = load i32, ptr %P
131 %Diff = sub i32 %V1, %V2
135 declare i32 @func(ptr%P) readonly
137 ;; Simple call CSE'ing.
138 define i32 @test5(ptr%P) {
139 ; CHECK-LABEL: @test5(
140 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]])
141 ; CHECK-NEXT: ret i32 0
143 %V1 = call i32 @func(ptr %P)
144 %V2 = call i32 @func(ptr %P)
145 %Diff = sub i32 %V1, %V2
149 ;; Trivial Store->load forwarding
150 define i32 @test6(ptr%P) {
151 ; CHECK-LABEL: @test6(
152 ; CHECK-NEXT: store i32 42, ptr [[P:%.*]], align 4
153 ; CHECK-NEXT: ret i32 42
156 %V1 = load i32, ptr %P
160 define i32 @test6a(ptr%P, i1 %b) {
161 ; CHECK-LABEL: @test6a(
162 ; CHECK-NEXT: store i32 42, ptr [[P:%.*]], align 4
163 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[B:%.*]])
164 ; CHECK-NEXT: ret i32 42
167 tail call void @llvm.assume(i1 %b)
168 %V1 = load i32, ptr %P
172 ;; Trivial dead store elimination.
173 define void @test7(ptr%P) {
174 ; CHECK-LABEL: @test7(
175 ; CHECK-NEXT: store i32 45, ptr [[P:%.*]], align 4
176 ; CHECK-NEXT: ret void
183 ;; Readnone functions aren't invalidated by stores.
184 define i32 @test8(ptr%P) {
185 ; CHECK-LABEL: @test8(
186 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR2:[0-9]+]]
187 ; CHECK-NEXT: store i32 4, ptr [[P]], align 4
188 ; CHECK-NEXT: ret i32 0
190 %V1 = call i32 @func(ptr %P) readnone
192 %V2 = call i32 @func(ptr %P) readnone
193 %Diff = sub i32 %V1, %V2
197 ;; Trivial DSE can't be performed across a readonly call. The call
198 ;; can observe the earlier write.
199 define i32 @test9(ptr%P) {
200 ; CHECK-LABEL: @test9(
201 ; CHECK-NEXT: store i32 4, ptr [[P:%.*]], align 4
202 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P]]) #[[ATTR1:[0-9]+]]
203 ; CHECK-NEXT: store i32 5, ptr [[P]], align 4
204 ; CHECK-NEXT: ret i32 [[V1]]
207 %V1 = call i32 @func(ptr %P) readonly
212 ;; Trivial DSE can be performed across a readnone call.
213 define i32 @test10(ptr%P) {
214 ; CHECK-LABEL: @test10(
215 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR2]]
216 ; CHECK-NEXT: store i32 5, ptr [[P]], align 4
217 ; CHECK-NEXT: ret i32 [[V1]]
220 %V1 = call i32 @func(ptr %P) readnone
225 ;; Trivial dead store elimination - should work for an entire series of dead stores too.
226 define void @test11(ptr%P) {
227 ; CHECK-LABEL: @test11(
228 ; CHECK-NEXT: store i32 45, ptr [[P:%.*]], align 4
229 ; CHECK-NEXT: ret void
238 define i32 @test12(i1 %B, ptr %P1, ptr %P2) {
239 ; CHECK-LABEL: @test12(
240 ; CHECK-NEXT: [[LOAD0:%.*]] = load i32, ptr [[P1:%.*]], align 4
241 ; CHECK-NEXT: [[TMP1:%.*]] = load atomic i32, ptr [[P2:%.*]] seq_cst, align 4
242 ; CHECK-NEXT: [[LOAD1:%.*]] = load i32, ptr [[P1]], align 4
243 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i32 [[LOAD0]], i32 [[LOAD1]]
244 ; CHECK-NEXT: ret i32 [[SEL]]
246 %load0 = load i32, ptr %P1
247 %1 = load atomic i32, ptr %P2 seq_cst, align 4
248 %load1 = load i32, ptr %P1
249 %sel = select i1 %B, i32 %load0, i32 %load1
253 define void @dse1(ptr%P) {
254 ; CHECK-LABEL: @dse1(
255 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
256 ; CHECK-NEXT: ret void
258 %v = load i32, ptr %P
263 define void @dse2(ptr%P) {
264 ; CHECK-LABEL: @dse2(
265 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] seq_cst, align 4
266 ; CHECK-NEXT: ret void
268 %v = load atomic i32, ptr %P seq_cst, align 4
273 define void @dse3(ptr%P) {
274 ; CHECK-LABEL: @dse3(
275 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] seq_cst, align 4
276 ; CHECK-NEXT: ret void
278 %v = load atomic i32, ptr %P seq_cst, align 4
279 store atomic i32 %v, ptr %P unordered, align 4
283 define i32 @dse4(ptr%P, ptr%Q) {
284 ; CHECK-LABEL: @dse4(
285 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[Q:%.*]], align 4
286 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] unordered, align 4
287 ; CHECK-NEXT: ret i32 0
289 %a = load i32, ptr %Q
290 %v = load atomic i32, ptr %P unordered, align 4
291 store atomic i32 %v, ptr %P unordered, align 4
292 %b = load i32, ptr %Q
293 %res = sub i32 %a, %b
297 ; Note that in this example, %P and %Q could in fact be the same
298 ; pointer. %v could be different than the value observed for %a
299 ; and that's okay because we're using relaxed memory ordering.
300 ; The only guarantee we have to provide is that each of the loads
301 ; has to observe some value written to that location. We do
302 ; not have to respect the order in which those writes were done.
303 define i32 @dse5(ptr%P, ptr%Q) {
304 ; CHECK-LABEL: @dse5(
305 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] unordered, align 4
306 ; CHECK-NEXT: [[A:%.*]] = load atomic i32, ptr [[Q:%.*]] unordered, align 4
307 ; CHECK-NEXT: ret i32 0
309 %v = load atomic i32, ptr %P unordered, align 4
310 %a = load atomic i32, ptr %Q unordered, align 4
311 store atomic i32 %v, ptr %P unordered, align 4
312 %b = load atomic i32, ptr %Q unordered, align 4
313 %res = sub i32 %a, %b
318 define void @dse_neg1(ptr%P) {
319 ; CHECK-LABEL: @dse_neg1(
320 ; CHECK-NEXT: store i32 5, ptr [[P:%.*]], align 4
321 ; CHECK-NEXT: ret void
323 %v = load i32, ptr %P
328 ; Could remove the store, but only if ordering was somehow
330 define void @dse_neg2(ptr%P) {
331 ; CHECK-LABEL: @dse_neg2(
332 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
333 ; CHECK-NEXT: store atomic i32 [[V]], ptr [[P]] seq_cst, align 4
334 ; CHECK-NEXT: ret void
336 %v = load i32, ptr %P
337 store atomic i32 %v, ptr %P seq_cst, align 4
341 @c = external global i32, align 4
342 declare i32 @reads_c(i32 returned)
343 define void @pr28763() {
344 ; CHECK-LABEL: @pr28763(
346 ; CHECK-NEXT: store i32 0, ptr @c, align 4
347 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @reads_c(i32 0)
348 ; CHECK-NEXT: store i32 2, ptr @c, align 4
349 ; CHECK-NEXT: ret void
352 %load = load i32, ptr @c, align 4
353 store i32 0, ptr @c, align 4
354 %call = call i32 @reads_c(i32 0)
355 store i32 2, ptr @c, align 4
359 define i1 @cse_freeze(i1 %a) {
360 ; CHECK-LABEL: @cse_freeze(
362 ; CHECK-NEXT: [[B:%.*]] = freeze i1 [[A:%.*]]
363 ; CHECK-NEXT: ret i1 [[B]]