Skip to content

Commit 1bdaaae

Browse files
committed
[NFC][LiveStacks] Use vectors instead of map and unordred_map
1 parent f85177e commit 1bdaaae

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

llvm/include/llvm/CodeGen/LiveStacks.h

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,42 @@ 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 getNumIntervals() const { return (unsigned)S2LI.size(); }
6057

6158
LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC);
6259

6360
LiveInterval &getInterval(int Slot) {
6461
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;
62+
return *S2LI[Slot - StartIdx];
6863
}
6964

7065
const LiveInterval &getInterval(int Slot) const {
7166
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;
67+
return *S2LI[Slot - StartIdx];
7568
}
7669

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

7976
const TargetRegisterClass *getIntervalRegClass(int Slot) const {
8077
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;
78+
return S2RC[Slot - StartIdx];
8679
}
8780

8881
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: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,10 @@ 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;
267+
for (auto *I : *LS) {
268+
LiveInterval &li = *I;
279269
LLVM_DEBUG(li.dump());
280270
int FI = li.reg().stackSlotIndex();
281271
if (MFI->isDeadObjectIndex(FI))

0 commit comments

Comments
 (0)