Skip to content

Commit

Permalink
Fix COPY folding.
Browse files Browse the repository at this point in the history
This causes some regressions, but optimizes many more cases.
  • Loading branch information
jacobly0 committed Jun 12, 2022
1 parent 580c30d commit 150e666
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 277 deletions.
24 changes: 22 additions & 2 deletions llvm/lib/Target/Z80/Z80InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,26 @@ MachineInstr *Z80InstrInfo::optimizeLoadInstr(MachineInstr &MI,
return nullptr;
}

bool Z80InstrInfo::isRegisterOperandSubClassEq(
const MachineOperand &RegMO, const TargetRegisterClass &RC) const {
assert(RegMO.isReg() && "Expected a register operand.");

const TargetRegisterInfo &TRI = getRegisterInfo();
const MachineInstr &MI = *RegMO.getParent();
const MachineFunction &MF = *MI.getMF();
const MachineRegisterInfo &MRI = MF.getRegInfo();

Register Reg = RegMO.getReg();
if (Reg.isPhysical())
return RC.contains(Reg);

assert(Reg.isVirtual() && "Expected a physical or virtual register.");
const TargetRegisterClass &RegRC = *MRI.getRegClass(Reg);
if (unsigned SubReg = RegMO.getSubReg())
return TRI.getMatchingSuperRegClass(&RegRC, &RC, SubReg) == &RegRC;
return RC.hasSubClassEq(&RegRC);
}

void Z80InstrInfo::updateOperandRegConstraints(MachineFunction &MF,
MachineInstr &NewMI) const {
MachineRegisterInfo &MRI = MF.getRegInfo();
Expand Down Expand Up @@ -1889,7 +1909,7 @@ MachineInstr *Z80InstrInfo::foldMemoryOperandImpl(
Opc = Z80::TST8ap;
break;
case TargetOpcode::COPY:
if (!Z80::R8RegClass.contains(MI.getOperand(1).getReg()))
if (!isRegisterOperandSubClassEq(MI.getOperand(1), Z80::R8RegClass))
return nullptr;
Opc = IsOff ? Z80::LD8or : Z80::LD8pr;
break;
Expand All @@ -1899,7 +1919,7 @@ MachineInstr *Z80InstrInfo::foldMemoryOperandImpl(
switch (MI.getOpcode()) {
default: return nullptr;
case TargetOpcode::COPY:
if (!Z80::R8RegClass.contains(MI.getOperand(0).getReg()))
if (!isRegisterOperandSubClassEq(MI.getOperand(0), Z80::R8RegClass))
return nullptr;
Opc = IsOff ? Z80::LD8ro : Z80::LD8rp;
break;
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/Z80/Z80InstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class Z80InstrInfo final : public Z80GenInstrInfo {
LiveIntervals *LIS = nullptr) const override;

private:
bool isRegisterOperandSubClassEq(const MachineOperand &RegMO,
const TargetRegisterClass &RC) const;
void updateOperandRegConstraints(MachineFunction &MF,
MachineInstr &NewMI) const;
MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
Expand Down
Loading

0 comments on commit 150e666

Please sign in to comment.