[llvm] [nfc] Expose `canReturn` from FunctionAttrs (PR #135650)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 14 10:26:45 PDT 2025
https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/135650
None
>From c58b167e7b34cdf8f65704a510711183b9d85964 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Mon, 14 Apr 2025 09:37:14 -0700
Subject: [PATCH] [nfc] Expose `canReturn` from FunctionAttrs
---
llvm/include/llvm/Analysis/CFG.h | 1 +
.../llvm/Transforms/IPO/FunctionAttrs.h | 2 ++
llvm/lib/Analysis/CFG.cpp | 34 ++++++++++++++++++
llvm/lib/Transforms/IPO/FunctionAttrs.cpp | 35 -------------------
4 files changed, 37 insertions(+), 35 deletions(-)
diff --git a/llvm/include/llvm/Analysis/CFG.h b/llvm/include/llvm/Analysis/CFG.h
index 23bc10a4a9d1b..8451e88146d7c 100644
--- a/llvm/include/llvm/Analysis/CFG.h
+++ b/llvm/include/llvm/Analysis/CFG.h
@@ -174,6 +174,7 @@ bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
return false;
}
+bool canReturn(const Function &F);
} // End llvm namespace
#endif
diff --git a/llvm/include/llvm/Transforms/IPO/FunctionAttrs.h b/llvm/include/llvm/Transforms/IPO/FunctionAttrs.h
index 6a21ff616d506..3a2c09afbebd3 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionAttrs.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionAttrs.h
@@ -30,6 +30,8 @@ class Module;
/// Returns the memory access properties of this copy of the function.
MemoryEffects computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR);
+bool canReturn(const Function &F);
+
/// Propagate function attributes for function summaries along the index's
/// callgraph during thinlink
bool thinLTOPropagateFunctionAttrs(
diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 841b835052380..8ced4a901557d 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -322,3 +322,37 @@ bool llvm::isPotentiallyReachable(
return isPotentiallyReachable(
A->getParent(), B->getParent(), ExclusionSet, DT, LI);
}
+
+static bool instructionDoesNotReturn(const Instruction &I) {
+ if (auto *CB = dyn_cast<CallBase>(&I))
+ return CB->hasFnAttr(Attribute::NoReturn);
+ return false;
+}
+
+// A basic block can only return if it terminates with a ReturnInst and does not
+// contain calls to noreturn functions.
+static bool basicBlockCanReturn(const BasicBlock &BB) {
+ if (!isa<ReturnInst>(BB.getTerminator()))
+ return false;
+ return none_of(BB, instructionDoesNotReturn);
+}
+
+// FIXME: this doesn't handle recursion.
+bool llvm::canReturn(const Function &F) {
+ SmallVector<const BasicBlock *, 16> Worklist;
+ SmallPtrSet<const BasicBlock *, 16> Visited;
+
+ Visited.insert(&F.front());
+ Worklist.push_back(&F.front());
+
+ do {
+ const BasicBlock *BB = Worklist.pop_back_val();
+ if (basicBlockCanReturn(*BB))
+ return true;
+ for (const BasicBlock *Succ : successors(BB))
+ if (Visited.insert(Succ).second)
+ Worklist.push_back(Succ);
+ } while (!Worklist.empty());
+
+ return false;
+}
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index ef7989507c89f..bbfed2ac2c090 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -2090,41 +2090,6 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
Changed.insert(F);
}
-static bool instructionDoesNotReturn(Instruction &I) {
- if (auto *CB = dyn_cast<CallBase>(&I))
- return CB->hasFnAttr(Attribute::NoReturn);
- return false;
-}
-
-// A basic block can only return if it terminates with a ReturnInst and does not
-// contain calls to noreturn functions.
-static bool basicBlockCanReturn(BasicBlock &BB) {
- if (!isa<ReturnInst>(BB.getTerminator()))
- return false;
- return none_of(BB, instructionDoesNotReturn);
-}
-
-// FIXME: this doesn't handle recursion.
-static bool canReturn(Function &F) {
- SmallVector<BasicBlock *, 16> Worklist;
- SmallPtrSet<BasicBlock *, 16> Visited;
-
- Visited.insert(&F.front());
- Worklist.push_back(&F.front());
-
- do {
- BasicBlock *BB = Worklist.pop_back_val();
- if (basicBlockCanReturn(*BB))
- return true;
- for (BasicBlock *Succ : successors(BB))
- if (Visited.insert(Succ).second)
- Worklist.push_back(Succ);
- } while (!Worklist.empty());
-
- return false;
-}
-
-
// Set the noreturn function attribute if possible.
static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
SmallSet<Function *, 8> &Changed) {
More information about the llvm-commits
mailing list