@@ -83,8 +83,13 @@ class SimplifyCFG {
83
83
// this set may or may not still be LoopHeaders.
84
84
// (ultimately this can be used to eliminate findLoopHeaders)
85
85
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).
88
93
llvm::SmallDenseMap<SILBasicBlock *, int , 8 > JumpThreadingCost;
89
94
90
95
// Dominance and post-dominance info for the current function
@@ -318,8 +323,8 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
318
323
return false ;
319
324
320
325
Cloner.cloneBranchTarget (SrcTerm);
321
- JumpThreadingCost[Cloner.getNewBB ()] = JumpThreadingCost[SrcTerm-> getDestBB ()];
322
-
326
+ JumpThreadingCost[Cloner.getNewBB ()] =
327
+ JumpThreadingCost[SrcTerm-> getDestBB ()];
323
328
324
329
// We have copied the threaded block into the edge.
325
330
auto *clonedSrc = Cloner.getNewBB ();
@@ -1090,7 +1095,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1090
1095
}
1091
1096
}
1092
1097
}
1093
-
1098
+ // Deduct the prior cost of cloning these blocks (initially zero).
1094
1099
ThreadingBudget -= JumpThreadingCost[SrcBB];
1095
1100
ThreadingBudget -= JumpThreadingCost[DestBB];
1096
1101
@@ -1126,6 +1131,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1126
1131
LLVM_DEBUG (llvm::dbgs () << " jump thread from bb" << SrcBB->getDebugID ()
1127
1132
<< " to bb" << DestBB->getDebugID () << ' \n ' );
1128
1133
1134
+ // Accumulate the cost of cloning the block to avoid indefinite cloning.
1129
1135
JumpThreadingCost[DestBB] += copyCosts;
1130
1136
1131
1137
// Duplicate the destination block into this one, rewriting uses of the BBArgs
@@ -1134,7 +1140,8 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1134
1140
Cloner.updateSSAAfterCloning ();
1135
1141
1136
1142
// 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.
1138
1145
JumpThreadingCost[Cloner.getNewBB ()] += copyCosts;
1139
1146
1140
1147
// Once all the instructions are copied, we can nuke BI itself. We also add
0 commit comments