Skip to content

Commit e7694f3

Browse files
committed
Use MCRegister in MCRegisterInfo's interfaces
Summary: As part of this, define DenseMapInfo for MCRegister (and Register while I'm at it) Depends on D65599 Reviewers: arsenm Subscribers: MatzeB, qcolombet, jvesely, wdng, nhaehnle, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D65605 llvm-svn: 367719
1 parent 511be2a commit e7694f3

File tree

10 files changed

+101
-56
lines changed

10 files changed

+101
-56
lines changed

llvm/include/llvm/CodeGen/Register.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Register {
2121

2222
public:
2323
Register(unsigned Val = 0): Reg(Val) {}
24+
Register(MCRegister Val): Reg(Val) {}
2425

2526
// Register numbers can represent physical registers, virtual registers, and
2627
// sometimes stack slots. The unsigned values are divided into these ranges:
@@ -114,6 +115,22 @@ class Register {
114115
}
115116
};
116117

118+
// Provide DenseMapInfo for Register
119+
template<> struct DenseMapInfo<Register> {
120+
static inline unsigned getEmptyKey() {
121+
return DenseMapInfo<unsigned>::getEmptyKey();
122+
}
123+
static inline unsigned getTombstoneKey() {
124+
return DenseMapInfo<unsigned>::getTombstoneKey();
125+
}
126+
static unsigned getHashValue(const unsigned &Val) {
127+
return DenseMapInfo<unsigned>::getHashValue(Val);
128+
}
129+
static bool isEqual(const unsigned &LHS, const unsigned &RHS) {
130+
return DenseMapInfo<unsigned>::isEqual(LHS, RHS);
131+
}
132+
};
133+
117134
}
118135

119136
#endif // ifndef LLVM_CODEGEN_REGISTER_H

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,13 @@ class TargetRegisterInfo : public MCRegisterInfo {
976976
const MachineRegisterInfo &MRI) const {
977977
return nullptr;
978978
}
979+
980+
/// Returns the physical register number of sub-register "Index"
981+
/// for physical register RegNo. Return zero if the sub-register does not
982+
/// exist.
983+
inline Register getSubReg(MCRegister Reg, unsigned Idx) const {
984+
return static_cast<const MCRegisterInfo *>(this)->getSubReg(Reg, Idx);
985+
}
979986
};
980987

981988
//===----------------------------------------------------------------------===//

llvm/include/llvm/MC/MCRegister.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_MC_REGISTER_H
1010
#define LLVM_MC_REGISTER_H
1111

12+
#include "llvm/ADT/DenseMapInfo.h"
1213
#include <cassert>
1314

1415
namespace llvm {
@@ -64,6 +65,22 @@ class MCRegister {
6465
}
6566
};
6667

68+
// Provide DenseMapInfo for MCRegister
69+
template<> struct DenseMapInfo<MCRegister> {
70+
static inline unsigned getEmptyKey() {
71+
return DenseMapInfo<unsigned>::getEmptyKey();
72+
}
73+
static inline unsigned getTombstoneKey() {
74+
return DenseMapInfo<unsigned>::getTombstoneKey();
75+
}
76+
static unsigned getHashValue(const unsigned &Val) {
77+
return DenseMapInfo<unsigned>::getHashValue(Val);
78+
}
79+
static bool isEqual(const unsigned &LHS, const unsigned &RHS) {
80+
return DenseMapInfo<unsigned>::isEqual(LHS, RHS);
81+
}
82+
};
83+
6784
}
6885

6986
#endif // ifndef LLVM_MC_REGISTER_H

