Skip to content

[Support] Erase blocks after DomTree::eraseNode #101195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/GenericDomTreeUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class GenericDomTreeUpdater {
/// insertEdge/deleteEdge or is unnecessary in the batch update.
bool isUpdateValid(typename DomTreeT::UpdateType Update) const;

/// Erase Basic Block node that has been unlinked from Function
/// Erase Basic Block node before it is unlinked from Function
/// in the DomTree and PostDomTree.
void eraseDelBBNode(BasicBlockT *DelBB);

Expand Down
8 changes: 3 additions & 5 deletions llvm/lib/Analysis/DomTreeUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ bool DomTreeUpdater::forceFlushDeletedBB() {
// delete only has an UnreachableInst inside.
assert(BB->size() == 1 && isa<UnreachableInst>(BB->getTerminator()) &&
"DelBB has been modified while awaiting deletion.");
BB->removeFromParent();
eraseDelBBNode(BB);
delete BB;
BB->eraseFromParent();
}
DeletedBBs.clear();
Callbacks.clear();
Expand All @@ -63,9 +62,8 @@ void DomTreeUpdater::deleteBB(BasicBlock *DelBB) {
return;
}

DelBB->removeFromParent();
eraseDelBBNode(DelBB);
delete DelBB;
DelBB->eraseFromParent();
}

void DomTreeUpdater::callbackDeleteBB(
Expand All @@ -77,8 +75,8 @@ void DomTreeUpdater::callbackDeleteBB(
return;
}

DelBB->removeFromParent();
eraseDelBBNode(DelBB);
DelBB->removeFromParent();
Callback(DelBB);
delete DelBB;
}
Expand Down
46 changes: 27 additions & 19 deletions llvm/lib/CodeGen/EarlyIfConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ class SSAIfConv {
bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);

/// convertIf - If-convert the last block passed to canConvertIf(), assuming
/// it is possible. Add any erased blocks to RemovedBlocks.
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
/// it is possible. Add any blocks that are to be erased to RemoveBlocks.
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
bool Predicate = false);
};
} // end anonymous namespace
Expand Down Expand Up @@ -678,9 +678,9 @@ void SSAIfConv::rewritePHIOperands() {
/// convertIf - Execute the if conversion after canConvertIf has determined the
/// feasibility.
///
/// Any basic blocks erased will be added to RemovedBlocks.
/// Any basic blocks that need to be erased will be added to RemoveBlocks.
///
void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
bool Predicate) {
assert(Head && Tail && TBB && FBB && "Call canConvertIf first.");

Expand Down Expand Up @@ -721,15 +721,18 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
DebugLoc HeadDL = Head->getFirstTerminator()->getDebugLoc();
TII->removeBranch(*Head);

// Erase the now empty conditional blocks. It is likely that Head can fall
// Mark the now empty conditional blocks for removal and move them to the end.
// It is likely that Head can fall
// through to Tail, and we can join the two blocks.
if (TBB != Tail) {
RemovedBlocks.push_back(TBB);
TBB->eraseFromParent();
RemoveBlocks.push_back(TBB);
if (TBB != &TBB->getParent()->back())
TBB->moveAfter(&TBB->getParent()->back());
}
if (FBB != Tail) {
RemovedBlocks.push_back(FBB);
FBB->eraseFromParent();
RemoveBlocks.push_back(FBB);
if (FBB != &FBB->getParent()->back())
FBB->moveAfter(&FBB->getParent()->back());
}

assert(Head->succ_empty() && "Additional head successors?");
Expand All @@ -740,8 +743,9 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
Head->splice(Head->end(), Tail,
Tail->begin(), Tail->end());
Head->transferSuccessorsAndUpdatePHIs(Tail);
RemovedBlocks.push_back(Tail);
Tail->eraseFromParent();
RemoveBlocks.push_back(Tail);
if (Tail != &Tail->getParent()->back())
Tail->moveAfter(&Tail->getParent()->back());
} else {
// We need a branch to Tail, let code placement work it out later.
LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n");
Expand Down Expand Up @@ -1062,11 +1066,13 @@ bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
while (IfConv.canConvertIf(MBB) && shouldConvertIf()) {
// If-convert MBB and update analyses.
invalidateTraces();
SmallVector<MachineBasicBlock*, 4> RemovedBlocks;
IfConv.convertIf(RemovedBlocks);
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
IfConv.convertIf(RemoveBlocks);
Changed = true;
updateDomTree(DomTree, IfConv, RemovedBlocks);
updateLoops(Loops, RemovedBlocks);
updateDomTree(DomTree, IfConv, RemoveBlocks);
for (MachineBasicBlock *MBB : RemoveBlocks)
MBB->eraseFromParent();
updateLoops(Loops, RemoveBlocks);
}
return Changed;
}
Expand Down Expand Up @@ -1200,11 +1206,13 @@ bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
bool Changed = false;
while (IfConv.canConvertIf(MBB, /*Predicate*/ true) && shouldConvertIf()) {
// If-convert MBB and update analyses.
SmallVector<MachineBasicBlock *, 4> RemovedBlocks;
IfConv.convertIf(RemovedBlocks, /*Predicate*/ true);
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
IfConv.convertIf(RemoveBlocks, /*Predicate*/ true);
Changed = true;
updateDomTree(DomTree, IfConv, RemovedBlocks);
updateLoops(Loops, RemovedBlocks);
updateDomTree(DomTree, IfConv, RemoveBlocks);
for (MachineBasicBlock *MBB : RemoveBlocks)
MBB->eraseFromParent();
updateLoops(Loops, RemoveBlocks);
}
return Changed;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@ void SSACCmpConv::convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks) {
Head->updateTerminator(CmpBB->getNextNode());

RemovedBlocks.push_back(CmpBB);
CmpBB->eraseFromParent();
LLVM_DEBUG(dbgs() << "Result:\n" << *Head);
++NumConverted;
}
Expand Down Expand Up @@ -918,6 +917,8 @@ bool AArch64ConditionalCompares::tryConvert(MachineBasicBlock *MBB) {
CmpConv.convert(RemovedBlocks);
Changed = true;
updateDomTree(RemovedBlocks);
for (MachineBasicBlock *MBB : RemovedBlocks)
MBB->eraseFromParent();
updateLoops(RemovedBlocks);
}
return Changed;
Expand Down
3 changes: 1 addition & 2 deletions llvm/unittests/IR/DominatorTreeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,10 @@ TEST(DominatorTree, DeletingEdgesIntroducesInfiniteLoop2) {
SwitchC->removeCase(SwitchC->case_begin());
DT->deleteEdge(C, C2);
PDT->deleteEdge(C, C2);
C2->removeFromParent();

EXPECT_EQ(DT->getNode(C2), nullptr);
PDT->eraseNode(C2);
delete C2;
C2->eraseFromParent();

EXPECT_TRUE(DT->verify());
EXPECT_TRUE(PDT->verify());
Expand Down
Loading