Skip to content

Commit

Permalink
[CodeGen] Update LiveIntervals in TargetInstrInfo::convertToThreeAddress
Browse files Browse the repository at this point in the history
Delegate updating of LiveIntervals to each target's
convertToThreeAddress implementation, instead of repairing LiveIntervals
after the fact in TwoAddressInstruction::convertInstTo3Addr.

Differential Revision: https://reviews.llvm.org/D113493
  • Loading branch information
jayfoad committed Nov 17, 2021
1 parent 1ed5a90 commit 3264e95
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 76 deletions.
6 changes: 5 additions & 1 deletion llvm/include/llvm/CodeGen/TargetInstrInfo.h
Expand Up @@ -411,8 +411,12 @@ class TargetInstrInfo : public MCInstrInfo {
/// This method returns a null pointer if the transformation cannot be
/// performed, otherwise it returns the last new instruction.
///
/// If \p LIS is not nullptr, the LiveIntervals info should be updated for
/// replacing \p MI with new instructions, even though this function does not
/// remove MI.
virtual MachineInstr *convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const {
LiveVariables *LV,
LiveIntervals *LIS) const {
return nullptr;
}

Expand Down
22 changes: 1 addition & 21 deletions llvm/lib/CodeGen/TwoAddressInstructionPass.cpp
Expand Up @@ -676,7 +676,7 @@ bool TwoAddressInstructionPass::convertInstTo3Addr(
MachineBasicBlock::iterator &mi, MachineBasicBlock::iterator &nmi,
Register RegA, Register RegB, unsigned &Dist) {
MachineInstrSpan MIS(mi, MBB);
MachineInstr *NewMI = TII->convertToThreeAddress(*mi, LV);
MachineInstr *NewMI = TII->convertToThreeAddress(*mi, LV, LIS);
if (!NewMI)
return false;

Expand All @@ -701,28 +701,8 @@ bool TwoAddressInstructionPass::convertInstTo3Addr(
std::make_pair(NewInstrNum, NewIdx));
}

// If convertToThreeAddress created a single new instruction, assume it has
// exactly the same effect on liveness as the old instruction. This is much
// more efficient than calling repairIntervalsInRange.
bool SingleInst = std::next(MIS.begin(), 2) == MIS.end();
if (LIS && SingleInst)
LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);

SmallVector<Register> OrigRegs;
if (LIS && !SingleInst) {
for (const MachineOperand &MO : mi->operands()) {
if (MO.isReg())
OrigRegs.push_back(MO.getReg());
}

LIS->RemoveMachineInstrFromMaps(*mi);
}

MBB->erase(mi); // Nuke the old inst.

if (LIS && !SingleInst)
LIS->repairIntervalsInRange(MBB, MIS.begin(), MIS.end(), OrigRegs);

for (MachineInstr &MI : MIS)
DistanceMap.insert(std::make_pair(&MI, Dist++));
Dist--;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
Expand Up @@ -1392,7 +1392,7 @@ bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
// Use of output modifiers forces VOP3 encoding for a VOP2 mac/fmac
// instruction, so we might as well convert it to the more flexible VOP3-only
// mad/fma form.
if (TII->convertToThreeAddress(*Def, nullptr))
if (TII->convertToThreeAddress(*Def, nullptr, nullptr))
Def->eraseFromParent();

return true;
Expand Down Expand Up @@ -1537,7 +1537,7 @@ bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
// Use of output modifiers forces VOP3 encoding for a VOP2 mac/fmac
// instruction, so we might as well convert it to the more flexible VOP3-only
// mad/fma form.
if (TII->convertToThreeAddress(*Def, nullptr))
if (TII->convertToThreeAddress(*Def, nullptr, nullptr))
Def->eraseFromParent();

return true;
Expand Down
12 changes: 11 additions & 1 deletion llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
Expand Up @@ -19,6 +19,7 @@
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
#include "SIMachineFunctionInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/LiveVariables.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineScheduler.h"
Expand Down Expand Up @@ -3122,7 +3123,8 @@ static void updateLiveVariables(LiveVariables *LV, MachineInstr &MI,
}

MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const {
LiveVariables *LV,
LiveIntervals *LIS) const {
unsigned Opc = MI.getOpcode();
bool IsF16 = false;
bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64 ||
Expand Down Expand Up @@ -3190,6 +3192,8 @@ MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
.add(*Src1)
.addImm(Imm);
updateLiveVariables(LV, MI, *MIB);
if (LIS)
LIS->ReplaceMachineInstrInMaps(MI, *MIB);
return MIB;
}
}
Expand All @@ -3204,6 +3208,8 @@ MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
.addImm(Imm)
.add(*Src2);
updateLiveVariables(LV, MI, *MIB);
if (LIS)
LIS->ReplaceMachineInstrInMaps(MI, *MIB);
return MIB;
}
}
Expand All @@ -3218,6 +3224,8 @@ MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
.addImm(Imm)
.add(*Src2);
updateLiveVariables(LV, MI, *MIB);
if (LIS)
LIS->ReplaceMachineInstrInMaps(MI, *MIB);
return MIB;
}
}
Expand All @@ -3241,6 +3249,8 @@ MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
.addImm(Clamp ? Clamp->getImm() : 0)
.addImm(Omod ? Omod->getImm() : 0);
updateLiveVariables(LV, MI, *MIB);
if (LIS)
LIS->ReplaceMachineInstrInMaps(MI, *MIB);
return MIB;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/SIInstrInfo.h
Expand Up @@ -340,8 +340,8 @@ class SIInstrInfo final : public AMDGPUGenInstrInfo {

unsigned getMachineCSELookAheadLimit() const override { return 500; }

MachineInstr *convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const override;
MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const override;

bool isSchedulingBoundary(const MachineInstr &MI,
const MachineBasicBlock *MBB,
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
Expand Up @@ -173,8 +173,9 @@ CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
return MHR;
}

MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const {
MachineInstr *
ARMBaseInstrInfo::convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const {
// FIXME: Thumb2 support.

if (!EnableARM3Addr)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/ARM/ARMBaseInstrInfo.h
Expand Up @@ -120,8 +120,8 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
// if there is not such an opcode.
virtual unsigned getUnindexedOpcode(unsigned Opc) const = 0;

MachineInstr *convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const override;
MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const override;

virtual const ARMBaseRegisterInfo &getRegisterInfo() const = 0;
const ARMSubtarget &getSubtarget() const { return Subtarget; }
Expand Down
18 changes: 17 additions & 1 deletion llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/LiveVariables.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
Expand Down Expand Up @@ -1674,7 +1675,8 @@ MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI,
CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4)

MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const {
LiveVariables *LV,
LiveIntervals *LIS) const {
switch (MI.getOpcode()) {
default:
break;
Expand Down Expand Up @@ -1716,6 +1718,20 @@ MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI,
}
}

if (LIS) {
SlotIndex Idx = LIS->ReplaceMachineInstrInMaps(MI, *MIB);

if (MI.getOperand(0).isEarlyClobber()) {
// Use operand 1 was tied to early-clobber def operand 0, so its live
// interval could have ended at an early-clobber slot. Now they are not
// tied we need to update it to the normal register slot.
LiveInterval &LI = LIS->getInterval(MI.getOperand(1).getReg());
LiveRange::Segment *S = LI.getSegmentContaining(Idx);
if (S->end == Idx.getRegSlot(true))
S->end = Idx.getRegSlot();
}
}

return MIB;
}
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.h
Expand Up @@ -160,8 +160,8 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
unsigned OpIdx1,
unsigned OpIdx2) const override;

MachineInstr *convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const override;
MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const override;

Register getVLENFactoredAmount(
MachineFunction &MF, MachineBasicBlock &MBB,
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp
Expand Up @@ -943,8 +943,9 @@ static void transferMIFlag(MachineInstr *OldMI, MachineInstr *NewMI,
NewMI->setFlag(Flag);
}

MachineInstr *SystemZInstrInfo::convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const {
MachineInstr *
SystemZInstrInfo::convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const {
MachineBasicBlock *MBB = MI.getParent();

// Try to convert an AND into an RISBG-type instruction.
Expand Down Expand Up @@ -985,6 +986,8 @@ MachineInstr *SystemZInstrInfo::convertToThreeAddress(MachineInstr &MI,
LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
}
}
if (LIS)
LIS->ReplaceMachineInstrInMaps(MI, *MIB);
transferDeadCC(&MI, MIB);
return MIB;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/SystemZ/SystemZInstrInfo.h
Expand Up @@ -272,8 +272,8 @@ class SystemZInstrInfo : public SystemZGenInstrInfo {
Register DestReg, int FrameIdx,
const TargetRegisterClass *RC,
const TargetRegisterInfo *TRI) const override;
MachineInstr *convertToThreeAddress(MachineInstr &MI,
LiveVariables *LV) const override;
MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const override;
MachineInstr *
foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
ArrayRef<unsigned> Ops,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86FixupLEAs.cpp
Expand Up @@ -212,7 +212,7 @@ FixupLEAPass::postRAConvertToLEA(MachineBasicBlock &MBB,
// These instructions are all fine to convert.
break;
}
return TII->convertToThreeAddress(MI, nullptr);
return TII->convertToThreeAddress(MI, nullptr, nullptr);
}

FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Expand Down

0 comments on commit 3264e95

Please sign in to comment.