Skip to content

Commit 72d25b4

Browse files
committed
[CodeGen][NewPM] Split MachineDominatorTree into a concrete analysis result
Prepare for new pass manager version of `MachineDominatorTreeAnalysis`. We may need a machine dominator tree version of `DomTreeUpdater` to handle `SplitCriticalEdge` in some CodeGen passes.
1 parent 1ea5688 commit 72d25b4

File tree

91 files changed

+404
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+404
-326
lines changed

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,37 @@ extern template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTree
4444
using MachineDomTree = DomTreeBase<MachineBasicBlock>;
4545
using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
4646

47+
namespace DomTreeBuilder {
48+
using MBBDomTree = MachineDomTree;
49+
using MBBUpdates = ArrayRef<llvm::cfg::Update<MachineBasicBlock *>>;
50+
using MBBDomTreeGraphDiff = GraphDiff<MachineBasicBlock *, false>;
51+
52+
extern template void Calculate<MBBDomTree>(MBBDomTree &DT);
53+
extern template void CalculateWithUpdates<MBBDomTree>(MBBDomTree &DT,
54+
MBBUpdates U);
55+
56+
extern template void InsertEdge<MBBDomTree>(MBBDomTree &DT,
57+
MachineBasicBlock *From,
58+
MachineBasicBlock *To);
59+
60+
extern template void DeleteEdge<MBBDomTree>(MBBDomTree &DT,
61+
MachineBasicBlock *From,
62+
MachineBasicBlock *To);
63+
64+
extern template void ApplyUpdates<MBBDomTree>(MBBDomTree &DT,
65+
MBBDomTreeGraphDiff &,
66+
MBBDomTreeGraphDiff *);
67+
68+
extern template bool Verify<MBBDomTree>(const MBBDomTree &DT,
69+
MBBDomTree::VerificationLevel VL);
70+
} // namespace DomTreeBuilder
71+
4772
//===-------------------------------------
4873
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
4974
/// compute a normal dominator tree.
5075
///
51-
class MachineDominatorTree : public MachineFunctionPass {
76+
class MachineDominatorTree : public MachineDomTree {
77+
friend class MachineDominatorTreeWrapperPass;
5278
/// Helper structure used to hold all the basic blocks
5379
/// involved in the split of a critical edge.
5480
struct CriticalEdge {
@@ -70,70 +96,61 @@ class MachineDominatorTree : public MachineFunctionPass {
7096
/// such as BB == elt.NewBB.
7197
mutable SmallSet<MachineBasicBlock *, 32> NewBBs;
7298

73-
/// The DominatorTreeBase that is used to compute a normal dominator tree.
74-
std::unique_ptr<MachineDomTree> DT;
75-
7699
/// Apply all the recorded critical edges to the DT.
77100
/// This updates the underlying DT information in a way that uses
78101
/// the fast query path of DT as much as possible.
102+
/// FIXME: This method should not be a const member!
79103
///
80104
/// \post CriticalEdgesToSplit.empty().
81105
void applySplitCriticalEdges() const;
82106

83107
public:
84-
static char ID; // Pass ID, replacement for typeid
108+
using Base = MachineDomTree;
85109

86-
MachineDominatorTree();
87-
explicit MachineDominatorTree(MachineFunction &MF) : MachineFunctionPass(ID) {
88-
calculate(MF);
89-
}
110+
MachineDominatorTree() = default;
111+
explicit MachineDominatorTree(MachineFunction &MF) { calculate(MF); }
90112

91113
MachineDomTree &getBase() {
92-
if (!DT)
93-
DT.reset(new MachineDomTree());
94114
applySplitCriticalEdges();
95-
return *DT;
115+
return *this;
96116
}
97117

98-
void getAnalysisUsage(AnalysisUsage &AU) const override;
99-
100118
MachineBasicBlock *getRoot() const {
101119
applySplitCriticalEdges();
102-
return DT->getRoot();
120+
return Base::getRoot();
103121
}
104122

105123
MachineDomTreeNode *getRootNode() const {
106124
applySplitCriticalEdges();
107-
return DT->getRootNode();
125+
return const_cast<MachineDomTreeNode *>(Base::getRootNode());
108126
}
109127

110-
bool runOnMachineFunction(MachineFunction &F) override;
111-
112128
void calculate(MachineFunction &F);
113129

114130
bool dominates(const MachineDomTreeNode *A,
115131
const MachineDomTreeNode *B) const {
116132
applySplitCriticalEdges();
117-
return DT->dominates(A, B);
133+
return Base::dominates(A, B);
118134
}
119135

120136
void getDescendants(MachineBasicBlock *A,
121137
SmallVectorImpl<MachineBasicBlock *> &Result) {
122138
applySplitCriticalEdges();
123-
DT->getDescendants(A, Result);
139+
Base::getDescendants(A, Result);
124140
}
125141

126142
bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const {
127143
applySplitCriticalEdges();
128-
return DT->dominates(A, B);
144+
return Base::dominates(A, B);
129145
}
130146

131147
// dominates - Return true if A dominates B. This performs the
132148
// special checks necessary if A and B are in the same basic block.
133149
bool dominates(const MachineInstr *A, const MachineInstr *B) const {
134150
applySplitCriticalEdges();
135151
const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
136-
if (BBA != BBB) return DT->dominates(BBA, BBB);
152+
if (BBA != BBB)
153+
return Base::dominates(BBA, BBB);
137154

138155
// Loop through the basic block until we find A or B.
139156
MachineBasicBlock::const_iterator I = BBA->begin();
@@ -146,34 +163,34 @@ class MachineDominatorTree : public MachineFunctionPass {
146163
bool properlyDominates(const MachineDomTreeNode *A,
147164
const MachineDomTreeNode *B) const {
148165
applySplitCriticalEdges();
149-
return DT->properlyDominates(A, B);
166+
return Base::properlyDominates(A, B);
150167
}
151168

152169
bool properlyDominates(const MachineBasicBlock *A,
153170
const MachineBasicBlock *B) const {
154171
applySplitCriticalEdges();
155-
return DT->properlyDominates(A, B);
172+
return Base::properlyDominates(A, B);
156173
}
157174

158175
/// findNearestCommonDominator - Find nearest common dominator basic block
159176
/// for basic block A and B. If there is no such block then return NULL.
160177
MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
161178
MachineBasicBlock *B) {
162179
applySplitCriticalEdges();
163-
return DT->findNearestCommonDominator(A, B);
180+
return Base::findNearestCommonDominator(A, B);
164181
}
165182

166183
MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
167184
applySplitCriticalEdges();
168-
return DT->getNode(BB);
185+
return Base::getNode(BB);
169186
}
170187

171188
/// getNode - return the (Post)DominatorTree node for the specified basic
172189
/// block. This is the same as using operator[] on this class.
173190
///
174191
MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
175192
applySplitCriticalEdges();
176-
return DT->getNode(BB);
193+
return Base::getNode(BB);
177194
}
178195

179196
/// addNewBlock - Add a new node to the dominator tree information. This
@@ -182,7 +199,7 @@ class MachineDominatorTree : public MachineFunctionPass {
182199
MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
183200
MachineBasicBlock *DomBB) {
184201
applySplitCriticalEdges();
185-
return DT->addNewBlock(BB, DomBB);
202+
return Base::addNewBlock(BB, DomBB);
186203
}
187204

188205
/// changeImmediateDominator - This method is used to update the dominator
@@ -191,43 +208,37 @@ class MachineDominatorTree : public MachineFunctionPass {
191208
void changeImmediateDominator(MachineBasicBlock *N,
192209
MachineBasicBlock *NewIDom) {
193210
applySplitCriticalEdges();
194-
DT->changeImmediateDominator(N, NewIDom);
211+
Base::changeImmediateDominator(N, NewIDom);
195212
}
196213

197214
void changeImmediateDominator(MachineDomTreeNode *N,
198215
MachineDomTreeNode *NewIDom) {
199216
applySplitCriticalEdges();
200-
DT->changeImmediateDominator(N, NewIDom);
217+
Base::changeImmediateDominator(N, NewIDom);
201218
}
202219

203220
/// eraseNode - Removes a node from the dominator tree. Block must not
204221
/// dominate any other blocks. Removes node from its immediate dominator's
205222
/// children list. Deletes dominator node associated with basic block BB.
206223
void eraseNode(MachineBasicBlock *BB) {
207224
applySplitCriticalEdges();
208-
DT->eraseNode(BB);
225+
Base::eraseNode(BB);
209226
}
210227

211228
/// splitBlock - BB is split and now it has one successor. Update dominator
212229
/// tree to reflect this change.
213230
void splitBlock(MachineBasicBlock* NewBB) {
214231
applySplitCriticalEdges();
215-
DT->splitBlock(NewBB);
232+
Base::splitBlock(NewBB);
216233
}
217234

218235
/// isReachableFromEntry - Return true if A is dominated by the entry
219236
/// block of the function containing it.
220237
bool isReachableFromEntry(const MachineBasicBlock *A) {
221238
applySplitCriticalEdges();
222-
return DT->isReachableFromEntry(A);
239+
return Base::isReachableFromEntry(A);
223240
}
224241

225-
void releaseMemory() override;
226-
227-
void verifyAnalysis() const override;
228-
229-
void print(raw_ostream &OS, const Module*) const override;
230-
231242
/// Record that the critical edge (FromBB, ToBB) has been
232243
/// split with NewBB.
233244
/// This is best to use this method instead of directly update the
@@ -251,6 +262,32 @@ class MachineDominatorTree : public MachineFunctionPass {
251262
}
252263
};
253264

265+
/// \brief Analysis pass which computes a \c MachineDominatorTree.
266+
class MachineDominatorTreeWrapperPass : public MachineFunctionPass {
267+
MachineDominatorTree DT;
268+
269+
public:
270+
static char ID;
271+
272+
MachineDominatorTreeWrapperPass();
273+
274+
MachineDominatorTree &getDomTree() { return DT; }
275+
const MachineDominatorTree &getDomTree() const { return DT; }
276+
277+
bool runOnMachineFunction(MachineFunction &MF) override;
278+
279+
void verifyAnalysis() const override;
280+
281+
void getAnalysisUsage(AnalysisUsage &AU) const override {
282+
AU.setPreservesAll();
283+
MachineFunctionPass::getAnalysisUsage(AU);
284+
}
285+
286+
void releaseMemory() override;
287+
288+
void print(raw_ostream &OS, const Module *M = nullptr) const override;
289+
};
290+
254291
//===-------------------------------------
255292
/// DominatorTree GraphTraits specialization so the DominatorTree can be
256293
/// iterable by generic graph iterators.

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void initializeMachineCopyPropagationPass(PassRegistry&);
189189
void initializeMachineCycleInfoPrinterPassPass(PassRegistry &);
190190
void initializeMachineCycleInfoWrapperPassPass(PassRegistry &);
191191
void initializeMachineDominanceFrontierPass(PassRegistry&);
192-
void initializeMachineDominatorTreePass(PassRegistry&);
192+
void initializeMachineDominatorTreeWrapperPassPass(PassRegistry &);
193193
void initializeMachineFunctionPrinterPassPass(PassRegistry&);
194194
void initializeMachineFunctionSplitterPass(PassRegistry &);
195195
void initializeMachineLateInstrsCleanupPass(PassRegistry&);

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,8 @@ void AsmPrinter::emitFunctionBody() {
17281728

17291729
if (isVerbose()) {
17301730
// Get MachineDominatorTree or compute it on the fly if it's unavailable
1731-
MDT = getAnalysisIfAvailable<MachineDominatorTree>();
1731+
auto MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
1732+
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
17321733
if (!MDT) {
17331734
OwnedMDT = std::make_unique<MachineDominatorTree>();
17341735
OwnedMDT->getBase().recalculate(*MF);

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
8080
initializeMachineCopyPropagationPass(Registry);
8181
initializeMachineCycleInfoPrinterPassPass(Registry);
8282
initializeMachineCycleInfoWrapperPassPass(Registry);
83-
initializeMachineDominatorTreePass(Registry);
83+
initializeMachineDominatorTreeWrapperPassPass(Registry);
8484
initializeMachineFunctionPrinterPassPass(Registry);
8585
initializeMachineLateInstrsCleanupPass(Registry);
8686
initializeMachineLICMPass(Registry);

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,15 @@ char &llvm::EarlyIfConverterID = EarlyIfConverter::ID;
790790
INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE,
791791
"Early If Converter", false, false)
792792
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
793-
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
793+
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
794794
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
795795
INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE,
796796
"Early If Converter", false, false)
797797

798798
void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
799799
AU.addRequired<MachineBranchProbabilityInfo>();
800-
AU.addRequired<MachineDominatorTree>();
801-
AU.addPreserved<MachineDominatorTree>();
800+
AU.addRequired<MachineDominatorTreeWrapperPass>();
801+
AU.addPreserved<MachineDominatorTreeWrapperPass>();
802802
AU.addRequired<MachineLoopInfo>();
803803
AU.addPreserved<MachineLoopInfo>();
804804
AU.addRequired<MachineTraceMetrics>();
@@ -1089,7 +1089,7 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10891089
TRI = STI.getRegisterInfo();
10901090
SchedModel = STI.getSchedModel();
10911091
MRI = &MF.getRegInfo();
1092-
DomTree = &getAnalysis<MachineDominatorTree>();
1092+
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
10931093
Loops = &getAnalysis<MachineLoopInfo>();
10941094
Traces = &getAnalysis<MachineTraceMetrics>();
10951095
MinInstr = nullptr;
@@ -1144,15 +1144,15 @@ char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID;
11441144

11451145
INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator",
11461146
false, false)
1147-
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1147+
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
11481148
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
11491149
INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false,
11501150
false)
11511151

11521152
void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
11531153
AU.addRequired<MachineBranchProbabilityInfo>();
1154-
AU.addRequired<MachineDominatorTree>();
1155-
AU.addPreserved<MachineDominatorTree>();
1154+
AU.addRequired<MachineDominatorTreeWrapperPass>();
1155+
AU.addPreserved<MachineDominatorTreeWrapperPass>();
11561156
AU.addRequired<MachineLoopInfo>();
11571157
AU.addPreserved<MachineLoopInfo>();
11581158
MachineFunctionPass::getAnalysisUsage(AU);
@@ -1223,7 +1223,7 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
12231223
TRI = STI.getRegisterInfo();
12241224
MRI = &MF.getRegInfo();
12251225
SchedModel.init(&STI);
1226-
DomTree = &getAnalysis<MachineDominatorTree>();
1226+
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
12271227
Loops = &getAnalysis<MachineLoopInfo>();
12281228
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
12291229

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate {
135135
VirtRegMap &vrm)
136136
: MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
137137
LSS(pass.getAnalysis<LiveStacks>()),
138-
MDT(pass.getAnalysis<MachineDominatorTree>()), VRM(vrm),
139-
MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
138+
MDT(pass.getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree()),
139+
VRM(vrm), MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
140140
TRI(*mf.getSubtarget().getRegisterInfo()),
141141
MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
142142
IPA(LIS, mf.getNumBlockIDs()) {}
@@ -192,8 +192,8 @@ class InlineSpiller : public Spiller {
192192
VirtRegAuxInfo &VRAI)
193193
: MF(MF), LIS(Pass.getAnalysis<LiveIntervals>()),
194194
LSS(Pass.getAnalysis<LiveStacks>()),
195-
MDT(Pass.getAnalysis<MachineDominatorTree>()), VRM(VRM),
196-
MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
195+
MDT(Pass.getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree()),
196+
VRM(VRM), MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
197197
TRI(*MF.getSubtarget().getRegisterInfo()),
198198
MBFI(Pass.getAnalysis<MachineBlockFrequencyInfo>()),
199199
HSpiller(Pass, MF, VRM), VRAI(VRAI) {}

llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
6464

6565
auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
6666
auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
67-
auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
67+
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
68+
auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
6869
LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
6970
LLVM_DEBUG(if (MLI) dbgs() << "LoopInfo is available\n");
7071

llvm/lib/CodeGen/LiveDebugVariables.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ char LiveDebugVariables::ID = 0;
7878

7979
INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
8080
"Debug Variable Analysis", false, false)
81-
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
81+
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
8282
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
8383
INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
8484
"Debug Variable Analysis", false, false)
8585

8686
void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
87-
AU.addRequired<MachineDominatorTree>();
87+
AU.addRequired<MachineDominatorTreeWrapperPass>();
8888
AU.addRequiredTransitive<LiveIntervals>();
8989
AU.setPreservesAll();
9090
MachineFunctionPass::getAnalysisUsage(AU);

0 commit comments

Comments
 (0)