llvm/include/llvm/MC/MCRegisterInfo.h

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class MCRegisterInfo {
150150
private:
151151
const MCRegisterDesc *Desc; // Pointer to the descriptor array
152152
unsigned NumRegs; // Number of entries in the array
153-
unsigned RAReg; // Return address register
154-
unsigned PCReg; // Program counter register
153+
MCRegister RAReg; // Return address register
154+
MCRegister PCReg; // Program counter register
155155
const MCRegisterClass *Classes; // Pointer to the regclass array
156156
unsigned NumClasses; // Number of entries in the array
157157
unsigned NumRegUnits; // Number of regunits.
@@ -177,8 +177,8 @@ class MCRegisterInfo {
177177
const DwarfLLVMRegPair *EHL2DwarfRegs; // LLVM to Dwarf regs mapping EH
178178
const DwarfLLVMRegPair *Dwarf2LRegs; // Dwarf to LLVM regs mapping
179179
const DwarfLLVMRegPair *EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
180-
DenseMap<unsigned, int> L2SEHRegs; // LLVM to SEH regs mapping
181-
DenseMap<unsigned, int> L2CVRegs; // LLVM to CV regs mapping
180+
DenseMap<MCRegister, int> L2SEHRegs; // LLVM to SEH regs mapping
181+
DenseMap<MCRegister, int> L2CVRegs; // LLVM to CV regs mapping
182182

183183
public:
184184
/// DiffListIterator - Base iterator class that can traverse the
@@ -204,7 +204,7 @@ class MCRegisterInfo {
204204
/// advance - Move to the next list position, return the applied
205205
/// differential. This function does not detect the end of the list, that
206206
/// is the caller's responsibility (by checking for a 0 return value).
207-
unsigned advance() {
207+
MCRegister advance() {
208208
assert(isValid() && "Cannot move off the end of the list.");
209209
MCPhysReg D = *List++;
210210
Val += D;
@@ -216,7 +216,7 @@ class MCRegisterInfo {
216216
bool isValid() const { return List; }
217217

218218
/// Dereference the iterator to get the value at the current position.
219-
unsigned operator*() const { return Val; }
219+
MCRegister operator*() const { return Val; }
220220

221221
/// Pre-increment to move to the next position.
222222
void operator++() {
@@ -311,51 +311,51 @@ class MCRegisterInfo {
311311
/// as the LLVM register number.
312312
/// FIXME: TableGen these numbers. Currently this requires target specific
313313
/// initialization code.
314-
void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
314+
void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg) {
315315
L2SEHRegs[LLVMReg] = SEHReg;
316316
}
317317

318-
void mapLLVMRegToCVReg(unsigned LLVMReg, int CVReg) {
318+
void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg) {
319319
L2CVRegs[LLVMReg] = CVReg;
320320
}
321321

322322
/// This method should return the register where the return
323323
/// address can be found.
324-
unsigned getRARegister() const {
324+
MCRegister getRARegister() const {
325325
return RAReg;
326326
}
327327

328328
/// Return the register which is the program counter.
329-
unsigned getProgramCounter() const {
329+
MCRegister getProgramCounter() const {
330330
return PCReg;
331331
}
332332

333-
const MCRegisterDesc &operator[](unsigned RegNo) const {
333+
const MCRegisterDesc &operator[](MCRegister RegNo) const {
334334
assert(RegNo < NumRegs &&
335335
"Attempting to access record for invalid register number!");
336336
return Desc[RegNo];
337337
}
338338

339339
/// Provide a get method, equivalent to [], but more useful with a
340340
/// pointer to this object.
341-
const MCRegisterDesc &get(unsigned RegNo) const {
341+
const MCRegisterDesc &get(MCRegister RegNo) const {
342342
return operator[](RegNo);
343343
}
344344

345345
/// Returns the physical register number of sub-register "Index"
346346
/// for physical register RegNo. Return zero if the sub-register does not
347347
/// exist.
348-
unsigned getSubReg(unsigned Reg, unsigned Idx) const;
348+
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const;
349349

350350
/// Return a super-register of the specified register
351351
/// Reg so its sub-register of index SubIdx is Reg.
352-
unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
353-
const MCRegisterClass *RC) const;
352+
MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
353+
const MCRegisterClass *RC) const;
354354

355355
/// For a given register pair, return the sub-register index
356356
/// if the second register is a sub-register of the first. Return zero
357357
/// otherwise.
358-
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const;
358+
unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const;
359359

360360
/// Get the size of the bit range covered by a sub-register index.
361361
/// If the index isn't continuous, return the sum of the sizes of its parts.
@@ -369,7 +369,7 @@ class MCRegisterInfo {
369369

370370
/// Return the human-readable symbolic target-specific name for the
371371
/// specified physical register.
372-
const char *getName(unsigned RegNo) const {
372+
const char *getName(MCRegister RegNo) const {
373373
return RegStrings + get(RegNo).Name;
374374
}
375375

@@ -397,7 +397,7 @@ class MCRegisterInfo {
397397
/// number. Returns -1 if there is no equivalent value. The second
398398
/// parameter allows targets to use different numberings for EH info and
399399
/// debugging info.
400-
int getDwarfRegNum(unsigned RegNum, bool isEH) const;
400+
int getDwarfRegNum(MCRegister RegNum, bool isEH) const;
401401

402402
/// Map a dwarf register back to a target register.
403403
int getLLVMRegNum(unsigned RegNum, bool isEH) const;
@@ -413,11 +413,11 @@ class MCRegisterInfo {
413413

414414
/// Map a target register to an equivalent SEH register
415415
/// number. Returns LLVM register number if there is no equivalent value.
416-
int getSEHRegNum(unsigned RegNum) const;
416+
int getSEHRegNum(MCRegister RegNum) const;
417417

418418
/// Map a target register to an equivalent CodeView register
419419
/// number.
420-
int getCodeViewRegNum(unsigned RegNum) const;
420+
int getCodeViewRegNum(MCRegister RegNum) const;
421421

422422
regclass_iterator regclass_begin() const { return Classes; }
423423
regclass_iterator regclass_end() const { return Classes+NumClasses; }
@@ -441,34 +441,34 @@ class MCRegisterInfo {
441441
}
442442

443443
/// Returns the encoding for RegNo
444-
uint16_t getEncodingValue(unsigned RegNo) const {
444+
uint16_t getEncodingValue(MCRegister RegNo) const {
445445
assert(RegNo < NumRegs &&
446446
"Attempting to get encoding for invalid register number!");
447447
return RegEncodingTable[RegNo];
448448
}
449449

450450
/// Returns true if RegB is a sub-register of RegA.
451-
bool isSubRegister(unsigned RegA, unsigned RegB) const {
451+
bool isSubRegister(MCRegister RegA, MCRegister RegB) const {
452452
return isSuperRegister(RegB, RegA);
453453
}
454454

455455
/// Returns true if RegB is a super-register of RegA.
456-
bool isSuperRegister(unsigned RegA, unsigned RegB) const;
456+
bool isSuperRegister(MCRegister RegA, MCRegister RegB) const;
457457

458458
/// Returns true if RegB is a sub-register of RegA or if RegB == RegA.
459-
bool isSubRegisterEq(unsigned RegA, unsigned RegB) const {
459+
bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
460460
return isSuperRegisterEq(RegB, RegA);
461461
}
462462

463463
/// Returns true if RegB is a super-register of RegA or if
464464
/// RegB == RegA.
465-
bool isSuperRegisterEq(unsigned RegA, unsigned RegB) const {
465+
bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const {
466466
return RegA == RegB || isSuperRegister(RegA, RegB);
467467
}
468468

469469
/// Returns true if RegB is a super-register or sub-register of RegA
470470
/// or if RegB == RegA.
471-
bool isSuperOrSubRegisterEq(unsigned RegA, unsigned RegB) const {
471+
bool isSuperOrSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
472472
return isSubRegisterEq(RegA, RegB) || isSuperRegister(RegA, RegB);
473473
}
474474
};
@@ -484,8 +484,8 @@ class MCRegisterInfo {
484484
/// If IncludeSelf is set, Reg itself is included in the list.
485485
class MCSubRegIterator : public MCRegisterInfo::DiffListIterator {
486486
public:
487-
MCSubRegIterator(unsigned Reg, const MCRegisterInfo *MCRI,
488-
bool IncludeSelf = false) {
487+
MCSubRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
488+
bool IncludeSelf = false) {
489489
init(Reg, MCRI->DiffLists + MCRI->get(Reg).SubRegs);
490490
// Initially, the iterator points to Reg itself.
491491
if (!IncludeSelf)
@@ -502,13 +502,13 @@ class MCSubRegIndexIterator {
502502
public:
503503
/// Constructs an iterator that traverses subregisters and their
504504
/// associated subregister indices.
505-
MCSubRegIndexIterator(unsigned Reg, const MCRegisterInfo *MCRI)
505+
MCSubRegIndexIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
506506
: SRIter(Reg, MCRI) {
507507
SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices;
508508
}
509509

510510
/// Returns current sub-register.
511-
unsigned getSubReg() const {
511+
MCRegister getSubReg() const {
512512
return *SRIter;
513513
}
514514

@@ -533,7 +533,7 @@ class MCSuperRegIterator : public MCRegisterInfo::DiffListIterator {
533533
public:
534534
MCSuperRegIterator() = default;
535535

536-
MCSuperRegIterator(unsigned Reg, const MCRegisterInfo *MCRI,
536+
MCSuperRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
537537
bool IncludeSelf = false) {
538538
init(Reg, MCRI->DiffLists + MCRI->get(Reg).SuperRegs);
539539
// Initially, the iterator points to Reg itself.
@@ -544,7 +544,7 @@ class MCSuperRegIterator : public MCRegisterInfo::DiffListIterator {
544544

545545
// Definition for isSuperRegister. Put it down here since it needs the
546546
// iterator defined above in addition to the MCRegisterInfo class itself.
547-
inline bool MCRegisterInfo::isSuperRegister(unsigned RegA, unsigned RegB) const{
547+
inline bool MCRegisterInfo::isSuperRegister(MCRegister RegA, MCRegister RegB) const{
548548
for (MCSuperRegIterator I(RegA, this); I.isValid(); ++I)
549549
if (*I == RegB)
550550
return true;
@@ -571,7 +571,7 @@ class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator {
571571
/// in Reg.
572572
MCRegUnitIterator() = default;
573573

574-
MCRegUnitIterator(unsigned Reg, const MCRegisterInfo *MCRI) {
574+
MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) {
575575
assert(Reg && "Null register has no regunits");
576576
// Decode the RegUnits MCRegisterDesc field.
577577
unsigned RU = MCRI->get(Reg).RegUnits;
@@ -602,7 +602,7 @@ class MCRegUnitMaskIterator {
602602

603603
/// Constructs an iterator that traverses the register units and their
604604
/// associated LaneMasks in Reg.
605-
MCRegUnitMaskIterator(unsigned Reg, const MCRegisterInfo *MCRI)
605+
MCRegUnitMaskIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
606606
: RUIter(Reg, MCRI) {
607607
uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks;
608608
MaskListIter = &MCRI->RegUnitMaskSequences[Idx];
@@ -669,7 +669,7 @@ class MCRegUnitRootIterator {
669669
/// any ordering or that entries are unique.
670670
class MCRegAliasIterator {
671671
private:
672-
unsigned Reg;
672+
MCRegister Reg;
673673
const MCRegisterInfo *MCRI;
674674
bool IncludeSelf;
675675

@@ -678,7 +678,7 @@ class MCRegAliasIterator {
678678
MCSuperRegIterator SI;
679679

680680
public:
681-
MCRegAliasIterator(unsigned Reg, const MCRegisterInfo *MCRI,
681+
MCRegAliasIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
682682
bool IncludeSelf)
683683
: Reg(Reg), MCRI(MCRI), IncludeSelf(IncludeSelf) {
684684
// Initialize the iterators.
@@ -694,7 +694,7 @@ class MCRegAliasIterator {
694694

695695
bool isValid() const { return RI.isValid(); }
696696

697-
unsigned operator*() const {
697+
MCRegister operator*() const {
698698
assert(SI.isValid() && "Cannot dereference an invalid iterator.");
699699
return *SI;
700700
}

llvm/lib/CodeGen/CalcSpillWeights.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
4848
}
4949

5050
// Return the preferred allocation register for reg, given a COPY instruction.
51-
static unsigned copyHint(const MachineInstr *mi, unsigned reg,
51+
static Register copyHint(const MachineInstr *mi, unsigned reg,
5252
const TargetRegisterInfo &tri,
5353
const MachineRegisterInfo &mri) {
54-
unsigned sub, hreg, hsub;
54+
unsigned sub, hsub;
55+
Register hreg;
5556
if (mi->getOperand(0).getReg() == reg) {
5657
sub = mi->getOperand(0).getSubReg();
5758
hreg = mi->getOperand(1).getReg();
@@ -66,10 +67,10 @@ static unsigned copyHint(const MachineInstr *mi, unsigned reg,
6667
return 0;
6768

6869
if (Register::isVirtualRegister(hreg))
69-
return sub == hsub ? hreg : 0;
70+
return sub == hsub ? hreg : Register();
7071

7172
const TargetRegisterClass *rc = mri.getRegClass(reg);
72-
unsigned CopiedPReg = (hsub ? tri.getSubReg(hreg, hsub) : hreg);
73+
Register CopiedPReg = (hsub ? tri.getSubReg(hreg, hsub) : hreg);
7374
if (rc->contains(CopiedPReg))
7475
return CopiedPReg;
7576

llvm/lib/CodeGen/RegAllocFast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
862862
}
863863

864864
// Handle subregister index.
865-
MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
865+
MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : Register());
866866
MO.setIsRenamable(true);
867867
MO.setSubReg(0);
868868

0 commit comments

Comments
 (0)