Skip to content

Commit 37e75cd

Browse files
authored
[CodeGen] Use BasicBlock numbers to map to MBBs (#101883)
Now that basic blocks have numbers, we can replace the BB-to-MBB maps and the visited set during ISel with vectors for faster lookup.
1 parent b7730a2 commit 37e75cd

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

llvm/include/llvm/CodeGen/FunctionLoweringInfo.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ class FunctionLoweringInfo {
7373
/// allocated to hold a pointer to the hidden sret parameter.
7474
Register DemoteRegister;
7575

76-
/// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
77-
DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
76+
/// A mapping from LLVM basic block number to their machine block.
77+
SmallVector<MachineBasicBlock *> MBBMap;
7878

7979
/// ValueMap - Since we emit code for the function a basic block at a time,
8080
/// we must remember which virtual registers hold the values for
@@ -172,9 +172,9 @@ class FunctionLoweringInfo {
172172
/// for a value.
173173
DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
174174

175-
/// VisitedBBs - The set of basic blocks visited thus far by instruction
176-
/// selection.
177-
SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
175+
/// The set of basic blocks visited thus far by instruction selection. Indexed
176+
/// by basic block number.
177+
SmallVector<bool> VisitedBBs;
178178

179179
/// PHINodesToUpdate - A list of phi instructions whose operand list will
180180
/// be updated after processing the current basic block.
@@ -213,7 +213,8 @@ class FunctionLoweringInfo {
213213
}
214214

215215
MachineBasicBlock *getMBB(const BasicBlock *BB) const {
216-
return MBBMap.lookup(BB);
216+
assert(BB->getNumber() < MBBMap.size() && "uninitialized MBBMap?");
217+
return MBBMap[BB->getNumber()];
217218
}
218219

219220
Register CreateReg(MVT VT, bool isDivergent = false);

llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ class IRTranslator : public MachineFunctionPass {
146146
/// virtual registers and offsets.
147147
ValueToVRegInfo VMap;
148148

149-
// N.b. it's not completely obvious that this will be sufficient for every
150-
// LLVM IR construct (with "invoke" being the obvious candidate to mess up our
151-
// lives.
152-
DenseMap<const BasicBlock *, MachineBasicBlock *> BBToMBB;
153-
154149
// One BasicBlock can be translated to multiple MachineBasicBlocks. For such
155150
// BasicBlocks translated to multiple MachineBasicBlocks, MachinePreds retains
156151
// a mapping between the edges arriving at the BasicBlock to the corresponding

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Align IRTranslator::getMemOpAlign(const Instruction &I) {
287287
}
288288

289289
MachineBasicBlock &IRTranslator::getMBB(const BasicBlock &BB) {
290-
MachineBasicBlock *&MBB = BBToMBB[&BB];
290+
MachineBasicBlock *MBB = FuncInfo.getMBB(&BB);
291291
assert(MBB && "BasicBlock was not encountered before");
292292
return *MBB;
293293
}
@@ -3907,8 +3907,9 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
39073907
bool HasMustTailInVarArgFn = false;
39083908

39093909
// Create all blocks, in IR order, to preserve the layout.
3910+
FuncInfo.MBBMap.resize(F.getMaxBlockNumber());
39103911
for (const BasicBlock &BB: F) {
3911-
auto *&MBB = BBToMBB[&BB];
3912+
auto *&MBB = FuncInfo.MBBMap[BB.getNumber()];
39123913

39133914
MBB = MF->CreateMachineBasicBlock(&BB);
39143915
MF->push_back(MBB);

llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
236236
// Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
237237
// also creates the initial PHI MachineInstrs, though none of the input
238238
// operands are populated.
239+
MBBMap.resize(Fn->getMaxBlockNumber());
239240
for (const BasicBlock &BB : *Fn) {
240241
// Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
241242
// are really data, and no instructions can live here.
@@ -261,7 +262,7 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
261262
}
262263

263264
MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);
264-
MBBMap[&BB] = MBB;
265+
MBBMap[BB.getNumber()] = MBB;
265266
MF->push_back(MBB);
266267

267268
// Transfer the address-taken flag. This is necessary because there could

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,11 +1643,12 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
16431643
}
16441644

16451645
// Iterate over all basic blocks in the function.
1646+
FuncInfo->VisitedBBs.assign(Fn.getMaxBlockNumber(), false);
16461647
for (const BasicBlock *LLVMBB : RPOT) {
16471648
if (OptLevel != CodeGenOptLevel::None) {
16481649
bool AllPredsVisited = true;
16491650
for (const BasicBlock *Pred : predecessors(LLVMBB)) {
1650-
if (!FuncInfo->VisitedBBs.count(Pred)) {
1651+
if (!FuncInfo->VisitedBBs[Pred->getNumber()]) {
16511652
AllPredsVisited = false;
16521653
break;
16531654
}
@@ -1661,7 +1662,7 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
16611662
FuncInfo->InvalidatePHILiveOutRegInfo(&PN);
16621663
}
16631664

1664-
FuncInfo->VisitedBBs.insert(LLVMBB);
1665+
FuncInfo->VisitedBBs[LLVMBB->getNumber()] = true;
16651666
}
16661667

16671668
BasicBlock::const_iterator const Begin =

0 commit comments

Comments
 (0)