1 ; RUN: opt < %s -S -early-cse | FileCheck %s
3 declare void @llvm.assume(i1) nounwind
6 define void @test1(i8 %V, i32 *%P) {
7 %A = bitcast i64 42 to double ;; dead
8 %B = add i32 4, 19 ;; constant folds
10 ; CHECK-NEXT: store i32 23, i32* %P
12 %C = zext i8 %V to i32
13 %D = zext i8 %V to i32 ;; CSE
14 store volatile i32 %C, i32* %P
15 store volatile i32 %D, i32* %P
16 ; CHECK-NEXT: %C = zext i8 %V to i32
17 ; CHECK-NEXT: store volatile i32 %C
18 ; CHECK-NEXT: store volatile i32 %C
22 store volatile i32 %E, i32* %P
23 store volatile i32 %F, i32* %P
24 ; CHECK-NEXT: %E = add i32 %C, %C
25 ; CHECK-NEXT: store volatile i32 %E
26 ; CHECK-NEXT: store volatile i32 %E
28 %G = add nuw i32 %C, %C ;; not a CSE with E
29 store volatile i32 %G, i32* %P
30 ; CHECK-NEXT: %G = add nuw i32 %C, %C
31 ; CHECK-NEXT: store volatile i32 %G
36 ;; Simple load value numbering.
37 ; CHECK-LABEL: @test2(
38 define i32 @test2(i32 *%P) {
41 %Diff = sub i32 %V1, %V2
46 ; CHECK-LABEL: @test2a(
47 define i32 @test2a(i32 *%P, i1 %b) {
49 tail call void @llvm.assume(i1 %b)
51 %Diff = sub i32 %V1, %V2
56 ;; Cross block load value numbering.
57 ; CHECK-LABEL: @test3(
58 define i32 @test3(i32 *%P, i1 %Cond) {
60 br i1 %Cond, label %T, label %F
66 %Diff = sub i32 %V1, %V2
72 ; CHECK-LABEL: @test3a(
73 define i32 @test3a(i32 *%P, i1 %Cond, i1 %b) {
75 br i1 %Cond, label %T, label %F
80 tail call void @llvm.assume(i1 %b)
82 %Diff = sub i32 %V1, %V2
88 ;; Cross block load value numbering stops when stores happen.
89 ; CHECK-LABEL: @test4(
90 define i32 @test4(i32 *%P, i1 %Cond) {
92 br i1 %Cond, label %T, label %F
100 %Diff = sub i32 %V1, %V2
103 ; CHECK: ret i32 %Diff
106 declare i32 @func(i32 *%P) readonly
108 ;; Simple call CSE'ing.
109 ; CHECK-LABEL: @test5(
110 define i32 @test5(i32 *%P) {
111 %V1 = call i32 @func(i32* %P)
112 %V2 = call i32 @func(i32* %P)
113 %Diff = sub i32 %V1, %V2
118 ;; Trivial Store->load forwarding
119 ; CHECK-LABEL: @test6(
120 define i32 @test6(i32 *%P) {
121 store i32 42, i32* %P
127 ; CHECK-LABEL: @test6a(
128 define i32 @test6a(i32 *%P, i1 %b) {
129 store i32 42, i32* %P
130 tail call void @llvm.assume(i1 %b)
136 ;; Trivial dead store elimination.
137 ; CHECK-LABEL: @test7(
138 define void @test7(i32 *%P) {
139 store i32 42, i32* %P
140 store i32 45, i32* %P
142 ; CHECK-NEXT: store i32 45
143 ; CHECK-NEXT: ret void
146 ;; Readnone functions aren't invalidated by stores.
147 ; CHECK-LABEL: @test8(
148 define i32 @test8(i32 *%P) {
149 %V1 = call i32 @func(i32* %P) readnone
151 %V2 = call i32 @func(i32* %P) readnone
152 %Diff = sub i32 %V1, %V2
157 ;; Trivial DSE can't be performed across a readonly call. The call
158 ;; can observe the earlier write.
159 ; CHECK-LABEL: @test9(
160 define i32 @test9(i32 *%P) {
162 %V1 = call i32 @func(i32* %P) readonly
165 ; CHECK: store i32 4, i32* %P
166 ; CHECK-NEXT: %V1 = call i32 @func(i32* %P)
167 ; CHECK-NEXT: store i32 5, i32* %P
168 ; CHECK-NEXT: ret i32 %V1
171 ;; Trivial DSE can be performed across a readnone call.
172 ; CHECK-LABEL: @test10
173 define i32 @test10(i32 *%P) {
175 %V1 = call i32 @func(i32* %P) readnone
178 ; CHECK-NEXT: %V1 = call i32 @func(i32* %P)
179 ; CHECK-NEXT: store i32 5, i32* %P
180 ; CHECK-NEXT: ret i32 %V1
183 ;; Trivial dead store elimination - should work for an entire series of dead stores too.
184 ; CHECK-LABEL: @test11(
185 define void @test11(i32 *%P) {
186 store i32 42, i32* %P
187 store i32 43, i32* %P
188 store i32 44, i32* %P
189 store i32 45, i32* %P
191 ; CHECK-NEXT: store i32 45
192 ; CHECK-NEXT: ret void