diff --git a/llvm/lib/Transforms/Scalar/LoopSink.cpp b/llvm/lib/Transforms/Scalar/LoopSink.cpp index 6eedf95e7575e..5c6ed8487bbd1 100644 --- a/llvm/lib/Transforms/Scalar/LoopSink.cpp +++ b/llvm/lib/Transforms/Scalar/LoopSink.cpp @@ -144,7 +144,23 @@ findBBsToSinkInto(const Loop &L, const SmallPtrSetImpl &UseBBs, BBsToSinkInto.erase(DominatedBB); } BBsToSinkInto.insert(ColdestBB); + continue; } + // Otherwise, see if we can stop the search through the cold BBs early. + // Since the ColdLoopBBs list is sorted in increasing magnitude of + // frequency the cold BB frequencies can only get larger. The + // BBsToSinkInto set can only get smaller and have a smaller + // adjustedSumFreq, due to the earlier checking. So once we find a cold BB + // with a frequency at least as large as the adjustedSumFreq of the + // current BBsToSinkInto set, the earlier frequency check can never be + // true for a future iteration. Note we could do check this more + // aggressively earlier, but in practice this ended up being more + // expensive overall (added checking to the critical path through the loop + // that often ended up continuing early due to an empty + // BBsDominatedByColdestBB set, and the frequency check there was false + // most of the time anyway). + if (adjustedSumFreq(BBsToSinkInto, BFI) <= BFI.getBlockFreq(ColdestBB)) + break; } // Can't sink into blocks that have no valid insertion point.