Skip to content

Commit d871b2e

Browse files
authored
[CodeGen] Use optimized domtree for MachineFunction (#102107)
The dominator tree gained an optimization to use block numbers instead of a DenseMap to store blocks. Given that machine basic blocks already have numbers, expose these via appropriate GraphTraits. For debugging, block number epochs are added to MachineFunction -- this greatly helps in finding uses of block numbers after RenumberBlocks(). In a few cases where dominator trees are preserved across renumberings, the dominator tree is updated to use the new numbers.
1 parent 80721e0 commit d871b2e

11 files changed

+98
-14
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,11 @@ template <> struct GraphTraits<MachineBasicBlock *> {
12971297
static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
12981298
static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
12991299
static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1300+
1301+
static unsigned getNumber(MachineBasicBlock *BB) {
1302+
assert(BB->getNumber() >= 0 && "negative block number");
1303+
return BB->getNumber();
1304+
}
13001305
};
13011306

13021307
template <> struct GraphTraits<const MachineBasicBlock *> {
@@ -1306,6 +1311,11 @@ template <> struct GraphTraits<const MachineBasicBlock *> {
13061311
static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
13071312
static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
13081313
static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1314+
1315+
static unsigned getNumber(const MachineBasicBlock *BB) {
1316+
assert(BB->getNumber() >= 0 && "negative block number");
1317+
return BB->getNumber();
1318+
}
13091319
};
13101320

13111321
// Provide specializations of GraphTraits to be able to treat a
@@ -1324,6 +1334,11 @@ template <> struct GraphTraits<Inverse<MachineBasicBlock*>> {
13241334

13251335
static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
13261336
static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1337+
1338+
static unsigned getNumber(MachineBasicBlock *BB) {
1339+
assert(BB->getNumber() >= 0 && "negative block number");
1340+
return BB->getNumber();
1341+
}
13271342
};
13281343

13291344
template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> {
@@ -1336,6 +1351,11 @@ template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> {
13361351

13371352
static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
13381353
static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1354+
1355+
static unsigned getNumber(const MachineBasicBlock *BB) {
1356+
assert(BB->getNumber() >= 0 && "negative block number");
1357+
return BB->getNumber();
1358+
}
13391359
};
13401360

13411361
// These accessors are handy for sharing templated code between IR and MIR.

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
293293
// numbered and this vector keeps track of the mapping from ID's to MBB's.
294294
std::vector<MachineBasicBlock*> MBBNumbering;
295295

296+
// MBBNumbering epoch, incremented after renumbering to detect use of old
297+
// block numbers.
298+
unsigned MBBNumberingEpoch = 0;
299+
296300
// Pool-allocate MachineFunction-lifetime and IR objects.
297301
BumpPtrAllocator Allocator;
298302

@@ -856,6 +860,11 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
856860
/// getNumBlockIDs - Return the number of MBB ID's allocated.
857861
unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
858862

863+
/// Return the numbering "epoch" of block numbers, incremented after each
864+
/// numbering. Intended for asserting that no renumbering was performed when
865+
/// used by, e.g., preserved analyses.
866+
unsigned getBlockNumberEpoch() const { return MBBNumberingEpoch; }
867+
859868
/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
860869
/// recomputes them. This guarantees that the MBB numbers are sequential,
861870
/// dense, and match the ordering of the blocks within the function. If a
@@ -1404,6 +1413,13 @@ template <> struct GraphTraits<MachineFunction*> :
14041413
}
14051414

14061415
static unsigned size (MachineFunction *F) { return F->size(); }
1416+
1417+
static unsigned getMaxNumber(MachineFunction *F) {
1418+
return F->getNumBlockIDs();
1419+
}
1420+
static unsigned getNumberEpoch(MachineFunction *F) {
1421+
return F->getBlockNumberEpoch();
1422+
}
14071423
};
14081424
template <> struct GraphTraits<const MachineFunction*> :
14091425
public GraphTraits<const MachineBasicBlock*> {
@@ -1423,6 +1439,13 @@ template <> struct GraphTraits<const MachineFunction*> :
14231439
static unsigned size (const MachineFunction *F) {
14241440
return F->size();
14251441
}
1442+
1443+
static unsigned getMaxNumber(const MachineFunction *F) {
1444+
return F->getNumBlockIDs();
1445+
}
1446+
static unsigned getNumberEpoch(const MachineFunction *F) {
1447+
return F->getBlockNumberEpoch();
1448+
}
14261449
};
14271450

14281451
// Provide specializations of GraphTraits to be able to treat a function as a
@@ -1435,12 +1458,26 @@ template <> struct GraphTraits<Inverse<MachineFunction*>> :
14351458
static NodeRef getEntryNode(Inverse<MachineFunction *> G) {
14361459
return &G.Graph->front();
14371460
}
1461+
1462+
static unsigned getMaxNumber(MachineFunction *F) {
1463+
return F->getNumBlockIDs();
1464+
}
1465+
static unsigned getNumberEpoch(MachineFunction *F) {
1466+
return F->getBlockNumberEpoch();
1467+
}
14381468
};
14391469
template <> struct GraphTraits<Inverse<const MachineFunction*>> :
14401470
public GraphTraits<Inverse<const MachineBasicBlock*>> {
14411471
static NodeRef getEntryNode(Inverse<const MachineFunction *> G) {
14421472
return &G.Graph->front();
14431473
}
1474+
1475+
static unsigned getMaxNumber(const MachineFunction *F) {
1476+
return F->getNumBlockIDs();
1477+
}
1478+
static unsigned getNumberEpoch(const MachineFunction *F) {
1479+
return F->getBlockNumberEpoch();
1480+
}
14441481
};
14451482

