hanchenye-llvm-project/llvm/test/Transforms/FunctionAttrs/nonnull.ll

232 lines
6.6 KiB
LLVM
Raw Normal View History

; RUN: opt -S -functionattrs -enable-nonnull-arg-prop %s | FileCheck %s
[PM][FunctionAttrs] add NoUnwind attribute inference to PostOrderFunctionAttrs pass Summary: This was motivated by absence of PrunEH functionality in new PM. It was decided that a proper way to do PruneEH is to add NoUnwind inference into PostOrderFunctionAttrs and then perform normal SimplifyCFG on top. This change generalizes attribute handling implemented for (a removal of) Convergent attribute, by introducing a generic builder-like class AttributeInferer It registers all the attribute inference requests, storing per-attribute predicates into a vector, and then goes through an SCC Node, scanning all the instructions for not breaking attribute assumptions. The main idea is that as soon all the instructions from all the functions of SCC Node conform to attribute assumptions then we are free to infer the attribute as set for all the functions of SCC Node. It handles two distinct cases of attributes: - those that might break due to derefinement of the function code for these attributes we are allowed to apply inference only if all the functions are "exact definitions". Example - NoUnwind. - those that do not care about derefinement for these attributes we are allowed to apply inference as soon as we see any function definition. Example - removal of Convergent attribute. Also in this commit: * Converted all the FunctionAttrs tests to use FileCheck and added new-PM invocations to them * FunctionAttrs/convergent.ll test demonstrates a difference in behavior between new and old PM implementations. Marked with FIXME. * PruneEH tests were converted to new-PM as well, using function-attrs+simplify-cfg combo as intended * some of "other" tests were updated since function-attrs now infers 'nounwind' even for old PM pipeline * -disable-nounwind-inference hidden option added as a possible workaround for a supposedly rare case when nounwind being inferred by default presents a problem Reviewers: chandlerc, jlebar Reviewed By: jlebar Subscribers: eraman, llvm-commits Differential Revision: https://reviews.llvm.org/D44415 llvm-svn: 328377
2018-03-24 05:46:16 +08:00
; RUN: opt -S -passes=function-attrs -enable-nonnull-arg-prop %s | FileCheck %s
declare nonnull i8* @ret_nonnull()
; Return a pointer trivially nonnull (call return attribute)
define i8* @test1() {
; CHECK: define nonnull i8* @test1
%ret = call i8* @ret_nonnull()
ret i8* %ret
}
; Return a pointer trivially nonnull (argument attribute)
define i8* @test2(i8* nonnull %p) {
; CHECK: define nonnull i8* @test2
ret i8* %p
}
; Given an SCC where one of the functions can not be marked nonnull,
; can we still mark the other one which is trivially nonnull
define i8* @scc_binder() {
; CHECK: define i8* @scc_binder
call i8* @test3()
ret i8* null
}
define i8* @test3() {
; CHECK: define nonnull i8* @test3
call i8* @scc_binder()
%ret = call i8* @ret_nonnull()
ret i8* %ret
}
; Given a mutual recursive set of functions, we can mark them
; nonnull if neither can ever return null. (In this case, they
; just never return period.)
define i8* @test4_helper() {
; CHECK: define noalias nonnull i8* @test4_helper
%ret = call i8* @test4()
ret i8* %ret
}
define i8* @test4() {
; CHECK: define noalias nonnull i8* @test4
%ret = call i8* @test4_helper()
ret i8* %ret
}
; Given a mutual recursive set of functions which *can* return null
; make sure we haven't marked them as nonnull.
define i8* @test5_helper() {
; CHECK: define noalias i8* @test5_helper
%ret = call i8* @test5()
ret i8* null
}
define i8* @test5() {
; CHECK: define noalias i8* @test5
%ret = call i8* @test5_helper()
ret i8* %ret
}
; Local analysis, but going through a self recursive phi
define i8* @test6() {
entry:
; CHECK: define nonnull i8* @test6
%ret = call i8* @ret_nonnull()
br label %loop
loop:
%phi = phi i8* [%ret, %entry], [%phi, %loop]
br i1 undef, label %loop, label %exit
exit:
ret i8* %phi
}
; Test propagation of nonnull callsite args back to caller.
declare void @use1(i8* %x)
declare void @use2(i8* %x, i8* %y);
declare void @use3(i8* %x, i8* %y, i8* %z);
declare void @use1nonnull(i8* nonnull %x);
declare void @use2nonnull(i8* nonnull %x, i8* nonnull %y);
declare void @use3nonnull(i8* nonnull %x, i8* nonnull %y, i8* nonnull %z);
declare i8 @use1safecall(i8* %x) readonly nounwind ; readonly+nounwind guarantees that execution continues to successor
; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.
define void @parent1(i8* %a, i8* %b, i8* %c) {
; CHECK-LABEL: @parent1(i8* %a, i8* %b, i8* %c)
; CHECK-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
; CHECK-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
; CHECK-NEXT: ret void
;
call void @use3(i8* %c, i8* %a, i8* %b)
call void @use3nonnull(i8* %b, i8* %c, i8* %a)
ret void
}
; Extend non-null to parent for all arguments.
define void @parent2(i8* %a, i8* %b, i8* %c) {
; CHECK-LABEL: @parent2(i8* nonnull %a, i8* nonnull %b, i8* nonnull %c)
; CHECK-NEXT: call void @use3nonnull(i8* %b, i8* %c, i8* %a)
; CHECK-NEXT: call void @use3(i8* %c, i8* %a, i8* %b)
; CHECK-NEXT: ret void
;
call void @use3nonnull(i8* %b, i8* %c, i8* %a)
call void @use3(i8* %c, i8* %a, i8* %b)
ret void
}
; Extend non-null to parent for 1st argument.
define void @parent3(i8* %a, i8* %b, i8* %c) {
; CHECK-LABEL: @parent3(i8* nonnull %a, i8* %b, i8* %c)
; CHECK-NEXT: call void @use1nonnull(i8* %a)
; CHECK-NEXT: call void @use3(i8* %c, i8* %b, i8* %a)
; CHECK-NEXT: ret void
;
call void @use1nonnull(i8* %a)
call void @use3(i8* %c, i8* %b, i8* %a)
ret void
}
; Extend non-null to parent for last 2 arguments.
define void @parent4(i8* %a, i8* %b, i8* %c) {
; CHECK-LABEL: @parent4(i8* %a, i8* nonnull %b, i8* nonnull %c)
; CHECK-NEXT: call void @use2nonnull(i8* %c, i8* %b)
; CHECK-NEXT: call void @use2(i8* %a, i8* %c)
; CHECK-NEXT: call void @use1(i8* %b)
; CHECK-NEXT: ret void
;
call void @use2nonnull(i8* %c, i8* %b)
call void @use2(i8* %a, i8* %c)
call void @use1(i8* %b)
ret void
}
; The callsite must execute in order for the attribute to transfer to the parent.
; It appears benign to extend non-null to the parent in this case, but we can't do that
; because it would incorrectly propagate the wrong information to its callers.
define void @parent5(i8* %a, i1 %a_is_notnull) {
; CHECK-LABEL: @parent5(i8* %a, i1 %a_is_notnull)
; CHECK-NEXT: br i1 %a_is_notnull, label %t, label %f
; CHECK: t:
; CHECK-NEXT: call void @use1nonnull(i8* %a)
; CHECK-NEXT: ret void
; CHECK: f:
; CHECK-NEXT: ret void
;
br i1 %a_is_notnull, label %t, label %f
t:
call void @use1nonnull(i8* %a)
ret void
f:
ret void
}
; The callsite must execute in order for the attribute to transfer to the parent.
; The volatile load might trap, so there's no guarantee that we'll ever get to the call.
define i8 @parent6(i8* %a, i8* %b) {
; CHECK-LABEL: @parent6(i8* %a, i8* %b)
; CHECK-NEXT: [[C:%.*]] = load volatile i8, i8* %b
; CHECK-NEXT: call void @use1nonnull(i8* %a)
; CHECK-NEXT: ret i8 [[C]]
;
%c = load volatile i8, i8* %b
call void @use1nonnull(i8* %a)
ret i8 %c
}
; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.
define i8 @parent7(i8* %a) {
; CHECK-LABEL: @parent7(i8* nonnull %a)
; CHECK-NEXT: [[RET:%.*]] = call i8 @use1safecall(i8* %a)
; CHECK-NEXT: call void @use1nonnull(i8* %a)
; CHECK-NEXT: ret i8 [[RET]]
;
%ret = call i8 @use1safecall(i8* %a)
call void @use1nonnull(i8* %a)
ret i8 %ret
}
; Make sure that an invoke works similarly to a call.
declare i32 @esfp(...)
define i1 @parent8(i8* %a, i8* %bogus1, i8* %b) personality i8* bitcast (i32 (...)* @esfp to i8*){
; CHECK-LABEL: @parent8(i8* nonnull %a, i8* nocapture readnone %bogus1, i8* nonnull %b)
; CHECK-NEXT: entry:
; CHECK-NEXT: invoke void @use2nonnull(i8* %a, i8* %b)
; CHECK-NEXT: to label %cont unwind label %exc
; CHECK: cont:
; CHECK-NEXT: [[NULL_CHECK:%.*]] = icmp eq i8* %b, null
; CHECK-NEXT: ret i1 [[NULL_CHECK]]
; CHECK: exc:
; CHECK-NEXT: [[LP:%.*]] = landingpad { i8*, i32 }
; CHECK-NEXT: filter [0 x i8*] zeroinitializer
; CHECK-NEXT: unreachable
;
entry:
invoke void @use2nonnull(i8* %a, i8* %b)
to label %cont unwind label %exc
cont:
%null_check = icmp eq i8* %b, null
ret i1 %null_check
exc:
%lp = landingpad { i8*, i32 }
filter [0 x i8*] zeroinitializer
unreachable
}
; CHECK: define nonnull i32* @gep1(
define i32* @gep1(i32* %p) {
%q = getelementptr inbounds i32, i32* %p, i32 1
ret i32* %q
}
; CHECK: define i32 addrspace(3)* @gep2(
define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
%q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
ret i32 addrspace(3)* %q
}