1 ; RUN: opt -instcombine -S < %s | FileCheck %s
3 ; This test makes sure that these instructions are properly eliminated.
5 target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128"
7 @X = constant i32 42 ; <i32*> [#uses=2]
8 @X2 = constant i32 47 ; <i32*> [#uses=1]
9 @Y = constant [2 x { i32, float }] [ { i32, float } { i32 12, float 1.000000e+00 }, { i32, float } { i32 37, float 0x3FF3B2FEC0000000 } ] ; <[2 x { i32, float }]*> [#uses=2]
10 @Z = constant [2 x { i32, float }] zeroinitializer ; <[2 x { i32, float }]*> [#uses=1]
12 @GLOBAL = internal constant [4 x i32] zeroinitializer
15 ; CHECK-LABEL: @test1(
16 ; CHECK-NOT: load
17 define i32 @test1() {
18 %B = load i32* @X ; <i32> [#uses=1]
19 ret i32 %B
20 }
22 ; CHECK-LABEL: @test2(
23 ; CHECK-NOT: load
24 define float @test2() {
25 %A = getelementptr [2 x { i32, float }]* @Y, i64 0, i64 1, i32 1 ; <float*> [#uses=1]
26 %B = load float* %A ; <float> [#uses=1]
27 ret float %B
28 }
30 ; CHECK-LABEL: @test3(
31 ; CHECK-NOT: load
32 define i32 @test3() {
33 %A = getelementptr [2 x { i32, float }]* @Y, i64 0, i64 0, i32 0 ; <i32*> [#uses=1]
34 %B = load i32* %A ; <i32> [#uses=1]
35 ret i32 %B
36 }
38 ; CHECK-LABEL: @test4(
39 ; CHECK-NOT: load
40 define i32 @test4() {
41 %A = getelementptr [2 x { i32, float }]* @Z, i64 0, i64 1, i32 0 ; <i32*> [#uses=1]
42 %B = load i32* %A ; <i32> [#uses=1]
43 ret i32 %B
44 }
46 ; CHECK-LABEL: @test5(
47 ; CHECK-NOT: load
48 define i32 @test5(i1 %C) {
49 %Y = select i1 %C, i32* @X, i32* @X2 ; <i32*> [#uses=1]
50 %Z = load i32* %Y ; <i32> [#uses=1]
51 ret i32 %Z
52 }
54 ; CHECK-LABEL: @test7(
55 ; CHECK-NOT: load
56 define i32 @test7(i32 %X) {
57 %V = getelementptr i32* null, i32 %X ; <i32*> [#uses=1]
58 %R = load i32* %V ; <i32> [#uses=1]
59 ret i32 %R
60 }
62 ; CHECK-LABEL: @test8(
63 ; CHECK-NOT: load
64 define i32 @test8(i32* %P) {
65 store i32 1, i32* %P
66 %X = load i32* %P ; <i32> [#uses=1]
67 ret i32 %X
68 }
70 ; CHECK-LABEL: @test9(
71 ; CHECK-NOT: load
72 define i32 @test9(i32* %P) {
73 %X = load i32* %P ; <i32> [#uses=1]
74 %Y = load i32* %P ; <i32> [#uses=1]
75 %Z = sub i32 %X, %Y ; <i32> [#uses=1]
76 ret i32 %Z
77 }
79 ; CHECK-LABEL: @test10(
80 ; CHECK-NOT: load
81 define i32 @test10(i1 %C.upgrd.1, i32* %P, i32* %Q) {
82 br i1 %C.upgrd.1, label %T, label %F
83 T: ; preds = %0
84 store i32 1, i32* %Q
85 store i32 0, i32* %P
86 br label %C
87 F: ; preds = %0
88 store i32 0, i32* %P
89 br label %C
90 C: ; preds = %F, %T
91 %V = load i32* %P ; <i32> [#uses=1]
92 ret i32 %V
93 }
95 ; CHECK-LABEL: @test11(
96 ; CHECK-NOT: load
97 define double @test11(double* %p) {
98 %t0 = getelementptr double* %p, i32 1
99 store double 2.0, double* %t0
100 %t1 = getelementptr double* %p, i32 1
101 %x = load double* %t1
102 ret double %x
103 }
105 ; CHECK-LABEL: @test12(
106 ; CHECK-NOT: load
107 define i32 @test12(i32* %P) {
108 %A = alloca i32
109 store i32 123, i32* %A
110 ; Cast the result of the load not the source
111 %Q = bitcast i32* %A to i32*
112 %V = load i32* %Q
113 ret i32 %V
114 }
116 ; CHECK-LABEL: @test13(
117 ; CHECK-NOT: load
118 define <16 x i8> @test13(<2 x i64> %x) {
119 %tmp = load <16 x i8>* bitcast ([4 x i32]* @GLOBAL to <16 x i8>*)
120 ret <16 x i8> %tmp
121 }
123 define i8 @test14(i8 %x, i32 %y) {
124 ; This test must not have the store of %x forwarded to the load -- there is an
125 ; intervening store if %y. However, the intervening store occurs with a different
126 ; type and size and to a different pointer value. This is ensuring that none of
127 ; those confuse the analysis into thinking that the second store does not alias
128 ; the first.
129 ; CHECK-LABEL: @test14(
130 ; CHECK: %[[R:.*]] = load i8*
131 ; CHECK-NEXT: ret i8 %[[R]]
132 %a = alloca i32
133 %a.i8 = bitcast i32* %a to i8*
134 store i8 %x, i8* %a.i8
135 store i32 %y, i32* %a
136 %r = load i8* %a.i8
137 ret i8 %r
138 }
140 @test15_global = external global i32
142 define i8 @test15(i8 %x, i32 %y) {
143 ; Same test as @test14 essentially, but using a global instead of an alloca.
144 ; CHECK-LABEL: @test15(
145 ; CHECK: %[[R:.*]] = load i8*
146 ; CHECK-NEXT: ret i8 %[[R]]
147 %g.i8 = bitcast i32* @test15_global to i8*
148 store i8 %x, i8* %g.i8
149 store i32 %y, i32* @test15_global
150 %r = load i8* %g.i8
151 ret i8 %r
152 }
154 define void @test16(i8* %x, i8* %a, i8* %b, i8* %c) {
155 ; Check that we canonicalize loads which are only stored to use integer types
156 ; when there is a valid integer type.
157 ; CHECK-LABEL: @test16(
158 ; CHECK: %[[L1:.*]] = load i32*
159 ; CHECK-NOT: load
160 ; CHECK: store i32 %[[L1]], i32*
161 ; CHECK: store i32 %[[L1]], i32*
162 ; CHECK-NOT: store
163 ; CHECK: %[[L1:.*]] = load i32*
164 ; CHECK-NOT: load
165 ; CHECK: store i32 %[[L1]], i32*
166 ; CHECK: store i32 %[[L1]], i32*
167 ; CHECK-NOT: store
168 ; CHECK: ret
170 entry:
171 %x.cast = bitcast i8* %x to float*
172 %a.cast = bitcast i8* %a to float*
173 %b.cast = bitcast i8* %b to float*
174 %c.cast = bitcast i8* %c to i32*
176 %x1 = load float* %x.cast
177 store float %x1, float* %a.cast
178 store float %x1, float* %b.cast
180 %x2 = load float* %x.cast
181 store float %x2, float* %b.cast
182 %x2.cast = bitcast float %x2 to i32
183 store i32 %x2.cast, i32* %c.cast
185 ret void
186 }
188 define void @test17(i8** %x, i8 %y) {
189 ; Check that in cases similar to @test16 we don't try to rewrite a load when
190 ; its only use is a store but it is used as the pointer to that store rather
191 ; than the value.
192 ;
193 ; CHECK-LABEL: @test17(
194 ; CHECK: %[[L:.*]] = load i8**
195 ; CHECK: store i8 %y, i8* %[[L]]
197 entry:
198 %x.load = load i8** %x
199 store i8 %y, i8* %x.load
201 ret void
202 }