1 ; RUN: opt -function-attrs -S < %s | FileCheck %s --check-prefixes=FNATTR
2 ; RUN: opt -passes=function-attrs -S < %s | FileCheck %s --check-prefixes=FNATTR
4 @g = global i32* null ; <i32**> [#uses=1]
6 ; FNATTR: define i32* @c1(i32* readnone returned %q)
7 define i32* @c1(i32* %q) {
11 ; FNATTR: define void @c2(i32* %q)
12 ; It would also be acceptable to mark %q as readnone. Update @c3 too.
13 define void @c2(i32* %q) {
14 store i32* %q, i32** @g
18 ; FNATTR: define void @c3(i32* %q)
19 define void @c3(i32* %q) {
20 call void @c2(i32* %q)
24 ; FNATTR: define i1 @c4(i32* %q, i32 %bitno)
25 define i1 @c4(i32* %q, i32 %bitno) {
26 %tmp = ptrtoint i32* %q to i32
27 %tmp2 = lshr i32 %tmp, %bitno
28 %bit = trunc i32 %tmp2 to i1
29 br i1 %bit, label %l1, label %l0
31 ret i1 0 ; escaping value not caught by def-use chaining.
33 ret i1 1 ; escaping value not caught by def-use chaining.
36 ; c4b is c4 but without the escaping part
37 ; FNATTR: define i1 @c4b(i32* %q, i32 %bitno)
38 define i1 @c4b(i32* %q, i32 %bitno) {
39 %tmp = ptrtoint i32* %q to i32
40 %tmp2 = lshr i32 %tmp, %bitno
41 %bit = trunc i32 %tmp2 to i1
42 br i1 %bit, label %l1, label %l0
44 ret i1 0 ; not escaping!
46 ret i1 0 ; not escaping!
49 @lookup_table = global [2 x i1] [ i1 0, i1 1 ]
51 ; FNATTR: define i1 @c5(i32* %q, i32 %bitno)
52 define i1 @c5(i32* %q, i32 %bitno) {
53 %tmp = ptrtoint i32* %q to i32
54 %tmp2 = lshr i32 %tmp, %bitno
55 %bit = and i32 %tmp2, 1
56 ; subtle escape mechanism follows
57 %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
58 %val = load i1, i1* %lookup
62 declare void @throw_if_bit_set(i8*, i8) readonly
64 ; FNATTR: define i1 @c6(i8* readonly %q, i8 %bit)
65 define i1 @c6(i8* %q, i8 %bit) personality i32 (...)* @__gxx_personality_v0 {
66 invoke void @throw_if_bit_set(i8* %q, i8 %bit)
67 to label %ret0 unwind label %ret1
71 %exn = landingpad {i8*, i32}
76 declare i32 @__gxx_personality_v0(...)
78 define i1* @lookup_bit(i32* %q, i32 %bitno) readnone nounwind {
79 %tmp = ptrtoint i32* %q to i32
80 %tmp2 = lshr i32 %tmp, %bitno
81 %bit = and i32 %tmp2, 1
82 %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
86 ; FNATTR: define i1 @c7(i32* readonly %q, i32 %bitno)
87 define i1 @c7(i32* %q, i32 %bitno) {
88 %ptr = call i1* @lookup_bit(i32* %q, i32 %bitno)
89 %val = load i1, i1* %ptr
94 ; FNATTR: define i32 @nc1(i32* %q, i32* nocapture %p, i1 %b)
95 define i32 @nc1(i32* %q, i32* %p, i1 %b) {
99 %x = phi i32* [ %p, %e ]
100 %y = phi i32* [ %q, %e ]
101 %tmp = bitcast i32* %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
109 ; FNATTR: define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* nocapture %p, i1 %b)
110 define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* %p, i1 %b) {
114 %x = phi i32 addrspace(1)* [ %p, %e ]
115 %y = phi i32* [ %q, %e ]
116 %tmp = addrspacecast i32 addrspace(1)* %x to i32* ; <i32*> [#uses=2]
117 %tmp2 = select i1 %b, i32* %tmp, i32* %y
118 %val = load i32, i32* %tmp2 ; <i32> [#uses=1]
119 store i32 0, i32* %tmp
120 store i32* %y, i32** @g
124 ; FNATTR: define void @nc2(i32* nocapture %p, i32* %q)
125 define void @nc2(i32* %p, i32* %q) {
126 %1 = call i32 @nc1(i32* %q, i32* %p, i1 0) ; <i32> [#uses=0]
131 ; FNATTR: define void @nc3(void ()* nocapture %p)
132 define void @nc3(void ()* %p) {
137 declare void @external(i8*) readonly nounwind
138 ; FNATTR: define void @nc4(i8* nocapture readonly %p)
139 define void @nc4(i8* %p) {
140 call void @external(i8* %p)
144 ; FNATTR: define void @nc5(void (i8*)* nocapture %f, i8* nocapture %p)
145 define void @nc5(void (i8*)* %f, i8* %p) {
146 call void %f(i8* %p) readonly nounwind
147 call void %f(i8* nocapture %p)
151 ; FNATTR: define void @test1_1(i8* nocapture readnone %x1_1, i8* %y1_1, i1 %c)
152 ; It would be acceptable to add readnone to %y1_1 and %y1_2.
153 define void @test1_1(i8* %x1_1, i8* %y1_1, i1 %c) {
154 call i8* @test1_2(i8* %x1_1, i8* %y1_1, i1 %c)
155 store i32* null, i32** @g
159 ; FNATTR: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* returned %y1_2, i1 %c)
160 define i8* @test1_2(i8* %x1_2, i8* %y1_2, i1 %c) {
161 br i1 %c, label %t, label %f
163 call void @test1_1(i8* %x1_2, i8* %y1_2, i1 %c)
164 store i32* null, i32** @g
170 ; FNATTR: define void @test2(i8* nocapture readnone %x2)
171 define void @test2(i8* %x2) {
172 call void @test2(i8* %x2)
173 store i32* null, i32** @g
177 ; FNATTR: define void @test3(i8* nocapture readnone %x3, i8* nocapture readnone %y3, i8* nocapture readnone %z3)
178 define void @test3(i8* %x3, i8* %y3, i8* %z3) {
179 call void @test3(i8* %z3, i8* %y3, i8* %x3)
180 store i32* null, i32** @g
184 ; FNATTR: define void @test4_1(i8* %x4_1, i1 %c)
185 define void @test4_1(i8* %x4_1, i1 %c) {
186 call i8* @test4_2(i8* %x4_1, i8* %x4_1, i8* %x4_1, i1 %c)
187 store i32* null, i32** @g
191 ; FNATTR: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone returned %y4_2, i8* nocapture readnone %z4_2, i1 %c)
192 define i8* @test4_2(i8* %x4_2, i8* %y4_2, i8* %z4_2, i1 %c) {
193 br i1 %c, label %t, label %f
195 call void @test4_1(i8* null, i1 %c)
196 store i32* null, i32** @g
202 declare i8* @test5_1(i8* %x5_1)
204 ; FNATTR: define void @test5_2(i8* %x5_2)
205 define void @test5_2(i8* %x5_2) {
206 call i8* @test5_1(i8* %x5_2)
207 store i32* null, i32** @g
211 declare void @test6_1(i8* %x6_1, i8* nocapture %y6_1, ...)
213 ; FNATTR: define void @test6_2(i8* %x6_2, i8* nocapture %y6_2, i8* %z6_2)
214 define void @test6_2(i8* %x6_2, i8* %y6_2, i8* %z6_2) {
215 call void (i8*, i8*, ...) @test6_1(i8* %x6_2, i8* %y6_2, i8* %z6_2)
216 store i32* null, i32** @g
220 ; FNATTR: define void @test_cmpxchg(i32* nocapture %p)
221 define void @test_cmpxchg(i32* %p) {
222 cmpxchg i32* %p, i32 0, i32 1 acquire monotonic
226 ; FNATTR: define void @test_cmpxchg_ptr(i32** nocapture %p, i32* %q)
227 define void @test_cmpxchg_ptr(i32** %p, i32* %q) {
228 cmpxchg i32** %p, i32* null, i32* %q acquire monotonic
232 ; FNATTR: define void @test_atomicrmw(i32* nocapture %p)
233 define void @test_atomicrmw(i32* %p) {
234 atomicrmw add i32* %p, i32 1 seq_cst
238 ; FNATTR: define void @test_volatile(i32* %x)
239 define void @test_volatile(i32* %x) {
241 %gep = getelementptr i32, i32* %x, i64 1
242 store volatile i32 0, i32* %gep, align 4
246 ; FNATTR: nocaptureLaunder(i8* nocapture %p)
247 define void @nocaptureLaunder(i8* %p) {
249 %b = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
254 @g2 = global i8* null
255 ; FNATTR: define void @captureLaunder(i8* %p)
256 define void @captureLaunder(i8* %p) {
257 %b = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
258 store i8* %b, i8** @g2
262 ; FNATTR: @nocaptureStrip(i8* nocapture %p)
263 define void @nocaptureStrip(i8* %p) {
265 %b = call i8* @llvm.strip.invariant.group.p0i8(i8* %p)
270 @g3 = global i8* null
271 ; FNATTR: define void @captureStrip(i8* %p)
272 define void @captureStrip(i8* %p) {
273 %b = call i8* @llvm.strip.invariant.group.p0i8(i8* %p)
274 store i8* %b, i8** @g3
278 ; FNATTR: define i1 @captureICmp(i32* readnone %x)
279 define i1 @captureICmp(i32* %x) {
280 %1 = icmp eq i32* %x, null
284 ; FNATTR: define i1 @captureICmpRev(i32* readnone %x)
285 define i1 @captureICmpRev(i32* %x) {
286 %1 = icmp eq i32* null, %x
290 ; FNATTR: define i1 @nocaptureInboundsGEPICmp(i32* nocapture readnone %x)
291 define i1 @nocaptureInboundsGEPICmp(i32* %x) {
292 %1 = getelementptr inbounds i32, i32* %x, i32 5
293 %2 = bitcast i32* %1 to i8*
294 %3 = icmp eq i8* %2, null
298 ; FNATTR: define i1 @nocaptureInboundsGEPICmpRev(i32* nocapture readnone %x)
299 define i1 @nocaptureInboundsGEPICmpRev(i32* %x) {
300 %1 = getelementptr inbounds i32, i32* %x, i32 5
301 %2 = bitcast i32* %1 to i8*
302 %3 = icmp eq i8* null, %2
306 ; FNATTR: define i1 @nocaptureDereferenceableOrNullICmp(i32* nocapture readnone dereferenceable_or_null(4) %x)
307 define i1 @nocaptureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) {
308 %1 = bitcast i32* %x to i8*
309 %2 = icmp eq i8* %1, null
313 ; FNATTR: define i1 @captureDereferenceableOrNullICmp(i32* readnone dereferenceable_or_null(4) %x)
314 define i1 @captureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) null_pointer_is_valid {
315 %1 = bitcast i32* %x to i8*
316 %2 = icmp eq i8* %1, null
320 declare i8* @llvm.launder.invariant.group.p0i8(i8*)
321 declare i8* @llvm.strip.invariant.group.p0i8(i8*)