Skip to content

Commit 8634aa6

Browse files
committed
Use std::optional
1 parent 0f06f80 commit 8634aa6

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/Support/GenericDomTreeConstruction.h"
2525
#include <cassert>
2626
#include <memory>
27+
#include <optional>
2728

2829
namespace llvm {
2930
class AnalysisUsage;
@@ -72,7 +73,6 @@ extern template bool Verify<MBBDomTree>(const MBBDomTree &DT,
7273
/// compute a normal dominator tree.
7374
///
7475
class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
75-
friend class MachineDominatorTreeWrapperPass;
7676
/// Helper structure used to hold all the basic blocks
7777
/// involved in the split of a critical edge.
7878
struct CriticalEdge {
@@ -265,16 +265,17 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
265265

266266
/// \brief Analysis pass which computes a \c MachineDominatorTree.
267267
class MachineDominatorTreeWrapperPass : public MachineFunctionPass {
268-
MachineDominatorTree DT;
269-
bool IsDomTreeEmpty = true;
268+
// MachineFunctionPass may verify the analysis result without running pass,
269+
// e.g. when `F.hasAvailableExternallyLinkage` is true.
270+
std::optional<MachineDominatorTree> DT;
270271

271272
public:
272273
static char ID;
273274

274275
MachineDominatorTreeWrapperPass();
275276

276-
MachineDominatorTree &getDomTree() { return DT; }
277-
const MachineDominatorTree &getDomTree() const { return DT; }
277+
MachineDominatorTree &getDomTree() { return *DT; }
278+
const MachineDominatorTree &getDomTree() const { return *DT; }
278279

279280
bool runOnMachineFunction(MachineFunction &MF) override;
280281

llvm/lib/CodeGen/MachineDominators.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,31 @@ MachineDominatorTreeWrapperPass::MachineDominatorTreeWrapperPass()
6767
*PassRegistry::getPassRegistry());
6868
}
6969

70-
char &llvm::MachineDominatorsID = MachineDominatorTreeWrapperPass::ID;
71-
72-
bool MachineDominatorTreeWrapperPass::runOnMachineFunction(MachineFunction &F) {
73-
IsDomTreeEmpty = false;
74-
DT.calculate(F);
75-
return false;
76-
}
77-
7870
void MachineDominatorTree::calculate(MachineFunction &F) {
7971
CriticalEdgesToSplit.clear();
8072
NewBBs.clear();
8173
recalculate(F);
8274
}
8375

84-
void MachineDominatorTreeWrapperPass::releaseMemory() {
85-
DT.CriticalEdgesToSplit.clear();
86-
DT.reset();
87-
IsDomTreeEmpty = true;
76+
char &llvm::MachineDominatorsID = MachineDominatorTreeWrapperPass::ID;
77+
78+
bool MachineDominatorTreeWrapperPass::runOnMachineFunction(MachineFunction &F) {
79+
DT = MachineDominatorTree(F);
80+
return false;
8881
}
8982

83+
void MachineDominatorTreeWrapperPass::releaseMemory() { DT.reset(); }
84+
9085
void MachineDominatorTreeWrapperPass::verifyAnalysis() const {
91-
if (VerifyMachineDomInfo && !IsDomTreeEmpty)
92-
if (!DT.verify(MachineDominatorTree::VerificationLevel::Basic))
86+
if (VerifyMachineDomInfo && DT)
87+
if (!DT->verify(MachineDominatorTree::VerificationLevel::Basic))
9388
report_fatal_error("MachineDominatorTree verification failed!");
9489
}
9590

9691
void MachineDominatorTreeWrapperPass::print(raw_ostream &OS,
9792
const Module *) const {
98-
DT.print(OS);
93+
if (DT)
94+
DT->print(OS);
9995
}
10096

10197
void MachineDominatorTree::applySplitCriticalEdges() const {
@@ -148,7 +144,6 @@ void MachineDominatorTree::applySplitCriticalEdges() const {
148144

149145
// Now, update DT with the collected dominance properties info.
150146
Idx = 0;
151-
dbgs() << "critical Edge to split: " << CriticalEdgesToSplit.size();
152147
for (CriticalEdge &Edge : CriticalEdgesToSplit) {
153148
// We know FromBB dominates NewBB.
154149
MachineDomTreeNode *NewDTNode =

0 commit comments

Comments
 (0)