1919#include " swift/SIL/SILCloner.h"
2020#include " swift/SILOptimizer/Analysis/LoopAnalysis.h"
2121#include " swift/SILOptimizer/Analysis/IsSelfRecursiveAnalysis.h"
22+ #include " swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
2223#include " swift/SILOptimizer/PassManager/Passes.h"
2324#include " swift/SILOptimizer/PassManager/Transforms.h"
2425#include " swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
@@ -211,7 +212,8 @@ static std::optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
211212// / heuristic that looks at the trip count and the cost of the instructions in
212213// / the loop to determine whether we should unroll this loop.
213214static bool canAndShouldUnrollLoop (SILLoop *Loop, uint64_t TripCount,
214- IsSelfRecursiveAnalysis *SRA) {
215+ IsSelfRecursiveAnalysis *SRA,
216+ DeadEndBlocks *deb) {
215217 assert (Loop->getSubLoops ().empty () && " Expect innermost loops" );
216218 if (TripCount > 32 )
217219 return false ;
@@ -227,7 +229,7 @@ static bool canAndShouldUnrollLoop(SILLoop *Loop, uint64_t TripCount,
227229 (Loop->getBlocks ())[0 ]->getParent ()->getModule ().getOptions ().UnrollThreshold ;
228230 for (auto *BB : Loop->getBlocks ()) {
229231 for (auto &Inst : *BB) {
230- if (!canDuplicateLoopInstruction (Loop, &Inst))
232+ if (!canDuplicateLoopInstruction (Loop, &Inst, deb ))
231233 return false ;
232234 if (instructionInlineCost (Inst) != InlineCost::Free)
233235 ++Cost;
@@ -388,7 +390,7 @@ updateSSA(SILFunction *Fn, SILLoop *Loop,
388390
389391// / Try to fully unroll the loop if we can determine the trip count and the trip
390392// / count is below a threshold.
391- static bool tryToUnrollLoop (SILLoop *Loop, IsSelfRecursiveAnalysis *SRA) {
393+ static bool tryToUnrollLoop (SILLoop *Loop, IsSelfRecursiveAnalysis *SRA, DeadEndBlocks *deb ) {
392394 assert (Loop->getSubLoops ().empty () && " Expecting innermost loops" );
393395
394396 LLVM_DEBUG (llvm::dbgs () << " Trying to unroll loop : \n " << *Loop);
@@ -409,7 +411,7 @@ static bool tryToUnrollLoop(SILLoop *Loop, IsSelfRecursiveAnalysis *SRA) {
409411 return false ;
410412 }
411413
412- if (!canAndShouldUnrollLoop (Loop, MaxTripCount.value (), SRA)) {
414+ if (!canAndShouldUnrollLoop (Loop, MaxTripCount.value (), SRA, deb )) {
413415 LLVM_DEBUG (llvm::dbgs () << " Not unrolling, exceeds cost threshold\n " );
414416 return false ;
415417 }
@@ -490,6 +492,7 @@ class LoopUnrolling : public SILFunctionTransform {
490492 auto *Fun = getFunction ();
491493 SILLoopInfo *LoopInfo = PM->getAnalysis <SILLoopAnalysis>()->get (Fun);
492494 IsSelfRecursiveAnalysis *SRA = PM->getAnalysis <IsSelfRecursiveAnalysis>();
495+ DeadEndBlocks *deb = PM->getAnalysis <DeadEndBlocksAnalysis>()->get (Fun);
493496
494497 LLVM_DEBUG (llvm::dbgs () << " Loop Unroll running on function : "
495498 << Fun->getName () << " \n " );
@@ -517,7 +520,7 @@ class LoopUnrolling : public SILFunctionTransform {
517520
518521 // Try to unroll innermost loops.
519522 for (auto *Loop : InnermostLoops)
520- Changed |= tryToUnrollLoop (Loop, SRA);
523+ Changed |= tryToUnrollLoop (Loop, SRA, deb );
521524
522525 if (Changed) {
523526 invalidateAnalysis (SILAnalysis::InvalidationKind::FunctionBody);
0 commit comments