[NFC] Factor out a function for future reuse
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354218 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/LoopSimplifyCFG.cpp b/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
index 0db5d9e..124c55e 100644
--- a/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
+++ b/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
@@ -90,6 +90,20 @@
Current->removeBlockFromLoop(BB);
}
+/// Find innermost loop that is reachable from \p BBs and contains loop \p L.
+static Loop *getInnermostSuccessorLoop(SmallPtrSetImpl<BasicBlock *> &BBs,
+ Loop &L, LoopInfo &LI) {
+ Loop *StillReachable = nullptr;
+ for (BasicBlock *BB : BBs) {
+ Loop *BBL = LI.getLoopFor(BB);
+ if (BBL && BBL->contains(L.getHeader()))
+ if (!StillReachable ||
+ BBL->getLoopDepth() > StillReachable->getLoopDepth())
+ StillReachable = BBL;
+ }
+ return StillReachable;
+}
+
namespace {
/// Helper class that can turn branches and switches with constant conditions
/// into unconditional branches.
@@ -369,14 +383,7 @@
// the current loop. We need to fix loop info accordingly. For this, we
// find the most nested loop that still contains L and remove L from all
// loops that are inside of it.
- Loop *StillReachable = nullptr;
- for (BasicBlock *BB : LiveExitBlocks) {
- Loop *BBL = LI.getLoopFor(BB);
- if (BBL && BBL->contains(L.getHeader()))
- if (!StillReachable ||
- BBL->getLoopDepth() > StillReachable->getLoopDepth())
- StillReachable = BBL;
- }
+ Loop *StillReachable = getInnermostSuccessorLoop(LiveExitBlocks, L, LI);
// Okay, our loop is no longer in the outer loop (and maybe not in some of
// its parents as well). Make the fixup.