Skip to content

Commit 97a60aa

Browse files
authored
[CodeGen] Turn MCRegUnit into an enum class (NFC) (#167943)
This changes `MCRegUnit` type from `unsigned` to `enum class : unsigned` and inserts necessary casts. The added `MCRegUnitToIndex` functor is used with `SparseSet`, `SparseMultiSet` and `IndexedMap` in a few places. `MCRegUnit` is opaque to users, so it didn't seem worth making it a full-fledged class like `Register`. Static type checking has detected one issue in `PrologueEpilogueInserter.cpp`, where `BitVector` created for `MCRegister` is indexed by both `MCRegister` and `MCRegUnit`. The number of casts could be reduced by using `IndexedMap` in more places and/or adding a `BitVector` adaptor, but the number of casts *per file* is still small and `IndexedMap` has limitations, so it didn't seem worth the effort. Pull Request: #167943
1 parent 6f3f108 commit 97a60aa

29 files changed

+144
-107
lines changed

llvm/include/llvm/CodeGen/LiveIntervalUnion.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ class LiveIntervalUnion {
191191

192192
void clear();
193193

194-
LiveIntervalUnion& operator[](unsigned idx) {
195-
assert(idx < Size && "idx out of bounds");
196-
return LIUs[idx];
194+
LiveIntervalUnion &operator[](MCRegUnit Unit) {
195+
assert(static_cast<unsigned>(Unit) < Size && "Unit out of bounds");
196+
return LIUs[static_cast<unsigned>(Unit)];
197197
}
198198

199-
const LiveIntervalUnion& operator[](unsigned Idx) const {
200-
assert(Idx < Size && "Idx out of bounds");
201-
return LIUs[Idx];
199+
const LiveIntervalUnion &operator[](MCRegUnit Unit) const {
200+
assert(static_cast<unsigned>(Unit) < Size && "Unit out of bounds");
201+
return LIUs[static_cast<unsigned>(Unit)];
202202
}
203203
};
204204
};

llvm/include/llvm/CodeGen/LiveIntervals.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,29 +413,32 @@ class LiveIntervals {
413413
/// Return the live range for register unit \p Unit. It will be computed if
414414
/// it doesn't exist.
415415
LiveRange &getRegUnit(MCRegUnit Unit) {
416-
LiveRange *LR = RegUnitRanges[Unit];
416+
LiveRange *LR = RegUnitRanges[static_cast<unsigned>(Unit)];
417417
if (!LR) {
418418
// Compute missing ranges on demand.
419419
// Use segment set to speed-up initial computation of the live range.
420-
RegUnitRanges[Unit] = LR = new LiveRange(UseSegmentSetForPhysRegs);
420+
RegUnitRanges[static_cast<unsigned>(Unit)] = LR =
421+
new LiveRange(UseSegmentSetForPhysRegs);
421422
computeRegUnitRange(*LR, Unit);
422423
}
423424
return *LR;
424425
}
425426

426427
/// Return the live range for register unit \p Unit if it has already been
427428
/// computed, or nullptr if it hasn't been computed yet.
428-
LiveRange *getCachedRegUnit(MCRegUnit Unit) { return RegUnitRanges[Unit]; }
429+
LiveRange *getCachedRegUnit(MCRegUnit Unit) {
430+
return RegUnitRanges[static_cast<unsigned>(Unit)];
431+
}
429432

430433
const LiveRange *getCachedRegUnit(MCRegUnit Unit) const {
431-
return RegUnitRanges[Unit];
434+
return RegUnitRanges[static_cast<unsigned>(Unit)];
432435
}
433436

434437
/// Remove computed live range for register unit \p Unit. Subsequent uses
435438
/// should rely on on-demand recomputation.
436439
void removeRegUnit(MCRegUnit Unit) {
437-
delete RegUnitRanges[Unit];
438-
RegUnitRanges[Unit] = nullptr;
440+
delete RegUnitRanges[static_cast<unsigned>(Unit)];
441+
RegUnitRanges[static_cast<unsigned>(Unit)] = nullptr;
439442
}
440443

441444
/// Remove associated live ranges for the register units associated with \p

llvm/include/llvm/CodeGen/LiveRegMatrix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ class LiveRegMatrix {
165165

166166
/// Directly access the live interval unions per regunit.
167167
/// This returns an array indexed by the regunit number.
168-
LiveIntervalUnion *getLiveUnions() { return &Matrix[0]; }
168+
LiveIntervalUnion *getLiveUnions() {
169+
return &Matrix[static_cast<MCRegUnit>(0)];
170+
}
169171

170172
Register getOneVReg(unsigned PhysReg) const;
171173
};

llvm/include/llvm/CodeGen/LiveRegUnits.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,23 @@ class LiveRegUnits {
8686
/// Adds register units covered by physical register \p Reg.
8787
void addReg(MCRegister Reg) {
8888
for (MCRegUnit Unit : TRI->regunits(Reg))
89-
Units.set(Unit);
89+
Units.set(static_cast<unsigned>(Unit));
9090
}
9191

9292
/// Adds register units covered by physical register \p Reg that are
9393
/// part of the lanemask \p Mask.
9494
void addRegMasked(MCRegister Reg, LaneBitmask Mask) {
95-
for (MCRegUnitMaskIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
96-
LaneBitmask UnitMask = (*Unit).second;
95+
for (MCRegUnitMaskIterator I(Reg, TRI); I.isValid(); ++I) {
96+
auto [Unit, UnitMask] = *I;
9797
if ((UnitMask & Mask).any())
98-
Units.set((*Unit).first);
98+
Units.set(static_cast<unsigned>(Unit));
9999
}
100100
}
101101

102102
/// Removes all register units covered by physical register \p Reg.
103103
void removeReg(MCRegister Reg) {
104104
for (MCRegUnit Unit : TRI->regunits(Reg))
105-
Units.reset(Unit);
105+
Units.reset(static_cast<unsigned>(Unit));
106106
}
107107

108108
/// Removes register units not preserved by the regmask \p RegMask.
@@ -116,7 +116,7 @@ class LiveRegUnits {
116116
/// Returns true if no part of physical register \p Reg is live.
117117
bool available(MCRegister Reg) const {
118118
for (MCRegUnit Unit : TRI->regunits(Reg)) {
119-
if (Units.test(Unit))
119+
if (Units.test(static_cast<unsigned>(Unit)))
120120
return false;
121121
}
122122
return true;

llvm/include/llvm/CodeGen/MachineTraceMetrics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ struct LiveRegUnit {
7878
const MachineInstr *MI = nullptr;
7979
unsigned Op = 0;
8080

81-
unsigned getSparseSetIndex() const { return RegUnit; }
81+
unsigned getSparseSetIndex() const { return static_cast<unsigned>(RegUnit); }
8282

8383
explicit LiveRegUnit(MCRegUnit RU) : RegUnit(RU) {}
8484
};
8585

86-
using LiveRegUnitSet = SparseSet<LiveRegUnit>;
86+
using LiveRegUnitSet = SparseSet<LiveRegUnit, MCRegUnit, MCRegUnitToIndex>;
8787

8888
/// Strategies for selecting traces.
8989
enum class MachineTraceStrategy {

llvm/include/llvm/CodeGen/RDFRegisters.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CODEGEN_RDFREGISTERS_H
1111

1212
#include "llvm/ADT/BitVector.h"
13+
#include "llvm/ADT/IndexedMap.h"
1314
#include "llvm/ADT/STLExtras.h"
1415
#include "llvm/ADT/iterator_range.h"
1516
#include "llvm/CodeGen/TargetRegisterInfo.h"
@@ -111,7 +112,7 @@ struct RegisterRef {
111112

112113
constexpr MCRegUnit asMCRegUnit() const {
113114
assert(isUnit());
114-
return Id & ~UnitFlag;
115+
return static_cast<MCRegUnit>(Id & ~UnitFlag);
115116
}
116117

117118
constexpr unsigned asMaskIdx() const {
@@ -160,7 +161,7 @@ struct PhysicalRegisterInfo {
160161
// Returns the set of aliased physical registers.
161162
std::set<RegisterId> getAliasSet(RegisterRef RR) const;
162163

163-
RegisterRef getRefForUnit(uint32_t U) const {
164+
RegisterRef getRefForUnit(MCRegUnit U) const {
164165
return RegisterRef(UnitInfos[U].Reg, UnitInfos[U].Mask);
165166
}
166167

@@ -170,7 +171,7 @@ struct PhysicalRegisterInfo {
170171

171172
std::set<RegisterId> getUnits(RegisterRef RR) const;
172173

173-
const BitVector &getUnitAliases(uint32_t U) const {
174+
const BitVector &getUnitAliases(MCRegUnit U) const {
174175
return AliasInfos[U].Regs;
175176
}
176177

@@ -201,9 +202,9 @@ struct PhysicalRegisterInfo {
201202
const TargetRegisterInfo &TRI;
202203
IndexedSet<const uint32_t *> RegMasks;
203204
std::vector<RegInfo> RegInfos;
204-
std::vector<UnitInfo> UnitInfos;
205+
IndexedMap<UnitInfo, MCRegUnitToIndex> UnitInfos;
205206
std::vector<MaskInfo> MaskInfos;
206-
std::vector<AliasInfo> AliasInfos;
207+
IndexedMap<AliasInfo, MCRegUnitToIndex> AliasInfos;
207208
};
208209

209210
struct RegisterRefEqualTo {

llvm/include/llvm/CodeGen/ReachingDefAnalysis.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ class MBBReachingDefsInfo {
7878
}
7979

8080
void append(unsigned MBBNumber, MCRegUnit Unit, int Def) {
81-
AllReachingDefs[MBBNumber][Unit].push_back(Def);
81+
AllReachingDefs[MBBNumber][static_cast<unsigned>(Unit)].push_back(Def);
8282
}
8383

8484
void prepend(unsigned MBBNumber, MCRegUnit Unit, int Def) {
85-
auto &Defs = AllReachingDefs[MBBNumber][Unit];
85+
auto &Defs = AllReachingDefs[MBBNumber][static_cast<unsigned>(Unit)];
8686
Defs.insert(Defs.begin(), Def);
8787
}
8888

8989
void replaceFront(unsigned MBBNumber, MCRegUnit Unit, int Def) {
90-
assert(!AllReachingDefs[MBBNumber][Unit].empty());
91-
*AllReachingDefs[MBBNumber][Unit].begin() = Def;
90+
assert(!AllReachingDefs[MBBNumber][static_cast<unsigned>(Unit)].empty());
91+
*AllReachingDefs[MBBNumber][static_cast<unsigned>(Unit)].begin() = Def;
9292
}
9393

9494
void clear() { AllReachingDefs.clear(); }
@@ -97,7 +97,7 @@ class MBBReachingDefsInfo {
9797
if (AllReachingDefs[MBBNumber].empty())
9898
// Block IDs are not necessarily dense.
9999
return ArrayRef<ReachingDef>();
100-
return AllReachingDefs[MBBNumber][Unit];
100+
return AllReachingDefs[MBBNumber][static_cast<unsigned>(Unit)];
101101
}
102102

103103
private:

llvm/include/llvm/CodeGen/Register.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,25 @@ class VirtRegOrUnit {
182182
unsigned VRegOrUnit;
183183

184184
public:
185-
constexpr explicit VirtRegOrUnit(MCRegUnit Unit) : VRegOrUnit(Unit) {
185+
constexpr explicit VirtRegOrUnit(MCRegUnit Unit)
186+
: VRegOrUnit(static_cast<unsigned>(Unit)) {
186187
assert(!Register::isVirtualRegister(VRegOrUnit));
187188
}
189+
188190
constexpr explicit VirtRegOrUnit(Register Reg) : VRegOrUnit(Reg.id()) {
189191
assert(Reg.isVirtual());
190192
}
191193

194+
// Catches implicit conversions to Register.
195+
template <typename T> explicit VirtRegOrUnit(T) = delete;
196+
192197
constexpr bool isVirtualReg() const {
193198
return Register::isVirtualRegister(VRegOrUnit);
194199
}
195200

196201
constexpr MCRegUnit asMCRegUnit() const {
197202
assert(!isVirtualReg() && "Not a register unit");
198-
return VRegOrUnit;
203+
return static_cast<MCRegUnit>(VRegOrUnit);
199204
}
200205

201206
constexpr Register asVirtualReg() const {

llvm/include/llvm/CodeGen/RegisterClassInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class RegisterClassInfo {
123123
MCRegister getLastCalleeSavedAlias(MCRegister PhysReg) const {
124124
MCRegister CSR;
125125
for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
126-
CSR = CalleeSavedAliases[Unit];
126+
CSR = CalleeSavedAliases[static_cast<unsigned>(Unit)];
127127
if (CSR)
128128
break;
129129
}

llvm/include/llvm/CodeGen/RegisterPressure.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ class LiveRegSet {
282282
unsigned getSparseIndexFromVirtRegOrUnit(VirtRegOrUnit VRegOrUnit) const {
283283
if (VRegOrUnit.isVirtualReg())
284284
return VRegOrUnit.asVirtualReg().virtRegIndex() + NumRegUnits;
285-
assert(VRegOrUnit.asMCRegUnit() < NumRegUnits);
286-
return VRegOrUnit.asMCRegUnit();
285+
assert(static_cast<unsigned>(VRegOrUnit.asMCRegUnit()) < NumRegUnits);
286+
return static_cast<unsigned>(VRegOrUnit.asMCRegUnit());
287287
}
288288

289289
VirtRegOrUnit getVirtRegOrUnitFromSparseIndex(unsigned SparseIndex) const {
290290
if (SparseIndex >= NumRegUnits)
291291
return VirtRegOrUnit(Register::index2VirtReg(SparseIndex - NumRegUnits));
292-
return VirtRegOrUnit(SparseIndex);
292+
return VirtRegOrUnit(static_cast<MCRegUnit>(SparseIndex));
293293
}
294294

295295
public:

0 commit comments

Comments
 (0)