Skip to content

Commit 7a1065c

Browse files
committed
Comment SimplifyCFG JumpThreadingCost.
Explain how this actually works since it isn't directly obvious.
1 parent e5e2cf1 commit 7a1065c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ class SimplifyCFG {
8383
// this set may or may not still be LoopHeaders.
8484
// (ultimately this can be used to eliminate findLoopHeaders)
8585
SmallPtrSet<SILBasicBlock *, 4> ClonedLoopHeaders;
86-
// The cost (~ number of copied instructions) of jump threading per basic
87-
// block. Used to prevent infinite jump threading loops.
86+
// The accumulated cost of jump threading per basic block. Initially
87+
// zero. Each clone increases the cost by ~ the number of copied instructions.
88+
// Effectively multiplying a block's cost is by the number of times it has
89+
// been cloned prevents any one block from being cloned indefinitely. Cloned
90+
// blocks inherit their original block's current cost to avoid indefinitely
91+
// optimizing the newly cloned blocks (primarily relevant for loops where the
92+
// number of predecessors can remain the same).
8893
llvm::SmallDenseMap<SILBasicBlock *, int, 8> JumpThreadingCost;
8994

9095
// Dominance and post-dominance info for the current function
@@ -318,8 +323,8 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
318323
return false;
319324

320325
Cloner.cloneBranchTarget(SrcTerm);
321-
JumpThreadingCost[Cloner.getNewBB()] = JumpThreadingCost[SrcTerm->getDestBB()];
322-
326+
JumpThreadingCost[Cloner.getNewBB()] =
327+
JumpThreadingCost[SrcTerm->getDestBB()];
323328

324329
// We have copied the threaded block into the edge.
325330
auto *clonedSrc = Cloner.getNewBB();
@@ -1090,7 +1095,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
10901095
}
10911096
}
10921097
}
1093-
1098+
// Deduct the prior cost of cloning these blocks (initially zero).
10941099
ThreadingBudget -= JumpThreadingCost[SrcBB];
10951100
ThreadingBudget -= JumpThreadingCost[DestBB];
10961101

@@ -1126,6 +1131,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
11261131
LLVM_DEBUG(llvm::dbgs() << "jump thread from bb" << SrcBB->getDebugID()
11271132
<< " to bb" << DestBB->getDebugID() << '\n');
11281133

1134+
// Accumulate the cost of cloning the block to avoid indefinite cloning.
11291135
JumpThreadingCost[DestBB] += copyCosts;
11301136

11311137
// Duplicate the destination block into this one, rewriting uses of the BBArgs
@@ -1134,7 +1140,8 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
11341140
Cloner.updateSSAAfterCloning();
11351141

11361142
// Also account the costs to the cloned DestBB, so the jump threading cannot
1137-
// loop by cloning the cloned block again.
1143+
// loop by cloning the cloned block again. This is primarily relevant for
1144+
// loops where the number of predecessors might not decrease with each clone.
11381145
JumpThreadingCost[Cloner.getNewBB()] += copyCosts;
11391146

11401147
// Once all the instructions are copied, we can nuke BI itself. We also add

0 commit comments

Comments
 (0)