[llvm] [LoopInfo] Merge changeTopLevelLoop and replaceChildLoopWith. NFC (PR #221503)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 5 17:27:41 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
Both replace a loop among its siblings with a new one.
---
Full diff: https://github.com/llvm/llvm-project/pull/221503.diff
4 Files Affected:
- (modified) llvm/include/llvm/Support/GenericLoopInfo.h (+11-14)
- (modified) llvm/include/llvm/Support/GenericLoopInfoImpl.h (-17)
- (modified) llvm/lib/Transforms/Scalar/LoopInterchange.cpp (+1-1)
- (modified) llvm/lib/Transforms/Utils/LoopSimplify.cpp (+1-4)
``````````diff
diff --git a/llvm/include/llvm/Support/GenericLoopInfo.h b/llvm/include/llvm/Support/GenericLoopInfo.h
index db4bf951edca9..9d3cc66028886 100644
--- a/llvm/include/llvm/Support/GenericLoopInfo.h
+++ b/llvm/include/llvm/Support/GenericLoopInfo.h
@@ -374,12 +374,6 @@ template <class BlockT, class LoopT> class LoopBase {
/// is not valid to replace the loop header with this method.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
- /// This is used when splitting loops up. It replaces the OldChild entry in
- /// our children list with NewChild, and updates the parent pointer of
- /// OldChild to be null and the NewChild to be this loop.
- /// This updates the loop depth of the new child.
- void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
-
/// Add the specified loop to be a child of this loop.
/// This updates the loop depth of the new child.
void addChildLoop(LoopT *NewChild) {
@@ -801,14 +795,17 @@ template <class BlockT, class LoopT> class LoopInfoBase {
BBMap[Number] = L;
}
- /// Replace the specified loop in the top-level loops list with the indicated
- /// loop.
- void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
- auto I = find(TopLevelLoops, OldLoop);
- assert(I != TopLevelLoops.end() && "Old loop not at top level!");
- *I = NewLoop;
- assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
- "Loops already embedded into a subloop!");
+ /// Replace a loop among its siblings (a parent loop's child list or the
+ /// top-level list) with a new loop.
+ void replaceLoop(LoopT *Old, LoopT *New) {
+ assert(!New->ParentLoop && "New loop already has a parent!");
+ LoopT *Parent = Old->ParentLoop;
+ auto &Siblings = Parent ? Parent->SubLoops : TopLevelLoops;
+ auto I = find(Siblings, Old);
+ assert(I != Siblings.end() && "Old loop is not among its siblings!");
+ *I = New;
+ Old->ParentLoop = nullptr;
+ New->ParentLoop = Parent;
}
/// This adds the specified loop to the collection of top-level loops.
diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index 17b9226da421c..5ac1c35957cfb 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -305,23 +305,6 @@ void LoopBase<BlockT, LoopT>::addBasicBlockToLoop(
}
}
-/// replaceChildLoopWith - This is used when splitting loops up. It replaces
-/// the OldChild entry in our children list with NewChild, and updates the
-/// parent pointer of OldChild to be null and the NewChild to be this loop.
-/// This updates the loop depth of the new child.
-template <class BlockT, class LoopT>
-void LoopBase<BlockT, LoopT>::replaceChildLoopWith(LoopT *OldChild,
- LoopT *NewChild) {
- assert(!isInvalid() && "Loop not in a valid state!");
- assert(OldChild->ParentLoop == this && "This loop is already broken!");
- assert(!NewChild->ParentLoop && "NewChild already has a parent!");
- typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
- assert(I != SubLoops.end() && "OldChild not in loop!");
- *I = NewChild;
- OldChild->ParentLoop = nullptr;
- NewChild->ParentLoop = static_cast<LoopT *>(this);
-}
-
/// verifyLoop - Verify loop structure
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::verifyLoop() const {
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 05e5da01d3e2f..b28d2af5cb7ff 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -2079,7 +2079,7 @@ void LoopInterchangeTransform::restructureLoops(
OuterLoopParent->addChildLoop(NewOuter);
} else {
removeChildLoop(NewInner, NewOuter);
- LI->changeTopLevelLoop(NewInner, NewOuter);
+ LI->replaceLoop(NewInner, NewOuter);
}
while (!NewOuter->isInnermost())
NewInner->addChildLoop(NewOuter->removeChildLoop(NewOuter->begin()));
diff --git a/llvm/lib/Transforms/Utils/LoopSimplify.cpp b/llvm/lib/Transforms/Utils/LoopSimplify.cpp
index 1a14880949907..ed69b7e9a27eb 100644
--- a/llvm/lib/Transforms/Utils/LoopSimplify.cpp
+++ b/llvm/lib/Transforms/Utils/LoopSimplify.cpp
@@ -277,10 +277,7 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
Loop *NewOuter = LI->AllocateLoop();
// Change the parent loop to use the outer loop as its child now.
- if (Loop *Parent = L->getParentLoop())
- Parent->replaceChildLoopWith(L, NewOuter);
- else
- LI->changeTopLevelLoop(L, NewOuter);
+ LI->replaceLoop(L, NewOuter);
// L is now a subloop of our outer loop.
NewOuter->addChildLoop(L);
``````````
</details>
https://github.com/llvm/llvm-project/pull/221503
More information about the llvm-commits
mailing list