1 ; RUN: opt < %s -instcombine -S -default-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
2 ; RUN: opt < %s -instcombine -S -default-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
3 ; RUN: opt < %s -instcombine -S | FileCheck %s -check-prefix=NODL
8 @int = global i32 zeroinitializer
10 ; Zero byte allocas should be merged if they can't be deleted.
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* @int
22 %A = alloca {{}}, i32 %size
23 call void (...)* @use( {{}}* %A )
27 ; Zero byte allocas should be deleted.
28 ; CHECK-LABEL: @test2(
30 define void @test2() {
31 %A = alloca i32 ; <i32*> [#uses=1]
32 store i32 123, i32* %A
36 ; Zero byte allocas should be deleted.
37 ; CHECK-LABEL: @test3(
39 define void @test3() {
40 %A = alloca { i32 } ; <{ i32 }*> [#uses=1]
41 %B = getelementptr { i32 }* %A, i32 0, i32 0 ; <i32*> [#uses=1]
42 store i32 123, i32* %B
46 ; CHECK-LABEL: @test4(
47 ; CHECK: = zext i32 %n to i64
48 ; CHECK: %A = alloca i32, i64 %
49 define i32* @test4(i32 %n) {
50 %A = alloca i32, i32 %n
54 ; Allocas which are only used by GEPs, bitcasts, and stores (transitively)
56 define void @test5() {
57 ; CHECK-LABEL: @test5(
65 %a.1 = getelementptr { i32 }* %a, i32 0, i32 0
66 store i32 123, i32* %a.1
67 store i32* %a.1, i32** %b
68 %b.1 = bitcast i32** %b to i32*
69 store i32 123, i32* %b.1
70 %a.2 = getelementptr { i32 }* %a, i32 0, i32 0
71 store atomic i32 2, i32* %a.2 unordered, align 4
72 %a.3 = getelementptr { i32 }* %a, i32 0, i32 0
73 store atomic i32 3, i32* %a.3 release, align 4
74 %a.4 = getelementptr { i32 }* %a, i32 0, i32 0
75 store atomic i32 4, i32* %a.4 seq_cst, align 4
79 declare void @f(i32* %p)
81 ; Check that we don't delete allocas in some erroneous cases.
82 define void @test6() {
83 ; CHECK-LABEL: @test6(
92 %a.1 = getelementptr { i32 }* %a, i32 0, i32 0
93 store volatile i32 123, i32* %a.1
94 tail call void @f(i32* %b)
99 %opaque_type = type opaque
100 %real_type = type { { i32, i32* } }
102 @opaque_global = external constant %opaque_type, align 4
104 define void @test7() {
106 %0 = alloca %real_type, align 4
107 %1 = bitcast %real_type* %0 to i8*
108 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* bitcast (%opaque_type* @opaque_global to i8*), i32 8, i32 1, i1 false)
112 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
115 ; Check that the GEP indices use the pointer size, or 64 if unknown
116 define void @test8() {
117 ; CHECK-LABEL: @test8(
118 ; CHECK: alloca [100 x i32]
119 ; CHECK: getelementptr inbounds [100 x i32]* %x1, i64 0, i64 0
122 ; P32: alloca [100 x i32]
123 ; P32: getelementptr inbounds [100 x i32]* %x1, i32 0, i32 0
125 ; NODL-LABEL: @test8(
126 ; NODL: alloca [100 x i32]
127 ; NODL: getelementptr inbounds [100 x i32]* %x1, i64 0, i64 0
128 %x = alloca i32, i32 100
129 call void (...)* @use(i32* %x)
134 %struct_type = type { i32, i32 }
135 declare void @test9_aux(<{ %struct_type }>* inalloca)
136 declare i8* @llvm.stacksave()
137 declare void @llvm.stackrestore(i8*)
139 define void @test9(%struct_type* %a) {
140 ; CHECK-LABEL: @test9(
142 %inalloca.save = call i8* @llvm.stacksave()
143 %argmem = alloca inalloca <{ %struct_type }>
144 ; CHECK: alloca inalloca i64, align 8
145 %0 = getelementptr inbounds <{ %struct_type }>* %argmem, i32 0, i32 0
146 %1 = bitcast %struct_type* %0 to i8*
147 %2 = bitcast %struct_type* %a to i8*
148 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* %2, i32 8, i32 4, i1 false)
149 call void @test9_aux(<{ %struct_type }>* inalloca %argmem)
150 call void @llvm.stackrestore(i8* %inalloca.save)