[llvm] d77f944 - [LoopInfo] Add getOutermostLoop() (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 10 02:48:30 PDT 2022
Author: Nikita Popov
Date: 2022-06-10T11:48:21+02:00
New Revision: d77f944832ae6fcb33397072c717c7971df6d3ee
URL: https://github.com/llvm/llvm-project/commit/d77f944832ae6fcb33397072c717c7971df6d3ee
DIFF: https://github.com/llvm/llvm-project/commit/d77f944832ae6fcb33397072c717c7971df6d3ee.diff
LOG: [LoopInfo] Add getOutermostLoop() (NFC)
This is a recurring pattern, add an API function for it.
Added:
Modified:
llvm/include/llvm/Analysis/LoopInfo.h
llvm/include/llvm/Analysis/LoopInfoImpl.h
llvm/lib/Analysis/CFG.cpp
llvm/lib/Analysis/DependenceAnalysis.cpp
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/lib/Transforms/Utils/LoopUtils.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h
index 8bef178dda18b..9351b83ad7479 100644
--- a/llvm/include/llvm/Analysis/LoopInfo.h
+++ b/llvm/include/llvm/Analysis/LoopInfo.h
@@ -112,6 +112,22 @@ template <class BlockT, class LoopT> class LoopBase {
/// parent is the innermost loop in which it is enclosed.
LoopT *getParentLoop() const { return ParentLoop; }
+ /// Get the outermost loop in which this loop is contained.
+ /// This may be the loop itself, if it already is the outermost loop.
+ const LoopT *getOutermostLoop() const {
+ const LoopT *L = static_cast<const LoopT *>(this);
+ while (L->ParentLoop)
+ L = L->ParentLoop;
+ return L;
+ }
+
+ LoopT *getOutermostLoop() {
+ LoopT *L = static_cast<LoopT *>(this);
+ while (L->ParentLoop)
+ L = L->ParentLoop;
+ return L;
+ }
+
/// This is a raw interface for bypassing addChildLoop.
void setParentLoop(LoopT *L) {
assert(!isInvalid() && "Loop not in a valid state!");
diff --git a/llvm/include/llvm/Analysis/LoopInfoImpl.h b/llvm/include/llvm/Analysis/LoopInfoImpl.h
index fdaa6df8832fe..a96a698f3afbc 100644
--- a/llvm/include/llvm/Analysis/LoopInfoImpl.h
+++ b/llvm/include/llvm/Analysis/LoopInfoImpl.h
@@ -453,8 +453,7 @@ static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT *> Backedges,
InvBlockTraits::child_end(PredBB));
} else {
// This is a discovered block. Find its outermost discovered loop.
- while (LoopT *Parent = Subloop->getParentLoop())
- Subloop = Parent;
+ Subloop = Subloop->getOutermostLoop();
// If it is already discovered to be a subloop of this loop, continue.
if (Subloop == L)
diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index ec25ee161e2cf..1902d72f2f89b 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -127,11 +127,7 @@ bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
// the outermost loop in the loop nest that contains BB.
static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
const Loop *L = LI->getLoopFor(BB);
- if (L) {
- while (const Loop *Parent = L->getParentLoop())
- L = Parent;
- }
- return L;
+ return L ? L->getOutermostLoop() : nullptr;
}
bool llvm::isPotentiallyReachableFromMany(
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 1c6a99ea788f1..1778b4617ec2d 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -803,10 +803,7 @@ bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
// If the expression is invariant in the outermost loop of the loop nest, it
// is invariant anywhere in the loop nest.
- const Loop *OutermostLoop = LoopNest;
- while (OutermostLoop->getParentLoop())
- OutermostLoop = OutermostLoop->getParentLoop();
- return SE->isLoopInvariant(Expression, OutermostLoop);
+ return SE->isLoopInvariant(Expression, LoopNest->getOutermostLoop());
}
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 13591c0638181..57d1366b4066f 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -8085,9 +8085,7 @@ void ScalarEvolution::forgetLoop(const Loop *L) {
}
void ScalarEvolution::forgetTopmostLoop(const Loop *L) {
- while (Loop *Parent = L->getParentLoop())
- L = Parent;
- forgetLoop(L);
+ forgetLoop(L->getOutermostLoop());
}
void ScalarEvolution::forgetValue(Value *V) {
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index c5582fb90bbfe..965b35a50978b 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -686,18 +686,12 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
}
}
-static Loop *getOutermostLoop(Loop *L) {
- while (Loop *Parent = L->getParentLoop())
- L = Parent;
- return L;
-}
-
void llvm::breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
LoopInfo &LI, MemorySSA *MSSA) {
auto *Latch = L->getLoopLatch();
assert(Latch && "multiple latches not yet supported");
auto *Header = L->getHeader();
- Loop *OutermostLoop = getOutermostLoop(L);
+ Loop *OutermostLoop = L->getOutermostLoop();
SE.forgetLoop(L);
More information about the llvm-commits
mailing list