diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -640,6 +640,10 @@ } 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 --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -77,6 +77,7 @@ 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 EnableNonnullArgPropagation( "enable-nonnull-arg-prop", cl::init(true), cl::Hidden, @@ -1424,6 +1425,22 @@ 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 Functions) { SCCNodesResult Res; Res.HasUnknownCall = false; @@ -1468,6 +1485,7 @@ 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 --- 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 @@ 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 @@ 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 @@ 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