Skip to content

Commit 0a35232

Browse files
committed
[MC] Assert that MCRegUnitIterator operates over MCRegisters
The signature of the ctor expects a MCRegister, but currently any unsigned value can be converted to a MCRegister. This patch checks that indeed the provided value is a physical register only. We want to eventually stop implicitly converting unsigned or Register to MCRegister (which is incorrect). The next step after this patch is changing uses of MCRegUnitIterator to explicitly cast Register or unsigned values to MCRegister. To that end, this patch also introduces 2 APIs that make that conversion checked and explicit. Differential Revision: https://reviews.llvm.org/D88705
1 parent 508ac0e commit 0a35232

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

llvm/include/llvm/CodeGen/Register.h

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ class Register {
110110
return MCRegister(Reg);
111111
}
112112

113+
/// Utility to check-convert this value to a MCRegister. The caller is
114+
/// expected to have already validated that this Register is, indeed,
115+
/// physical.
116+
MCRegister asMCReg() const {
117+
assert(Reg == MCRegister::NoRegister ||
118+
MCRegister::isPhysicalRegister(Reg));
119+
return MCRegister(Reg);
120+
}
121+
113122
bool isValid() const { return Reg != MCRegister::NoRegister; }
114123

115124
/// Comparisons between register objects

llvm/include/llvm/MC/MCRegister.h

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class MCRegister {
6868
return Reg;
6969
}
7070

71+
/// Check the provided unsigned value is a valid MCRegister.
72+
static MCRegister from(unsigned Val) {
73+
assert(Val == NoRegister || isPhysicalRegister(Val));
74+
return MCRegister(Val);
75+
}
76+
7177
unsigned id() const {
7278
return Reg;
7379
}

llvm/include/llvm/MC/MCRegisterInfo.h

+1
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator {
675675

676676
MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) {
677677
assert(Reg && "Null register has no regunits");
678+
assert(MCRegister::isPhysicalRegister(Reg.id()));
678679
// Decode the RegUnits MCRegisterDesc field.
679680
unsigned RU = MCRI->get(Reg).RegUnits;
680681
unsigned Scale = RU & 15;

0 commit comments

Comments
 (0)