Skip to content

Commit ea9acc9

Browse files
authored
CodeGen: Surface shouldRewriteCopySrc utility function (#158524)
Change shouldRewriteCopySrc to return the common register class and expose it as a utility function. I've found myself reproducing essentially the same logic in multiple places. The purpose of this function is to jsut work through the API constraints of which combination of register class and subreg indexes you have. i.e. you need to use a different function if you have 0, 1, or 2 subregister indexes involved in a pair of copy-like operations.
1 parent 64dba81 commit ea9acc9

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,31 @@ class LLVM_ABI TargetRegisterInfo : public MCRegisterInfo {
678678
getMatchingSuperRegClass(const TargetRegisterClass *A,
679679
const TargetRegisterClass *B, unsigned Idx) const;
680680

681+
/// Find a common register class that can accomodate both the source and
682+
/// destination operands of a copy-like instruction:
683+
///
684+
/// DefRC:DefSubReg = COPY SrcRC:SrcSubReg
685+
///
686+
/// This is a generalized form of getMatchingSuperRegClass,
687+
/// getCommonSuperRegClass, and getCommonSubClass which handles 0, 1, or 2
688+
/// subregister indexes. Those utilities should be preferred if the number of
689+
/// non-0 subregister indexes is known.
690+
const TargetRegisterClass *
691+
findCommonRegClass(const TargetRegisterClass *DefRC, unsigned DefSubReg,
692+
const TargetRegisterClass *SrcRC,
693+
unsigned SrcSubReg) const;
694+
681695
// For a copy-like instruction that defines a register of class DefRC with
682696
// subreg index DefSubReg, reading from another source with class SrcRC and
683697
// subregister SrcSubReg return true if this is a preferable copy
684698
// instruction or an earlier use should be used.
685699
virtual bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
686700
unsigned DefSubReg,
687701
const TargetRegisterClass *SrcRC,
688-
unsigned SrcSubReg) const;
702+
unsigned SrcSubReg) const {
703+
// If this source does not incur a cross register bank copy, use it.
704+
return findCommonRegClass(DefRC, DefSubReg, SrcRC, SrcSubReg) != nullptr;
705+
}
689706

690707
/// Returns the largest legal sub-class of RC that
691708
/// supports the sub-register index Idx.

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -412,25 +412,21 @@ getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
412412
return BestRC;
413413
}
414414

415-
/// Check if the registers defined by the pair (RegisterClass, SubReg)
416-
/// share the same register file.
417-
static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
418-
const TargetRegisterClass *DefRC,
419-
unsigned DefSubReg,
420-
const TargetRegisterClass *SrcRC,
421-
unsigned SrcSubReg) {
415+
const TargetRegisterClass *TargetRegisterInfo::findCommonRegClass(
416+
const TargetRegisterClass *DefRC, unsigned DefSubReg,
417+
const TargetRegisterClass *SrcRC, unsigned SrcSubReg) const {
422418
// Same register class.
423419
//
424420
// When processing uncoalescable copies / bitcasts, it is possible we reach
425421
// here with the same register class, but mismatched subregister indices.
426422
if (DefRC == SrcRC && DefSubReg == SrcSubReg)
427-
return true;
423+
return DefRC;
428424

429425
// Both operands are sub registers. Check if they share a register class.
430426
unsigned SrcIdx, DefIdx;
431427
if (SrcSubReg && DefSubReg) {
432-
return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
433-
SrcIdx, DefIdx) != nullptr;
428+
return getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg, SrcIdx,
429+
DefIdx);
434430
}
435431

436432
// At most one of the register is a sub register, make it Src to avoid
@@ -442,18 +438,10 @@ static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
442438

443439
// One of the register is a sub register, check if we can get a superclass.
444440
if (SrcSubReg)
445-
return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
441+
return getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg);
446442

447443
// Plain copy.
448-
return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
449-
}
450-
451-
bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
452-
unsigned DefSubReg,
453-
const TargetRegisterClass *SrcRC,
454-
unsigned SrcSubReg) const {
455-
// If this source does not incur a cross register bank copy, use it.
456-
return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
444+
return getCommonSubClass(DefRC, SrcRC);
457445
}
458446

459447
float TargetRegisterInfo::getSpillWeightScaleFactor(

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -710,22 +710,9 @@ bool SIFoldOperandsImpl::updateOperand(FoldCandidate &Fold) const {
710710
// Verify the register is compatible with the operand.
711711
if (const TargetRegisterClass *OpRC =
712712
TII->getRegClass(MI->getDesc(), Fold.UseOpNo, TRI)) {
713-
const TargetRegisterClass *OldRC = MRI->getRegClass(Old.getReg());
714713
const TargetRegisterClass *NewRC = MRI->getRegClass(New->getReg());
715-
unsigned NewSubReg = New->getSubReg();
716-
unsigned OldSubReg = Old.getSubReg();
717-
718-
const TargetRegisterClass *ConstrainRC = OpRC;
719-
if (NewSubReg && OldSubReg) {
720-
unsigned PreA, PreB;
721-
ConstrainRC = TRI->getCommonSuperRegClass(OpRC, OldSubReg, NewRC,
722-
NewSubReg, PreA, PreB);
723-
} else if (OldSubReg) {
724-
ConstrainRC = TRI->getMatchingSuperRegClass(OldRC, OpRC, OldSubReg);
725-
} else if (NewSubReg) {
726-
ConstrainRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, NewSubReg);
727-
}
728-
714+
const TargetRegisterClass *ConstrainRC =
715+
TRI->findCommonRegClass(OpRC, Old.getSubReg(), NewRC, New->getSubReg());
729716
if (!ConstrainRC)
730717
return false;
731718

0 commit comments

Comments
 (0)