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(
18 %B = load i32* @X ; <i32> [#uses=1]
22 ; CHECK-LABEL: @test2(
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]
30 ; CHECK-LABEL: @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]
38 ; CHECK-LABEL: @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]
46 ; CHECK-LABEL: @test5(
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]
54 ; CHECK-LABEL: @test7(
56 define i32 @test7(i32 %X) {
57 %V = getelementptr i32* null, i32 %X ; <i32*> [#uses=1]
58 %R = load i32* %V ; <i32> [#uses=1]
62 ; CHECK-LABEL: @test8(
64 define i32 @test8(i32* %P) {
66 %X = load i32* %P ; <i32> [#uses=1]
70 ; CHECK-LABEL: @test9(
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]
79 ; CHECK-LABEL: @test10(
81 define i32 @test10(i1 %C.upgrd.1, i32* %P, i32* %Q) {
82 br i1 %C.upgrd.1, label %T, label %F
91 %V = load i32* %P ; <i32> [#uses=1]
95 ; CHECK-LABEL: @test11(
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
105 ; CHECK-LABEL: @test12(
107 define i32 @test12(i32* %P) {
109 store i32 123, i32* %A
110 ; Cast the result of the load not the source
111 %Q = bitcast i32* %A to i32*
116 ; CHECK-LABEL: @test13(
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>*)
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
129 ; CHECK-LABEL: @test14(
130 ; CHECK: %[[R:.*]] = load i8*
131 ; CHECK-NEXT: ret i8 %[[R]]
133 %a.i8 = bitcast i32* %a to i8*
134 store i8 %x, i8* %a.i8
135 store i32 %y, i32* %a
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
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*
160 ; CHECK: store i32 %[[L1]], i32*
161 ; CHECK: store i32 %[[L1]], i32*
163 ; CHECK: %[[L1:.*]] = load i32*
165 ; CHECK: store i32 %[[L1]], i32*
166 ; CHECK: store i32 %[[L1]], i32*
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
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
193 ; CHECK-LABEL: @test17(
194 ; CHECK: %[[L:.*]] = load i8**
195 ; CHECK: store i8 %y, i8* %[[L]]
198 %x.load = load i8** %x
199 store i8 %y, i8* %x.load