Skip to content

Commit bc48a26

Browse files
committed
[LoopPeel] Use reference instead of pointer for DT argument
Cleanup code in peelLoop API. We already have usage of DT without guarding against a null DT, so this change constant folds the remaining null DT checks. Also make the argument a reference so that it is clear the argument is a nonnull DT. Extracted from D118472.
1 parent 453620f commit bc48a26

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

llvm/include/llvm/Transforms/Utils/LoopPeel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace llvm {
2121
bool canPeel(Loop *L);
2222

2323
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
24-
DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA);
24+
DominatorTree &DT, AssumptionCache *AC, bool PreserveLCSSA);
2525

2626
TargetTransformInfo::PeelingPreferences
2727
gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,

llvm/lib/Transforms/Scalar/LoopFuse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ struct LoopFuser {
767767
LLVM_DEBUG(dbgs() << "Attempting to peel first " << PeelCount
768768
<< " iterations of the first loop. \n");
769769

770-
FC0.Peeled = peelLoop(FC0.L, PeelCount, &LI, &SE, &DT, &AC, true);
770+
FC0.Peeled = peelLoop(FC0.L, PeelCount, &LI, &SE, DT, &AC, true);
771771
if (FC0.Peeled) {
772772
LLVM_DEBUG(dbgs() << "Done Peeling\n");
773773

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ static LoopUnrollResult tryToUnrollLoop(
12811281
<< " iterations";
12821282
});
12831283

1284-
if (peelLoop(L, PP.PeelCount, LI, &SE, &DT, &AC, PreserveLCSSA)) {
1284+
if (peelLoop(L, PP.PeelCount, LI, &SE, DT, &AC, PreserveLCSSA)) {
12851285
simplifyLoopAfterUnroll(L, true, LI, &SE, &DT, &AC, &TTI);
12861286
// If the loop was peeled, we already "used up" the profile information
12871287
// we had, so we don't want to unroll or peel again.

llvm/lib/Transforms/Utils/LoopPeel.cpp

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ TargetTransformInfo::PeelingPreferences llvm::gatherPeelingPreferences(
737737
/// for the bulk of dynamic execution, can be further simplified by scalar
738738
/// optimizations.
739739
bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
740-
ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
740+
ScalarEvolution *SE, DominatorTree &DT, AssumptionCache *AC,
741741
bool PreserveLCSSA) {
742742
assert(PeelCount > 0 && "Attempt to peel out zero iterations?");
743743
assert(canPeel(L) && "Attempt to peel a loop which is not peelable?");
@@ -756,23 +756,21 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
756756
// routes which can lead to the exit: we can reach it from the peeled
757757
// iterations too.
758758
DenseMap<BasicBlock *, BasicBlock *> NonLoopBlocksIDom;
759-
if (DT) {
760-
for (auto *BB : L->blocks()) {
761-
auto *BBDomNode = DT->getNode(BB);
762-
SmallVector<BasicBlock *, 16> ChildrenToUpdate;
763-
for (auto *ChildDomNode : BBDomNode->children()) {
764-
auto *ChildBB = ChildDomNode->getBlock();
765-
if (!L->contains(ChildBB))
766-
ChildrenToUpdate.push_back(ChildBB);
767-
}
768-
// The new idom of the block will be the nearest common dominator
769-
// of all copies of the previous idom. This is equivalent to the
770-
// nearest common dominator of the previous idom and the first latch,
771-
// which dominates all copies of the previous idom.
772-
BasicBlock *NewIDom = DT->findNearestCommonDominator(BB, Latch);
773-
for (auto *ChildBB : ChildrenToUpdate)
774-
NonLoopBlocksIDom[ChildBB] = NewIDom;
759+
for (auto *BB : L->blocks()) {
760+
auto *BBDomNode = DT.getNode(BB);
761+
SmallVector<BasicBlock *, 16> ChildrenToUpdate;
762+
for (auto *ChildDomNode : BBDomNode->children()) {
763+
auto *ChildBB = ChildDomNode->getBlock();
764+
if (!L->contains(ChildBB))
765+
ChildrenToUpdate.push_back(ChildBB);
775766
}
767+
// The new idom of the block will be the nearest common dominator
768+
// of all copies of the previous idom. This is equivalent to the
769+
// nearest common dominator of the previous idom and the first latch,
770+
// which dominates all copies of the previous idom.
771+
BasicBlock *NewIDom = DT.findNearestCommonDominator(BB, Latch);
772+
for (auto *ChildBB : ChildrenToUpdate)
773+
NonLoopBlocksIDom[ChildBB] = NewIDom;
776774
}
777775

778776
Function *F = Header->getParent();
@@ -822,11 +820,11 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
822820
// If (cond) goto Header
823821
// Exit:
824822

825-
BasicBlock *InsertTop = SplitEdge(PreHeader, Header, DT, LI);
823+
BasicBlock *InsertTop = SplitEdge(PreHeader, Header, &DT, LI);
826824
BasicBlock *InsertBot =
827-
SplitBlock(InsertTop, InsertTop->getTerminator(), DT, LI);
825+
SplitBlock(InsertTop, InsertTop->getTerminator(), &DT, LI);
828826
BasicBlock *NewPreHeader =
829-
SplitBlock(InsertBot, InsertBot->getTerminator(), DT, LI);
827+
SplitBlock(InsertBot, InsertBot->getTerminator(), &DT, LI);
830828

831829
InsertTop->setName(Header->getName() + ".peel.begin");
832830
InsertBot->setName(Header->getName() + ".peel.next");
@@ -852,23 +850,21 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
852850
ValueToValueMapTy VMap;
853851

854852
cloneLoopBlocks(L, Iter, InsertTop, InsertBot, ExitEdges, NewBlocks,
855-
LoopBlocks, VMap, LVMap, DT, LI,
853+
LoopBlocks, VMap, LVMap, &DT, LI,
856854
LoopLocalNoAliasDeclScopes);
857855

858856
// Remap to use values from the current iteration instead of the
859857
// previous one.
860858
remapInstructionsInBlocks(NewBlocks, VMap);
861859

862-
if (DT) {
863-
// Update IDoms of the blocks reachable through exits.
864-
if (Iter == 0)
865-
for (auto BBIDom : NonLoopBlocksIDom)
866-
DT->changeImmediateDominator(BBIDom.first,
867-
cast<BasicBlock>(LVMap[BBIDom.second]));
860+
// Update IDoms of the blocks reachable through exits.
861+
if (Iter == 0)
862+
for (auto BBIDom : NonLoopBlocksIDom)
863+
DT.changeImmediateDominator(BBIDom.first,
864+
cast<BasicBlock>(LVMap[BBIDom.second]));
868865
#ifdef EXPENSIVE_CHECKS
869-
assert(DT->verify(DominatorTree::VerificationLevel::Fast));
866+
assert(DT.verify(DominatorTree::VerificationLevel::Fast));
870867
#endif
871-
}
872868

873869
auto *LatchBRCopy = cast<BranchInst>(VMap[LatchBR]);
874870
updateBranchWeights(InsertBot, LatchBRCopy, ExitWeight, FallThroughWeight);
@@ -877,7 +873,7 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
877873
LatchBRCopy->setMetadata(LLVMContext::MD_loop, nullptr);
878874

879875
InsertTop = InsertBot;
880-
InsertBot = SplitBlock(InsertBot, InsertBot->getTerminator(), DT, LI);
876+
InsertBot = SplitBlock(InsertBot, InsertBot->getTerminator(), &DT, LI);
881877
InsertBot->setName(Header->getName() + ".peel.next");
882878

883879
F->getBasicBlockList().splice(InsertTop->getIterator(),
@@ -912,10 +908,10 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
912908
SE->forgetTopmostLoop(L);
913909

914910
// Finally DomtTree must be correct.
915-
assert(DT->verify(DominatorTree::VerificationLevel::Fast));
911+
assert(DT.verify(DominatorTree::VerificationLevel::Fast));
916912

917913
// FIXME: Incrementally update loop-simplify
918-
simplifyLoop(L, DT, LI, SE, AC, nullptr, PreserveLCSSA);
914+
simplifyLoop(L, &DT, LI, SE, AC, nullptr, PreserveLCSSA);
919915

920916
NumPeeled++;
921917

0 commit comments

Comments
 (0)