1 ; RUN: opt -S -functionattrs -enable-nonnull-arg-prop %s | FileCheck %s --check-prefixes=BOTH,FNATTR
2 ; RUN: opt -S -passes=function-attrs -enable-nonnull-arg-prop %s | FileCheck %s --check-prefixes=BOTH,FNATTR
3 ; RUN: opt -attributor --attributor-disable=false -attributor-max-iterations-verify -attributor-max-iterations=8 -S < %s | FileCheck %s --check-prefixes=BOTH,ATTRIBUTOR
5 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 declare nonnull i8* @ret_nonnull()
9 ; Return a pointer trivially nonnull (call return attribute)
11 ; BOTH: define nonnull i8* @test1
12 %ret = call i8* @ret_nonnull()
16 ; Return a pointer trivially nonnull (argument attribute)
17 define i8* @test2(i8* nonnull %p) {
18 ; BOTH: define nonnull i8* @test2
22 ; Given an SCC where one of the functions can not be marked nonnull,
23 ; can we still mark the other one which is trivially nonnull
24 define i8* @scc_binder(i1 %c) {
25 ; FNATTR: define i8* @scc_binder
26 ; ATTRIBUTOR: define noalias i8* @scc_binder
27 br i1 %c, label %rec, label %end
29 call i8* @test3(i1 %c)
35 define i8* @test3(i1 %c) {
36 ; BOTH: define nonnull i8* @test3
37 call i8* @scc_binder(i1 %c)
38 %ret = call i8* @ret_nonnull()
42 ; Given a mutual recursive set of functions, we can mark them
43 ; nonnull if neither can ever return null. (In this case, they
44 ; just never return period.)
45 define i8* @test4_helper() {
46 ; FNATTR: define noalias nonnull i8* @test4_helper
47 ; ATTRIBUTOR: define noalias nonnull align 536870912 dereferenceable(4294967295) i8* @test4_helper
48 %ret = call i8* @test4()
53 ; FNATTR: define noalias nonnull i8* @test4
54 ; ATTRIBUTOR: define noalias nonnull align 536870912 dereferenceable(4294967295) i8* @test4
55 %ret = call i8* @test4_helper()
59 ; Given a mutual recursive set of functions which *can* return null
60 ; make sure we haven't marked them as nonnull.
61 define i8* @test5_helper(i1 %c) {
62 ; FNATTR: define noalias i8* @test5_helper
63 ; ATTRIBUTOR: define noalias i8* @test5_helper
64 br i1 %c, label %rec, label %end
66 %ret = call i8* @test5(i1 %c)
72 define i8* @test5(i1 %c) {
73 ; FNATTR: define noalias i8* @test5
74 ; ATTRIBUTOR: define noalias i8* @test5
75 %ret = call i8* @test5_helper(i1 %c)
79 ; Local analysis, but going through a self recursive phi
82 ; BOTH: define nonnull i8* @test6
83 %ret = call i8* @ret_nonnull()
86 %phi = phi i8* [%ret, %entry], [%phi, %loop]
87 br i1 undef, label %loop, label %exit
92 ; BOTH: define i8* @test7
93 define i8* @test7(i8* %a) {
94 %b = getelementptr inbounds i8, i8* %a, i64 0
98 ; BOTH: define nonnull i8* @test8
99 define i8* @test8(i8* %a) {
100 %b = getelementptr inbounds i8, i8* %a, i64 1
104 ; BOTH: define i8* @test9
105 define i8* @test9(i8* %a, i64 %n) {
106 %b = getelementptr inbounds i8, i8* %a, i64 %n
110 declare void @llvm.assume(i1)
111 ; FNATTR: define i8* @test10
112 ; FIXME: missing nonnull
113 ; ATTRIBUTOR: define i8* @test10
114 define i8* @test10(i8* %a, i64 %n) {
115 %cmp = icmp ne i64 %n, 0
116 call void @llvm.assume(i1 %cmp)
117 %b = getelementptr inbounds i8, i8* %a, i64 %n
122 ; char* test11(char *p) {
123 ; return p? p: nonnull();
125 ; FNATTR: define i8* @test11
126 ; FIXME: missing nonnull
127 ; ATTRIBUTOR: define i8* @test11
128 define i8* @test11(i8*) local_unnamed_addr {
129 %2 = icmp eq i8* %0, null
130 br i1 %2, label %3, label %5
132 ; <label>:3: ; preds = %1
133 %4 = tail call i8* @ret_nonnull()
136 ; <label>:5: ; preds = %3, %1
137 %6 = phi i8* [ %4, %3 ], [ %0, %1 ]
142 ; Simple CallSite Test
143 declare void @test12_helper(i8*)
144 define void @test12(i8* nonnull %a) {
145 ; ATTRIBUTOR: define void @test12(i8* nonnull %a)
146 ; ATTRIBUTOR-NEXT: tail call void @test12_helper(i8* nonnull %a)
147 tail call void @test12_helper(i8* %a)
152 ; Simple Argument Tests
153 declare i8* @unknown()
154 define void @test13_helper() {
155 %nonnullptr = tail call i8* @ret_nonnull()
156 %maybenullptr = tail call i8* @unknown()
157 tail call void @test13(i8* %nonnullptr, i8* %nonnullptr, i8* %maybenullptr)
158 tail call void @test13(i8* %nonnullptr, i8* %maybenullptr, i8* %nonnullptr)
161 define internal void @test13(i8* %a, i8* %b, i8* %c) {
162 ; ATTRIBUTOR: define internal void @test13(i8* nocapture nonnull readnone %a, i8* nocapture readnone %b, i8* nocapture readnone %c)
166 declare nonnull i8* @nonnull()
169 ; Complex propagation
170 ; Argument of f1, f2, f3 can be marked with nonnull.
173 ; 1. In f1:bb6, %arg can be marked with nonnull because of the comparison in bb1
174 ; 2. Because f2 is internal function, f2(i32* %arg) -> @f2(i32* nonnull %arg)
175 ; 3. In f1:bb4 %tmp5 is nonnull and f3 is internal function.
176 ; Then, f3(i32* %arg) -> @f3(i32* nonnull %arg)
177 ; 4. We get nonnull in whole f1 call sites so f1(i32* %arg) -> @f1(i32* nonnull %arg)
180 define internal i32* @f1(i32* %arg) {
181 ; FIXME: missing nonnull It should be nonnull @f1(i32* nonnull readonly %arg)
182 ; ATTRIBUTOR: define internal nonnull i32* @f1(i32* readonly %arg)
185 %tmp = icmp eq i32* %arg, null
186 br i1 %tmp, label %bb9, label %bb1
189 %tmp2 = load i32, i32* %arg, align 4
190 %tmp3 = icmp eq i32 %tmp2, 0
191 br i1 %tmp3, label %bb6, label %bb4
194 %tmp5 = getelementptr inbounds i32, i32* %arg, i64 1
195 ; ATTRIBUTOR: %tmp5b = tail call i32* @f3(i32* nonnull %tmp5)
196 %tmp5b = tail call i32* @f3(i32* %tmp5)
200 ; FIXME: missing nonnull. It should be @f2(i32* nonnull %arg)
201 ; ATTRIBUTOR: %tmp7 = tail call nonnull i32* @f2(i32* readonly %arg)
202 %tmp7 = tail call i32* @f2(i32* %arg)
205 bb9: ; preds = %bb4, %bb
206 %tmp10 = phi i32* [ %tmp5, %bb4 ], [ inttoptr (i64 4 to i32*), %bb ]
210 define internal i32* @f2(i32* %arg) {
211 ; FIXME: missing nonnull. It should be nonnull @f2(i32* nonnull %arg)
212 ; ATTRIBUTOR: define internal nonnull i32* @f2(i32* readonly %arg)
215 ; FIXME: missing nonnull. It should be @f1(i32* nonnull readonly %arg)
216 ; ATTRIBUTOR: %tmp = tail call nonnull i32* @f1(i32* readonly %arg)
217 %tmp = tail call i32* @f1(i32* %arg)
221 define dso_local noalias i32* @f3(i32* %arg) {
222 ; FIXME: missing nonnull. It should be nonnull @f3(i32* nonnull readonly %arg)
223 ; ATTRIBUTOR: define dso_local noalias i32* @f3(i32* nocapture readonly %arg)
225 ; FIXME: missing nonnull. It should be @f1(i32* nonnull readonly %arg)
226 ; ATTRIBUTOR: %tmp = call i32* @f1(i32* readonly %arg)
227 %tmp = call i32* @f1(i32* %arg)
232 define void @f15(i8* %arg) {
233 ; ATTRIBUTOR: tail call void @use1(i8* nonnull dereferenceable(4) %arg)
235 tail call void @use1(i8* dereferenceable(4) %arg)
239 declare void @fun0() #1
240 declare void @fun1(i8*) #1
241 declare void @fun2(i8*, i8*) #1
242 declare void @fun3(i8*, i8*, i8*) #1
243 ; TEST 16 simple path test
245 ; fun2(nonnull %a, nonnull %b)
247 ; fun2(nonnull %a, %b)
248 ; We can say that %a is nonnull but %b is not.
249 define void @f16(i8* %a, i8 * %b, i8 %c) {
250 ; FIXME: missing nonnull on %a
251 ; ATTRIBUTOR: define void @f16(i8* %a, i8* %b, i8 %c)
252 %cmp = icmp eq i8 %c, 0
253 br i1 %cmp, label %if.then, label %if.else
255 tail call void @fun2(i8* nonnull %a, i8* nonnull %b)
258 tail call void @fun2(i8* nonnull %a, i8* %b)
261 ; TEST 17 explore child BB test
263 ; ... (willreturn & nounwind)
265 ; ... (willreturn & nounwind)
267 ; We can say that %a is nonnull
268 define void @f17(i8* %a, i8 %c) {
269 ; FIXME: missing nonnull on %a
270 ; ATTRIBUTOR: define void @f17(i8* %a, i8 %c)
271 %cmp = icmp eq i8 %c, 0
272 br i1 %cmp, label %if.then, label %if.else
274 tail call void @fun0()
277 tail call void @fun0()
280 tail call void @fun1(i8* nonnull %a)
283 ; TEST 18 More complex test
285 ; ... (willreturn & nounwind)
287 ; ... (willreturn & nounwind)
289 ; ... (willreturn & nounwind)
291 ; ... (willreturn & nounwind)
294 define void @f18(i8* %a, i8* %b, i8 %c) {
295 ; FIXME: missing nonnull on %a
296 ; ATTRIBUTOR: define void @f18(i8* %a, i8* %b, i8 %c)
297 %cmp1 = icmp eq i8 %c, 0
298 br i1 %cmp1, label %if.then, label %if.else
300 tail call void @fun0()
303 tail call void @fun0()
306 %cmp2 = icmp eq i8 %c, 1
307 br i1 %cmp2, label %cont.then, label %cont.else
309 tail call void @fun1(i8* nonnull %b)
312 tail call void @fun0()
315 tail call void @fun1(i8* nonnull %a)
321 define void @f19(i8* %a, i8* %b, i8 %c) {
322 ; FIXME: missing nonnull on %b
323 ; ATTRIBUTOR: define void @f19(i8* %a, i8* %b, i8 %c)
324 br label %loop.header
326 %cmp2 = icmp eq i8 %c, 0
327 br i1 %cmp2, label %loop.body, label %loop.exit
329 tail call void @fun1(i8* nonnull %b)
330 tail call void @fun1(i8* nonnull %a)
331 br label %loop.header
333 tail call void @fun1(i8* nonnull %b)
337 ; Test propagation of nonnull callsite args back to caller.
339 declare void @use1(i8* %x)
340 declare void @use2(i8* %x, i8* %y);
341 declare void @use3(i8* %x, i8* %y, i8* %z);
343 declare void @use1nonnull(i8* nonnull %x);
344 declare void @use2nonnull(i8* nonnull %x, i8* nonnull %y);
345 declare void @use3nonnull(i8* nonnull %x, i8* nonnull %y, i8* nonnull %z);
347 declare i8 @use1safecall(i8* %x) readonly nounwind ; readonly+nounwind guarantees that execution continues to successor
349 ; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.
351 define void @parent1(i8* %a, i8* %b, i8* %c) {
352 ; BOTH-LABEL: @parent1(i8* %a, i8* %b, i8* %c)
353 ; BOTH-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
354 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
355 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
356 ; BOTH-NEXT: ret void
357 call void @use3(i8* %c, i8* %a, i8* %b)
358 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
362 ; Extend non-null to parent for all arguments.
364 define void @parent2(i8* %a, i8* %b, i8* %c) {
365 ; FNATTR-LABEL: @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
366 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
367 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
369 ; ATTRIBUTOR-LABEL: @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
370 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
371 ; ATTRIBUTOR-NEXT: call void @use3(i8* nonnull %c, i8* nonnull %a, i8* nonnull %b)
373 ; BOTH-NEXT: ret void
374 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
375 call void @use3(i8* %c, i8* %a, i8* %b)
379 ; Extend non-null to parent for 1st argument.
381 define void @parent3(i8* %a, i8* %b, i8* %c) {
382 ; FNATTR-LABEL: @parent3(i8* nonnull %a, i8* %b, i8* %c)
383 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
384 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
386 ; ATTRIBUTOR-LABEL: @parent3(i8* nonnull %a, i8* %b, i8* %c)
387 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
388 ; ATTRIBUTOR-NEXT: call void @use3(i8* %c, i8* %b, i8* nonnull %a)
390 ; BOTH-NEXT: ret void
392 call void @use1nonnull(i8* %a)
393 call void @use3(i8* %c, i8* %b, i8* %a)
397 ; Extend non-null to parent for last 2 arguments.
399 define void @parent4(i8* %a, i8* %b, i8* %c) {
400 ; CHECK-LABEL: @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
401 ; CHECK-NEXT: call void @use2nonnull(i8* %c, i8* %b)
402 ; CHECK-NEXT: call void @use2(i8* %a, i8* %c)
403 ; CHECK-NEXT: call void @use1(i8* %b)
405 ; ATTRIBUTOR-LABEL: @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
406 ; ATTRIBUTOR-NEXT: call void @use2nonnull(i8* nonnull %c, i8* nonnull %b)
407 ; ATTRIBUTOR-NEXT: call void @use2(i8* %a, i8* nonnull %c)
408 ; ATTRIBUTOR-NEXT: call void @use1(i8* nonnull %b)
412 call void @use2nonnull(i8* %c, i8* %b)
413 call void @use2(i8* %a, i8* %c)
414 call void @use1(i8* %b)
418 ; The callsite must execute in order for the attribute to transfer to the parent.
419 ; It appears benign to extend non-null to the parent in this case, but we can't do that
420 ; because it would incorrectly propagate the wrong information to its callers.
422 define void @parent5(i8* %a, i1 %a_is_notnull) {
423 ; BOTH: @parent5(i8* %a, i1 %a_is_notnull)
424 ; BOTH-NEXT: br i1 %a_is_notnull, label %t, label %f
426 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
427 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
428 ; BOTH-NEXT: ret void
430 ; BOTH-NEXT: ret void
432 br i1 %a_is_notnull, label %t, label %f
434 call void @use1nonnull(i8* %a)
440 ; The callsite must execute in order for the attribute to transfer to the parent.
441 ; The volatile load can't trap, so we can guarantee that we'll get to the call.
443 define i8 @parent6(i8* %a, i8* %b) {
444 ; FNATTR-LABEL: @parent6(i8* nonnull %a, i8* %b)
445 ; ATTRIBUTOR-LABEL: @parent6(i8* nonnull %a, i8* %b)
446 ; BOTH-NEXT: [[C:%.*]] = load volatile i8, i8* %b
447 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
448 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
449 ; BOTH-NEXT: ret i8 [[C]]
451 %c = load volatile i8, i8* %b
452 call void @use1nonnull(i8* %a)
456 ; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.
458 define i8 @parent7(i8* %a) {
459 ; FNATTR-LABEL: @parent7(i8* nonnull %a)
460 ; FNATTR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
461 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
464 ; ATTRIBUTOR-LABEL: @parent7(i8* nonnull %a)
465 ; ATTRIBUTOR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* nonnull %a)
466 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
468 ; BOTH-NEXT: ret i8 [[RET]]
470 %ret = call i8 @use1safecall(i8* %a)
471 call void @use1nonnull(i8* %a)
475 ; Make sure that an invoke works similarly to a call.
477 declare i32 @esfp(...)
479 define i1 @parent8(i8* %a, i8* %bogus1, i8* %b) personality i8* bitcast (i32 (...)* @esfp to i8*){
480 ; BOTH-LABEL: @parent8(i8* nonnull %a, i8* nocapture readnone %bogus1, i8* nonnull %b)
482 ; FNATTR-NEXT: invoke void @use2nonnull(i8* %a, i8* %b)
483 ; ATTRIBUTOR-NEXT: invoke void @use2nonnull(i8* nonnull %a, i8* nonnull %b)
484 ; BOTH-NEXT: to label %cont unwind label %exc
486 ; BOTH-NEXT: [[NULL_CHECK:%.*]] = icmp eq i8* %b, null
487 ; BOTH-NEXT: ret i1 [[NULL_CHECK]]
489 ; BOTH-NEXT: [[LP:%.*]] = landingpad { i8*, i32 }
490 ; BOTH-NEXT: filter [0 x i8*] zeroinitializer
491 ; BOTH-NEXT: unreachable
494 invoke void @use2nonnull(i8* %a, i8* %b)
495 to label %cont unwind label %exc
498 %null_check = icmp eq i8* %b, null
502 %lp = landingpad { i8*, i32 }
503 filter [0 x i8*] zeroinitializer
507 ; BOTH: define nonnull i32* @gep1(
508 define i32* @gep1(i32* %p) {
509 %q = getelementptr inbounds i32, i32* %p, i32 1
513 define i32* @gep1_no_null_opt(i32* %p) #0 {
514 ; Should't be able to derive nonnull based on gep.
515 ; BOTH: define i32* @gep1_no_null_opt(
516 %q = getelementptr inbounds i32, i32* %p, i32 1
520 ; BOTH: define i32 addrspace(3)* @gep2(
521 define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
522 %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
523 ret i32 addrspace(3)* %q
526 ; FNATTR: define i32 addrspace(3)* @as(i32 addrspace(3)* readnone returned dereferenceable(4) %p)
527 ; FIXME: We should propagate dereferenceable here but *not* nonnull
528 ; ATTRIBUTOR: define dereferenceable_or_null(4) i32 addrspace(3)* @as(i32 addrspace(3)* readnone returned dereferenceable(4) dereferenceable_or_null(4) %p)
529 define i32 addrspace(3)* @as(i32 addrspace(3)* dereferenceable(4) %p) {
530 ret i32 addrspace(3)* %p
533 ; BOTH: define internal nonnull i32* @g2()
534 define internal i32* @g2() {
535 ret i32* inttoptr (i64 4 to i32*)
543 declare void @use_i32_ptr(i32*) readnone nounwind
544 ; ATTRIBUTOR: define internal void @called_by_weak(i32* nocapture nonnull readnone %a)
545 define internal void @called_by_weak(i32* %a) {
546 call void @use_i32_ptr(i32* %a)
550 ; Check we do not annotate the function interface of this weak function.
551 ; ATTRIBUTOR: define weak_odr void @weak_caller(i32* nonnull %a)
552 define weak_odr void @weak_caller(i32* nonnull %a) {
553 call void @called_by_weak(i32* %a)
558 ; ATTRIBUTOR: define internal void @control(i32* nocapture nonnull readnone align 16 dereferenceable(8) %a)
559 define internal void @control(i32* dereferenceable(4) %a) {
560 call void @use_i32_ptr(i32* %a)
563 ; Avoid nonnull as we do not touch naked functions
564 ; ATTRIBUTOR: define internal void @naked(i32* dereferenceable(4) %a)
565 define internal void @naked(i32* dereferenceable(4) %a) naked {
566 call void @use_i32_ptr(i32* %a)
569 ; Avoid nonnull as we do not touch optnone
570 ; ATTRIBUTOR: define internal void @optnone(i32* dereferenceable(4) %a)
571 define internal void @optnone(i32* dereferenceable(4) %a) optnone noinline {
572 call void @use_i32_ptr(i32* %a)
575 define void @make_live(i32* nonnull dereferenceable(8) %a) {
576 call void @naked(i32* nonnull dereferenceable(8) align 16 %a)
577 call void @control(i32* nonnull dereferenceable(8) align 16 %a)
578 call void @optnone(i32* nonnull dereferenceable(8) align 16 %a)
582 attributes #0 = { "null-pointer-is-valid"="true" }
583 attributes #1 = { nounwind willreturn}