57
57
#include " llvm/CodeGen/LiveVariables.h"
58
58
#include " llvm/CodeGen/MachineDominators.h"
59
59
#include " llvm/CodeGen/MachineFunctionPass.h"
60
+ #include " llvm/CodeGen/MachinePostDominators.h"
60
61
#include " llvm/Target/TargetMachine.h"
61
62
62
63
using namespace llvm ;
@@ -76,6 +77,7 @@ class SILowerControlFlow {
76
77
LiveIntervals *LIS = nullptr ;
77
78
LiveVariables *LV = nullptr ;
78
79
MachineDominatorTree *MDT = nullptr ;
80
+ MachinePostDominatorTree *PDT = nullptr ;
79
81
MachineRegisterInfo *MRI = nullptr ;
80
82
SetVector<MachineInstr*> LoweredEndCf;
81
83
DenseSet<Register> LoweredIf;
@@ -138,8 +140,8 @@ class SILowerControlFlow {
138
140
139
141
public:
140
142
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) {}
143
145
bool run (MachineFunction &MF);
144
146
};
145
147
@@ -159,6 +161,7 @@ class SILowerControlFlowLegacy : public MachineFunctionPass {
159
161
AU.addUsedIfAvailable <LiveIntervalsWrapperPass>();
160
162
// Should preserve the same set that TwoAddressInstructions does.
161
163
AU.addPreserved <MachineDominatorTreeWrapperPass>();
164
+ AU.addPreserved <MachinePostDominatorTreeWrapperPass>();
162
165
AU.addPreserved <SlotIndexesWrapperPass>();
163
166
AU.addPreserved <LiveIntervalsWrapperPass>();
164
167
AU.addPreserved <LiveVariablesWrapperPass>();
@@ -506,13 +509,18 @@ MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
506
509
MachineBasicBlock *SplitBB = &MBB;
507
510
if (NeedBlockSplit) {
508
511
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);
516
524
}
517
525
Opcode = OrTermrOpc;
518
526
InsPt = MI;
@@ -727,26 +735,27 @@ bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
727
735
MachineBasicBlock *Succ = *MBB.succ_begin ();
728
736
MachineBasicBlock *FallThrough = nullptr ;
729
737
738
+ using DomTreeT = DomTreeBase<MachineBasicBlock>;
739
+ SmallVector<DomTreeT::UpdateType, 8 > DTUpdates;
740
+
730
741
while (!MBB.predecessors ().empty ()) {
731
742
MachineBasicBlock *P = *MBB.pred_begin ();
732
743
if (P->getFallThrough (false ) == &MBB)
733
744
FallThrough = P;
734
745
P->ReplaceUsesOfBlockWith (&MBB, Succ);
746
+ DTUpdates.push_back ({DomTreeT::Insert, P, Succ});
747
+ DTUpdates.push_back ({DomTreeT::Delete, P, &MBB});
735
748
}
736
749
MBB.removeSuccessor (Succ);
737
750
if (LIS) {
738
751
for (auto &I : MBB.instrs ())
739
752
LIS->RemoveMachineInstrFromMaps (I);
740
753
}
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
+
750
759
MBB.clear ();
751
760
MBB.eraseFromParent ();
752
761
if (FallThrough && !FallThrough->isLayoutSuccessor (Succ)) {
@@ -875,7 +884,11 @@ bool SILowerControlFlowLegacy::runOnMachineFunction(MachineFunction &MF) {
875
884
LiveVariables *LV = LVWrapper ? &LVWrapper->getLV () : nullptr ;
876
885
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
877
886
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);
879
892
}
880
893
881
894
PreservedAnalyses
@@ -885,13 +898,16 @@ SILowerControlFlowPass::run(MachineFunction &MF,
885
898
LiveVariables *LV = MFAM.getCachedResult <LiveVariablesAnalysis>(MF);
886
899
MachineDominatorTree *MDT =
887
900
MFAM.getCachedResult <MachineDominatorTreeAnalysis>(MF);
901
+ MachinePostDominatorTree *PDT =
902
+ MFAM.getCachedResult <MachinePostDominatorTreeAnalysis>(MF);
888
903
889
- bool Changed = SILowerControlFlow (LIS, LV, MDT).run (MF);
904
+ bool Changed = SILowerControlFlow (LIS, LV, MDT, PDT ).run (MF);
890
905
if (!Changed)
891
906
return PreservedAnalyses::all ();
892
907
893
908
auto PA = getMachineFunctionPassPreservedAnalyses ();
894
909
PA.preserve <MachineDominatorTreeAnalysis>();
910
+ PA.preserve <MachinePostDominatorTreeAnalysis>();
895
911
PA.preserve <SlotIndexesAnalysis>();
896
912
PA.preserve <LiveIntervalsAnalysis>();
897
913
PA.preserve <LiveVariablesAnalysis>();
0 commit comments