[PATCH] D119571: [funcattrs] use DominatorTree to improve noreturn
Nick Desaulniers via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 11 12:58:47 PST 2022
nickdesaulniers updated this revision to Diff 408007.
nickdesaulniers added a comment.
- drop DomTree as per @nikic
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119571/new/
https://reviews.llvm.org/D119571
Files:
llvm/lib/Transforms/IPO/FunctionAttrs.cpp
llvm/test/Transforms/FunctionAttrs/noreturn.ll
Index: llvm/test/Transforms/FunctionAttrs/noreturn.ll
===================================================================
--- llvm/test/Transforms/FunctionAttrs/noreturn.ll
+++ llvm/test/Transforms/FunctionAttrs/noreturn.ll
@@ -40,9 +40,8 @@
ret i32 %c
}
-; CHECK-NOT: Function Attrs: {{.*}}noreturn
+; CHECK: Function Attrs: {{.*}}noreturn
; CHECK: @caller5()
-; We currently don't handle unreachable blocks.
define i32 @caller5() {
entry:
%c = call i32 @noreturn()
@@ -87,4 +86,4 @@
}
declare token @llvm.coro.id.retcon.once(i32 %size, i32 %align, i8* %buffer, i8* %prototype, i8* %alloc, i8* %free)
-declare i1 @llvm.coro.end(i8*, i1)
\ No newline at end of file
+declare i1 @llvm.coro.end(i8*, i1)
Index: llvm/lib/Transforms/IPO/FunctionAttrs.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -1614,6 +1614,27 @@
return none_of(BB, instructionDoesNotReturn);
}
+static bool canReturn(Function &F) {
+ if (F.empty())
+ return true;
+
+ SmallVector<BasicBlock *, 16> Worklist;
+ SmallSetVector<BasicBlock *, 16> Visited;
+ Worklist.push_back(&F.front());
+
+ while (!Worklist.empty()) {
+ BasicBlock *BB = Worklist.pop_back_val();
+ if (basicBlockCanReturn(*BB))
+ return true;
+ if (Visited.contains(BB))
+ continue;
+ Visited.insert(BB);
+ for (BasicBlock *Succ : successors(BB))
+ Worklist.push_back(Succ);
+ }
+ return false;
+}
+
// Set the noreturn function attribute if possible.
static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
SmallSet<Function *, 8> &Changed) {
@@ -1622,9 +1643,7 @@
F->doesNotReturn())
continue;
- // The function can return if any basic blocks can return.
- // FIXME: this doesn't handle recursion or unreachable blocks.
- if (none_of(*F, basicBlockCanReturn)) {
+ if (!canReturn(*F)) {
F->setDoesNotReturn();
Changed.insert(F);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119571.408007.patch
Type: text/x-patch
Size: 2030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220211/4e93d7b6/attachment.bin>
More information about the llvm-commits
mailing list