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