Skip to content

Commit 8c0f413

Browse files
committed
[nfc] Expose canReturn from FunctionAttrs
1 parent 2a83c0c commit 8c0f413

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

llvm/include/llvm/Analysis/CFG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
174174

175175
return false;
176176
}
177+
bool canReturn(const Function &F);
177178
} // End llvm namespace
178179

179180
#endif

llvm/include/llvm/Transforms/IPO/FunctionAttrs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Module;
3030
/// Returns the memory access properties of this copy of the function.
3131
MemoryEffects computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR);
3232

33+
bool canReturn(const Function &F);
34+
3335
/// Propagate function attributes for function summaries along the index's
3436
/// callgraph during thinlink
3537
bool thinLTOPropagateFunctionAttrs(

llvm/lib/Analysis/CFG.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,37 @@ bool llvm::isPotentiallyReachable(
322322
return isPotentiallyReachable(
323323
A->getParent(), B->getParent(), ExclusionSet, DT, LI);
324324
}
325+
326+
static bool instructionDoesNotReturn(const Instruction &I) {
327+
if (auto *CB = dyn_cast<CallBase>(&I))
328+
return CB->hasFnAttr(Attribute::NoReturn);
329+
return false;
330+
}
331+
332+
// A basic block can only return if it terminates with a ReturnInst and does not
333+
// contain calls to noreturn functions.
334+
static bool basicBlockCanReturn(const BasicBlock &BB) {
335+
if (!isa<ReturnInst>(BB.getTerminator()))
336+
return false;
337+
return none_of(BB, instructionDoesNotReturn);
338+
}
339+
340+
// FIXME: this doesn't handle recursion.
341+
bool llvm::canReturn(const Function &F) {
342+
SmallVector<const BasicBlock *, 16> Worklist;
343+
SmallPtrSet<const BasicBlock *, 16> Visited;
344+
345+
Visited.insert(&F.front());
346+
Worklist.push_back(&F.front());
347+
348+
do {
349+
const BasicBlock *BB = Worklist.pop_back_val();
350+
if (basicBlockCanReturn(*BB))
351+
return true;
352+
for (const BasicBlock *Succ : successors(BB))
353+
if (Visited.insert(Succ).second)
354+
Worklist.push_back(Succ);
355+
} while (!Worklist.empty());
356+
357+
return false;
358+
}

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,41 +2090,6 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
20902090
Changed.insert(F);
20912091
}
20922092

2093-
static bool instructionDoesNotReturn(Instruction &I) {
2094-
if (auto *CB = dyn_cast<CallBase>(&I))
2095-
return CB->hasFnAttr(Attribute::NoReturn);
2096-
return false;
2097-
}
2098-
2099-
// A basic block can only return if it terminates with a ReturnInst and does not
2100-
// contain calls to noreturn functions.
2101-
static bool basicBlockCanReturn(BasicBlock &BB) {
2102-
if (!isa<ReturnInst>(BB.getTerminator()))
2103-
return false;
2104-
return none_of(BB, instructionDoesNotReturn);
2105-
}
2106-
2107-
// FIXME: this doesn't handle recursion.
2108-
static bool canReturn(Function &F) {
2109-
SmallVector<BasicBlock *, 16> Worklist;
2110-
SmallPtrSet<BasicBlock *, 16> Visited;
2111-
2112-
Visited.insert(&F.front());
2113-
Worklist.push_back(&F.front());
2114-
2115-
do {
2116-
BasicBlock *BB = Worklist.pop_back_val();
2117-
if (basicBlockCanReturn(*BB))
2118-
return true;
2119-
for (BasicBlock *Succ : successors(BB))
2120-
if (Visited.insert(Succ).second)
2121-
Worklist.push_back(Succ);
2122-
} while (!Worklist.empty());
2123-
2124-
return false;
2125-
}
2126-
2127-
21282093
// Set the noreturn function attribute if possible.
21292094
static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
21302095
SmallSet<Function *, 8> &Changed) {

0 commit comments

Comments
 (0)