OSDN Git Service

[NFC] Teach getInnermostLoopFor walk up the loop trees
authorMax Kazantsev <max.kazantsev@azul.com>
Sun, 17 Feb 2019 18:21:51 +0000 (18:21 +0000)
committerMax Kazantsev <max.kazantsev@azul.com>
Sun, 17 Feb 2019 18:21:51 +0000 (18:21 +0000)
This should be NFC in current use case of this method, but it will
help to use it for solving more compex tasks in follow-up patches.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354227 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopSimplifyCFG.cpp

index 4f1213d..eb8de34 100644 (file)
@@ -94,15 +94,19 @@ static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop,
 /// contains the header of loop \p L.
 static Loop *getInnermostLoopFor(SmallPtrSetImpl<BasicBlock *> &BBs,
                                  Loop &L, LoopInfo &LI) {
-  Loop *StillReachable = nullptr;
+  Loop *Innermost = nullptr;
   for (BasicBlock *BB : BBs) {
     Loop *BBL = LI.getLoopFor(BB);
-    if (BBL && BBL->contains(L.getHeader()))
-      if (!StillReachable ||
-          BBL->getLoopDepth() > StillReachable->getLoopDepth())
-        StillReachable = BBL;
+    while (BBL && !BBL->contains(L.getHeader()))
+      BBL = BBL->getParentLoop();
+    if (BBL == &L)
+      BBL = BBL->getParentLoop();
+    if (!BBL)
+      continue;
+    if (!Innermost || BBL->getLoopDepth() > Innermost->getLoopDepth())
+      Innermost = BBL;
   }
-  return StillReachable;
+  return Innermost;
 }
 
 namespace {