InstCombine: don't drop 'inalloca' in PromoteCastOfAllocation (PR19569)
[oota-llvm.git] / test / Transforms / InstCombine / alloca.ll
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
4
5
6 declare 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
14 define 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* @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
30 define 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
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
43         ret void
44 }
45
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
51   ret i32* %A
52 }
53
54 ; Allocas which are only used by GEPs, bitcasts, and stores (transitively)
55 ; should be deleted.
56 define void @test5() {
57 ; CHECK-LABEL: @test5(
58 ; CHECK-NOT: alloca
59 ; CHECK-NOT: store
60 ; CHECK: ret
61
62 entry:
63   %a = alloca { i32 }
64   %b = alloca i32*
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
76   ret void
77 }
78
79 declare void @f(i32* %p)
80
81 ; Check that we don't delete allocas in some erroneous cases.
82 define void @test6() {
83 ; CHECK-LABEL: @test6(
84 ; CHECK-NOT: ret
85 ; CHECK: alloca
86 ; CHECK-NEXT: alloca
87 ; CHECK: ret
88
89 entry:
90   %a = alloca { i32 }
91   %b = alloca i32
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)
95   ret void
96 }
97
98 ; PR14371
99 %opaque_type = type opaque
100 %real_type = type { { i32, i32* } }
101
102 @opaque_global = external constant %opaque_type, align 4
103
104 define void @test7() {
105 entry:
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)
109   ret void
110 }
111
112 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
113
114
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
120
121 ; P32-LABEL: @test8(
122 ; P32: alloca [100 x i32]
123 ; P32: getelementptr inbounds [100 x i32]* %x1, i32 0, i32 0
124
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)
130   ret void
131 }
132
133 ; PR19569
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*)
138
139 define void @test9(%struct_type* %a) {
140 ; CHECK-LABEL: @test9(
141 entry:
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)
151   ret void
152 }