2828#include " llvm/Analysis/LazyValueInfo.h"
2929#include " llvm/Analysis/Loads.h"
3030#include " llvm/Analysis/MemoryBuiltins.h"
31+ #include " llvm/Analysis/MustExecute.h"
3132#include " llvm/Analysis/ScalarEvolution.h"
3233#include " llvm/Analysis/ValueTracking.h"
3334#include " llvm/IR/Argument.h"
3637#include " llvm/IR/IRBuilder.h"
3738#include " llvm/IR/InstIterator.h"
3839#include " llvm/IR/IntrinsicInst.h"
40+ #include " llvm/IR/NoFolder.h"
3941#include " llvm/IR/Verifier.h"
4042#include " llvm/InitializePasses.h"
41- #include " llvm/IR/NoFolder.h"
4243#include " llvm/Support/CommandLine.h"
4344#include " llvm/Support/Debug.h"
4445#include " llvm/Support/raw_ostream.h"
@@ -2491,28 +2492,34 @@ struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
24912492
24922493// / ------------------------ Will-Return Attributes ----------------------------
24932494
2494- // Helper function that checks whether a function has any cycle.
2495- // TODO: Replace with more efficent code
2496- static bool containsCycle (Function &F) {
2497- SmallPtrSet<BasicBlock *, 32 > Visited;
2498-
2499- // Traverse BB by dfs and check whether successor is already visited.
2500- for (BasicBlock *BB : depth_first (&F)) {
2501- Visited.insert (BB);
2502- for (auto *SuccBB : successors (BB)) {
2503- if (Visited.count (SuccBB))
2495+ // Helper function that checks whether a function has any cycle which we don't
2496+ // know if it is bounded or not.
2497+ // Loops with maximum trip count are considered bounded, any other cycle not.
2498+ static bool mayContainUnboundedCycle (Function &F, Attributor &A) {
2499+ ScalarEvolution *SE =
2500+ A.getInfoCache ().getAnalysisResultForFunction <ScalarEvolutionAnalysis>(F);
2501+ LoopInfo *LI = A.getInfoCache ().getAnalysisResultForFunction <LoopAnalysis>(F);
2502+ // If either SCEV or LoopInfo is not available for the function then we assume
2503+ // any cycle to be unbounded cycle.
2504+ // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2505+ // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2506+ if (!SE || !LI) {
2507+ for (scc_iterator<Function *> SCCI = scc_begin (&F); !SCCI.isAtEnd (); ++SCCI)
2508+ if (SCCI.hasCycle ())
25042509 return true ;
2505- }
2510+ return false ;
25062511 }
2507- return false ;
2508- }
25092512
2510- // Helper function that checks the function have a loop which might become an
2511- // endless loop
2512- // FIXME: Any cycle is regarded as endless loop for now.
2513- // We have to allow some patterns.
2514- static bool containsPossiblyEndlessLoop (Function *F) {
2515- return containsCycle (*F);
2513+ // If there's irreducible control, the function may contain non-loop cycles.
2514+ if (mayContainIrreducibleControl (F, LI))
2515+ return true ;
2516+
2517+ // Any loop that does not have a max trip count is considered unbounded cycle.
2518+ for (auto *L : LI->getLoopsInPreorder ()) {
2519+ if (!SE->getSmallConstantMaxTripCount (L))
2520+ return true ;
2521+ }
2522+ return false ;
25162523}
25172524
25182525struct AAWillReturnImpl : public AAWillReturn {
@@ -2523,7 +2530,7 @@ struct AAWillReturnImpl : public AAWillReturn {
25232530 AAWillReturn::initialize (A);
25242531
25252532 Function *F = getAssociatedFunction ();
2526- if (!F || !A.isFunctionIPOAmendable (*F) || containsPossiblyEndlessLoop (F ))
2533+ if (!F || !A.isFunctionIPOAmendable (*F) || mayContainUnboundedCycle (*F, A ))
25272534 indicatePessimisticFixpoint ();
25282535 }
25292536
0 commit comments