Skip to content

Commit 3c5f135

Browse files
committed
[NFC][LiveStacks] Use vectors instead of map and unordred_map
1 parent 353a1e5 commit 3c5f135

File tree

4 files changed

+49
-62
lines changed

4 files changed

+49
-62
lines changed

llvm/include/llvm/CodeGen/LiveStacks.h

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,43 @@ class LiveStacks {
4040
///
4141
VNInfo::Allocator VNInfoAllocator;
4242

43-
/// S2IMap - Stack slot indices to live interval mapping.
44-
using SS2IntervalMap = std::unordered_map<int, LiveInterval>;
45-
SS2IntervalMap S2IMap;
46-
47-
/// S2RCMap - Stack slot indices to register class mapping.
48-
std::map<int, const TargetRegisterClass *> S2RCMap;
43+
int StartIdx = -1;
44+
SmallVector<LiveInterval *> S2LI;
45+
SmallVector<const TargetRegisterClass *> S2RC;
4946

5047
public:
51-
using iterator = SS2IntervalMap::iterator;
52-
using const_iterator = SS2IntervalMap::const_iterator;
48+
using iterator = SmallVector<LiveInterval *>::iterator;
49+
using const_iterator = SmallVector<LiveInterval *>::const_iterator;
5350

54-
const_iterator begin() const { return S2IMap.begin(); }
55-
const_iterator end() const { return S2IMap.end(); }
56-
iterator begin() { return S2IMap.begin(); }
57-
iterator end() { return S2IMap.end(); }
51+
const_iterator begin() const { return S2LI.begin(); }
52+
const_iterator end() const { return S2LI.end(); }
53+
iterator begin() { return S2LI.begin(); }
54+
iterator end() { return S2LI.end(); }
5855

59-
unsigned getNumIntervals() const { return (unsigned)S2IMap.size(); }
56+
unsigned getStartIdx() const { return StartIdx; }
57+
unsigned getNumIntervals() const { return (unsigned)S2LI.size(); }
6058

6159
LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC);
6260

6361
LiveInterval &getInterval(int Slot) {
6462
assert(Slot >= 0 && "Spill slot indice must be >= 0");
65-
SS2IntervalMap::iterator I = S2IMap.find(Slot);
66-
assert(I != S2IMap.end() && "Interval does not exist for stack slot");
67-
return I->second;
63+
return *S2LI[Slot - StartIdx];
6864
}
6965

7066
const LiveInterval &getInterval(int Slot) const {
7167
assert(Slot >= 0 && "Spill slot indice must be >= 0");
72-
SS2IntervalMap::const_iterator I = S2IMap.find(Slot);
73-
assert(I != S2IMap.end() && "Interval does not exist for stack slot");
74-
return I->second;
68+
return *S2LI[Slot - StartIdx];
7569
}
7670

77-
bool hasInterval(int Slot) const { return S2IMap.count(Slot); }
71+
bool hasInterval(int Slot) const {
72+
if (Slot < StartIdx || StartIdx == -1)
73+
return false;
74+
return !getInterval(Slot).empty();
75+
}
7876

7977
const TargetRegisterClass *getIntervalRegClass(int Slot) const {
8078
assert(Slot >= 0 && "Spill slot indice must be >= 0");
81-
std::map<int, const TargetRegisterClass *>::const_iterator I =
82-
S2RCMap.find(Slot);
83-
assert(I != S2RCMap.end() &&
84-
"Register class info does not exist for stack slot");
85-
return I->second;
79+
return S2RC[Slot - StartIdx];
8680
}
8781

8882
VNInfo::Allocator &getVNInfoAllocator() { return VNInfoAllocator; }

llvm/lib/CodeGen/LiveStacks.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ void LiveStacksWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
3737
}
3838

3939
void LiveStacks::releaseMemory() {
40+
for (int Idx = 0; Idx < (int)S2LI.size(); ++Idx)
41+
S2LI[Idx]->~LiveInterval();
4042
// Release VNInfo memory regions, VNInfo objects don't need to be dtor'd.
4143
VNInfoAllocator.Reset();
42-
S2IMap.clear();
43-
S2RCMap.clear();
44+
S2LI.clear();
45+
S2RC.clear();
4446
}
4547

