Skip to content

Commit dfec9e3

Browse files
committed
[TRI][RISCV] Add methods to get common register class of two registers
Here we add two methods `getCommonMinimalPhysRegClass` and a LLT version `getCommonMinimalPhysRegClassLLT`, which return the most sub register class of the right type that contains these two input registers. We don't overload the `getMinimalPhysRegClass` as there will be ambiguities. We use it to simplify some code in RISC-V target.
1 parent 8db7327 commit dfec9e3

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,28 @@ class TargetRegisterInfo : public MCRegisterInfo {
347347
const TargetRegisterClass *getMinimalPhysRegClass(MCRegister Reg,
348348
MVT VT = MVT::Other) const;
349349

350+
/// Returns the common Register Class of two physical registers of the given
351+
/// type, picking the most sub register class of the right type that contains
352+
/// these two physregs.
353+
const TargetRegisterClass *
354+
getCommonMinimalPhysRegClass(MCRegister Reg1, MCRegister Reg2,
355+
MVT VT = MVT::Other) const;
356+
350357
/// Returns the Register Class of a physical register of the given type,
351358
/// picking the most sub register class of the right type that contains this
352359
/// physreg. If there is no register class compatible with the given type,
353360
/// returns nullptr.
354361
const TargetRegisterClass *getMinimalPhysRegClassLLT(MCRegister Reg,
355362
LLT Ty = LLT()) const;
356363

364+
/// Returns the common Register Class of two physical registers of the given
365+
/// type, picking the most sub register class of the right type that contains
366+
/// these two physregs. If there is no register class compatible with the
367+
/// given type, returns nullptr.
368+
const TargetRegisterClass *
369+
getCommonMinimalPhysRegClassLLT(MCRegister Reg1, MCRegister Reg2,
370+
LLT Ty = LLT()) const;
371+
357372
/// Return the maximal subclass of the given register class that is
358373
/// allocatable or NULL.
359374
const TargetRegisterClass *

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,25 @@ TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
222222
return BestRC;
223223
}
224224

225+
const TargetRegisterClass *TargetRegisterInfo::getCommonMinimalPhysRegClass(
226+
MCRegister Reg1, MCRegister Reg2, MVT VT) const {
227+
assert(Register::isPhysicalRegister(Reg1) &&
228+
Register::isPhysicalRegister(Reg2) &&
229+
"Reg1/Reg2 must be a physical register");
230+
231+
// Pick the most sub register class of the right type that contains
232+
// this physreg.
233+
const TargetRegisterClass *BestRC = nullptr;
234+
for (const TargetRegisterClass *RC : regclasses()) {
235+
if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
236+
RC->contains(Reg1, Reg2) && (!BestRC || BestRC->hasSubClass(RC)))
237+
BestRC = RC;
238+
}
239+
240+
assert(BestRC && "Couldn't find the register class");
241+
return BestRC;
242+
}
243+
225244
const TargetRegisterClass *
226245
TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
227246
assert(Register::isPhysicalRegister(reg) &&
@@ -239,6 +258,24 @@ TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
239258
return BestRC;
240259
}
241260

261+
const TargetRegisterClass *TargetRegisterInfo::getCommonMinimalPhysRegClassLLT(
262+
MCRegister Reg1, MCRegister Reg2, LLT Ty) const {
263+
assert(Register::isPhysicalRegister(Reg1) &&
264+
Register::isPhysicalRegister(Reg2) &&
265+
"Reg1/Reg2 must be a physical register");
266+
267+
// Pick the most sub register class of the right type that contains
268+
// this physreg.
269+
const TargetRegisterClass *BestRC = nullptr;
270+
for (const TargetRegisterClass *RC : regclasses()) {
271+
if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) &&
272+
RC->contains(Reg1, Reg2) && (!BestRC || BestRC->hasSubClass(RC)))
273+
BestRC = RC;
274+
}
275+
276+
return BestRC;
277+
}
278+
242279
/// getAllocatableSetForRC - Toggle the bits that represent allocatable
243280
/// registers for the specific register class.
244281
static void getAllocatableSetForRC(const MachineFunction &MF,

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,11 @@ void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
564564
}
565565

566566
// VR->VR copies.
567-
static const TargetRegisterClass *RVVRegClasses[] = {
568-
&RISCV::VRRegClass, &RISCV::VRM2RegClass, &RISCV::VRM4RegClass,
569-
&RISCV::VRM8RegClass, &RISCV::VRN2M1RegClass, &RISCV::VRN2M2RegClass,
570-
&RISCV::VRN2M4RegClass, &RISCV::VRN3M1RegClass, &RISCV::VRN3M2RegClass,
571-
&RISCV::VRN4M1RegClass, &RISCV::VRN4M2RegClass, &RISCV::VRN5M1RegClass,
572-
&RISCV::VRN6M1RegClass, &RISCV::VRN7M1RegClass, &RISCV::VRN8M1RegClass};
573-
for (const auto &RegClass : RVVRegClasses) {
574-
if (RegClass->contains(DstReg, SrcReg)) {
575-
copyPhysRegVector(MBB, MBBI, DL, DstReg, SrcReg, KillSrc, RegClass);
576-
return;
577-
}
567+
const TargetRegisterClass *RegClass =
568+
TRI->getCommonMinimalPhysRegClass(SrcReg, DstReg);
569+
if (RISCVRegisterInfo::isRVVRegClass(RegClass)) {
570+
copyPhysRegVector(MBB, MBBI, DL, DstReg, SrcReg, KillSrc, RegClass);
571+
return;
578572
}
579573

580574
llvm_unreachable("Impossible reg-to-reg copy");

0 commit comments

Comments
 (0)