Skip to content

Commit 0b71d80

Browse files
authored
[InstrRef][NFC] Avoid un-necessary DenseMap queries (#99048)
This patch adjusts how some data is stored to avoid a number of un-necessary DenseMap queries. There's no change to the compiler behaviour, and it's measurably faster on the compile time tracker. The BlockOrders vector in buildVLocValueMap collects the blocks over which a variables value have to be determined: however the Cmp ordering function makes two DenseMap queries to determine the RPO-order of blocks being compared. And given that sorting is O(N log(N)) comparisons this isn't fast. So instead, fetch the RPO-numbers of the block collection, order those, and then map back to the blocks themselves. The OrderToBB collection mapped an RPO-number to an MBB: it's completely un-necessary to have DenseMap here, we can just use the RPO number as an array index. Switch it to a SmallVector and deal with a few consequences when iterating. (And for good measure I've jammed in a couple of reserve calls).
1 parent 9b9194a commit 0b71d80

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,12 +3109,8 @@ void InstrRefBasedLDV::buildVLocValueMap(
31093109
SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore;
31103110

31113111
// The order in which to examine them (RPO).
3112-
SmallVector<MachineBasicBlock *, 8> BlockOrders;
3113-
3114-
// RPO ordering function.
3115-
auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) {
3116-
return BBToOrder[A] < BBToOrder[B];
3117-
};
3112+
SmallVector<MachineBasicBlock *, 16> BlockOrders;
3113+
SmallVector<unsigned, 32> BlockOrderNums;
31183114

31193115
getBlocksForScope(DILoc, BlocksToExplore, AssignBlocks);
31203116

@@ -3132,11 +3128,16 @@ void InstrRefBasedLDV::buildVLocValueMap(
31323128
for (const auto *MBB : BlocksToExplore)
31333129
MutBlocksToExplore.insert(const_cast<MachineBasicBlock *>(MBB));
31343130

3135-
// Picks out relevants blocks RPO order and sort them.
3131+
// Picks out relevants blocks RPO order and sort them. Sort their
3132+
// order-numbers and map back to MBB pointers later, to avoid repeated
3133+
// DenseMap queries during comparisons.
31363134
for (const auto *MBB : BlocksToExplore)
3137-
BlockOrders.push_back(const_cast<MachineBasicBlock *>(MBB));
3135+
BlockOrderNums.push_back(BBToOrder[MBB]);
31383136

3139-
llvm::sort(BlockOrders, Cmp);
3137+
llvm::sort(BlockOrderNums);
3138+
for (unsigned int I : BlockOrderNums)
3139+
BlockOrders.push_back(OrderToBB[I]);
3140+
BlockOrderNums.clear();
31403141
unsigned NumBlocks = BlockOrders.size();
31413142

31423143
// Allocate some vectors for storing the live ins and live outs. Large.
@@ -3396,16 +3397,24 @@ void InstrRefBasedLDV::initialSetup(MachineFunction &MF) {
33963397
return DL.getLine() != 0;
33973398
return false;
33983399
};
3399-
// Collect a set of all the artificial blocks.
3400-
for (auto &MBB : MF)
3400+
3401+
// Collect a set of all the artificial blocks. Collect the size too, ilist
3402+
// size calls are O(n).
3403+
unsigned int Size = 0;
3404+
for (auto &MBB : MF) {
3405+
++Size;
34013406
if (none_of(MBB.instrs(), hasNonArtificialLocation))
34023407
ArtificialBlocks.insert(&MBB);
3408+
}
34033409

34043410
// Compute mappings of block <=> RPO order.
34053411
ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
34063412
unsigned int RPONumber = 0;
3413+
OrderToBB.reserve(Size);
3414+
BBToOrder.reserve(Size);
3415+
BBNumToRPO.reserve(Size);
34073416
auto processMBB = [&](MachineBasicBlock *MBB) {
3408-
OrderToBB[RPONumber] = MBB;
3417+
OrderToBB.push_back(MBB);
34093418
BBToOrder[MBB] = RPONumber;
34103419
BBNumToRPO[MBB->getNumber()] = RPONumber;
34113420
++RPONumber;
@@ -3724,14 +3733,13 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
37243733

37253734
// Walk back through each block / instruction, collecting DBG_VALUE
37263735
// instructions and recording what machine value their operands refer to.
3727-
for (auto &OrderPair : OrderToBB) {
3728-
MachineBasicBlock &MBB = *OrderPair.second;
3729-
CurBB = MBB.getNumber();
3736+
for (MachineBasicBlock *MBB : OrderToBB) {
3737+
CurBB = MBB->getNumber();
37303738
VTracker = &vlocs[CurBB];
3731-
VTracker->MBB = &MBB;
3732-
MTracker->loadFromArray(MInLocs[MBB], CurBB);
3739+
VTracker->MBB = MBB;
3740+
MTracker->loadFromArray(MInLocs[*MBB], CurBB);
37333741
CurInst = 1;
3734-
for (auto &MI : MBB) {
3742+
for (auto &MI : *MBB) {
37353743
process(MI, &MOutLocs, &MInLocs);
37363744
++CurInst;
37373745
}

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ class InstrRefBasedLDV : public LDVImpl {
11531153
SmallPtrSet<MachineBasicBlock *, 16> ArtificialBlocks;
11541154

11551155
// Mapping of blocks to and from their RPOT order.
1156-
DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
1156+
SmallVector<MachineBasicBlock *> OrderToBB;
11571157
DenseMap<const MachineBasicBlock *, unsigned int> BBToOrder;
11581158
DenseMap<unsigned, unsigned> BBNumToRPO;
11591159

0 commit comments

Comments
 (0)