diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h index b5b83c7ff1164..d801842e23b0f 100644 --- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h +++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h @@ -418,6 +418,23 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo { return true; } + /// Returns true if CopyMI should be considered for register + /// definition rematerialization. Otherwise, returns false. + /// + /// Rematerialization can replace a source register with its value + /// from its definition. Its applied in the register coalescer, + /// after instruction selection and before register allocation. + /// + /// Subtargets can override this method to classify rematerialization + /// candidates. Note that this cannot be defined in tablegen because it + /// operates at a higher level. + virtual bool shouldReMaterializeTrivialRegDef( + const MachineFunction *MF, const MachineInstr &CopyMI, + const Register &DestReg, const Register &SrcReg, + const LiveIntervals *LIS) const { + return true; + } + /// Re-issue the specified 'original' instruction at the /// specific location targeting a new destination register. /// The register in Orig->getOperand(0).getReg() will be substituted by diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp index c1474773faa76..7206c9aea26cb 100644 --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -1029,6 +1029,14 @@ bool AArch64InstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const { } } +bool AArch64InstrInfo::shouldReMaterializeTrivialRegDef( + const MachineFunction *MF, const MachineInstr &CopyMI, + const Register &DestReg, const Register &SrcReg, + const LiveIntervals *LIS) const { + return !Subtarget.canLowerToZeroCycleRegMove(CopyMI, DestReg, SrcReg) && + !Subtarget.canLowerToZeroCycleRegZeroing(CopyMI, DestReg, SrcReg); +} + bool AArch64InstrInfo::isFalkorShiftExtFast(const MachineInstr &MI) { switch (MI.getOpcode()) { default: @@ -5025,6 +5033,9 @@ void AArch64InstrInfo::copyGPRRegTuple(MachineBasicBlock &MBB, } } +/// NOTE: must maintain consistency with +/// `AArch64Subtarget::canLowerToZeroCycleRegMove` and +/// `AArch64Subtarget::canLowerToZeroCycleRegZeroing`. void AArch64InstrInfo::copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h index 7c255da333e4b..100183e086c3e 100644 --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h @@ -189,6 +189,11 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo { bool isAsCheapAsAMove(const MachineInstr &MI) const override; + bool shouldReMaterializeTrivialRegDef( + const MachineFunction *MF, const MachineInstr &CopyMI, + const Register &DestReg, const Register &SrcReg, + const LiveIntervals *LIS) const override; + bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg, Register &DstReg, unsigned &SubIdx) const override;