1 ; RUN: opt -functionattrs -S < %s | FileCheck %s --check-prefixes=FNATTR,EITHER
2 ; RUN: opt -passes=function-attrs -S < %s | FileCheck %s --check-prefixes=FNATTR,EITHER
3 ; RUN: opt -attributor -attributor-manifest-internal -attributor-disable=false -S < %s | FileCheck %s --check-prefixes=ATTRIBUTOR,EITHER
4 ; RUN: opt -passes=attributor -attributor-manifest-internal -attributor-disable=false -S < %s | FileCheck %s --check-prefixes=ATTRIBUTOR,EITHER
6 @g = global i32* null ; <i32**> [#uses=1]
8 ; FNATTR: define i32* @c1(i32* readnone returned %q)
9 ; ATTRIBUTOR: define i32* @c1(i32* readnone returned "no-capture-maybe-returned" %q)
10 define i32* @c1(i32* %q) {
14 ; FNATTR: define void @c2(i32* %q)
15 ; ATTRIBUTOR: define void @c2(i32* writeonly %q)
16 ; It would also be acceptable to mark %q as readnone. Update @c3 too.
17 define void @c2(i32* %q) {
18 store i32* %q, i32** @g
22 ; FNATTR: define void @c3(i32* %q)
23 ; ATTRIBUTOR: define void @c3(i32* writeonly %q)
24 define void @c3(i32* %q) {
25 call void @c2(i32* %q)
29 ; FNATTR: define i1 @c4(i32* %q, i32 %bitno)
30 ; ATTRIBUTOR: define i1 @c4(i32* readnone %q, i32 %bitno)
31 define i1 @c4(i32* %q, i32 %bitno) {
32 %tmp = ptrtoint i32* %q to i32
33 %tmp2 = lshr i32 %tmp, %bitno
34 %bit = trunc i32 %tmp2 to i1
35 br i1 %bit, label %l1, label %l0
37 ret i1 0 ; escaping value not caught by def-use chaining.
39 ret i1 1 ; escaping value not caught by def-use chaining.
42 ; c4b is c4 but without the escaping part
43 ; FNATTR: define i1 @c4b(i32* %q, i32 %bitno)
44 ; ATTRIBUTOR: define i1 @c4b(i32* nocapture readnone %q, i32 %bitno)
45 define i1 @c4b(i32* %q, i32 %bitno) {
46 %tmp = ptrtoint i32* %q to i32
47 %tmp2 = lshr i32 %tmp, %bitno
48 %bit = trunc i32 %tmp2 to i1
49 br i1 %bit, label %l1, label %l0
51 ret i1 0 ; not escaping!
53 ret i1 0 ; not escaping!
56 @lookup_table = global [2 x i1] [ i1 0, i1 1 ]
58 ; FNATTR: define i1 @c5(i32* %q, i32 %bitno)
59 ; ATTRIBUTOR: define i1 @c5(i32* readonly %q, i32 %bitno)
60 define i1 @c5(i32* %q, i32 %bitno) {
61 %tmp = ptrtoint i32* %q to i32
62 %tmp2 = lshr i32 %tmp, %bitno
63 %bit = and i32 %tmp2, 1
64 ; subtle escape mechanism follows
65 %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
66 %val = load i1, i1* %lookup
70 declare void @throw_if_bit_set(i8*, i8) readonly
72 ; EITHER: define i1 @c6(i8* readonly %q, i8 %bit)
73 define i1 @c6(i8* %q, i8 %bit) personality i32 (...)* @__gxx_personality_v0 {
74 invoke void @throw_if_bit_set(i8* %q, i8 %bit)
75 to label %ret0 unwind label %ret1
79 %exn = landingpad {i8*, i32}
84 declare i32 @__gxx_personality_v0(...)
86 define i1* @lookup_bit(i32* %q, i32 %bitno) readnone nounwind {
87 %tmp = ptrtoint i32* %q to i32
88 %tmp2 = lshr i32 %tmp, %bitno
89 %bit = and i32 %tmp2, 1
90 %lookup = getelementptr [2 x i1], [2 x i1]* @lookup_table, i32 0, i32 %bit
94 ; EITHER: define i1 @c7(i32* readonly %q, i32 %bitno)
95 define i1 @c7(i32* %q, i32 %bitno) {
96 %ptr = call i1* @lookup_bit(i32* %q, i32 %bitno)
97 %val = load i1, i1* %ptr
102 ; EITHER: define i32 @nc1(i32* %q, i32* nocapture %p, i1 %b)
103 define i32 @nc1(i32* %q, i32* %p, i1 %b) {
107 %x = phi i32* [ %p, %e ]
108 %y = phi i32* [ %q, %e ]
109 %tmp = bitcast i32* %x to i32* ; <i32*> [#uses=2]
110 %tmp2 = select i1 %b, i32* %tmp, i32* %y
111 %val = load i32, i32* %tmp2 ; <i32> [#uses=1]
112 store i32 0, i32* %tmp
113 store i32* %y, i32** @g
117 ; EITHER: define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* nocapture %p, i1 %b)
118 define i32 @nc1_addrspace(i32* %q, i32 addrspace(1)* %p, i1 %b) {
122 %x = phi i32 addrspace(1)* [ %p, %e ]
123 %y = phi i32* [ %q, %e ]
124 %tmp = addrspacecast i32 addrspace(1)* %x to i32* ; <i32*> [#uses=2]
125 %tmp2 = select i1 %b, i32* %tmp, i32* %y
126 %val = load i32, i32* %tmp2 ; <i32> [#uses=1]
127 store i32 0, i32* %tmp
128 store i32* %y, i32** @g
132 ; EITHER: define void @nc2(i32* nocapture %p, i32* %q)
133 define void @nc2(i32* %p, i32* %q) {
134 %1 = call i32 @nc1(i32* %q, i32* %p, i1 0) ; <i32> [#uses=0]
139 ; FNATTR: define void @nc3(void ()* nocapture %p)
140 ; ATTRIBUTOR: define void @nc3(void ()* nocapture nonnull %p)
141 define void @nc3(void ()* %p) {
146 declare void @external(i8*) readonly nounwind
147 ; EITHER: define void @nc4(i8* nocapture readonly %p)
148 define void @nc4(i8* %p) {
149 call void @external(i8* %p)
153 ; FNATTR: define void @nc5(void (i8*)* nocapture %f, i8* nocapture %p)
154 ; ATTRIBUTOR: define void @nc5(void (i8*)* nocapture nonnull %f, i8* nocapture %p)
155 define void @nc5(void (i8*)* %f, i8* %p) {
156 call void %f(i8* %p) readonly nounwind
157 call void %f(i8* nocapture %p)
161 ; FNATTR: define void @test1_1(i8* nocapture readnone %x1_1, i8* %y1_1, i1 %c)
162 ; ATTRIBUTOR: define void @test1_1(i8* nocapture readnone %x1_1, i8* nocapture readnone %y1_1, i1 %c)
163 ; It would be acceptable to add readnone to %y1_1 and %y1_2.
164 define void @test1_1(i8* %x1_1, i8* %y1_1, i1 %c) {
165 call i8* @test1_2(i8* %x1_1, i8* %y1_1, i1 %c)
166 store i32* null, i32** @g
170 ; FNATTR: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* returned %y1_2, i1 %c)
171 ; ATTRIBUTOR: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* readnone returned "no-capture-maybe-returned" %y1_2, i1 %c)
172 define i8* @test1_2(i8* %x1_2, i8* %y1_2, i1 %c) {
173 br i1 %c, label %t, label %f
175 call void @test1_1(i8* %x1_2, i8* %y1_2, i1 %c)
176 store i32* null, i32** @g
182 ; EITHER: define void @test2(i8* nocapture readnone %x2)
183 define void @test2(i8* %x2) {
184 call void @test2(i8* %x2)
185 store i32* null, i32** @g
189 ; EITHER: define void @test3(i8* nocapture readnone %x3, i8* nocapture readnone %y3, i8* nocapture readnone %z3)
190 define void @test3(i8* %x3, i8* %y3, i8* %z3) {
191 call void @test3(i8* %z3, i8* %y3, i8* %x3)
192 store i32* null, i32** @g
196 ; FNATTR: define void @test4_1(i8* %x4_1, i1 %c)
197 ; ATTRIBUTOR: define void @test4_1(i8* nocapture readnone %x4_1, i1 %c)
198 define void @test4_1(i8* %x4_1, i1 %c) {
199 call i8* @test4_2(i8* %x4_1, i8* %x4_1, i8* %x4_1, i1 %c)
200 store i32* null, i32** @g
204 ; FNATTR: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone returned %y4_2, i8* nocapture readnone %z4_2, i1 %c)
205 ; ATTRIBUTOR: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone returned "no-capture-maybe-returned" %y4_2, i8* nocapture readnone %z4_2, i1 %c)
206 define i8* @test4_2(i8* %x4_2, i8* %y4_2, i8* %z4_2, i1 %c) {
207 br i1 %c, label %t, label %f
209 call void @test4_1(i8* null, i1 %c)
210 store i32* null, i32** @g
216 declare i8* @test5_1(i8* %x5_1)
218 ; EITHER: define void @test5_2(i8* %x5_2)
219 define void @test5_2(i8* %x5_2) {
220 call i8* @test5_1(i8* %x5_2)
221 store i32* null, i32** @g
225 declare void @test6_1(i8* %x6_1, i8* nocapture %y6_1, ...)
227 ; EITHER: define void @test6_2(i8* %x6_2, i8* nocapture %y6_2, i8* %z6_2)
228 define void @test6_2(i8* %x6_2, i8* %y6_2, i8* %z6_2) {
229 call void (i8*, i8*, ...) @test6_1(i8* %x6_2, i8* %y6_2, i8* %z6_2)
230 store i32* null, i32** @g
234 ; FNATTR: define void @test_cmpxchg(i32* nocapture %p)
235 ; ATTRIBUTOR: define void @test_cmpxchg(i32* nocapture nonnull dereferenceable(4) %p)
236 define void @test_cmpxchg(i32* %p) {
237 cmpxchg i32* %p, i32 0, i32 1 acquire monotonic
241 ; FNATTR: define void @test_cmpxchg_ptr(i32** nocapture %p, i32* %q)
242 ; ATTRIBUTOR: define void @test_cmpxchg_ptr(i32** nocapture nonnull dereferenceable(8) %p, i32* %q)
243 define void @test_cmpxchg_ptr(i32** %p, i32* %q) {
244 cmpxchg i32** %p, i32* null, i32* %q acquire monotonic
248 ; FNATTR: define void @test_atomicrmw(i32* nocapture %p)
249 ; ATTRIBUTOR: define void @test_atomicrmw(i32* nocapture nonnull dereferenceable(4) %p)
250 define void @test_atomicrmw(i32* %p) {
251 atomicrmw add i32* %p, i32 1 seq_cst
255 ; EITHER: define void @test_volatile(i32* %x)
256 define void @test_volatile(i32* %x) {
258 %gep = getelementptr i32, i32* %x, i64 1
259 store volatile i32 0, i32* %gep, align 4
263 ; EITHER: nocaptureLaunder(i8* nocapture %p)
264 define void @nocaptureLaunder(i8* %p) {
266 %b = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
271 @g2 = global i8* null
272 ; EITHER: define void @captureLaunder(i8* %p)
273 define void @captureLaunder(i8* %p) {
274 %b = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
275 store i8* %b, i8** @g2
279 ; FNATTR: @nocaptureStrip(i8* nocapture %p)
280 ; ATTRIBUTOR: @nocaptureStrip(i8* nocapture writeonly %p)
281 define void @nocaptureStrip(i8* %p) {
283 %b = call i8* @llvm.strip.invariant.group.p0i8(i8* %p)
288 @g3 = global i8* null
289 ; FNATTR: define void @captureStrip(i8* %p)
290 ; ATTRIBUTOR: define void @captureStrip(i8* writeonly %p)
291 define void @captureStrip(i8* %p) {
292 %b = call i8* @llvm.strip.invariant.group.p0i8(i8* %p)
293 store i8* %b, i8** @g3
297 ; EITHER: define i1 @captureICmp(i32* readnone %x)
298 define i1 @captureICmp(i32* %x) {
299 %1 = icmp eq i32* %x, null
303 ; EITHER: define i1 @captureICmpRev(i32* readnone %x)
304 define i1 @captureICmpRev(i32* %x) {
305 %1 = icmp eq i32* null, %x
309 ; EITHER: define i1 @nocaptureInboundsGEPICmp(i32* nocapture readnone %x)
310 define i1 @nocaptureInboundsGEPICmp(i32* %x) {
311 %1 = getelementptr inbounds i32, i32* %x, i32 5
312 %2 = bitcast i32* %1 to i8*
313 %3 = icmp eq i8* %2, null
317 ; EITHER: define i1 @nocaptureInboundsGEPICmpRev(i32* nocapture readnone %x)
318 define i1 @nocaptureInboundsGEPICmpRev(i32* %x) {
319 %1 = getelementptr inbounds i32, i32* %x, i32 5
320 %2 = bitcast i32* %1 to i8*
321 %3 = icmp eq i8* null, %2
325 ; EITHER: define i1 @nocaptureDereferenceableOrNullICmp(i32* nocapture readnone dereferenceable_or_null(4) %x)
326 define i1 @nocaptureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) {
327 %1 = bitcast i32* %x to i8*
328 %2 = icmp eq i8* %1, null
332 ; EITHER: define i1 @captureDereferenceableOrNullICmp(i32* readnone dereferenceable_or_null(4) %x)
333 define i1 @captureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) "null-pointer-is-valid"="true" {
334 %1 = bitcast i32* %x to i8*
335 %2 = icmp eq i8* %1, null
339 declare void @unknown(i8*)
340 define void @test_callsite() {
342 ; We know that 'null' in AS 0 does not alias anything and cannot be captured. Though the latter is not qurried -> derived atm.
343 ; ATTRIBUTOR: call void @unknown(i8* noalias null)
344 call void @unknown(i8* null)
348 declare i8* @unknownpi8pi8(i8*,i8* returned)
349 define i8* @test_returned1(i8* %A, i8* returned %B) nounwind readonly {
350 ; ATTRIBUTOR: define i8* @test_returned1(i8* nocapture readonly %A, i8* readonly returned %B)
352 %p = call i8* @unknownpi8pi8(i8* %A, i8* %B)
356 define i8* @test_returned2(i8* %A, i8* %B) {
357 ; ATTRIBUTOR: define i8* @test_returned2(i8* nocapture readonly %A, i8* readonly returned %B)
359 %p = call i8* @unknownpi8pi8(i8* %A, i8* %B) nounwind readonly
363 declare i8* @llvm.launder.invariant.group.p0i8(i8*)
364 declare i8* @llvm.strip.invariant.group.p0i8(i8*)