19
19
#include " swift/SIL/SILCloner.h"
20
20
#include " swift/SILOptimizer/Analysis/LoopAnalysis.h"
21
21
#include " swift/SILOptimizer/Analysis/IsSelfRecursiveAnalysis.h"
22
+ #include " swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
22
23
#include " swift/SILOptimizer/PassManager/Passes.h"
23
24
#include " swift/SILOptimizer/PassManager/Transforms.h"
24
25
#include " swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
@@ -211,7 +212,8 @@ static std::optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
211
212
// / heuristic that looks at the trip count and the cost of the instructions in
212
213
// / the loop to determine whether we should unroll this loop.
213
214
static bool canAndShouldUnrollLoop (SILLoop *Loop, uint64_t TripCount,
214
- IsSelfRecursiveAnalysis *SRA) {
215
+ IsSelfRecursiveAnalysis *SRA,
216
+ DeadEndBlocks *deb) {
215
217
assert (Loop->getSubLoops ().empty () && " Expect innermost loops" );
216
218
if (TripCount > 32 )
217
219
return false ;
@@ -227,7 +229,7 @@ static bool canAndShouldUnrollLoop(SILLoop *Loop, uint64_t TripCount,
227
229
(Loop->getBlocks ())[0 ]->getParent ()->getModule ().getOptions ().UnrollThreshold ;
228
230
for (auto *BB : Loop->getBlocks ()) {
229
231
for (auto &Inst : *BB) {
230
- if (!canDuplicateLoopInstruction (Loop, &Inst))
232
+ if (!canDuplicateLoopInstruction (Loop, &Inst, deb ))
231
233
return false ;
232
234
if (instructionInlineCost (Inst) != InlineCost::Free)
233
235
++Cost;
@@ -388,7 +390,7 @@ updateSSA(SILFunction *Fn, SILLoop *Loop,
388
390
389
391
// / Try to fully unroll the loop if we can determine the trip count and the trip
390
392
// / count is below a threshold.
391
- static bool tryToUnrollLoop (SILLoop *Loop, IsSelfRecursiveAnalysis *SRA) {
393
+ static bool tryToUnrollLoop (SILLoop *Loop, IsSelfRecursiveAnalysis *SRA, DeadEndBlocks *deb ) {
392
394
assert (Loop->getSubLoops ().empty () && " Expecting innermost loops" );
393
395
394
396
LLVM_DEBUG (llvm::dbgs () << " Trying to unroll loop : \n " << *Loop);
@@ -409,7 +411,7 @@ static bool tryToUnrollLoop(SILLoop *Loop, IsSelfRecursiveAnalysis *SRA) {
409
411
return false ;
410
412
}
411
413
412
- if (!canAndShouldUnrollLoop (Loop, MaxTripCount.value (), SRA)) {
414
+ if (!canAndShouldUnrollLoop (Loop, MaxTripCount.value (), SRA, deb )) {
413
415
LLVM_DEBUG (llvm::dbgs () << " Not unrolling, exceeds cost threshold\n " );
414
416
return false ;
415
417
}
@@ -490,6 +492,7 @@ class LoopUnrolling : public SILFunctionTransform {
490
492
auto *Fun = getFunction ();
491
493
SILLoopInfo *LoopInfo = PM->getAnalysis <SILLoopAnalysis>()->get (Fun);
492
494
IsSelfRecursiveAnalysis *SRA = PM->getAnalysis <IsSelfRecursiveAnalysis>();
495
+ DeadEndBlocks *deb = PM->getAnalysis <DeadEndBlocksAnalysis>()->get (Fun);
493
496
494
497
LLVM_DEBUG (llvm::dbgs () << " Loop Unroll running on function : "
495
498
<< Fun->getName () << " \n " );
@@ -517,7 +520,7 @@ class LoopUnrolling : public SILFunctionTransform {
517
520
518
521
// Try to unroll innermost loops.
519
522
for (auto *Loop : InnermostLoops)
520
- Changed |= tryToUnrollLoop (Loop, SRA);
523
+ Changed |= tryToUnrollLoop (Loop, SRA, deb );
521
524
522
525
if (Changed) {
523
526
invalidateAnalysis (SILAnalysis::InvalidationKind::FunctionBody);
0 commit comments