1; RUN: opt < %s -instcombine -S -data-layout="E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" | FileCheck %s -check-prefix=CHECK -check-prefix=ALL
2; RUN: opt < %s -instcombine -S -data-layout="E-p:32:32:32-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" | FileCheck %s -check-prefix=P32 -check-prefix=ALL
3; RUN: opt < %s -instcombine -S | FileCheck %s -check-prefix=NODL -check-prefix=ALL
4
5
6declare void @use(...)
7
8@int = global i32 zeroinitializer
9
10; Zero byte allocas should be merged if they can't be deleted.
11; CHECK-LABEL: @test(
12; CHECK: alloca
13; CHECK-NOT: alloca
14define void @test() {
15        %X = alloca [0 x i32]           ; <[0 x i32]*> [#uses=1]
16        call void (...) @use( [0 x i32]* %X )
17        %Y = alloca i32, i32 0          ; <i32*> [#uses=1]
18        call void (...) @use( i32* %Y )
19        %Z = alloca {  }                ; <{  }*> [#uses=1]
20        call void (...) @use( {  }* %Z )
21        %size = load i32, i32* @int
22        %A = alloca {{}}, i32 %size
23        call void (...) @use( {{}}* %A )
24        ret void
25}
26
27; Zero byte allocas should be deleted.
28; CHECK-LABEL: @test2(
29; CHECK-NOT: alloca
30define void @test2() {
31        %A = alloca i32         ; <i32*> [#uses=1]
32        store i32 123, i32* %A
33        ret void
34}
35
36; Zero byte allocas should be deleted.
37; CHECK-LABEL: @test3(
38; CHECK-NOT: alloca
39define void @test3() {
40        %A = alloca { i32 }             ; <{ i32 }*> [#uses=1]
41        %B = getelementptr { i32 }, { i32 }* %A, i32 0, i32 0            ; <i32*> [#uses=1]
42        store i32 123, i32* %B
43        ret void
44}
45
46; CHECK-LABEL: @test4(
47; CHECK: = zext i32 %n to i64
48; CHECK: %A = alloca i32, i64 %
49define i32* @test4(i32 %n) {
50  %A = alloca i32, i32 %n
51  ret i32* %A
52}
53
54; Allocas which are only used by GEPs, bitcasts, addrspacecasts, and stores
55; (transitively) should be deleted.
56define void @test5() {
57; CHECK-LABEL: @test5(
58; CHECK-NOT: alloca
59; CHECK-NOT: store
60; CHECK: ret
61
62entry:
63  %a = alloca { i32 }
64  %b = alloca i32*
65  %c = alloca i32
66  %a.1 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
67  store i32 123, i32* %a.1
68  store i32* %a.1, i32** %b
69  %b.1 = bitcast i32** %b to i32*
70  store i32 123, i32* %b.1
71  %a.2 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
72  store atomic i32 2, i32* %a.2 unordered, align 4
73  %a.3 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
74  store atomic i32 3, i32* %a.3 release, align 4
75  %a.4 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
76  store atomic i32 4, i32* %a.4 seq_cst, align 4
77  %c.1 = addrspacecast i32* %c to i32 addrspace(1)*
78  store i32 123, i32 addrspace(1)* %c.1
79  ret void
80}
81
82declare void @f(i32* %p)
83
84; Check that we don't delete allocas in some erroneous cases.
85define void @test6() {
86; CHECK-LABEL: @test6(
87; CHECK-NOT: ret
88; CHECK: alloca
89; CHECK-NEXT: alloca
90; CHECK: ret
91
92entry:
93  %a = alloca { i32 }
94  %b = alloca i32
95  %a.1 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
96  store volatile i32 123, i32* %a.1
97  tail call void @f(i32* %b)
98  ret void
99}
100
101; PR14371
102%opaque_type = type opaque
103%real_type = type { { i32, i32* } }
104
105@opaque_global = external constant %opaque_type, align 4
106
107define void @test7() {
108entry:
109  %0 = alloca %real_type, align 4
110  %1 = bitcast %real_type* %0 to i8*
111  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* bitcast (%opaque_type* @opaque_global to i8*), i32 8, i1 false)
112  ret void
113}
114
115declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
116
117
118; Check that the GEP indices use the pointer size, or 64 if unknown
119define void @test8() {
120; CHECK-LABEL: @test8(
121; CHECK: alloca [100 x i32]
122; CHECK: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i64 0, i64 0
123
124; P32-LABEL: @test8(
125; P32: alloca [100 x i32]
126; P32: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i32 0, i32 0
127
128; NODL-LABEL: @test8(
129; NODL: alloca [100 x i32]
130; NODL: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i64 0, i64 0
131  %x = alloca i32, i32 100
132  call void (...) @use(i32* %x)
133  ret void
134}
135
136; PR19569
137%struct_type = type { i32, i32 }
138declare void @test9_aux(<{ %struct_type }>* inalloca)
139declare i8* @llvm.stacksave()
140declare void @llvm.stackrestore(i8*)
141
142define void @test9(%struct_type* %a) {
143; CHECK-LABEL: @test9(
144entry:
145  %inalloca.save = call i8* @llvm.stacksave()
146  %argmem = alloca inalloca <{ %struct_type }>
147; CHECK: alloca inalloca i64, align 8
148  %0 = getelementptr inbounds <{ %struct_type }>, <{ %struct_type }>* %argmem, i32 0, i32 0
149  %1 = bitcast %struct_type* %0 to i8*
150  %2 = bitcast %struct_type* %a to i8*
151  call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %1, i8* align 4 %2, i32 8, i1 false)
152  call void @test9_aux(<{ %struct_type }>* inalloca %argmem)
153  call void @llvm.stackrestore(i8* %inalloca.save)
154  ret void
155}
156
157define void @test10() {
158entry:
159; ALL-LABEL: @test10(
160; ALL: %v32 = alloca i1, align 8
161; ALL: %v64 = alloca i1, align 8
162; ALL: %v33 = alloca i1, align 8
163  %v32 = alloca i1, align 8
164  %v64 = alloca i1, i64 1, align 8
165  %v33 = alloca i1, i33 1, align 8
166  call void (...) @use(i1* %v32, i1* %v64, i1* %v33)
167  ret void
168}
169
170define void @test11() {
171entry:
172; ALL-LABEL: @test11(
173; ALL: %y = alloca i32
174; ALL: call void (...) @use(i32* nonnull @int) [ "blah"(i32* %y) ]
175; ALL: ret void
176  %y = alloca i32
177  call void (...) @use(i32* nonnull @int) [ "blah"(i32* %y) ]
178  ret void
179}
180