Skip to content

Commit f92afe7

Browse files
authored
[AMDGPU] Preserve post dominator tree through SILowerControlFlow (#153528)
Change dominator tree updates to also handle post dominator tree.
1 parent f393f2a commit f92afe7

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/CodeGen/LiveVariables.h"
5858
#include "llvm/CodeGen/MachineDominators.h"
5959
#include "llvm/CodeGen/MachineFunctionPass.h"
60+
#include "llvm/CodeGen/MachinePostDominators.h"
6061
#include "llvm/Target/TargetMachine.h"
6162

6263
using namespace llvm;
@@ -76,6 +77,7 @@ class SILowerControlFlow {
7677
LiveIntervals *LIS = nullptr;
7778
LiveVariables *LV = nullptr;
7879
MachineDominatorTree *MDT = nullptr;
80+
MachinePostDominatorTree *PDT = nullptr;
7981
MachineRegisterInfo *MRI = nullptr;
8082
SetVector<MachineInstr*> LoweredEndCf;
8183
DenseSet<Register> LoweredIf;
@@ -138,8 +140,8 @@ class SILowerControlFlow {
138140

139141
public:
140142
SILowerControlFlow(LiveIntervals *LIS, LiveVariables *LV,
141-
MachineDominatorTree *MDT)
142-
: LIS(LIS), LV(LV), MDT(MDT) {}
143+
MachineDominatorTree *MDT, MachinePostDominatorTree *PDT)
144+
: LIS(LIS), LV(LV), MDT(MDT), PDT(PDT) {}
143145
bool run(MachineFunction &MF);
144146
};
145147

@@ -159,6 +161,7 @@ class SILowerControlFlowLegacy : public MachineFunctionPass {
159161
AU.addUsedIfAvailable<LiveIntervalsWrapperPass>();
160162
// Should preserve the same set that TwoAddressInstructions does.
161163
AU.addPreserved<MachineDominatorTreeWrapperPass>();
164+
AU.addPreserved<MachinePostDominatorTreeWrapperPass>();
162165
AU.addPreserved<SlotIndexesWrapperPass>();
163166
AU.addPreserved<LiveIntervalsWrapperPass>();
164167
AU.addPreserved<LiveVariablesWrapperPass>();
@@ -506,13 +509,18 @@ MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
506509
MachineBasicBlock *SplitBB = &MBB;
507510
if (NeedBlockSplit) {
508511
SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
509-
if (MDT && SplitBB != &MBB) {
510-
MachineDomTreeNode *MBBNode = (*MDT)[&MBB];
511-
SmallVector<MachineDomTreeNode *> Children(MBBNode->begin(),
512-
MBBNode->end());
513-
MachineDomTreeNode *SplitBBNode = MDT->addNewBlock(SplitBB, &MBB);
514-
for (MachineDomTreeNode *Child : Children)
515-
MDT->changeImmediateDominator(Child, SplitBBNode);
512+
if (SplitBB != &MBB && (MDT || PDT)) {
513+
using DomTreeT = DomTreeBase<MachineBasicBlock>;
514+
SmallVector<DomTreeT::UpdateType, 16> DTUpdates;
515+
for (MachineBasicBlock *Succ : SplitBB->successors()) {
516+
DTUpdates.push_back({DomTreeT::Insert, SplitBB, Succ});
517+
DTUpdates.push_back({DomTreeT::Delete, &MBB, Succ});
518+
}
519+
DTUpdates.push_back({DomTreeT::Insert, &MBB, SplitBB});
520+
if (MDT)
521+
MDT->applyUpdates(DTUpdates);
522+
if (PDT)
523+
PDT->applyUpdates(DTUpdates);
516524
}
517525
Opcode = OrTermrOpc;
518526
InsPt = MI;
@@ -727,26 +735,27 @@ bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
727735
MachineBasicBlock *Succ = *MBB.succ_begin();
728736
MachineBasicBlock *FallThrough = nullptr;
729737

738+
using DomTreeT = DomTreeBase<MachineBasicBlock>;
739+
SmallVector<DomTreeT::UpdateType, 8> DTUpdates;
740+
730741
while (!MBB.predecessors().empty()) {
731742
MachineBasicBlock *P = *MBB.pred_begin();
732743
if (P->getFallThrough(false) == &MBB)
733744
FallThrough = P;
734745
P->ReplaceUsesOfBlockWith(&MBB, Succ);
746+
DTUpdates.push_back({DomTreeT::Insert, P, Succ});
747+
DTUpdates.push_back({DomTreeT::Delete, P, &MBB});
735748
}
736749
MBB.removeSuccessor(Succ);
737750
if (LIS) {
738751
for (auto &I : MBB.instrs())
739752
LIS->RemoveMachineInstrFromMaps(I);
740753
}
741-
if (MDT) {
742-
// If Succ, the single successor of MBB, is dominated by MBB, MDT needs
743-
// updating by changing Succ's idom to the one of MBB; otherwise, MBB must
744-
// be a leaf node in MDT and could be erased directly.
745-
if (MDT->dominates(&MBB, Succ))
746-
MDT->changeImmediateDominator(MDT->getNode(Succ),
747-
MDT->getNode(&MBB)->getIDom());
748-
MDT->eraseNode(&MBB);
749-
}
754+
if (MDT)
755+
MDT->applyUpdates(DTUpdates);
756+
if (PDT)
757+
PDT->applyUpdates(DTUpdates);
758+
750759
MBB.clear();
751760
MBB.eraseFromParent();
752761
if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
@@ -875,7 +884,11 @@ bool SILowerControlFlowLegacy::runOnMachineFunction(MachineFunction &MF) {
875884
LiveVariables *LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
876885
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
877886
MachineDominatorTree *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
878-
return SILowerControlFlow(LIS, LV, MDT).run(MF);
887+
auto *PDTWrapper =
888+
getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>();
889+
MachinePostDominatorTree *PDT =
890+
PDTWrapper ? &PDTWrapper->getPostDomTree() : nullptr;
891+
return SILowerControlFlow(LIS, LV, MDT, PDT).run(MF);
879892
}
880893

881894
PreservedAnalyses
@@ -885,13 +898,16 @@ SILowerControlFlowPass::run(MachineFunction &MF,
885898
LiveVariables *LV = MFAM.getCachedResult<LiveVariablesAnalysis>(MF);
886899
MachineDominatorTree *MDT =
887900
MFAM.getCachedResult<MachineDominatorTreeAnalysis>(MF);
901+
MachinePostDominatorTree *PDT =
902+
MFAM.getCachedResult<MachinePostDominatorTreeAnalysis>(MF);
888903

889-
bool Changed = SILowerControlFlow(LIS, LV, MDT).run(MF);
904+
bool Changed = SILowerControlFlow(LIS, LV, MDT, PDT).run(MF);
890905
if (!Changed)
891906
return PreservedAnalyses::all();
892907

893908
auto PA = getMachineFunctionPassPreservedAnalyses();
894909
PA.preserve<MachineDominatorTreeAnalysis>();
910+
PA.preserve<MachinePostDominatorTreeAnalysis>();
895911
PA.preserve<SlotIndexesAnalysis>();
896912
PA.preserve<LiveIntervalsAnalysis>();
897913
PA.preserve<LiveVariablesAnalysis>();

0 commit comments

Comments
 (0)