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 -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() {
25 ; BOTH: define i8* @scc_binder
31 ; BOTH: define nonnull i8* @test3
32 call i8* @scc_binder()
33 %ret = call i8* @ret_nonnull()
37 ; Given a mutual recursive set of functions, we can mark them
38 ; nonnull if neither can ever return null. (In this case, they
39 ; just never return period.)
40 define i8* @test4_helper() {
41 ; FNATTR: define noalias nonnull i8* @test4_helper
42 ; ATTRIBUTOR: define nonnull i8* @test4_helper
43 %ret = call i8* @test4()
48 ; FNATTR: define noalias nonnull i8* @test4
49 ; ATTRIBUTOR: define nonnull i8* @test4
50 %ret = call i8* @test4_helper()
54 ; Given a mutual recursive set of functions which *can* return null
55 ; make sure we haven't marked them as nonnull.
56 define i8* @test5_helper() {
57 ; FNATTR: define noalias i8* @test5_helper
58 ; ATTRIBUTOR: define i8* @test5_helper
59 %ret = call i8* @test5()
64 ; FNATTR: define noalias i8* @test5
65 ; ATTRIBUTOR: define i8* @test5
66 %ret = call i8* @test5_helper()
70 ; Local analysis, but going through a self recursive phi
73 ; BOTH: define nonnull i8* @test6
74 %ret = call i8* @ret_nonnull()
77 %phi = phi i8* [%ret, %entry], [%phi, %loop]
78 br i1 undef, label %loop, label %exit
83 ; BOTH: define i8* @test7
84 define i8* @test7(i8* %a) {
85 %b = getelementptr inbounds i8, i8* %a, i64 0
89 ; BOTH: define nonnull i8* @test8
90 define i8* @test8(i8* %a) {
91 %b = getelementptr inbounds i8, i8* %a, i64 1
95 ; BOTH: define i8* @test9
96 define i8* @test9(i8* %a, i64 %n) {
97 %b = getelementptr inbounds i8, i8* %a, i64 %n
101 declare void @llvm.assume(i1)
102 ; FNATTR: define i8* @test10
103 ; FIXME: missing nonnull
104 ; ATTRIBUTOR: define i8* @test10
105 define i8* @test10(i8* %a, i64 %n) {
106 %cmp = icmp ne i64 %n, 0
107 call void @llvm.assume(i1 %cmp)
108 %b = getelementptr inbounds i8, i8* %a, i64 %n
113 ; char* test11(char *p) {
114 ; return p? p: nonnull();
116 ; FNATTR: define i8* @test11
117 ; FIXME: missing nonnull
118 ; ATTRIBUTOR: define i8* @test11
119 define i8* @test11(i8*) local_unnamed_addr {
120 %2 = icmp eq i8* %0, null
121 br i1 %2, label %3, label %5
123 ; <label>:3: ; preds = %1
124 %4 = tail call i8* @ret_nonnull()
127 ; <label>:5: ; preds = %3, %1
128 %6 = phi i8* [ %4, %3 ], [ %0, %1 ]
133 ; Simple CallSite Test
134 declare void @test12_helper(i8*)
135 define void @test12(i8* nonnull %a) {
136 ; ATTRIBUTOR: define void @test12(i8* nonnull %a)
137 ; ATTRIBUTOR-NEXT: tail call void @test12_helper(i8* nonnull %a)
138 tail call void @test12_helper(i8* %a)
143 ; Simple Argument Tests
144 declare i8* @unknown()
145 define void @test13_helper() {
146 %nonnullptr = tail call i8* @ret_nonnull()
147 %maybenullptr = tail call i8* @unknown()
148 tail call void @test13(i8* %nonnullptr, i8* %nonnullptr, i8* %maybenullptr)
149 tail call void @test13(i8* %nonnullptr, i8* %maybenullptr, i8* %nonnullptr)
152 define internal void @test13(i8* %a, i8* %b, i8* %c) {
153 ; ATTRIBUTOR: define internal void @test13(i8* nonnull %a, i8* %b, i8* %c)
157 declare nonnull i8* @nonnull()
160 ; Complex propagation
161 ; Argument of f1, f2, f3 can be marked with nonnull.
164 ; 1. In f1:bb6, %arg can be marked with nonnull because of the comparison in bb1
165 ; 2. Because f2 is internal function, f2(i32* %arg) -> @f2(i32* nonnull %arg)
166 ; 3. In f1:bb4 %tmp5 is nonnull and f3 is internal function.
167 ; Then, f3(i32* %arg) -> @f3(i32* nonnull %arg)
168 ; 4. We get nonnull in whole f1 call sites so f1(i32* %arg) -> @f1(i32* nonnull %arg)
171 define internal i32* @f1(i32* %arg) {
172 ; FIXME: missing nonnull It should be nonnull @f1(i32* nonnull %arg)
173 ; ATTRIBUTOR: define internal nonnull i32* @f1(i32* %arg)
176 %tmp = icmp eq i32* %arg, null
177 br i1 %tmp, label %bb9, label %bb1
180 %tmp2 = load i32, i32* %arg, align 4
181 %tmp3 = icmp eq i32 %tmp2, 0
182 br i1 %tmp3, label %bb6, label %bb4
185 %tmp5 = getelementptr inbounds i32, i32* %arg, i64 1
186 ; ATTRIBUTOR: %tmp5b = tail call i32* @f3(i32* nonnull %tmp5)
187 %tmp5b = tail call i32* @f3(i32* %tmp5)
191 ; FIXME: missing nonnull. It should be @f2(i32* nonnull %arg)
192 ; ATTRIBUTOR: %tmp7 = tail call i32* @f2(i32* %arg)
193 %tmp7 = tail call i32* @f2(i32* %arg)
196 bb9: ; preds = %bb4, %bb
197 %tmp10 = phi i32* [ %tmp5, %bb4 ], [ inttoptr (i64 4 to i32*), %bb ]
201 define internal i32* @f2(i32* %arg) {
202 ; FIXME: missing nonnull. It should be nonnull @f2(i32* nonnull %arg)
203 ; ATTRIBUTOR: define internal nonnull i32* @f2(i32* %arg)
206 ; FIXME: missing nonnull. It should be @f1(i32* nonnull %arg)
207 ; ATTRIBUTOR: %tmp = tail call i32* @f1(i32* %arg)
208 %tmp = tail call i32* @f1(i32* %arg)
212 define dso_local noalias i32* @f3(i32* %arg) {
213 ; FIXME: missing nonnull. It should be nonnull @f3(i32* nonnull %arg)
214 ; ATTRIBUTOR: define dso_local noalias i32* @f3(i32* %arg)
216 ; FIXME: missing nonnull. It should be @f1(i32* nonnull %arg)
217 ; ATTRIBUTOR: %tmp = call i32* @f1(i32* %arg)
218 %tmp = call i32* @f1(i32* %arg)
221 ; Test propagation of nonnull callsite args back to caller.
223 declare void @use1(i8* %x)
224 declare void @use2(i8* %x, i8* %y);
225 declare void @use3(i8* %x, i8* %y, i8* %z);
227 declare void @use1nonnull(i8* nonnull %x);
228 declare void @use2nonnull(i8* nonnull %x, i8* nonnull %y);
229 declare void @use3nonnull(i8* nonnull %x, i8* nonnull %y, i8* nonnull %z);
231 declare i8 @use1safecall(i8* %x) readonly nounwind ; readonly+nounwind guarantees that execution continues to successor
233 ; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.
235 define void @parent1(i8* %a, i8* %b, i8* %c) {
236 ; BOTH-LABEL: @parent1(i8* %a, i8* %b, i8* %c)
237 ; BOTH-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
238 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
239 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
240 ; BOTH-NEXT: ret void
241 call void @use3(i8* %c, i8* %a, i8* %b)
242 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
246 ; Extend non-null to parent for all arguments.
248 define void @parent2(i8* %a, i8* %b, i8* %c) {
249 ; FNATTR-LABEL: @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
250 ; FNATTR-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
251 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
253 ; FIXME: missing "nonnull", it should be
254 ; @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
255 ; call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
256 ; call void @use3(i8* nonnull %c, i8* nonnull %a, i8* nonnull %b)
258 ; ATTRIBUTOR-LABEL: @parent2(i8* %a, i8* %b, i8* %c)
259 ; ATTRIBUTOR-NEXT: call void @use3nonnull(i8* nonnull %b, i8* nonnull %c, i8* nonnull %a)
260 ; ATTRIBUTOR-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
262 ; BOTH-NEXT: ret void
263 call void @use3nonnull(i8* %b, i8* %c, i8* %a)
264 call void @use3(i8* %c, i8* %a, i8* %b)
268 ; Extend non-null to parent for 1st argument.
270 define void @parent3(i8* %a, i8* %b, i8* %c) {
271 ; FNATTR-LABEL: @parent3(i8* nonnull %a, i8* %b, i8* %c)
272 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
273 ; FNATTR-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
275 ; FIXME: missing "nonnull", it should be,
276 ; @parent3(i8* nonnull %a, i8* %b, i8* %c)
277 ; call void @use1nonnull(i8* nonnull %a)
278 ; call void @use3(i8* %c, i8* %b, i8* nonnull %a)
279 ; ATTRIBUTOR-LABEL: @parent3(i8* %a, i8* %b, i8* %c)
280 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
281 ; ATTRIBUTOR-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
283 ; BOTH-NEXT: ret void
285 call void @use1nonnull(i8* %a)
286 call void @use3(i8* %c, i8* %b, i8* %a)
290 ; Extend non-null to parent for last 2 arguments.
292 define void @parent4(i8* %a, i8* %b, i8* %c) {
293 ; CHECK-LABEL: @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
294 ; CHECK-NEXT: call void @use2nonnull(i8* %c, i8* %b)
295 ; CHECK-NEXT: call void @use2(i8* %a, i8* %c)
296 ; CHECK-NEXT: call void @use1(i8* %b)
298 ; FIXME : missing "nonnull", it should be
299 ; @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
300 ; call void @use2nonnull(i8* nonnull %c, i8* nonull %b)
301 ; call void @use2(i8* %a, i8* nonnull %c)
302 ; call void @use1(i8* nonnull %b)
304 ; ATTRIBUTOR-LABEL: @parent4(i8* %a, i8* %b, i8* %c)
305 ; ATTRIBUTOR-NEXT: call void @use2nonnull(i8* nonnull %c, i8* nonnull %b)
306 ; ATTRIBUTOR-NEXT: call void @use2(i8* %a, i8* %c)
307 ; ATTRIBUTOR-NEXT: call void @use1(i8* %b)
311 call void @use2nonnull(i8* %c, i8* %b)
312 call void @use2(i8* %a, i8* %c)
313 call void @use1(i8* %b)
317 ; The callsite must execute in order for the attribute to transfer to the parent.
318 ; It appears benign to extend non-null to the parent in this case, but we can't do that
319 ; because it would incorrectly propagate the wrong information to its callers.
321 define void @parent5(i8* %a, i1 %a_is_notnull) {
322 ; BOTH: @parent5(i8* %a, i1 %a_is_notnull)
323 ; BOTH-NEXT: br i1 %a_is_notnull, label %t, label %f
325 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
326 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
327 ; BOTH-NEXT: ret void
329 ; BOTH-NEXT: ret void
331 br i1 %a_is_notnull, label %t, label %f
333 call void @use1nonnull(i8* %a)
339 ; The callsite must execute in order for the attribute to transfer to the parent.
340 ; The volatile load might trap, so there's no guarantee that we'll ever get to the call.
342 define i8 @parent6(i8* %a, i8* %b) {
343 ; BOTH-LABEL: @parent6(i8* %a, i8* %b)
344 ; BOTH-NEXT: [[C:%.*]] = load volatile i8, i8* %b
345 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
346 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
347 ; BOTH-NEXT: ret i8 [[C]]
349 %c = load volatile i8, i8* %b
350 call void @use1nonnull(i8* %a)
354 ; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.
356 define i8 @parent7(i8* %a) {
357 ; FNATTR-LABEL: @parent7(i8* nonnull %a)
358 ; FNATTR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
359 ; FNATTR-NEXT: call void @use1nonnull(i8* %a)
361 ; FIXME : missing "nonnull", it should be
362 ; @parent7(i8* nonnull %a)
363 ; [[RET:%.*]] = call i8 @use1safecall(i8* nonnull %a)
364 ; call void @use1nonnull(i8* nonnull %a)
367 ; ATTRIBUTOR-LABEL: @parent7(i8* %a)
368 ; ATTRIBUTOR-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
369 ; ATTRIBUTOR-NEXT: call void @use1nonnull(i8* nonnull %a)
371 ; BOTH-NEXT: ret i8 [[RET]]
373 %ret = call i8 @use1safecall(i8* %a)
374 call void @use1nonnull(i8* %a)
378 ; Make sure that an invoke works similarly to a call.
380 declare i32 @esfp(...)
382 define i1 @parent8(i8* %a, i8* %bogus1, i8* %b) personality i8* bitcast (i32 (...)* @esfp to i8*){
383 ; FNATTR-LABEL: @parent8(i8* nonnull %a, i8* nocapture readnone %bogus1, i8* nonnull %b)
384 ; FIXME : missing "nonnull", it should be @parent8(i8* nonnull %a, i8* %bogus1, i8* nonnull %b)
385 ; ATTRIBUTOR-LABEL: @parent8(i8* %a, i8* %bogus1, i8* %b)
387 ; FNATTR-NEXT: invoke void @use2nonnull(i8* %a, i8* %b)
388 ; ATTRIBUTOR-NEXT: invoke void @use2nonnull(i8* nonnull %a, i8* nonnull %b)
389 ; BOTH-NEXT: to label %cont unwind label %exc
391 ; BOTH-NEXT: [[NULL_CHECK:%.*]] = icmp eq i8* %b, null
392 ; BOTH-NEXT: ret i1 [[NULL_CHECK]]
394 ; BOTH-NEXT: [[LP:%.*]] = landingpad { i8*, i32 }
395 ; BOTH-NEXT: filter [0 x i8*] zeroinitializer
396 ; BOTH-NEXT: unreachable
399 invoke void @use2nonnull(i8* %a, i8* %b)
400 to label %cont unwind label %exc
403 %null_check = icmp eq i8* %b, null
407 %lp = landingpad { i8*, i32 }
408 filter [0 x i8*] zeroinitializer
412 ; BOTH: define nonnull i32* @gep1(
413 define i32* @gep1(i32* %p) {
414 %q = getelementptr inbounds i32, i32* %p, i32 1
418 define i32* @gep1_no_null_opt(i32* %p) #0 {
419 ; Should't be able to derive nonnull based on gep.
420 ; BOTH: define i32* @gep1_no_null_opt(
421 %q = getelementptr inbounds i32, i32* %p, i32 1
425 ; BOTH: define i32 addrspace(3)* @gep2(
426 define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
427 %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
428 ret i32 addrspace(3)* %q
431 ; BOTH: define internal nonnull i32* @g2()
432 define internal i32* @g2() {
433 ret i32* inttoptr (i64 4 to i32*)
441 attributes #0 = { "null-pointer-is-valid"="true" }