@@ -72,9 +72,9 @@ namespace {
72
72
llvm::SmallDenseMap<SILBasicBlock*, unsigned , 32 > WorklistMap;
73
73
// Keep track of loop headers - we don't want to jump-thread through them.
74
74
SmallPtrSet<SILBasicBlock *, 32 > LoopHeaders;
75
- // The number of times jump threading was done through a block.
76
- // Used to prevent infinite jump threading loops.
77
- llvm::SmallDenseMap<SILBasicBlock*, unsigned , 8 > JumpThreadedBlocks ;
75
+ // The cost (~ number of copied instructions) of jump threading per basic
76
+ // block. Used to prevent infinite jump threading loops.
77
+ llvm::SmallDenseMap<SILBasicBlock *, int , 8 > JumpThreadingCost ;
78
78
79
79
// Dominance and post-dominance info for the current function
80
80
DominanceInfo *DT = nullptr ;
@@ -1014,15 +1014,19 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1014
1014
}
1015
1015
}
1016
1016
1017
+ ThreadingBudget -= JumpThreadingCost[SrcBB];
1018
+ ThreadingBudget -= JumpThreadingCost[DestBB];
1019
+
1017
1020
// If we don't have anything that we can simplify, don't do it.
1018
- if (ThreadingBudget = = 0 )
1021
+ if (ThreadingBudget < = 0 )
1019
1022
return false ;
1020
1023
1021
1024
// If it looks potentially interesting, decide whether we *can* do the
1022
1025
// operation and whether the block is small enough to be worth duplicating.
1026
+ int copyCosts = 0 ;
1023
1027
for (auto &Inst : *DestBB) {
1024
- ThreadingBudget - = getThreadingCost (&Inst);
1025
- if (ThreadingBudget <= 0 )
1028
+ copyCosts + = getThreadingCost (&Inst);
1029
+ if (ThreadingBudget <= copyCosts )
1026
1030
return false ;
1027
1031
1028
1032
// We need to update ssa if a value is used outside the duplicated block.
@@ -1044,15 +1048,11 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
1044
1048
return false ;
1045
1049
}
1046
1050
1047
- // Limit the number we jump-thread through a block.
1048
- // Otherwise we may end up with jump-threading indefinitely.
1049
- unsigned &NumThreaded = JumpThreadedBlocks[DestBB];
1050
- if (++NumThreaded > 16 )
1051
- return false ;
1052
-
1053
1051
LLVM_DEBUG (llvm::dbgs () << " jump thread from bb" << SrcBB->getDebugID ()
1054
1052
<< " to bb" << DestBB->getDebugID () << ' \n ' );
1055
1053
1054
+ JumpThreadingCost[DestBB] += copyCosts;
1055
+
1056
1056
// Okay, it looks like we want to do this and we can. Duplicate the
1057
1057
// destination block into this one, rewriting uses of the BBArgs to use the
1058
1058
// branch arguments as we go.
@@ -1267,6 +1267,12 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
1267
1267
if (LoopHeaders.count (DestBB))
1268
1268
LoopHeaders.insert (BB);
1269
1269
1270
+ auto Iter = JumpThreadingCost.find (DestBB);
1271
+ if (Iter != JumpThreadingCost.end ()) {
1272
+ int costs = Iter->second ;
1273
+ JumpThreadingCost[BB] += costs;
1274
+ }
1275
+
1270
1276
removeFromWorklist (DestBB);
1271
1277
DestBB->eraseFromParent ();
1272
1278
++NumBlocksMerged;
0 commit comments