Author: Florian Hahn Date: 2021-01-12T20:02:34Z New Revision: 6cd44b204c6c6f2e915270af6792f247c4c23abc
URL: https://github.com/llvm/llvm-project/commit/6cd44b204c6c6f2e915270af6792f247c4c23abc DIFF: https://github.com/llvm/llvm-project/commit/6cd44b204c6c6f2e915270af6792f247c4c23abc.diff LOG: [FunctionAttrs] Derive willreturn for fns with readonly` & `mustprogress`. Similar to D94125, derive `willreturn` for functions that are `readonly` and `mustprogress` in FunctionAttrs. To quote the reasoning from D94125: Since D86233 we have `mustprogress` which, in combination with `readonly`, implies `willreturn`. The idea is that every side-effect has to be modeled as a "write". Consequently, `readonly` means there is no side-effect, and `mustprogress` guarantees that we cannot "loop" forever without side-effect. Reviewed By: jdoerfert, nikic Differential Revision: https://reviews.llvm.org/D94502 Added: Modified: llvm/include/llvm/IR/Function.h llvm/lib/Transforms/IPO/FunctionAttrs.cpp llvm/test/Transforms/FunctionAttrs/willreturn.ll Removed: ################################################################################ diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index 019e3a98a1af..7e209bb3769b 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -640,6 +640,10 @@ class Function : public GlobalObject, public ilist_node<Function> { } void setMustProgress() { addFnAttr(Attribute::MustProgress); } + /// Determine if the function will return. + bool willReturn() const { return hasFnAttribute(Attribute::WillReturn); } + void setWillReturn() { addFnAttr(Attribute::WillReturn); } + /// True if the ABI mandates (or the user requested) that this /// function be in a unwind table. bool hasUWTable() const { diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp index 5cf5e9463b45..2e24cad1393b 100644 --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -77,6 +77,7 @@ STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); STATISTIC(NumNoRecurse, "Number of functions marked as norecurse"); STATISTIC(NumNoUnwind, "Number of functions marked as nounwind"); STATISTIC(NumNoFree, "Number of functions marked as nofree"); +STATISTIC(NumWillReturn, "Number of functions marked as willreturn"); static cl::opt<bool> EnableNonnullArgPropagation( "enable-nonnull-arg-prop", cl::init(true), cl::Hidden, @@ -1424,6 +1425,22 @@ static bool addNoReturnAttrs(const SCCNodeSet &SCCNodes) { return Changed; } +// Set the willreturn function attribute if possible. +static bool addWillReturn(const SCCNodeSet &SCCNodes) { + bool Changed = false; + + for (Function *F : SCCNodes) { + if (!F || !F->onlyReadsMemory() || !F->mustProgress() || F->willReturn()) + continue; + + F->setWillReturn(); + NumWillReturn++; + Changed = true; + } + + return Changed; +} + static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) { SCCNodesResult Res; Res.HasUnknownCall = false; @@ -1468,6 +1485,7 @@ static bool deriveAttrsInPostOrder(ArrayRef<Function *> Functions, Changed |= addArgumentAttrs(Nodes.SCCNodes); Changed |= inferConvergent(Nodes.SCCNodes); Changed |= addNoReturnAttrs(Nodes.SCCNodes); + Changed |= addWillReturn(Nodes.SCCNodes); // If we have no external nodes participating in the SCC, we can deduce some // more precise attributes as well. diff --git a/llvm/test/Transforms/FunctionAttrs/willreturn.ll b/llvm/test/Transforms/FunctionAttrs/willreturn.ll index 56ca12638e9c..d92151c299fe 100644 --- a/llvm/test/Transforms/FunctionAttrs/willreturn.ll +++ b/llvm/test/Transforms/FunctionAttrs/willreturn.ll @@ -1,9 +1,8 @@ ; RUN: opt -function-attrs -S %s | FileCheck %s -; TODO define void @mustprogress_readnone() mustprogress { -; CHECK-NOT: Function Attrs: {{.*}} willreturn -; CHECK: define void @mustprogress_readnone() +; CHECK: Function Attrs: {{.*}} noreturn {{.*}} readnone willreturn +; CHECK-NEXT: define void @mustprogress_readnone() ; entry: br label %while.body @@ -12,10 +11,9 @@ while.body: br label %while.body } -; TODO define i32 @mustprogress_load(i32* %ptr) mustprogress { -; CHECK-NOT: Function Attrs: {{.*}} willreturn -; CHECK: define i32 @mustprogress_load( +; CHECK: Function Attrs: {{.*}} readonly willreturn +; CHECK-NEXT: define i32 @mustprogress_load( ; entry: %r = load i32, i32* %ptr @@ -35,16 +33,15 @@ declare void @unknown_fn() define void @mustprogress_call_unknown_fn() mustprogress { ; CHECK-NOT: Function Attrs: {{.*}} willreturn -; CHECK: define void @mustprogress_call_unknown_fn( +; CHECK: define void @mustprogress_call_unknown_fn( ; call void @unknown_fn() ret void } -; TODO define i32 @mustprogress_call_known_functions(i32* %ptr) mustprogress { -; CHECK-NOT: Function Attrs: {{.*}} willreturn -; CHECK: define i32 @mustprogress_call_known_functions( +; CHECK: Function Attrs: {{.*}} readonly willreturn +; CHECK-NEXT: define i32 @mustprogress_call_known_functions( ; call void @mustprogress_readnone() %r = call i32 @mustprogress_load(i32* %ptr) @@ -53,10 +50,9 @@ define i32 @mustprogress_call_known_functions(i32* %ptr) mustprogress { declare i32 @__gxx_personality_v0(...) -; TODO define i64 @mustprogress_mayunwind() mustprogress personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { -; CHECK-NOT: Function Attrs: {{.*}} willreturn -; CHECK: define i64 @mustprogress_mayunwind( +; CHECK: Function Attrs: {{.*}} readnone willreturn +; CHECK-NEXT: define i64 @mustprogress_mayunwind( ; %a = invoke i64 @fn_noread() to label %A unwind label %B _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits