1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine -S < %s | FileCheck %s
3 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
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
16 ; CHECK-LABEL: @test1(
17 ; CHECK-NEXT: ret i32 42
19 %B = load i32, i32* @X ; <i32> [#uses=1]
23 define float @test2() {
24 ; CHECK-LABEL: @test2(
25 ; CHECK-NEXT: ret float 0x3FF3B2FEC0000000
27 %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Y, i64 0, i64 1, i32 1 ; <float*> [#uses=1]
28 %B = load float, float* %A ; <float> [#uses=1]
33 ; CHECK-LABEL: @test3(
34 ; CHECK-NEXT: ret i32 12
36 %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Y, i64 0, i64 0, i32 0 ; <i32*> [#uses=1]
37 %B = load i32, i32* %A ; <i32> [#uses=1]
42 ; CHECK-LABEL: @test4(
43 ; CHECK-NEXT: ret i32 0
45 %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Z, i64 0, i64 1, i32 0 ; <i32*> [#uses=1]
46 %B = load i32, i32* %A ; <i32> [#uses=1]
50 define i32 @test5(i1 %C) {
51 ; CHECK-LABEL: @test5(
52 ; CHECK-NEXT: [[Z:%.*]] = select i1 [[C:%.*]], i32 42, i32 47
53 ; CHECK-NEXT: ret i32 [[Z]]
55 %Y = select i1 %C, i32* @X, i32* @X2 ; <i32*> [#uses=1]
56 %Z = load i32, i32* %Y ; <i32> [#uses=1]
60 define i32 @test7(i32 %X) {
61 ; CHECK-LABEL: @test7(
62 ; CHECK-NEXT: store i32 undef, i32* null, align 536870912
63 ; CHECK-NEXT: ret i32 undef
65 %V = getelementptr i32, i32* null, i32 %X ; <i32*> [#uses=1]
66 %R = load i32, i32* %V ; <i32> [#uses=1]
70 define i32 @test7_no_null_opt(i32 %X) #0 {
71 ; CHECK-LABEL: @test7_no_null_opt(
72 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
73 ; CHECK-NEXT: [[V:%.*]] = getelementptr i32, i32* null, i64 [[TMP1]]
74 ; CHECK-NEXT: [[R:%.*]] = load i32, i32* [[V]], align 4
75 ; CHECK-NEXT: ret i32 [[R]]
77 %V = getelementptr i32, i32* null, i32 %X ; <i32*> [#uses=1]
78 %R = load i32, i32* %V ; <i32> [#uses=1]
81 attributes #0 = { "null-pointer-is-valid"="true" }
83 define i32 @test8(i32* %P) {
84 ; CHECK-LABEL: @test8(
85 ; CHECK-NEXT: store i32 1, i32* [[P:%.*]], align 4
86 ; CHECK-NEXT: ret i32 1
89 %X = load i32, i32* %P ; <i32> [#uses=1]
93 define i32 @test9(i32* %P) {
94 ; CHECK-LABEL: @test9(
95 ; CHECK-NEXT: ret i32 0
97 %X = load i32, i32* %P ; <i32> [#uses=1]
98 %Y = load i32, i32* %P ; <i32> [#uses=1]
99 %Z = sub i32 %X, %Y ; <i32> [#uses=1]
103 define i32 @test10(i1 %C.upgrd.1, i32* %P, i32* %Q) {
104 ; CHECK-LABEL: @test10(
105 ; CHECK-NEXT: br i1 [[C_UPGRD_1:%.*]], label [[T:%.*]], label [[F:%.*]]
107 ; CHECK-NEXT: store i32 1, i32* [[Q:%.*]], align 4
108 ; CHECK-NEXT: br label [[C:%.*]]
110 ; CHECK-NEXT: br label [[C]]
112 ; CHECK-NEXT: store i32 0, i32* [[P:%.*]], align 4
113 ; CHECK-NEXT: ret i32 0
115 br i1 %C.upgrd.1, label %T, label %F
124 %V = load i32, i32* %P ; <i32> [#uses=1]
128 define double @test11(double* %p) {
129 ; CHECK-LABEL: @test11(
130 ; CHECK-NEXT: [[T0:%.*]] = getelementptr double, double* [[P:%.*]], i64 1
131 ; CHECK-NEXT: store double 2.000000e+00, double* [[T0]], align 8
132 ; CHECK-NEXT: ret double 2.000000e+00
134 %t0 = getelementptr double, double* %p, i32 1
135 store double 2.0, double* %t0
136 %t1 = getelementptr double, double* %p, i32 1
137 %x = load double, double* %t1
141 define i32 @test12(i32* %P) {
142 ; CHECK-LABEL: @test12(
143 ; CHECK-NEXT: ret i32 123
146 store i32 123, i32* %A
147 ; Cast the result of the load not the source
148 %Q = bitcast i32* %A to i32*
149 %V = load i32, i32* %Q
153 define <16 x i8> @test13(<2 x i64> %x) {
154 ; CHECK-LABEL: @test13(
155 ; CHECK-NEXT: ret <16 x i8> zeroinitializer
157 %tmp = load <16 x i8>, <16 x i8>* bitcast ([4 x i32]* @GLOBAL to <16 x i8>*)
161 ; This test must not have the store of %x forwarded to the load -- there is an
162 ; intervening store if %y. However, the intervening store occurs with a different
163 ; type and size and to a different pointer value. This is ensuring that none of
164 ; those confuse the analysis into thinking that the second store does not alias
167 define i8 @test14(i8 %x, i32 %y) {
168 ; CHECK-LABEL: @test14(
169 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
170 ; CHECK-NEXT: [[A_I8:%.*]] = bitcast i32* [[A]] to i8*
171 ; CHECK-NEXT: store i8 [[X:%.*]], i8* [[A_I8]], align 4
172 ; CHECK-NEXT: store i32 [[Y:%.*]], i32* [[A]], align 4
173 ; CHECK-NEXT: [[R:%.*]] = load i8, i8* [[A_I8]], align 4
174 ; CHECK-NEXT: ret i8 [[R]]
177 %a.i8 = bitcast i32* %a to i8*
178 store i8 %x, i8* %a.i8
179 store i32 %y, i32* %a
180 %r = load i8, i8* %a.i8
184 @test15_global = external global i32
186 ; Same test as @test14 essentially, but using a global instead of an alloca.
188 define i8 @test15(i8 %x, i32 %y) {
189 ; CHECK-LABEL: @test15(
190 ; CHECK-NEXT: store i8 [[X:%.*]], i8* bitcast (i32* @test15_global to i8*), align 4
191 ; CHECK-NEXT: store i32 [[Y:%.*]], i32* @test15_global, align 4
192 ; CHECK-NEXT: [[R:%.*]] = load i8, i8* bitcast (i32* @test15_global to i8*), align 4
193 ; CHECK-NEXT: ret i8 [[R]]
195 %g.i8 = bitcast i32* @test15_global to i8*
196 store i8 %x, i8* %g.i8
197 store i32 %y, i32* @test15_global
198 %r = load i8, i8* %g.i8
202 ; Check that we canonicalize loads which are only stored to use integer types
203 ; when there is a valid integer type.
205 define void @test16(i8* %x, i8* %a, i8* %b, i8* %c) {
206 ; CHECK-LABEL: @test16(
208 ; CHECK-NEXT: [[C_CAST:%.*]] = bitcast i8* [[C:%.*]] to i32*
209 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i8* [[X:%.*]] to i32*
210 ; CHECK-NEXT: [[X11:%.*]] = load i32, i32* [[TMP0]], align 4
211 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[A:%.*]] to i32*
212 ; CHECK-NEXT: store i32 [[X11]], i32* [[TMP1]], align 4
213 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i8* [[B:%.*]] to i32*
214 ; CHECK-NEXT: store i32 [[X11]], i32* [[TMP2]], align 4
215 ; CHECK-NEXT: [[TMP3:%.*]] = bitcast i8* [[X]] to i32*
216 ; CHECK-NEXT: [[X22:%.*]] = load i32, i32* [[TMP3]], align 4
217 ; CHECK-NEXT: [[TMP4:%.*]] = bitcast i8* [[B]] to i32*
218 ; CHECK-NEXT: store i32 [[X22]], i32* [[TMP4]], align 4
219 ; CHECK-NEXT: store i32 [[X22]], i32* [[C_CAST]], align 4
220 ; CHECK-NEXT: ret void
223 %x.cast = bitcast i8* %x to float*
224 %a.cast = bitcast i8* %a to float*
225 %b.cast = bitcast i8* %b to float*
226 %c.cast = bitcast i8* %c to i32*
228 %x1 = load float, float* %x.cast
229 store float %x1, float* %a.cast
230 store float %x1, float* %b.cast
232 %x2 = load float, float* %x.cast
233 store float %x2, float* %b.cast
234 %x2.cast = bitcast float %x2 to i32
235 store i32 %x2.cast, i32* %c.cast
240 ; Check that in cases similar to @test16 we don't try to rewrite a load when
241 ; its only use is a store but it is used as the pointer to that store rather
244 define void @test17(i8** %x, i8 %y) {
245 ; CHECK-LABEL: @test17(
247 ; CHECK-NEXT: [[X_LOAD:%.*]] = load i8*, i8** [[X:%.*]], align 8
248 ; CHECK-NEXT: store i8 [[Y:%.*]], i8* [[X_LOAD]], align 1
249 ; CHECK-NEXT: ret void
252 %x.load = load i8*, i8** %x
253 store i8 %y, i8* %x.load
258 ; Check that we don't try change the type of the load by inserting a bitcast
259 ; generating invalid IR.
260 %swift.error = type opaque
261 declare void @useSwiftError(%swift.error** swifterror)
263 define void @test18(%swift.error** swifterror %err) {
264 ; CHECK-LABEL: @test18(
266 ; CHECK-NEXT: [[SWIFTERROR:%.*]] = alloca swifterror %swift.error*, align 8
267 ; CHECK-NEXT: store %swift.error* null, %swift.error** [[SWIFTERROR]], align 8
268 ; CHECK-NEXT: call void @useSwiftError(%swift.error** nonnull swifterror [[SWIFTERROR]])
269 ; CHECK-NEXT: [[ERR_RES:%.*]] = load %swift.error*, %swift.error** [[SWIFTERROR]], align 8
270 ; CHECK-NEXT: store %swift.error* [[ERR_RES]], %swift.error** [[ERR:%.*]], align 8
271 ; CHECK-NEXT: ret void
274 %swifterror = alloca swifterror %swift.error*, align 8
275 store %swift.error* null, %swift.error** %swifterror, align 8
276 call void @useSwiftError(%swift.error** nonnull swifterror %swifterror)
277 %err.res = load %swift.error*, %swift.error** %swifterror, align 8
278 store %swift.error* %err.res, %swift.error** %err, align 8
282 ; Make sure we preseve the type of the store to a swifterror pointer.
284 declare void @initi8(i8**)
285 define void @test19(%swift.error** swifterror %err) {
286 ; CHECK-LABEL: @test19(
288 ; CHECK-NEXT: [[TMP:%.*]] = alloca i8*, align 8
289 ; CHECK-NEXT: call void @initi8(i8** nonnull [[TMP]])
290 ; CHECK-NEXT: [[SWIFTERROR:%.*]] = bitcast i8** [[TMP]] to %swift.error**
291 ; CHECK-NEXT: [[ERR_RES:%.*]] = load %swift.error*, %swift.error** [[SWIFTERROR]], align 8
292 ; CHECK-NEXT: store %swift.error* [[ERR_RES]], %swift.error** [[ERR:%.*]], align 8
293 ; CHECK-NEXT: ret void
296 %tmp = alloca i8*, align 8
297 call void @initi8(i8** %tmp)
298 %swifterror = bitcast i8** %tmp to %swift.error**
299 %err.res = load %swift.error*, %swift.error** %swifterror, align 8
300 store %swift.error* %err.res, %swift.error** %err, align 8