14461483
void verifyMachineFunction(const std::string &Banner,

llvm/lib/CodeGen/BasicBlockSections.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@
7272
#include "llvm/ADT/StringRef.h"
7373
#include "llvm/CodeGen/BasicBlockSectionUtils.h"
7474
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
75+
#include "llvm/CodeGen/MachineDominators.h"
7576
#include "llvm/CodeGen/MachineFunction.h"
7677
#include "llvm/CodeGen/MachineFunctionPass.h"
78+
#include "llvm/CodeGen/MachinePostDominators.h"
7779
#include "llvm/CodeGen/Passes.h"
7880
#include "llvm/CodeGen/TargetInstrInfo.h"
7981
#include "llvm/InitializePasses.h"
@@ -393,12 +395,21 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
393395
auto R1 = handleBBSections(MF);
394396
// Handle basic block address map after basic block sections are finalized.
395397
auto R2 = handleBBAddrMap(MF);
398+
399+
// We renumber blocks, so update the dominator tree we want to preserve.
400+
if (auto *WP = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>())
401+
WP->getDomTree().updateBlockNumbers();
402+
if (auto *WP = getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>())
403+
WP->getPostDomTree().updateBlockNumbers();
404+
396405
return R1 || R2;
397406
}
398407

399408
void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const {
400409
AU.setPreservesAll();
401410
AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
411+
AU.addUsedIfAvailable<MachineDominatorTreeWrapperPass>();
412+
AU.addUsedIfAvailable<MachinePostDominatorTreeWrapperPass>();
402413
MachineFunctionPass::getAnalysisUsage(AU);
403414
}
404415

