Move the personality function from LandingPadInst to Function
[oota-llvm.git] / test / Transforms / FunctionAttrs / nocapture.ll
1 ; RUN: opt < %s -functionattrs -S | FileCheck %s
2 @g = global i32* null           ; <i32**> [#uses=1]
3
4 ; CHECK: define i32* @c1(i32* readnone %q)
5 define i32* @c1(i32* %q) {
6         ret i32* %q
7 }
8
9 ; CHECK: define void @c2(i32* %q)
10 ; It would also be acceptable to mark %q as readnone. Update @c3 too.
11 define void @c2(i32* %q) {
12         store i32* %q, i32** @g
13         ret void
14 }
15
16 ; CHECK: define void @c3(i32* %q)
17 define void @c3(i32* %q) {
18         call void @c2(i32* %q)
19         ret void
20 }
21
22 ; CHECK: define i1 @c4(i32* %q, i32 %bitno)
23 define i1 @c4(i32* %q, i32 %bitno) {
24         %tmp = ptrtoint i32* %q to i32
25         %tmp2 = lshr i32 %tmp, %bitno
26         %bit = trunc i32 %tmp2 to i1
27         br i1 %bit, label %l1, label %l0
28 l0:
29         ret i1 0 ; escaping value not caught by def-use chaining.
30 l1:
31         ret i1 1 ; escaping value not caught by def-use chaining.
32 }
33
34 @lookup_table = global [2 x i1] [ i1 0, i1 1 ]
35
36 ; CHECK: define i1 @c5(i32* %q, i32 %bitno)
37 define i1 @c5(i32* %q, i32 %bitno) {
38         %tmp = ptrtoint i32* %q to i32
39         %tmp2 = lshr i32 %tmp, %bitno
40         %bit = and i32 %tmp2, 1
41         ; subtle escape mechanism follows
42         %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
43         %val = load i1, i1* %lookup
44         ret i1 %val
45 }
46
47 declare void @throw_if_bit_set(i8*, i8) readonly
48
49 ; CHECK: define i1 @c6(i8* readonly %q, i8 %bit)
50 define i1 @c6(i8* %q, i8 %bit) personality i32 (...)* @__gxx_personality_v0 {
51         invoke void @throw_if_bit_set(i8* %q, i8 %bit)
52                 to label %ret0 unwind label %ret1
53 ret0:
54         ret i1 0
55 ret1:
56         %exn = landingpad {i8*, i32}
57                  cleanup
58         ret i1 1
59 }
60
61 declare i32 @__gxx_personality_v0(...)
62
63 define i1* @lookup_bit(i32* %q, i32 %bitno) readnone nounwind {
64         %tmp = ptrtoint i32* %q to i32
65         %tmp2 = lshr i32 %tmp, %bitno
66         %bit = and i32 %tmp2, 1
67         %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
68         ret i1* %lookup
69 }
70
71 ; CHECK: define i1 @c7(i32* readonly %q, i32 %bitno)
72 define i1 @c7(i32* %q, i32 %bitno) {
73         %ptr = call i1* @lookup_bit(i32* %q, i32 %bitno)
74         %val = load i1, i1* %ptr
75         ret i1 %val
76 }
77
78
79 ; CHECK: define i32 @nc1(i32* %q, i32* nocapture %p, i1 %b)
80 define i32 @nc1(i32* %q, i32* %p, i1 %b) {
81 e:
82         br label %l
83 l:
84         %x = phi i32* [ %p, %e ]
85         %y = phi i32* [ %q, %e ]
86         %tmp = bitcast i32* %x to i32*          ; <i32*> [#uses=2]
87         %tmp2 = select i1 %b, i32* %tmp, i32* %y
88         %val = load i32, i32* %tmp2             ; <i32> [#uses=1]
89         store i32 0, i32* %tmp
90         store i32* %y, i32** @g
91         ret i32 %val
92 }
93
94 ; CHECK: define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* nocapture %p, i1 %b)
95 define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* %p, i1 %b) {
96 e:
97         br label %l
98 l:
99         %x = phi i32 addrspace(1)* [ %p, %e ]
100         %y = phi i32* [ %q, %e ]
101         %tmp = addrspacecast i32 addrspace(1)* %x to i32*               ; <i32*> [#uses=2]
102         %tmp2 = select i1 %b, i32* %tmp, i32* %y
103         %val = load i32, i32* %tmp2             ; <i32> [#uses=1]
104         store i32 0, i32* %tmp
105         store i32* %y, i32** @g
106         ret i32 %val
107 }
108
109 ; CHECK: define void @nc2(i32* nocapture %p, i32* %q)
110 define void @nc2(i32* %p, i32* %q) {
111         %1 = call i32 @nc1(i32* %q, i32* %p, i1 0)              ; <i32> [#uses=0]
112         ret void
113 }
114
115 ; CHECK: define void @nc3(void ()* nocapture %p)
116 define void @nc3(void ()* %p) {
117         call void %p()
118         ret void
119 }
120
121 declare void @external(i8*) readonly nounwind
122 ; CHECK: define void @nc4(i8* nocapture readonly %p)
123 define void @nc4(i8* %p) {
124         call void @external(i8* %p)
125         ret void
126 }
127
128 ; CHECK: define void @nc5(void (i8*)* nocapture %f, i8* nocapture %p)
129 define void @nc5(void (i8*)* %f, i8* %p) {
130         call void %f(i8* %p) readonly nounwind
131         call void %f(i8* nocapture %p)
132         ret void
133 }
134
135 ; CHECK: define void @test1_1(i8* nocapture readnone %x1_1, i8* %y1_1)
136 ; It would be acceptable to add readnone to %y1_1 and %y1_2.
137 define void @test1_1(i8* %x1_1, i8* %y1_1) {
138   call i8* @test1_2(i8* %x1_1, i8* %y1_1)
139   store i32* null, i32** @g
140   ret void
141 }
142
143 ; CHECK: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* %y1_2)
144 define i8* @test1_2(i8* %x1_2, i8* %y1_2) {
145   call void @test1_1(i8* %x1_2, i8* %y1_2)
146   store i32* null, i32** @g
147   ret i8* %y1_2
148 }
149
150 ; CHECK: define void @test2(i8* nocapture readnone %x2)
151 define void @test2(i8* %x2) {
152   call void @test2(i8* %x2)
153   store i32* null, i32** @g
154   ret void
155 }
156
157 ; CHECK: define void @test3(i8* nocapture readnone %x3, i8* nocapture readnone %y3, i8* nocapture readnone %z3)
158 define void @test3(i8* %x3, i8* %y3, i8* %z3) {
159   call void @test3(i8* %z3, i8* %y3, i8* %x3)
160   store i32* null, i32** @g
161   ret void
162 }
163
164 ; CHECK: define void @test4_1(i8* %x4_1)
165 define void @test4_1(i8* %x4_1) {
166   call i8* @test4_2(i8* %x4_1, i8* %x4_1, i8* %x4_1)
167   store i32* null, i32** @g
168   ret void
169 }
170
171 ; CHECK: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone %y4_2, i8* nocapture readnone %z4_2)
172 define i8* @test4_2(i8* %x4_2, i8* %y4_2, i8* %z4_2) {
173   call void @test4_1(i8* null)
174   store i32* null, i32** @g
175   ret i8* %y4_2
176 }
177
178 declare i8* @test5_1(i8* %x5_1)
179
180 ; CHECK: define void @test5_2(i8* %x5_2)
181 define void @test5_2(i8* %x5_2) {
182   call i8* @test5_1(i8* %x5_2)
183   store i32* null, i32** @g
184   ret void
185 }
186
187 declare void @test6_1(i8* %x6_1, i8* nocapture %y6_1, ...)
188
189 ; CHECK: define void @test6_2(i8* %x6_2, i8* nocapture %y6_2, i8* %z6_2)
190 define void @test6_2(i8* %x6_2, i8* %y6_2, i8* %z6_2) {
191   call void (i8*, i8*, ...) @test6_1(i8* %x6_2, i8* %y6_2, i8* %z6_2)
192   store i32* null, i32** @g
193   ret void
194 }
195