1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2 target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64"
3 @C.0.1248 = internal constant [128 x float] [ float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00 ], align 32 ; <[128 x float]*> [#uses=1]
5 define float @test1(i32 %hash, float %x, float %y, float %z, float %w) {
7 %lookupTable = alloca [128 x float], align 16 ; <[128 x float]*> [#uses=5]
8 %lookupTable1 = bitcast [128 x float]* %lookupTable to i8* ; <i8*> [#uses=1]
9 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %lookupTable1, i8* align 16 bitcast ([128 x float]* @C.0.1248 to i8*), i64 512, i1 false)
11 ; CHECK-LABEL: @test1(
13 ; CHECK-NOT: call{{.*}}@llvm.memcpy
15 %tmp3 = shl i32 %hash, 2 ; <i32> [#uses=1]
16 %tmp5 = and i32 %tmp3, 124 ; <i32> [#uses=4]
17 %tmp753 = getelementptr [128 x float], [128 x float]* %lookupTable, i32 0, i32 %tmp5 ; <float*> [#uses=1]
18 %tmp9 = load float, float* %tmp753 ; <float> [#uses=1]
19 %tmp11 = fmul float %tmp9, %x ; <float> [#uses=1]
20 %tmp13 = fadd float %tmp11, 0.000000e+00 ; <float> [#uses=1]
21 %tmp17.sum52 = or i32 %tmp5, 1 ; <i32> [#uses=1]
22 %tmp1851 = getelementptr [128 x float], [128 x float]* %lookupTable, i32 0, i32 %tmp17.sum52 ; <float*> [#uses=1]
23 %tmp19 = load float, float* %tmp1851 ; <float> [#uses=1]
24 %tmp21 = fmul float %tmp19, %y ; <float> [#uses=1]
25 %tmp23 = fadd float %tmp21, %tmp13 ; <float> [#uses=1]
26 %tmp27.sum50 = or i32 %tmp5, 2 ; <i32> [#uses=1]
27 %tmp2849 = getelementptr [128 x float], [128 x float]* %lookupTable, i32 0, i32 %tmp27.sum50 ; <float*> [#uses=1]
28 %tmp29 = load float, float* %tmp2849 ; <float> [#uses=1]
29 %tmp31 = fmul float %tmp29, %z ; <float> [#uses=1]
30 %tmp33 = fadd float %tmp31, %tmp23 ; <float> [#uses=1]
31 %tmp37.sum48 = or i32 %tmp5, 3 ; <i32> [#uses=1]
32 %tmp3847 = getelementptr [128 x float], [128 x float]* %lookupTable, i32 0, i32 %tmp37.sum48 ; <float*> [#uses=1]
33 %tmp39 = load float, float* %tmp3847 ; <float> [#uses=1]
34 %tmp41 = fmul float %tmp39, %w ; <float> [#uses=1]
35 %tmp43 = fadd float %tmp41, %tmp33 ; <float> [#uses=1]
39 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) nounwind
40 declare void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* nocapture, i8* nocapture, i64, i1) nounwind
41 declare void @llvm.memcpy.p0i8.p1i8.i64(i8* nocapture, i8 addrspace(1)* nocapture, i64, i1) nounwind
42 declare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture, i64, i1) nounwind
44 %T = type { i8, [123 x i8] }
45 %U = type { i32, i32, i32, i32, i32 }
47 @G = constant %T {i8 1, [123 x i8] zeroinitializer }
48 @H = constant [2 x %U] zeroinitializer, align 16
50 define void @test2() {
53 %a = bitcast %T* %A to i8*
54 %b = bitcast %T* %B to i8*
56 ; CHECK-LABEL: @test2(
58 ; %A alloca is deleted
59 ; CHECK-NEXT: alloca [124 x i8]
60 ; CHECK-NEXT: getelementptr inbounds [124 x i8], [124 x i8]*
62 ; use @G instead of %A
63 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 8 %{{.*}}, i8* align 16 getelementptr inbounds (%T, %T* @G, i64 0, i32 0)
64 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
65 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %b, i8* align 4 %a, i64 124, i1 false)
66 call void @bar(i8* %b)
70 define void @test2_no_null_opt() #0 {
73 %a = bitcast %T* %A to i8*
74 %b = bitcast %T* %B to i8*
76 ; CHECK-LABEL: @test2_no_null_opt(
78 ; %A alloca is deleted
79 ; CHECK-NEXT: alloca [124 x i8]
80 ; CHECK-NEXT: getelementptr inbounds [124 x i8], [124 x i8]*
82 ; use @G instead of %A
83 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %{{.*}}, i8* align 16 getelementptr inbounds (%T, %T* @G, i64 0, i32 0)
84 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
85 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %b, i8* align 4 %a, i64 124, i1 false)
86 call void @bar(i8* %b)
90 define void @test2_addrspacecast() {
93 %a = addrspacecast %T* %A to i8 addrspace(1)*
94 %b = addrspacecast %T* %B to i8 addrspace(1)*
96 ; CHECK-LABEL: @test2_addrspacecast(
98 ; %A alloca is deleted
99 ; This doesn't exactly match what test2 does, because folding the type
100 ; cast into the alloca doesn't work for the addrspacecast yet.
101 ; CHECK-NEXT: alloca [124 x i8]
102 ; CHECK-NEXT: getelementptr
103 ; CHECK-NEXT: addrspacecast
105 ; use @G instead of %A
106 ; CHECK-NEXT: call void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* align 4 %{{.*}},
107 call void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
108 call void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* align 4 %b, i8 addrspace(1)* align 4 %a, i64 124, i1 false)
109 call void @bar_as1(i8 addrspace(1)* %b)
113 declare void @bar(i8*)
114 declare void @bar_as1(i8 addrspace(1)*)
117 ;; Should be able to eliminate the alloca.
118 define void @test3() {
120 %a = bitcast %T* %A to i8*
121 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
122 call void @bar(i8* %a) readonly
123 ; CHECK-LABEL: @test3(
124 ; CHECK-NEXT: call void @bar(i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0))
128 define void @test3_addrspacecast() {
130 %a = bitcast %T* %A to i8*
131 call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %a, i8 addrspace(1)* align 4 addrspacecast (%T* @G to i8 addrspace(1)*), i64 124, i1 false)
132 call void @bar(i8* %a) readonly
133 ; CHECK-LABEL: @test3_addrspacecast(
134 ; CHECK-NEXT: call void @bar(i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0))
139 define void @test4() {
141 %a = bitcast %T* %A to i8*
142 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
143 call void @baz(i8* byval %a)
144 ; CHECK-LABEL: @test4(
145 ; CHECK-NEXT: call void @baz(i8* byval getelementptr inbounds (%T, %T* @G, i64 0, i32 0))
149 declare void @llvm.lifetime.start.p0i8(i64, i8*)
150 define void @test5() {
152 %a = bitcast %T* %A to i8*
153 call void @llvm.lifetime.start.p0i8(i64 -1, i8* %a)
154 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
155 call void @baz(i8* byval %a)
156 ; CHECK-LABEL: @test5(
157 ; CHECK-NEXT: call void @baz(i8* byval getelementptr inbounds (%T, %T* @G, i64 0, i32 0))
162 declare void @baz(i8* byval)
165 define void @test6() {
166 %A = alloca %U, align 16
167 %a = bitcast %U* %A to i8*
168 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %a, i8* align 16 bitcast ([2 x %U]* @H to i8*), i64 20, i1 false)
169 call void @bar(i8* %a) readonly
170 ; CHECK-LABEL: @test6(
171 ; CHECK-NEXT: call void @bar(i8* bitcast ([2 x %U]* @H to i8*))
175 define void @test7() {
176 %A = alloca %U, align 16
177 %a = bitcast %U* %A to i8*
178 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%U* getelementptr ([2 x %U], [2 x %U]* @H, i64 0, i32 0) to i8*), i64 20, i1 false)
179 call void @bar(i8* %a) readonly
180 ; CHECK-LABEL: @test7(
181 ; CHECK-NEXT: call void @bar(i8* bitcast ([2 x %U]* @H to i8*))
185 define void @test8() {
186 %A = alloca %U, align 16
187 %a = bitcast %U* %A to i8*
188 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%U* getelementptr ([2 x %U], [2 x %U]* @H, i64 0, i32 1) to i8*), i64 20, i1 false)
189 call void @bar(i8* %a) readonly
190 ; CHECK-LABEL: @test8(
197 define void @test8_addrspacecast() {
198 %A = alloca %U, align 16
199 %a = bitcast %U* %A to i8*
200 call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %a, i8 addrspace(1)* align 4 addrspacecast (%U* getelementptr ([2 x %U], [2 x %U]* @H, i64 0, i32 1) to i8 addrspace(1)*), i64 20, i1 false)
201 call void @bar(i8* %a) readonly
202 ; CHECK-LABEL: @test8_addrspacecast(
208 define void @test9() {
209 %A = alloca %U, align 4
210 %a = bitcast %U* %A to i8*
211 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%U* getelementptr ([2 x %U], [2 x %U]* @H, i64 0, i32 1) to i8*), i64 20, i1 false)
212 call void @bar(i8* %a) readonly
213 ; CHECK-LABEL: @test9(
214 ; CHECK-NEXT: call void @bar(i8* bitcast (%U* getelementptr inbounds ([2 x %U], [2 x %U]* @H, i64 0, i64 1) to i8*))
218 define void @test9_addrspacecast() {
219 %A = alloca %U, align 4
220 %a = bitcast %U* %A to i8*
221 call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %a, i8 addrspace(1)* align 4 addrspacecast (%U* getelementptr ([2 x %U], [2 x %U]* @H, i64 0, i32 1) to i8 addrspace(1)*), i64 20, i1 false)
222 call void @bar(i8* %a) readonly
223 ; CHECK-LABEL: @test9_addrspacecast(
224 ; CHECK-NEXT: call void @bar(i8* bitcast (%U* getelementptr inbounds ([2 x %U], [2 x %U]* @H, i64 0, i64 1) to i8*))
228 @bbb = local_unnamed_addr global [1000000 x i8] zeroinitializer, align 16
229 @_ZL3KKK = internal unnamed_addr constant [3 x i8] c"\01\01\02", align 1
231 ; Should not replace alloca with global because of size mismatch.
232 define void @test9_small_global() {
233 ; CHECK-LABEL: @test9_small_global(
234 ; CHECK-NOT: call void @llvm.memcpy.p0i8.p0i8.i64({{.*}}@bbb,{{.*}}@_ZL3KKK,
235 ; CHECK: alloca [1000000 x i8]
237 %cc = alloca [1000000 x i8], align 16
238 %cc.0..sroa_idx = getelementptr inbounds [1000000 x i8], [1000000 x i8]* %cc, i64 0, i64 0
239 %arraydecay = getelementptr inbounds [1000000 x i8], [1000000 x i8]* %cc, i32 0, i32 0
240 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %arraydecay, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZL3KKK, i32 0, i32 0), i64 3, i1 false)
241 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 getelementptr inbounds ([1000000 x i8], [1000000 x i8]* @bbb, i32 0, i32 0), i8* align 16 %arraydecay, i64 1000000, i1 false)
245 ; Should replace alloca with global as they have exactly the same size.
246 define void @test10_same_global() {
247 ; CHECK-LABEL: @test10_same_global(
249 ; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64({{.*}}@bbb,{{.*}}@_ZL3KKK,{{.*}}, i64 3,
251 %cc = alloca [3 x i8], align 1
252 %cc.0..sroa_idx = getelementptr inbounds [3 x i8], [3 x i8]* %cc, i64 0, i64 0
253 %arraydecay = getelementptr inbounds [3 x i8], [3 x i8]* %cc, i32 0, i32 0
254 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %arraydecay, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZL3KKK, i32 0, i32 0), i64 3, i1 false)
255 call void @llvm.memcpy.p0i8.p0i8.i64(i8* getelementptr inbounds ([1000000 x i8], [1000000 x i8]* @bbb, i32 0, i32 0), i8* %arraydecay, i64 3, i1 false)
259 attributes #0 = { "null-pointer-is-valid"="true" }