1 ; RUN: opt -functionattrs -attributor -attributor-disable=false -S < %s | FileCheck %s
3 ; Test cases specifically designed for the "no-capture" argument attribute.
4 ; We use FIXME's to indicate problems and missing attributes.
6 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8 ; TEST comparison against NULL
10 ; int is_null_return(int *p) {
14 ; FIXME: no-capture missing for %p
15 ; CHECK: define i32 @is_null_return(i32* readnone %p)
16 define i32 @is_null_return(i32* %p) #0 {
18 %cmp = icmp eq i32* %p, null
19 %conv = zext i1 %cmp to i32
23 ; TEST comparison against NULL in control flow
25 ; int is_null_control(int *p) {
33 ; FIXME: no-capture missing for %p
34 ; CHECK: define i32 @is_null_control(i32* readnone %p)
35 define i32 @is_null_control(i32* %p) #0 {
37 %retval = alloca i32, align 4
38 %cmp = icmp eq i32* %p, null
39 br i1 %cmp, label %if.then, label %if.end
41 if.then: ; preds = %entry
42 store i32 1, i32* %retval, align 4
45 if.end: ; preds = %entry
46 %cmp1 = icmp eq i32* null, %p
47 br i1 %cmp1, label %if.then2, label %if.end3
49 if.then2: ; preds = %if.end
50 store i32 1, i32* %retval, align 4
53 if.end3: ; preds = %if.end
54 store i32 0, i32* %retval, align 4
57 return: ; preds = %if.end3, %if.then2, %if.then
58 %0 = load i32, i32* %retval, align 4
64 ; double *srec0(double *a) {
69 ; CHECK: define noalias nonnull align 536870912 dereferenceable(4294967295) double* @srec0(double* nocapture readnone %a)
70 define double* @srec0(double* %a) #0 {
72 %call = call double* @srec0(double* %a)
76 ; TEST singleton SCC with lots of nested recursive calls
78 ; int* srec16(int* a) {
79 ; return srec16(srec16(srec16(srec16(
80 ; srec16(srec16(srec16(srec16(
81 ; srec16(srec16(srec16(srec16(
82 ; srec16(srec16(srec16(srec16(
87 ; Other arguments are possible here due to the no-return behavior.
89 ; CHECK: define noalias nonnull align 536870912 dereferenceable(4294967295) i32* @srec16(i32* nocapture readnone %a)
90 define i32* @srec16(i32* %a) #0 {
92 %call = call i32* @srec16(i32* %a)
93 ; CHECK: %call = call i32* @srec16(i32* %a)
94 ; CHECK-NEXT: unreachable
95 %call1 = call i32* @srec16(i32* %call)
96 %call2 = call i32* @srec16(i32* %call1)
97 %call3 = call i32* @srec16(i32* %call2)
98 %call4 = call i32* @srec16(i32* %call3)
99 %call5 = call i32* @srec16(i32* %call4)
100 %call6 = call i32* @srec16(i32* %call5)
101 %call7 = call i32* @srec16(i32* %call6)
102 %call8 = call i32* @srec16(i32* %call7)
103 %call9 = call i32* @srec16(i32* %call8)
104 %call10 = call i32* @srec16(i32* %call9)
105 %call11 = call i32* @srec16(i32* %call10)
106 %call12 = call i32* @srec16(i32* %call11)
107 %call13 = call i32* @srec16(i32* %call12)
108 %call14 = call i32* @srec16(i32* %call13)
109 %call15 = call i32* @srec16(i32* %call14)
113 ; TEST SCC with various calls, casts, and comparisons agains NULL
115 ; FIXME: no-capture missing for %a
116 ; CHECK: define float* @scc_A(i32* readnone returned %a)
118 ; FIXME: no-capture missing for %a
119 ; CHECK: define i64* @scc_B(double* readnone returned %a)
121 ; FIXME: readnone missing for %s
122 ; FIXME: no-capture missing for %a
123 ; CHECK: define i8* @scc_C(i16* returned %a)
125 ; float *scc_A(int *a) {
126 ; return (float*)(a ? (int*)scc_A((int*)scc_B((double*)scc_C((short*)a))) : a);
129 ; long *scc_B(double *a) {
130 ; return (long*)(a ? scc_C((short*)scc_B((double*)scc_A((int*)a))) : a);
133 ; void *scc_C(short *a) {
134 ; return scc_A((int*)(scc_A(a) ? scc_B((double*)a) : scc_C(a)));
136 define float* @scc_A(i32* %a) {
138 %tobool = icmp ne i32* %a, null
139 br i1 %tobool, label %cond.true, label %cond.false
141 cond.true: ; preds = %entry
142 %0 = bitcast i32* %a to i16*
143 %call = call i8* @scc_C(i16* %0)
144 %1 = bitcast i8* %call to double*
145 %call1 = call i64* @scc_B(double* %1)
146 %2 = bitcast i64* %call1 to i32*
147 %call2 = call float* @scc_A(i32* %2)
148 %3 = bitcast float* %call2 to i32*
151 cond.false: ; preds = %entry
154 cond.end: ; preds = %cond.false, %cond.true
155 %cond = phi i32* [ %3, %cond.true ], [ %a, %cond.false ]
156 %4 = bitcast i32* %cond to float*
160 define i64* @scc_B(double* %a) {
162 %tobool = icmp ne double* %a, null
163 br i1 %tobool, label %cond.true, label %cond.false
165 cond.true: ; preds = %entry
166 %0 = bitcast double* %a to i32*
167 %call = call float* @scc_A(i32* %0)
168 %1 = bitcast float* %call to double*
169 %call1 = call i64* @scc_B(double* %1)
170 %2 = bitcast i64* %call1 to i16*
171 %call2 = call i8* @scc_C(i16* %2)
174 cond.false: ; preds = %entry
175 %3 = bitcast double* %a to i8*
178 cond.end: ; preds = %cond.false, %cond.true
179 %cond = phi i8* [ %call2, %cond.true ], [ %3, %cond.false ]
180 %4 = bitcast i8* %cond to i64*
184 define i8* @scc_C(i16* %a) {
186 %bc = bitcast i16* %a to i32*
187 %call = call float* @scc_A(i32* %bc)
188 %bc2 = bitcast float* %call to i8*
189 %tobool = icmp ne i8* %bc2, null
190 br i1 %tobool, label %cond.true, label %cond.false
192 cond.true: ; preds = %entry
193 %0 = bitcast i16* %a to double*
194 %call1 = call i64* @scc_B(double* %0)
195 %1 = bitcast i64* %call1 to i8*
198 cond.false: ; preds = %entry
199 %call2 = call i8* @scc_C(i16* %a)
202 cond.end: ; preds = %cond.false, %cond.true
203 %cond = phi i8* [ %1, %cond.true ], [ %call2, %cond.false ]
204 %2 = bitcast i8* %cond to i32*
205 %call3 = call float* @scc_A(i32* %2)
206 %3 = bitcast float* %call3 to i8*
211 ; TEST call to external function, marked no-capture
213 ; void external_no_capture(int /* no-capture */ *p);
214 ; void test_external_no_capture(int *p) {
215 ; external_no_capture(p);
218 ; CHECK: define void @test_external_no_capture(i32* nocapture %p)
219 declare void @external_no_capture(i32* nocapture)
221 define void @test_external_no_capture(i32* %p) #0 {
223 call void @external_no_capture(i32* %p)
227 ; TEST call to external var-args function, marked no-capture
229 ; void test_var_arg_call(char *p, int a) {
233 ; CHECK: define void @test_var_arg_call(i8* nocapture %p, i32 %a)
234 define void @test_var_arg_call(i8* %p, i32 %a) #0 {
236 %call = call i32 (i8*, ...) @printf(i8* %p, i32 %a)
240 declare i32 @printf(i8* nocapture, ...)
243 ; TEST "captured" only through return
245 ; long *not_captured_but_returned_0(long *a) {
250 ; There should *not* be a no-capture attribute on %a
251 ; CHECK: define i64* @not_captured_but_returned_0(i64* returned %a)
252 define i64* @not_captured_but_returned_0(i64* %a) #0 {
254 store i64 0, i64* %a, align 8
258 ; TEST "captured" only through return
260 ; long *not_captured_but_returned_1(long *a) {
265 ; There should *not* be a no-capture attribute on %a
266 ; CHECK: define nonnull i64* @not_captured_but_returned_1(i64* %a)
267 define i64* @not_captured_but_returned_1(i64* %a) #0 {
269 %add.ptr = getelementptr inbounds i64, i64* %a, i64 1
270 store i64 1, i64* %add.ptr, align 8
274 ; TEST calls to "captured" only through return functions
276 ; void test_not_captured_but_returned_calls(long *a) {
277 ; not_captured_but_returned_0(a);
278 ; not_captured_but_returned_1(a);
281 ; FIXME: no-capture missing for %a
282 ; CHECK: define void @test_not_captured_but_returned_calls(i64* %a)
283 define void @test_not_captured_but_returned_calls(i64* %a) #0 {
285 %call = call i64* @not_captured_but_returned_0(i64* %a)
286 %call1 = call i64* @not_captured_but_returned_1(i64* %a)
290 ; TEST "captured" only through transitive return
292 ; long* negative_test_not_captured_but_returned_call_0a(long *a) {
293 ; return not_captured_but_returned_0(a);
296 ; There should *not* be a no-capture attribute on %a
297 ; CHECK: define i64* @negative_test_not_captured_but_returned_call_0a(i64* returned %a)
298 define i64* @negative_test_not_captured_but_returned_call_0a(i64* %a) #0 {
300 %call = call i64* @not_captured_but_returned_0(i64* %a)
304 ; TEST captured through write
306 ; void negative_test_not_captured_but_returned_call_0b(long *a) {
307 ; *a = (long)not_captured_but_returned_0(a);
310 ; There should *not* be a no-capture attribute on %a
311 ; CHECK: define void @negative_test_not_captured_but_returned_call_0b(i64* %a)
312 define void @negative_test_not_captured_but_returned_call_0b(i64* %a) #0 {
314 %call = call i64* @not_captured_but_returned_0(i64* %a)
315 %0 = ptrtoint i64* %call to i64
316 store i64 %0, i64* %a, align 8
320 ; TEST "captured" only through transitive return
322 ; long* negative_test_not_captured_but_returned_call_1a(long *a) {
323 ; return not_captured_but_returned_1(a);
326 ; There should *not* be a no-capture attribute on %a
327 ; CHECK: define nonnull i64* @negative_test_not_captured_but_returned_call_1a(i64* %a)
328 define i64* @negative_test_not_captured_but_returned_call_1a(i64* %a) #0 {
330 %call = call i64* @not_captured_but_returned_1(i64* %a)
334 ; TEST captured through write
336 ; void negative_test_not_captured_but_returned_call_1b(long *a) {
337 ; *a = (long)not_captured_but_returned_1(a);
340 ; There should *not* be a no-capture attribute on %a
341 ; CHECK: define void @negative_test_not_captured_but_returned_call_1b(i64* %a)
342 define void @negative_test_not_captured_but_returned_call_1b(i64* %a) #0 {
344 %call = call i64* @not_captured_but_returned_1(i64* %a)
345 %0 = ptrtoint i64* %call to i64
346 store i64 %0, i64* %call, align 8
350 ; TEST return argument or unknown call result
352 ; int* ret_arg_or_unknown(int* b) {
358 ; Verify we do *not* assume b is returned or not captured.
360 ; CHECK: define i32* @ret_arg_or_unknown(i32* readnone %b)
361 ; CHECK: define i32* @ret_arg_or_unknown_through_phi(i32* readnone %b)
362 declare i32* @unknown()
364 define i32* @ret_arg_or_unknown(i32* %b) #0 {
366 %cmp = icmp eq i32* %b, null
367 br i1 %cmp, label %ret_arg, label %ret_unknown
373 %call = call i32* @unknown()
377 define i32* @ret_arg_or_unknown_through_phi(i32* %b) #0 {
379 %cmp = icmp eq i32* %b, null
380 br i1 %cmp, label %ret_arg, label %ret_unknown
386 %call = call i32* @unknown()
390 %phi = phi i32* [ %b, %ret_arg ], [ %call, %ret_unknown ]
395 ; TEST not captured by readonly external function
397 ; CHECK: define void @not_captured_by_readonly_call(i32* nocapture %b)
398 declare i32* @readonly_unknown(i32*, i32*) readonly
400 define void @not_captured_by_readonly_call(i32* %b) #0 {
402 %call = call i32* @readonly_unknown(i32* %b, i32* %b)
407 ; TEST not captured by readonly external function if return chain is known
409 ; Make sure the returned flag on %r is strong enough to justify nocapture on %b but **not** on %r.
411 ; FIXME: The "returned" information is not propagated to the fullest extend causing us to miss "nocapture" on %b in the following:
412 ; CHECK: define i32* @not_captured_by_readonly_call_not_returned_either1(i32* readonly %b, i32* readonly returned %r)
414 ; CHECK: define i32* @not_captured_by_readonly_call_not_returned_either2(i32* readonly %b, i32* readonly returned %r)
415 ; CHECK: define i32* @not_captured_by_readonly_call_not_returned_either3(i32* readonly %b, i32* readonly returned %r)
417 ; FIXME: The "nounwind" information is not derived to the fullest extend causing us to miss "nocapture" on %b in the following:
418 ; CHECK: define i32* @not_captured_by_readonly_call_not_returned_either4(i32* readonly %b, i32* readonly returned %r)
419 define i32* @not_captured_by_readonly_call_not_returned_either1(i32* %b, i32* returned %r) #0 {
421 %call = call i32* @readonly_unknown(i32* %b, i32* %r) nounwind
425 declare i32* @readonly_unknown_r1a(i32*, i32* returned) readonly
426 define i32* @not_captured_by_readonly_call_not_returned_either2(i32* %b, i32* %r) #0 {
428 %call = call i32* @readonly_unknown_r1a(i32* %b, i32* %r) nounwind
432 declare i32* @readonly_unknown_r1b(i32*, i32* returned) readonly nounwind
433 define i32* @not_captured_by_readonly_call_not_returned_either3(i32* %b, i32* %r) #0 {
435 %call = call i32* @readonly_unknown_r1b(i32* %b, i32* %r)
439 define i32* @not_captured_by_readonly_call_not_returned_either4(i32* %b, i32* %r) #0 {
441 %call = call i32* @readonly_unknown_r1a(i32* %b, i32* %r)
445 attributes #0 = { noinline nounwind uwtable }