From 5f7a5353301b776ffb0e5fb048992898507bf7ee Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Sun, 3 Oct 2021 16:14:06 -0700 Subject: [PATCH] [SCEV] Cap the number of instructions scanned when infering flags This addresses a comment from review on D109845. The concern was raised that an unbounded scan would be expensive. Long term plan is to cache this search - likely reusing the existing mechanism for loop side effects - but let's be simple and conservative for now. --- llvm/lib/Analysis/ScalarEvolution.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index f9d7ff2..b51e8a7 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -6597,9 +6597,19 @@ const Instruction *ScalarEvolution::getDefiningScopeBound(const SCEV *S) { static bool isGuaranteedToTransferExecutionToSuccessor(BasicBlock::const_iterator Begin, BasicBlock::const_iterator End) { - return llvm::all_of( make_range(Begin, End), [](const Instruction &I) { - return isGuaranteedToTransferExecutionToSuccessor(&I); - }); + // Limit number of instructions we look at, to avoid scanning through large + // blocks. The current limit is chosen arbitrarily. + unsigned ScanLimit = 32; + for (const Instruction &I : make_range(Begin, End)) { + if (isa(I)) + continue; + if (--ScanLimit == 0) + return false; + + if (!isGuaranteedToTransferExecutionToSuccessor(&I)) + return false; + } + return true; } bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A, -- 2.7.4