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 @@ -1424,6 +1424,21 @@ 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()) + continue; + + F->addFnAttr(Attribute::WillReturn); + F->removeFnAttr(Attribute::NoReturn); + } + + return Changed; +} + static SCCNodesResult createSCCNodeSet(ArrayRef Functions) { SCCNodesResult Res; Res.HasUnknownCall = false; @@ -1468,6 +1483,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: {{.*}} 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