Skip to content

Commit 6d103d7

Browse files
authored
[Support] Erase blocks after DomTree::eraseNode (llvm#101195)
Change eraseNode to require that the basic block is still contained inside the function. This is a preparation for using numbers of basic blocks inside the dominator tree, which are invalid for blocks that are not inside a function.
1 parent a4c6ebe commit 6d103d7

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

llvm/include/llvm/Analysis/GenericDomTreeUpdater.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class GenericDomTreeUpdater {
232232
/// insertEdge/deleteEdge or is unnecessary in the batch update.
233233
bool isUpdateValid(typename DomTreeT::UpdateType Update) const;
234234

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

llvm/lib/Analysis/DomTreeUpdater.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ bool DomTreeUpdater::forceFlushDeletedBB() {
4242
// delete only has an UnreachableInst inside.
4343
assert(BB->size() == 1 && isa<UnreachableInst>(BB->getTerminator()) &&
4444
"DelBB has been modified while awaiting deletion.");
45-
BB->removeFromParent();
4645
eraseDelBBNode(BB);
47-
delete BB;
46+
BB->eraseFromParent();
4847
}
4948
DeletedBBs.clear();
5049
Callbacks.clear();
@@ -63,9 +62,8 @@ void DomTreeUpdater::deleteBB(BasicBlock *DelBB) {
6362
return;
6463
}
6564

66-
DelBB->removeFromParent();
6765
eraseDelBBNode(DelBB);
68-
delete DelBB;
66+
DelBB->eraseFromParent();
6967
}
7068

7169
void DomTreeUpdater::callbackDeleteBB(
@@ -77,8 +75,8 @@ void DomTreeUpdater::callbackDeleteBB(
7775
return;
7876
}
7977

80-
DelBB->removeFromParent();
8178
eraseDelBBNode(DelBB);
79+
DelBB->removeFromParent();
8280
Callback(DelBB);
8381
delete DelBB;
8482
}

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class SSAIfConv {
181181
bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);
182182

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

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

724-
// Erase the now empty conditional blocks. It is likely that Head can fall
724+
// Mark the now empty conditional blocks for removal and move them to the end.
725+
// It is likely that Head can fall
725726
// through to Tail, and we can join the two blocks.
726727
if (TBB != Tail) {
727-
RemovedBlocks.push_back(TBB);
728-
TBB->eraseFromParent();
728+
RemoveBlocks.push_back(TBB);
729+
if (TBB != &TBB->getParent()->back())
730+
TBB->moveAfter(&TBB->getParent()->back());
729731
}
730732
if (FBB != Tail) {
731-
RemovedBlocks.push_back(FBB);
732-
FBB->eraseFromParent();
733+
RemoveBlocks.push_back(FBB);
734+
if (FBB != &FBB->getParent()->back())
735+
FBB->moveAfter(&FBB->getParent()->back());
733736
}
734737

735738
assert(Head->succ_empty() && "Additional head successors?");
@@ -740,8 +743,9 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
740743
Head->splice(Head->end(), Tail,
741744
Tail->begin(), Tail->end());
742745
Head->transferSuccessorsAndUpdatePHIs(Tail);
743-
RemovedBlocks.push_back(Tail);
744-
Tail->eraseFromParent();
746+
RemoveBlocks.push_back(Tail);
747+
if (Tail != &Tail->getParent()->back())
748+
Tail->moveAfter(&Tail->getParent()->back());
745749
} else {
746750
// We need a branch to Tail, let code placement work it out later.
747751
LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n");
@@ -1062,11 +1066,13 @@ bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
10621066
while (IfConv.canConvertIf(MBB) && shouldConvertIf()) {
10631067
// If-convert MBB and update analyses.
10641068
invalidateTraces();
1065-
SmallVector<MachineBasicBlock*, 4> RemovedBlocks;
1066-
IfConv.convertIf(RemovedBlocks);
1069+
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
1070+
IfConv.convertIf(RemoveBlocks);
10671071
Changed = true;
1068-
updateDomTree(DomTree, IfConv, RemovedBlocks);
1069-
updateLoops(Loops, RemovedBlocks);
1072+
updateDomTree(DomTree, IfConv, RemoveBlocks);
1073+
for (MachineBasicBlock *MBB : RemoveBlocks)
1074+
MBB->eraseFromParent();
1075+
updateLoops(Loops, RemoveBlocks);
10701076
}
10711077
return Changed;
10721078
}
@@ -1200,11 +1206,13 @@ bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
12001206
bool Changed = false;
12011207
while (IfConv.canConvertIf(MBB, /*Predicate*/ true) && shouldConvertIf()) {
12021208
// If-convert MBB and update analyses.
1203-
SmallVector<MachineBasicBlock *, 4> RemovedBlocks;
1204-
IfConv.convertIf(RemovedBlocks, /*Predicate*/ true);
1209+
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
1210+
IfConv.convertIf(RemoveBlocks, /*Predicate*/ true);
12051211
Changed = true;
1206-
updateDomTree(DomTree, IfConv, RemovedBlocks);
1207-
updateLoops(Loops, RemovedBlocks);
1212+
updateDomTree(DomTree, IfConv, RemoveBlocks);
1213+
for (MachineBasicBlock *MBB : RemoveBlocks)
1214+
MBB->eraseFromParent();
1215+
updateLoops(Loops, RemoveBlocks);
12081216
}
12091217
return Changed;
12101218
}

llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ void SSACCmpConv::convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks) {
711711
Head->updateTerminator(CmpBB->getNextNode());
712712

713713
RemovedBlocks.push_back(CmpBB);
714-
CmpBB->eraseFromParent();
715714
LLVM_DEBUG(dbgs() << "Result:\n" << *Head);
716715
++NumConverted;
717716
}
@@ -918,6 +917,8 @@ bool AArch64ConditionalCompares::tryConvert(MachineBasicBlock *MBB) {
918917
CmpConv.convert(RemovedBlocks);
919918
Changed = true;
920919
updateDomTree(RemovedBlocks);
920+
for (MachineBasicBlock *MBB : RemovedBlocks)
921+
MBB->eraseFromParent();
921922
updateLoops(RemovedBlocks);
922923
}
923924
return Changed;

llvm/unittests/IR/DominatorTreeTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,10 @@ TEST(DominatorTree, DeletingEdgesIntroducesInfiniteLoop2) {
607607
SwitchC->removeCase(SwitchC->case_begin());
608608
DT->deleteEdge(C, C2);
609609
PDT->deleteEdge(C, C2);
610-
C2->removeFromParent();
611610

612611
EXPECT_EQ(DT->getNode(C2), nullptr);
613612
PDT->eraseNode(C2);
614-
delete C2;
613+
C2->eraseFromParent();
615614

616615
EXPECT_TRUE(DT->verify());
617616
EXPECT_TRUE(PDT->verify());

0 commit comments

Comments
 (0)