4648
void LiveStacks::init(MachineFunction &MF) {
@@ -52,20 +54,22 @@ void LiveStacks::init(MachineFunction &MF) {
5254
LiveInterval &
5355
LiveStacks::getOrCreateInterval(int Slot, const TargetRegisterClass *RC) {
5456
assert(Slot >= 0 && "Spill slot indice must be >= 0");
55-
SS2IntervalMap::iterator I = S2IMap.find(Slot);
56-
if (I == S2IMap.end()) {
57-
I = S2IMap
58-
.emplace(
59-
std::piecewise_construct, std::forward_as_tuple(Slot),
60-
std::forward_as_tuple(Register::index2StackSlot(Slot), 0.0F))
61-
.first;
62-
S2RCMap.insert(std::make_pair(Slot, RC));
57+
if (StartIdx == -1)
58+
StartIdx = Slot;
59+
60+
int Idx = Slot - StartIdx;
61+
assert(Idx >= 0 && "Slot not in order ?");
62+
if (Idx < (int)S2LI.size()) {
63+
S2RC[Idx] = TRI->getCommonSubClass(S2RC[Idx], RC);
6364
} else {
64-
// Use the largest common subclass register class.
65-
const TargetRegisterClass *&OldRC = S2RCMap[Slot];
66-
OldRC = TRI->getCommonSubClass(OldRC, RC);
65+
S2RC.resize(Idx + 1);
66+
S2LI.resize(Idx + 1);
67+
S2LI[Idx] = this->VNInfoAllocator.Allocate<LiveInterval>();
68+
new (S2LI[Idx]) LiveInterval(Register::index2StackSlot(Slot), 0.0F);
69+
S2RC[Idx] = RC;
6770
}
68-
return I->second;
71+
assert(S2RC.size() == S2LI.size());
72+
return *S2LI[Idx];
6973
}
7074

7175
AnalysisKey LiveStacksAnalysis::Key;
@@ -96,13 +100,12 @@ void LiveStacksWrapperLegacy::print(raw_ostream &OS, const Module *) const {
96100
}
97101

98102
/// print - Implement the dump method.
99-
void LiveStacks::print(raw_ostream &OS, const Module*) const {
103+
void LiveStacks::print(raw_ostream &OS, const Module *) const {
100104

101105
OS << "********** INTERVALS **********\n";
102-
for (const_iterator I = begin(), E = end(); I != E; ++I) {
103-
I->second.print(OS);
104-
int Slot = I->first;
105-
const TargetRegisterClass *RC = getIntervalRegClass(Slot);
106+
for (int Idx = 0; Idx < (int)S2LI.size(); ++Idx) {
107+
S2LI[Idx]->print(OS);
108+
const TargetRegisterClass *RC = S2RC[Idx];
106109
if (RC)
107110
OS << " [" << TRI->getRegClassName(RC) << "]\n";
108111
else

llvm/lib/CodeGen/StackSlotColoring.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,14 @@ void StackSlotColoring::InitializeSlots() {
262262
UsedColors[0].resize(LastFI);
263263
Assignments.resize(LastFI);
264264

265-
using Pair = std::iterator_traits<LiveStacks::iterator>::value_type;
266-
267-
SmallVector<Pair *, 16> Intervals;
268-
269-
Intervals.reserve(LS->getNumIntervals());
270-
for (auto &I : *LS)
271-
Intervals.push_back(&I);
272-
llvm::sort(Intervals,
273-
[](Pair *LHS, Pair *RHS) { return LHS->first < RHS->first; });
274-
275265
// Gather all spill slots into a list.
276266
LLVM_DEBUG(dbgs() << "Spill slot intervals:\n");
277-
for (auto *I : Intervals) {
278-
LiveInterval &li = I->second;
279-
LLVM_DEBUG(li.dump());
280-
int FI = li.reg().stackSlotIndex();
281-
if (MFI->isDeadObjectIndex(FI))
267+
for (auto [Idx, I] : llvm::enumerate(*LS)) {
268+
int FI = Idx + LS->getStartIdx();
269+
if (!I || MFI->isDeadObjectIndex(FI))
282270
continue;
271+
LiveInterval &li = *I;
272+
LLVM_DEBUG(li.dump());
283273

284274
SSIntervals.push_back(&li);
285275
OrigAlignments[FI] = MFI->getObjectAlign(FI);

llvm/lib/Target/AMDGPU/AMDGPUMarkLastScratchLoad.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ bool AMDGPUMarkLastScratchLoad::run(MachineFunction &MF) {
102102

103103
bool Changed = false;
104104

105-
for (auto &[SS, LI] : *LS) {
106-
for (const LiveRange::Segment &Segment : LI.segments) {
105+
for (auto *LI : *LS) {
106+
for (const LiveRange::Segment &Segment : LI->segments) {
107107

108108
// Ignore segments that run to the end of basic block because in this case
109109
// slot is still live at the end of it.
110110
if (Segment.end.isBlock())
111111
continue;
112112

113-
const int FrameIndex = LI.reg().stackSlotIndex();
113+
const int FrameIndex = LI->reg().stackSlotIndex();
114114
MachineInstr *LastLoad = nullptr;
115115

116116
MachineInstr *MISegmentEnd = SI->getInstructionFromIndex(Segment.end);

0 commit comments

Comments
 (0)