Skip to content

Commit 3979f27

Browse files
committed
[NFC][StackColoring] Use block numbers instead of maps
1 parent c012ebd commit 3979f27

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ class StackColoring {
396396

397397
/// Which slots are marked as LIVE_OUT, coming out of each basic block.
398398
BitVector LiveOut;
399+
400+
bool isEmpty() { return Begin.empty(); }
399401
};
400402

401-
/// Maps active slots (per bit) for each basic block.
402-
using LivenessMap = DenseMap<const MachineBasicBlock *, BlockLifetimeInfo>;
403-
LivenessMap BlockLiveness;
403+
SmallVector<BlockLifetimeInfo, 0> BlockLiveness;
404404

405405
/// Maps basic blocks to a serial number.
406406
SmallVector<const MachineBasicBlock *, 8> BasicBlockNumbering;
@@ -438,9 +438,6 @@ class StackColoring {
438438
bool run(MachineFunction &Func);
439439

440440
private:
441-
/// Used in collectMarkers
442-
using BlockBitVecMap = DenseMap<const MachineBasicBlock *, BitVector>;
443-
444441
/// Debug.
445442
void dump() const;
446443
void dumpIntervals() const;
@@ -538,9 +535,7 @@ LLVM_DUMP_METHOD void StackColoring::dumpBV(const char *tag,
538535
}
539536

540537
LLVM_DUMP_METHOD void StackColoring::dumpBB(MachineBasicBlock *MBB) const {
541-
LivenessMap::const_iterator BI = BlockLiveness.find(MBB);
542-
assert(BI != BlockLiveness.end() && "Block not found");
543-
const BlockLifetimeInfo &BlockInfo = BI->second;
538+
const BlockLifetimeInfo &BlockInfo = BlockLiveness[MBB->getNumber()];
544539

545540
dumpBV("BEGIN", BlockInfo.Begin);
546541
dumpBV("END", BlockInfo.End);
@@ -624,7 +619,7 @@ bool StackColoring::isLifetimeStartOrEnd(const MachineInstr &MI,
624619

625620
unsigned StackColoring::collectMarkers(unsigned NumSlot) {
626621
unsigned MarkersFound = 0;
627-
BlockBitVecMap SeenStartMap;
622+
SmallVector<BitVector> SeenStartMap;
628623
InterestingSlots.clear();
629624
InterestingSlots.resize(NumSlot);
630625
ConservativeSlots.clear();
@@ -634,6 +629,8 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
634629
SmallVector<int, 8> NumStartLifetimes(NumSlot, 0);
635630
SmallVector<int, 8> NumEndLifetimes(NumSlot, 0);
636631

632+
SeenStartMap.resize(MF->getNumBlockIDs());
633+
637634
// Step 1: collect markers and populate the "InterestingSlots"
638635
// and "ConservativeSlots" sets.
639636
for (MachineBasicBlock *MBB : depth_first(MF)) {
@@ -642,10 +639,11 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
642639
// to this bb).
643640
BitVector BetweenStartEnd;
644641
BetweenStartEnd.resize(NumSlot);
642+
SeenStartMap[MBB->getNumber()].resize(NumSlot);
645643
for (const MachineBasicBlock *Pred : MBB->predecessors()) {
646-
BlockBitVecMap::const_iterator I = SeenStartMap.find(Pred);
647-
if (I != SeenStartMap.end()) {
648-
BetweenStartEnd |= I->second;
644+
BitVector &PredSet = SeenStartMap[Pred->getNumber()];
645+
if (!PredSet.empty()) {
646+
BetweenStartEnd |= PredSet;
649647
}
650648
}
651649

@@ -693,7 +691,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
693691
}
694692
}
695693
}
696-
BitVector &SeenStart = SeenStartMap[MBB];
694+
BitVector &SeenStart = SeenStartMap[MBB->getNumber()];
697695
SeenStart |= BetweenStartEnd;
698696
}
699697
if (!MarkersFound) {
@@ -720,6 +718,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
720718

721719
LLVM_DEBUG(dumpBV("Conservative slots", ConservativeSlots));
722720

721+
BlockLiveness.resize(MF->getNumBlockIDs());
723722
// Step 2: compute begin/end sets for each block
724723

725724
// NOTE: We use a depth-first iteration to ensure that we obtain a
@@ -729,7 +728,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
729728
BasicBlockNumbering.push_back(MBB);
730729

731730
// Keep a reference to avoid repeated lookups.
732-
BlockLifetimeInfo &BlockInfo = BlockLiveness[MBB];
731+
BlockLifetimeInfo &BlockInfo = BlockLiveness[MBB->getNumber()];
733732

734733
BlockInfo.Begin.resize(NumSlot);
735734
BlockInfo.End.resize(NumSlot);
@@ -786,19 +785,19 @@ void StackColoring::calculateLocalLiveness() {
786785

787786
for (const MachineBasicBlock *BB : BasicBlockNumbering) {
788787
// Use an iterator to avoid repeated lookups.
789-
LivenessMap::iterator BI = BlockLiveness.find(BB);
790-
assert(BI != BlockLiveness.end() && "Block not found");
791-
BlockLifetimeInfo &BlockInfo = BI->second;
788+
BlockLifetimeInfo &BlockInfo = BlockLiveness[BB->getNumber()];
789+
if (BlockInfo.isEmpty())
790+
continue;
792791

793792
// Compute LiveIn by unioning together the LiveOut sets of all preds.
794793
LocalLiveIn.clear();
795794
for (MachineBasicBlock *Pred : BB->predecessors()) {
796-
LivenessMap::const_iterator I = BlockLiveness.find(Pred);
795+
BlockLifetimeInfo &PrefInfo = BlockLiveness[Pred->getNumber()];
797796
// PR37130: transformations prior to stack coloring can
798797
// sometimes leave behind statically unreachable blocks; these
799798
// can be safely skipped here.
800-
if (I != BlockLiveness.end())
801-
LocalLiveIn |= I->second.LiveOut;
799+
if (!PrefInfo.isEmpty())
800+
LocalLiveIn |= PrefInfo.LiveOut;
802801
}
803802

804803
// Compute LiveOut by subtracting out lifetimes that end in this
@@ -842,7 +841,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
842841
DefinitelyInUse.resize(NumSlots);
843842

844843
// Start the interval of the slots that we previously found to be 'in-use'.
845-
BlockLifetimeInfo &MBBLiveness = BlockLiveness[&MBB];
844+
BlockLifetimeInfo &MBBLiveness = BlockLiveness[MBB.getNumber()];
846845
for (int pos = MBBLiveness.LiveIn.find_first(); pos != -1;
847846
pos = MBBLiveness.LiveIn.find_next(pos)) {
848847
Starts[pos] = Indexes->getMBBStartIdx(&MBB);
@@ -899,15 +898,14 @@ bool StackColoring::removeAllMarkers() {
899898
Markers.clear();
900899

901900
for (MachineBasicBlock &MBB : *MF) {
902-
if (BlockLiveness.contains(&MBB))
903-
continue;
904-
for (MachineInstr &MI : make_early_inc_range(MBB)) {
905-
if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
906-
MI.getOpcode() == TargetOpcode::LIFETIME_END) {
907-
Count++;
908-
MI.eraseFromParent();
901+
if (BlockLiveness.empty() || BlockLiveness[MBB.getNumber()].isEmpty())
902+
for (MachineInstr &MI : make_early_inc_range(MBB)) {
903+
if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
904+
MI.getOpcode() == TargetOpcode::LIFETIME_END) {
905+
Count++;
906+
MI.eraseFromParent();
907+
}
909908
}
910-
}
911909
}
912910

913911
LLVM_DEBUG(dbgs() << "Removed " << Count << " markers.\n");

0 commit comments

Comments
 (0)