llvm/lib/CodeGen/MIRSampleProfile.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,18 @@ bool MIRProfileLoaderPass::runOnMachineFunction(MachineFunction &MF) {
366366
LLVM_DEBUG(dbgs() << "MIRProfileLoader pass working on Func: "
367367
<< MF.getFunction().getName() << "\n");
368368
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
369+
auto *MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
370+
auto *MPDT =
371+
&getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
372+
373+
MF.RenumberBlocks();
374+
MDT->updateBlockNumbers();
375+
MPDT->updateBlockNumbers();
376+
369377
MIRSampleLoader->setInitVals(
370-
&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(),
371-
&getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree(),
372-
&getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI,
378+
MDT, MPDT, &getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI,
373379
&getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE());
374380

375-
MF.RenumberBlocks();
376381
if (ViewBFIBefore && ViewBlockLayoutWithBFI != GVDT_None &&
377382
(ViewBlockFreqFuncName.empty() ||
378383
MF.getFunction().getName() == ViewBlockFreqFuncName)) {

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3649,6 +3649,7 @@ void MachineBlockPlacement::assignBlockOrder(
36493649
const std::vector<const MachineBasicBlock *> &NewBlockOrder) {
36503650
assert(F->size() == NewBlockOrder.size() && "Incorrect size of block order");
36513651
F->RenumberBlocks();
3652+
MPDT->updateBlockNumbers();
36523653

36533654
bool HasChanges = false;
36543655
for (size_t I = 0; I < NewBlockOrder.size(); I++) {

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
375375
// numbering, shrink MBBNumbering now.
376376
assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
377377
MBBNumbering.resize(BlockNo);
378+
MBBNumberingEpoch++;
378379
}
379380

380381
/// This method iterates over the basic blocks and assigns their IsBeginSection

llvm/lib/CodeGen/UnreachableBlockElim.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
195195
}
196196

197197
F.RenumberBlocks();
198+
if (MDT)
199+
MDT->updateBlockNumbers();
198200

199201
return (!DeadBlocks.empty() || ModifiedPHI);
200202
}

llvm/lib/Target/ARM/ARMConstantIslandPass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
414414
// Renumber all of the machine basic blocks in the function, guaranteeing that
415415
// the numbers agree with the position of the block in the function.
416416
MF->RenumberBlocks();
417+
DT->updateBlockNumbers();
417418

418419
// Try to reorder and otherwise adjust the block layout to make good use
419420
// of the TB[BH] instructions.
@@ -425,6 +426,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
425426
T2JumpTables.clear();
426427
// Blocks may have shifted around. Keep the numbering up to date.
427428
MF->RenumberBlocks();
429+
DT->updateBlockNumbers();
428430
}
429431

430432
// Align any non-fallthrough blocks
@@ -670,8 +672,10 @@ void ARMConstantIslands::doInitialJumpTablePlacement(
670672
}
671673

672674
// If we did anything then we need to renumber the subsequent blocks.
673-
if (LastCorrectlyNumberedBB)
675+
if (LastCorrectlyNumberedBB) {
674676
MF->RenumberBlocks(LastCorrectlyNumberedBB);
677+
DT->updateBlockNumbers();
678+
}
675679
}
676680

677681
/// BBHasFallthrough - Return true if the specified basic block can fallthrough
@@ -972,6 +976,7 @@ static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
972976
void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
973977
// Renumber the MBB's to keep them consecutive.
974978
NewBB->getParent()->RenumberBlocks(NewBB);
979+
DT->updateBlockNumbers();
975980

976981
// Insert an entry into BBInfo to align it properly with the (newly
977982
// renumbered) block numbers.
@@ -1034,6 +1039,7 @@ MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
10341039
// This is almost the same as updateForInsertedWaterBlock, except that
10351040
// the Water goes after OrigBB, not NewBB.
10361041
MF->RenumberBlocks(NewBB);
1042+
DT->updateBlockNumbers();
10371043

