Skip to content

Commit 8c0577a

Browse files
[CodeGen][MachineVerifier] Use TypeSize instead of unsigned for getRegSizeInBits
This patch changes getRegSizeInBits to return a TypeSize instead of an unsigned in the case that a virtual register has a scalable LLT. In the case that register is physical, a Fixed TypeSize is returned. The MachineVerifier pass is updated to allow copies between fixed and scalable operands as long as the Src size will fit into the Dest size. This is a precommit which will be stacked on by a change to GISel to generate COPYs with a scalable destination but a fixed size source.
1 parent 801a30a commit 8c0577a

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ class TargetRegisterInfo : public MCRegisterInfo {
283283
// DenseMapInfo<unsigned> uses -1u and -2u.
284284

285285
/// Return the size in bits of a register from class RC.
286-
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const {
287-
return getRegClassInfo(RC).RegSize;
286+
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const {
287+
return TypeSize::Fixed(getRegClassInfo(RC).RegSize);
288288
}
289289

290290
/// Return the size in bytes of the stack slot allocated to hold a spilled
@@ -858,7 +858,7 @@ class TargetRegisterInfo : public MCRegisterInfo {
858858
const TargetRegisterClass *RC) const = 0;
859859

860860
/// Returns size in bits of a phys/virtual/generic register.
861-
unsigned getRegSizeInBits(Register Reg, const MachineRegisterInfo &MRI) const;
861+
TypeSize getRegSizeInBits(Register Reg, const MachineRegisterInfo &MRI) const;
862862

863863
/// Get the weight in units of pressure for this register unit.
864864
virtual unsigned getRegUnitWeight(unsigned RegUnit) const = 0;

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,16 +1937,17 @@ void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
19371937

19381938
// If we have only one valid type, this is likely a copy between a virtual
19391939
// and physical register.
1940-
unsigned SrcSize = 0;
1941-
unsigned DstSize = 0;
1940+
TypeSize SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
1941+
TypeSize DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
1942+
19421943
if (SrcReg.isPhysical() && DstTy.isValid()) {
19431944
const TargetRegisterClass *SrcRC =
19441945
TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy);
19451946
if (SrcRC)
19461947
SrcSize = TRI->getRegSizeInBits(*SrcRC);
19471948
}
19481949

1949-
if (SrcSize == 0)
1950+
if (SrcSize.isZero())
19501951
SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
19511952

19521953
if (DstReg.isPhysical() && SrcTy.isValid()) {
@@ -1956,10 +1957,21 @@ void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
19561957
DstSize = TRI->getRegSizeInBits(*DstRC);
19571958
}
19581959

1959-
if (DstSize == 0)
1960+
if (DstSize.isZero())
19601961
DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
19611962

1962-
if (SrcSize != 0 && DstSize != 0 && SrcSize != DstSize) {
1963+
// If the Dst is scalable and the Src is fixed, then the Dst can only hold
1964+
// the Src if the minimum size Dst can hold is at least as big as Src.
1965+
if (DstSize.isScalable() && !SrcSize.isScalable() &&
1966+
DstSize.getKnownMinValue() <= SrcSize.getFixedValue())
1967+
break;
1968+
// If the Src is scalable and the Dst is fixed, then Dest can only hold
1969+
// the Src is known to fit in Dest
1970+
if (SrcSize.isScalable() && !DstSize.isScalable() &&
1971+
TypeSize::isKnownLE(DstSize, SrcSize))
1972+
break;
1973+
1974+
if (SrcSize.isNonZero() && DstSize.isNonZero() && SrcSize != DstSize) {
19631975
if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
19641976
report("Copy Instruction is illegal with mismatching sizes", MI);
19651977
errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
499499
return true;
500500
}
501501

502-
unsigned
502+
TypeSize
503503
TargetRegisterInfo::getRegSizeInBits(Register Reg,
504504
const MachineRegisterInfo &MRI) const {
505505
const TargetRegisterClass *RC{};
@@ -508,16 +508,15 @@ TargetRegisterInfo::getRegSizeInBits(Register Reg,
508508
// Instead, we need to access a register class that contains Reg and
509509
// get the size of that register class.
510510
RC = getMinimalPhysRegClass(Reg);
511-
} else {
512-
LLT Ty = MRI.getType(Reg);
513-
unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
514-
// If Reg is not a generic register, query the register class to
515-
// get its size.
516-
if (RegSize)
517-
return RegSize;
518-
// Since Reg is not a generic register, it must have a register class.
519-
RC = MRI.getRegClass(Reg);
511+
assert(RC && "Unable to deduce the register class");
512+
return getRegSizeInBits(*RC);
520513
}
514+
LLT Ty = MRI.getType(Reg);
515+
if (Ty.isValid())
516+
return Ty.getSizeInBits();
517+
518+
// Since Reg is not a generic register, it may have a register class.
519+
RC = MRI.getRegClass(Reg);
521520
assert(RC && "Unable to deduce the register class");
522521
return getRegSizeInBits(*RC);
523522
}

0 commit comments

Comments
 (0)