1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -passes=early-cse -earlycse-debug-hash | FileCheck %s
3 ; RUN: opt < %s -S -passes='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:%.*]]), !prof !0
141 ; CHECK-NEXT: ret i32 0
143 %V1 = call i32 @func(ptr %P), !prof !0
144 %V2 = call i32 @func(ptr %P), !prof !1
145 %Diff = sub i32 %V1, %V2
149 !0 = !{!"branch_weights", i32 95}
150 !1 = !{!"branch_weights", i32 95}
152 ;; Trivial Store->load forwarding
153 define i32 @test6(ptr%P) {
154 ; CHECK-LABEL: @test6(
155 ; CHECK-NEXT: store i32 42, ptr [[P:%.*]], align 4
156 ; CHECK-NEXT: ret i32 42
159 %V1 = load i32, ptr %P
163 define i32 @test6a(ptr%P, i1 %b) {
164 ; CHECK-LABEL: @test6a(
165 ; CHECK-NEXT: store i32 42, ptr [[P:%.*]], align 4
166 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[B:%.*]])
167 ; CHECK-NEXT: ret i32 42
170 tail call void @llvm.assume(i1 %b)
171 %V1 = load i32, ptr %P
175 ;; Trivial dead store elimination.
176 define void @test7(ptr%P) {
177 ; CHECK-LABEL: @test7(
178 ; CHECK-NEXT: store i32 45, ptr [[P:%.*]], align 4
179 ; CHECK-NEXT: ret void
186 ;; Readnone functions aren't invalidated by stores.
187 define i32 @test8(ptr%P) {
188 ; CHECK-LABEL: @test8(
189 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR2:[0-9]+]]
190 ; CHECK-NEXT: store i32 4, ptr [[P]], align 4
191 ; CHECK-NEXT: ret i32 0
193 %V1 = call i32 @func(ptr %P) readnone
195 %V2 = call i32 @func(ptr %P) readnone
196 %Diff = sub i32 %V1, %V2
200 ;; Trivial DSE can't be performed across a readonly call. The call
201 ;; can observe the earlier write.
202 define i32 @test9(ptr%P) {
203 ; CHECK-LABEL: @test9(
204 ; CHECK-NEXT: store i32 4, ptr [[P:%.*]], align 4
205 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P]]) #[[ATTR1:[0-9]+]]
206 ; CHECK-NEXT: store i32 5, ptr [[P]], align 4
207 ; CHECK-NEXT: ret i32 [[V1]]
210 %V1 = call i32 @func(ptr %P) readonly
215 ;; Trivial DSE can be performed across a readnone call.
216 define i32 @test10(ptr%P) {
217 ; CHECK-LABEL: @test10(
218 ; CHECK-NEXT: [[V1:%.*]] = call i32 @func(ptr [[P:%.*]]) #[[ATTR2]]
219 ; CHECK-NEXT: store i32 5, ptr [[P]], align 4
220 ; CHECK-NEXT: ret i32 [[V1]]
223 %V1 = call i32 @func(ptr %P) readnone
228 ;; Trivial dead store elimination - should work for an entire series of dead stores too.
229 define void @test11(ptr%P) {
230 ; CHECK-LABEL: @test11(
231 ; CHECK-NEXT: store i32 45, ptr [[P:%.*]], align 4
232 ; CHECK-NEXT: ret void
241 define i32 @test12(i1 %B, ptr %P1, ptr %P2) {
242 ; CHECK-LABEL: @test12(
243 ; CHECK-NEXT: [[LOAD0:%.*]] = load i32, ptr [[P1:%.*]], align 4
244 ; CHECK-NEXT: [[TMP1:%.*]] = load atomic i32, ptr [[P2:%.*]] seq_cst, align 4
245 ; CHECK-NEXT: [[LOAD1:%.*]] = load i32, ptr [[P1]], align 4
246 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i32 [[LOAD0]], i32 [[LOAD1]]
247 ; CHECK-NEXT: ret i32 [[SEL]]
249 %load0 = load i32, ptr %P1
250 %1 = load atomic i32, ptr %P2 seq_cst, align 4
251 %load1 = load i32, ptr %P1
252 %sel = select i1 %B, i32 %load0, i32 %load1
256 define void @dse1(ptr%P) {
257 ; CHECK-LABEL: @dse1(
258 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
259 ; CHECK-NEXT: ret void
261 %v = load i32, ptr %P
266 define void @dse2(ptr%P) {
267 ; CHECK-LABEL: @dse2(
268 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] seq_cst, align 4
269 ; CHECK-NEXT: ret void
271 %v = load atomic i32, ptr %P seq_cst, align 4
276 define void @dse3(ptr%P) {
277 ; CHECK-LABEL: @dse3(
278 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] seq_cst, align 4
279 ; CHECK-NEXT: ret void
281 %v = load atomic i32, ptr %P seq_cst, align 4
282 store atomic i32 %v, ptr %P unordered, align 4
286 define i32 @dse4(ptr%P, ptr%Q) {
287 ; CHECK-LABEL: @dse4(
288 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[Q:%.*]], align 4
289 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] unordered, align 4
290 ; CHECK-NEXT: ret i32 0
292 %a = load i32, ptr %Q
293 %v = load atomic i32, ptr %P unordered, align 4
294 store atomic i32 %v, ptr %P unordered, align 4
295 %b = load i32, ptr %Q
296 %res = sub i32 %a, %b
300 ; Note that in this example, %P and %Q could in fact be the same
301 ; pointer. %v could be different than the value observed for %a
302 ; and that's okay because we're using relaxed memory ordering.
303 ; The only guarantee we have to provide is that each of the loads
304 ; has to observe some value written to that location. We do
305 ; not have to respect the order in which those writes were done.
306 define i32 @dse5(ptr%P, ptr%Q) {
307 ; CHECK-LABEL: @dse5(
308 ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[P:%.*]] unordered, align 4
309 ; CHECK-NEXT: [[A:%.*]] = load atomic i32, ptr [[Q:%.*]] unordered, align 4
310 ; CHECK-NEXT: ret i32 0
312 %v = load atomic i32, ptr %P unordered, align 4
313 %a = load atomic i32, ptr %Q unordered, align 4
314 store atomic i32 %v, ptr %P unordered, align 4
315 %b = load atomic i32, ptr %Q unordered, align 4
316 %res = sub i32 %a, %b
321 define void @dse_neg1(ptr%P) {
322 ; CHECK-LABEL: @dse_neg1(
323 ; CHECK-NEXT: store i32 5, ptr [[P:%.*]], align 4
324 ; CHECK-NEXT: ret void
326 %v = load i32, ptr %P
331 ; Could remove the store, but only if ordering was somehow
333 define void @dse_neg2(ptr%P) {
334 ; CHECK-LABEL: @dse_neg2(
335 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
336 ; CHECK-NEXT: store atomic i32 [[V]], ptr [[P]] seq_cst, align 4
337 ; CHECK-NEXT: ret void
339 %v = load i32, ptr %P
340 store atomic i32 %v, ptr %P seq_cst, align 4
344 @c = external global i32, align 4
345 declare i32 @reads_c(i32 returned)
346 define void @pr28763() {
347 ; CHECK-LABEL: @pr28763(
349 ; CHECK-NEXT: store i32 0, ptr @c, align 4
350 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @reads_c(i32 0)
351 ; CHECK-NEXT: store i32 2, ptr @c, align 4
352 ; CHECK-NEXT: ret void
355 %load = load i32, ptr @c, align 4
356 store i32 0, ptr @c, align 4
357 %call = call i32 @reads_c(i32 0)
358 store i32 2, ptr @c, align 4
362 define i1 @cse_freeze(i1 %a) {
363 ; CHECK-LABEL: @cse_freeze(
365 ; CHECK-NEXT: [[B:%.*]] = freeze i1 [[A:%.*]]
366 ; CHECK-NEXT: ret i1 [[B]]