10381044
// Insert an entry into BBInfo to align it properly with the (newly
10391045
// renumbered) block numbers.
@@ -2485,6 +2491,7 @@ MachineBasicBlock *ARMConstantIslands::adjustJTTargetBlockForward(
24852491
BB->updateTerminator(OldNext != MF->end() ? &*OldNext : nullptr);
24862492
// Update numbering to account for the block being moved.
24872493
MF->RenumberBlocks();
2494+
DT->updateBlockNumbers();
24882495
++NumJTMoved;
24892496
return nullptr;
24902497
}
@@ -2513,6 +2520,7 @@ MachineBasicBlock *ARMConstantIslands::adjustJTTargetBlockForward(
25132520

25142521
// Update internal data structures to account for the newly inserted MBB.
25152522
MF->RenumberBlocks(NewBB);
2523+
DT->updateBlockNumbers();
25162524

25172525
// Update the CFG.
25182526
NewBB->addSuccessor(BB);

llvm/lib/Target/CSKY/CSKYConstantIslandPass.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "llvm/ADT/StringRef.h"
2929
#include "llvm/CodeGen/MachineBasicBlock.h"
3030
#include "llvm/CodeGen/MachineConstantPool.h"
31-
#include "llvm/CodeGen/MachineDominators.h"
3231
#include "llvm/CodeGen/MachineFrameInfo.h"
3332
#include "llvm/CodeGen/MachineFunction.h"
3433
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -217,11 +216,6 @@ class CSKYConstantIslands : public MachineFunctionPass {
217216

218217
bool runOnMachineFunction(MachineFunction &F) override;
219218

220-
void getAnalysisUsage(AnalysisUsage &AU) const override {
221-
AU.addRequired<MachineDominatorTreeWrapperPass>();
222-
MachineFunctionPass::getAnalysisUsage(AU);
223-
}
224-
225219
MachineFunctionProperties getRequiredProperties() const override {
226220
return MachineFunctionProperties().set(
227221
MachineFunctionProperties::Property::NoVRegs);

llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ struct Entry {
186186
/// Explore them.
187187
static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
188188
const WebAssemblyExceptionInfo &WEI,
189-
const MachineDominatorTree &MDT) {
189+
MachineDominatorTree &MDT) {
190190
// Remember original layout ordering, so we can update terminators after
191191
// reordering to point to the original layout successor.
192192
MF.RenumberBlocks();
193+
MDT.updateBlockNumbers();
193194

194195
// Prepare for a topological sort: Record the number of predecessors each
195196
// block has, ignoring loop backedges.
@@ -330,6 +331,7 @@ static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
330331
}
331332
assert(Entries.empty() && "Active sort region list not finished");
332333
MF.RenumberBlocks();
334+
MDT.updateBlockNumbers();
333335

334336
#ifndef NDEBUG
335337
SmallSetVector<const SortRegion *, 8> OnStack;

llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ STATISTIC(NumCatchUnwindMismatches, "Number of catch unwind mismatches found");
4545

4646
namespace {
4747
class WebAssemblyCFGStackify final : public MachineFunctionPass {
48+
MachineDominatorTree *MDT;
49+
4850
StringRef getPassName() const override { return "WebAssembly CFG Stackify"; }
4951

5052
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -252,7 +254,6 @@ void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) {
252254
void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
253255
assert(!MBB.isEHPad());
254256
MachineFunction &MF = *MBB.getParent();
255-
auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
256257
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
257258
const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
258259

@@ -264,7 +265,7 @@ void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
264265
int MBBNumber = MBB.getNumber();
265266
for (MachineBasicBlock *Pred : MBB.predecessors()) {
266267
if (Pred->getNumber() < MBBNumber) {
267-
Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
268+
Header = Header ? MDT->findNearestCommonDominator(Header, Pred) : Pred;
268269
if (explicitlyBranchesTo(Pred, &MBB))
269270
IsBranchedTo = true;
270271
}
@@ -1439,6 +1440,7 @@ void WebAssemblyCFGStackify::recalculateScopeTops(MachineFunction &MF) {
14391440
// Renumber BBs and recalculate ScopeTop info because new BBs might have been
14401441
// created and inserted during fixing unwind mismatches.
14411442
MF.RenumberBlocks();
1443+
MDT->updateBlockNumbers();
14421444
ScopeTops.clear();
14431445
ScopeTops.resize(MF.getNumBlockIDs());
14441446
for (auto &MBB : reverse(MF)) {
@@ -1741,6 +1743,7 @@ bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
17411743
"********** Function: "
17421744
<< MF.getName() << '\n');
17431745
const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
1746+
MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
17441747

17451748
releaseMemory();
17461749

0 commit comments

Comments
 (0)