[llvm] [AMDGPU] Post-RA Peephole for Two-Address Instructions (PR #207731)
Carl Ritson via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 15 00:04:43 PDT 2026
================
@@ -763,13 +802,185 @@ MachineInstrBuilder SIPreEmitPeephole::createUnpackedMI(MachineInstr &I,
return NewMI;
}
+bool SIPreEmitPeephole::isLateThreeAddrPeepholeCandidate(
+ const MachineInstr &MI) const {
+ // This is a subset of the opcodes that are convertible to three-address
+ // instructions according to . This is intended. For some cases,
+ // convertToThreeAddress could early-clobber, which would require us to roll
+ // back the transformation. However, rollback is not side-effect free for some
+ // cases in convertToThreeAddress that fold immediates. Therefore, we limit
+ // this peephole to only the instructions for which rollback will not be
+ // necessary.
+ switch (MI.getOpcode()) {
+ case AMDGPU::V_MAC_F16_e32:
+ case AMDGPU::V_MAC_F16_e64:
+ case AMDGPU::V_MAC_F32_e32:
+ case AMDGPU::V_MAC_F32_e64:
+ case AMDGPU::V_MAC_LEGACY_F32_e32:
+ case AMDGPU::V_MAC_LEGACY_F32_e64:
+ case AMDGPU::V_FMAC_F16_e32:
+ case AMDGPU::V_FMAC_F16_e64:
+ case AMDGPU::V_FMAC_F16_t16_e64:
+ case AMDGPU::V_FMAC_F16_fake16_e64:
+ case AMDGPU::V_FMAC_F32_e32:
+ case AMDGPU::V_FMAC_F32_e64:
+ case AMDGPU::V_FMAC_LEGACY_F32_e32:
+ case AMDGPU::V_FMAC_LEGACY_F32_e64:
+ case AMDGPU::V_FMAC_F64_e32:
+ case AMDGPU::V_FMAC_F64_e64:
+ return true;
+ default:
+ return false;
+ }
+}
+
+Register SIPreEmitPeephole::checkCopy(MachineInstr &TwoAddress,
+ MachineInstr &MaybeCopy, Register Src,
+ const TargetRegisterClass *NewDstRC) {
+ if (MaybeCopy.isBundle())
+ return Register();
+
+ if (!TII->isFoldableCopy(MaybeCopy) ||
+ MaybeCopy.getOpcode() == AMDGPU::WWM_COPY)
+ return Register();
+
+ if (TII->hasAnyModifiersSet(MaybeCopy))
+ return Register();
+
+ auto CopySrc = MaybeCopy.getOperand(TII->getFoldableCopySrcIdx(MaybeCopy));
+ if (!CopySrc.isReg() || CopySrc.getReg() != Src ||
+ CopySrc.getSubReg() != AMDGPU::NoSubRegister)
+ return Register();
+
+ MachineOperand Dst = MaybeCopy.getOperand(0);
+ if (!Dst.isReg() || Dst.getSubReg() != AMDGPU::NoSubRegister)
+ return Register();
+
+ Register DstReg = Dst.getReg();
+ if (TRI->getPhysRegBaseClass(DstReg) != TRI->getPhysRegBaseClass(Src))
+ return Register();
+
+ if (NewDstRC && !NewDstRC->contains(DstReg.asMCReg()))
----------------
perlfu wrote:
This is a non-obvious behaviour, that `NewDstRC` can be null to avoid checking. It should probably be in the comment that describes the function.
Or this check should be hoisted out of this function to where it matters.
Also, do we need `asMCReg` here?
https://github.com/llvm/llvm-project/pull/207731
More information about the llvm-commits
mailing list