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 %a, i8* nocapture %b, i8* nocapture %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 %arg)
182 ; ATTRIBUTOR: define internal nonnull i32* @f1(i32* %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* %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* %arg)
215 ; FIXME: missing nonnull. It should be @f1(i32* nonnull %arg)
216 ; ATTRIBUTOR: %tmp = tail call nonnull i32* @f1(i32* %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 %arg)
223 ; ATTRIBUTOR: define dso_local noalias i32* @f3(i32* %arg)
225 ; FIXME: missing nonnull. It should be @f1(i32* nonnull %arg)
226 ; ATTRIBUTOR: %tmp = call i32* @f1(i32* %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 ; Test propagation of nonnull callsite args back to caller.
241 declare void @use1(i8* %x)
242 declare void @use2(i8* %x, i8* %y);
243 declare void @use3(i8* %x, i8* %y, i8* %z);
245 declare void @use1nonnull(i8* nonnull %x);
246 declare void @use2nonnull(i8* nonnull %x, i8* nonnull %y);
247 declare void @use3nonnull(i8* nonnull %x, i8* nonnull %y, i8* nonnull %z);
249 declare i8 @use1safecall(i8* %x) readonly nounwind ; readonly+nounwind guarantees that execution continues to successor
251 ; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.
253 define void @parent1(i8* %a, i8* %b, i8* %c) {
254 ; BOTH-LABEL: @parent1(i8* %a, i8* %b, i8* %c)
255 ; BOTH-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
256 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
257 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
258 ; BOTH-NEXT: ret void
259 call void @use3(i8* %c, i8* %a, i8* %b)
260 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
264 ; Extend non-null to parent for all arguments.
266 define void @parent2(i8* %a, i8* %b, i8* %c) {
267 ; FNATTR-LABEL: @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
268 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
269 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
271 ; FIXME: missing "nonnull", it should be
272 ; @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
273 ; call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
274 ; call void @use3(i8* nonnull %c, i8* nonnull %a, i8* nonnull %b)
276 ; ATTRIBUTOR-LABEL: @parent2(i8* %a, i8* %b, i8* %c)
277 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
278 ; ATTRIBUTOR-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
280 ; BOTH-NEXT: ret void
281 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
282 call void @use3(i8* %c, i8* %a, i8* %b)
286 ; Extend non-null to parent for 1st argument.
288 define void @parent3(i8* %a, i8* %b, i8* %c) {
289 ; FNATTR-LABEL: @parent3(i8* nonnull %a, i8* %b, i8* %c)
290 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
291 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
293 ; FIXME: missing "nonnull", it should be,
294 ; @parent3(i8* nonnull %a, i8* %b, i8* %c)
295 ; call void @use1nonnull(i8* nonnull %a)
296 ; call void @use3(i8* %c, i8* %b, i8* nonnull %a)
297 ; ATTRIBUTOR-LABEL: @parent3(i8* %a, i8* %b, i8* %c)
298 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
299 ; ATTRIBUTOR-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
301 ; BOTH-NEXT: ret void
303 call void @use1nonnull(i8* %a)
304 call void @use3(i8* %c, i8* %b, i8* %a)
308 ; Extend non-null to parent for last 2 arguments.
310 define void @parent4(i8* %a, i8* %b, i8* %c) {
311 ; CHECK-LABEL: @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
312 ; CHECK-NEXT: call void @use2nonnull(i8* %c, i8* %b)
313 ; CHECK-NEXT: call void @use2(i8* %a, i8* %c)
314 ; CHECK-NEXT: call void @use1(i8* %b)
316 ; FIXME : missing "nonnull", it should be
317 ; @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
318 ; call void @use2nonnull(i8* nonnull %c, i8* nonull %b)
319 ; call void @use2(i8* %a, i8* nonnull %c)
320 ; call void @use1(i8* nonnull %b)
322 ; ATTRIBUTOR-LABEL: @parent4(i8* %a, i8* %b, i8* %c)
323 ; ATTRIBUTOR-NEXT: call void @use2nonnull(i8* nonnull %c, i8* nonnull %b)
324 ; ATTRIBUTOR-NEXT: call void @use2(i8* %a, i8* %c)
325 ; ATTRIBUTOR-NEXT: call void @use1(i8* %b)
329 call void @use2nonnull(i8* %c, i8* %b)
330 call void @use2(i8* %a, i8* %c)
331 call void @use1(i8* %b)
335 ; The callsite must execute in order for the attribute to transfer to the parent.
336 ; It appears benign to extend non-null to the parent in this case, but we can't do that
337 ; because it would incorrectly propagate the wrong information to its callers.
339 define void @parent5(i8* %a, i1 %a_is_notnull) {
340 ; BOTH: @parent5(i8* %a, i1 %a_is_notnull)
341 ; BOTH-NEXT: br i1 %a_is_notnull, label %t, label %f
343 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
344 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
345 ; BOTH-NEXT: ret void
347 ; BOTH-NEXT: ret void
349 br i1 %a_is_notnull, label %t, label %f
351 call void @use1nonnull(i8* %a)
357 ; The callsite must execute in order for the attribute to transfer to the parent.
358 ; The volatile load can't trap, so we can guarantee that we'll get to the call.
360 define i8 @parent6(i8* %a, i8* %b) {
361 ; FNATTR-LABEL: @parent6(i8* nonnull %a, i8* %b)
362 ; FIXME: missing "nonnull"
363 ; ATTRIBUTOR-LABEL: @parent6(i8* %a, i8* %b)
364 ; BOTH-NEXT: [[C:%.*]] = load volatile i8, i8* %b
365 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
366 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
367 ; BOTH-NEXT: ret i8 [[C]]
369 %c = load volatile i8, i8* %b
370 call void @use1nonnull(i8* %a)
374 ; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.
376 define i8 @parent7(i8* %a) {
377 ; FNATTR-LABEL: @parent7(i8* nonnull %a)
378 ; FNATTR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
379 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
381 ; FIXME : missing "nonnull", it should be
382 ; @parent7(i8* nonnull %a)
383 ; [[RET:%.*]] = call i8 @use1safecall(i8* nonnull %a)
384 ; call void @use1nonnull(i8* nonnull %a)
387 ; ATTRIBUTOR-LABEL: @parent7(i8* %a)
388 ; ATTRIBUTOR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
389 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
391 ; BOTH-NEXT: ret i8 [[RET]]
393 %ret = call i8 @use1safecall(i8* %a)
394 call void @use1nonnull(i8* %a)
398 ; Make sure that an invoke works similarly to a call.
400 declare i32 @esfp(...)
402 define i1 @parent8(i8* %a, i8* %bogus1, i8* %b) personality i8* bitcast (i32 (...)* @esfp to i8*){
403 ; FNATTR-LABEL: @parent8(i8* nonnull %a, i8* nocapture readnone %bogus1, i8* nonnull %b)
404 ; FIXME : missing "nonnull", it should be @parent8(i8* nonnull %a, i8* %bogus1, i8* nonnull %b)
405 ; ATTRIBUTOR-LABEL: @parent8(i8* %a, i8* nocapture %bogus1, i8* %b)
407 ; FNATTR-NEXT: invoke void @use2nonnull(i8* %a, i8* %b)
408 ; ATTRIBUTOR-NEXT: invoke void @use2nonnull(i8* nonnull %a, i8* nonnull %b)
409 ; BOTH-NEXT: to label %cont unwind label %exc
411 ; BOTH-NEXT: [[NULL_CHECK:%.*]] = icmp eq i8* %b, null
412 ; BOTH-NEXT: ret i1 [[NULL_CHECK]]
414 ; BOTH-NEXT: [[LP:%.*]] = landingpad { i8*, i32 }
415 ; BOTH-NEXT: filter [0 x i8*] zeroinitializer
416 ; BOTH-NEXT: unreachable
419 invoke void @use2nonnull(i8* %a, i8* %b)
420 to label %cont unwind label %exc
423 %null_check = icmp eq i8* %b, null
427 %lp = landingpad { i8*, i32 }
428 filter [0 x i8*] zeroinitializer
432 ; BOTH: define nonnull i32* @gep1(
433 define i32* @gep1(i32* %p) {
434 %q = getelementptr inbounds i32, i32* %p, i32 1
438 define i32* @gep1_no_null_opt(i32* %p) #0 {
439 ; Should't be able to derive nonnull based on gep.
440 ; BOTH: define i32* @gep1_no_null_opt(
441 %q = getelementptr inbounds i32, i32* %p, i32 1
445 ; BOTH: define i32 addrspace(3)* @gep2(
446 define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
447 %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
448 ret i32 addrspace(3)* %q
451 ; BOTH: define internal nonnull i32* @g2()
452 define internal i32* @g2() {
453 ret i32* inttoptr (i64 4 to i32*)
461 ; ATTRIBUTOR: define internal void @called_by_weak(i32* nocapture nonnull %a)
462 define internal void @called_by_weak(i32* %a) {
466 ; Check we do not annotate the function interface of this weak function.
467 ; ATTRIBUTOR: define weak_odr void @weak_caller(i32* nonnull %a)
468 define weak_odr void @weak_caller(i32* nonnull %a) {
469 call void @called_by_weak(i32* %a)
473 attributes #0 = { "null-pointer-is-valid"="true" }