Skip to content

Commit

Permalink
Revert "[CodeGen]Allow targets to use target specific COPY instructio…
Browse files Browse the repository at this point in the history
…ns for live range splitting"

And dependent commits.

Details in D150388.

This reverts commit 825b7f0.
This reverts commit 7a98f08.
This reverts commit b4a62b1.
This reverts commit b7836d8.

No conflicts in the code, few tests had conflicts in autogenerated CHECKs:
llvm/test/CodeGen/Thumb2/mve-float32regloops.ll
llvm/test/CodeGen/AMDGPU/fix-frame-reg-in-custom-csr-spills.ll

Reviewed By: alexfh

Differential Revision: https://reviews.llvm.org/D156381
  • Loading branch information
vitalybuka committed Jul 27, 2023
1 parent fa140fe commit a496c8b
Show file tree
Hide file tree
Showing 106 changed files with 5,802 additions and 7,777 deletions.
17 changes: 0 additions & 17 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Expand Up @@ -1044,16 +1044,6 @@ class TargetInstrInfo : public MCInstrInfo {
return isCopyInstrImpl(MI);
}

bool isFullCopyInstr(const MachineInstr &MI) const {
auto DestSrc = isCopyInstr(MI);
if (!DestSrc)
return false;

const MachineOperand *DestRegOp = DestSrc->Destination;
const MachineOperand *SrcRegOp = DestSrc->Source;
return !DestRegOp->getSubReg() && !SrcRegOp->getSubReg();
}

/// If the specific machine instruction is an instruction that adds an
/// immediate value and a physical register, and stores the result in
/// the given physical register \c Reg, return a pair of the source
Expand Down Expand Up @@ -1968,13 +1958,6 @@ class TargetInstrInfo : public MCInstrInfo {
return false;
}

/// Allows targets to use appropriate copy instruction while spilitting live
/// range of a register in register allocation.
virtual unsigned getLiveRangeSplitOpcode(Register Reg,
const MachineFunction &MF) const {
return TargetOpcode::COPY;
}

/// During PHI eleimination lets target to make necessary checks and
/// insert the copy to the PHI destination register in a target specific
/// manner.
Expand Down
15 changes: 3 additions & 12 deletions llvm/lib/CodeGen/CalcSpillWeights.cpp
Expand Up @@ -97,7 +97,7 @@ bool VirtRegAuxInfo::isRematerializable(const LiveInterval &LI,
// Trace copies introduced by live range splitting. The inline
// spiller can rematerialize through these copies, so the spill
// weight must reflect this.
while (TII.isFullCopyInstr(*MI)) {
while (MI->isFullCopy()) {
// The copy destination must match the interval register.
if (MI->getOperand(0).getReg() != Reg)
return false;
Expand Down Expand Up @@ -224,16 +224,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
continue;

NumInstr++;
bool identityCopy = false;
auto DestSrc = TII.isCopyInstr(*MI);
if (DestSrc) {
const MachineOperand *DestRegOp = DestSrc->Destination;
const MachineOperand *SrcRegOp = DestSrc->Source;
identityCopy = DestRegOp->getReg() == SrcRegOp->getReg() &&
DestRegOp->getSubReg() == SrcRegOp->getSubReg();
}

if (identityCopy || MI->isImplicitDef())
if (MI->isIdentityCopy() || MI->isImplicitDef())
continue;
if (!Visited.insert(MI).second)
continue;
Expand Down Expand Up @@ -267,7 +258,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
}

// Get allocation hints from copies.
if (!TII.isCopyInstr(*MI))
if (!MI->isCopy())
continue;
Register HintReg = copyHint(MI, LI.reg(), TRI, MRI);
if (!HintReg)
Expand Down
34 changes: 16 additions & 18 deletions llvm/lib/CodeGen/InlineSpiller.cpp
Expand Up @@ -256,11 +256,11 @@ Spiller *llvm::createInlineSpiller(MachineFunctionPass &Pass,
// This minimizes register pressure and maximizes the store-to-load distance for
// spill slots which can be important in tight loops.

/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
/// otherwise return 0.
static Register isCopyOf(const MachineInstr &MI, Register Reg,
const TargetInstrInfo &TII) {
if (!TII.isCopyInstr(MI))
/// If MI is a COPY to or from Reg, return the other register, otherwise return
/// 0.
static Register isCopyOf(const MachineInstr &MI, Register Reg) {
assert(!MI.isBundled());
if (!MI.isCopy())
return Register();

const MachineOperand &DstOp = MI.getOperand(0);
Expand All @@ -277,10 +277,9 @@ static Register isCopyOf(const MachineInstr &MI, Register Reg,
}

/// Check for a copy bundle as formed by SplitKit.
static Register isCopyOfBundle(const MachineInstr &FirstMI, Register Reg,
const TargetInstrInfo &TII) {
static Register isCopyOfBundle(const MachineInstr &FirstMI, Register Reg) {
if (!FirstMI.isBundled())
return isCopyOf(FirstMI, Reg, TII);
return isCopyOf(FirstMI, Reg);

assert(!FirstMI.isBundledWithPred() && FirstMI.isBundledWithSucc() &&
"expected to see first instruction in bundle");
Expand All @@ -289,12 +288,11 @@ static Register isCopyOfBundle(const MachineInstr &FirstMI, Register Reg,
MachineBasicBlock::const_instr_iterator I = FirstMI.getIterator();
while (I->isBundledWithSucc()) {
const MachineInstr &MI = *I;
auto CopyInst = TII.isCopyInstr(MI);
if (!CopyInst)
if (!MI.isCopy())
return Register();

const MachineOperand &DstOp = *CopyInst->Destination;
const MachineOperand &SrcOp = *CopyInst->Source;
const MachineOperand &DstOp = MI.getOperand(0);
const MachineOperand &SrcOp = MI.getOperand(1);
if (DstOp.getReg() == Reg) {
if (!SnipReg)
SnipReg = SrcOp.getReg();
Expand Down Expand Up @@ -360,7 +358,7 @@ bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
MachineInstr &MI = *RI++;

// Allow copies to/from Reg.
if (isCopyOfBundle(MI, Reg, TII))
if (isCopyOfBundle(MI, Reg))
continue;

// Allow stack slot loads.
Expand Down Expand Up @@ -398,7 +396,7 @@ void InlineSpiller::collectRegsToSpill() {
return;

for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
Register SnipReg = isCopyOfBundle(MI, Reg, TII);
Register SnipReg = isCopyOfBundle(MI, Reg);
if (!isSibling(SnipReg))
continue;
LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Expand Down Expand Up @@ -521,14 +519,14 @@ void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
// Find all spills and copies of VNI.
for (MachineInstr &MI :
llvm::make_early_inc_range(MRI.use_nodbg_bundles(Reg))) {
if (!MI.mayStore() && !TII.isCopyInstr(MI))
if (!MI.isCopy() && !MI.mayStore())
continue;
SlotIndex Idx = LIS.getInstructionIndex(MI);
if (LI->getVNInfoAt(Idx) != VNI)
continue;

// Follow sibling copies down the dominator tree.
if (Register DstReg = isCopyOfBundle(MI, Reg, TII)) {
if (Register DstReg = isCopyOfBundle(MI, Reg)) {
if (isSibling(DstReg)) {
LiveInterval &DstLI = LIS.getInterval(DstReg);
VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
Expand Down Expand Up @@ -872,7 +870,7 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
if (Ops.back().first != MI || MI->isBundled())
return false;

bool WasCopy = TII.isCopyInstr(*MI).has_value();
bool WasCopy = MI->isCopy();
Register ImpReg;

// TII::foldMemoryOperand will do what we need here for statepoint
Expand Down Expand Up @@ -1157,7 +1155,7 @@ void InlineSpiller::spillAroundUses(Register Reg) {
Idx = VNI->def;

// Check for a sibling copy.
Register SibReg = isCopyOfBundle(MI, Reg, TII);
Register SibReg = isCopyOfBundle(MI, Reg);
if (SibReg && isSibling(SibReg)) {
// This may actually be a copy between snippets.
if (isRegToSpill(SibReg)) {
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/LiveRangeEdit.cpp
Expand Up @@ -352,8 +352,7 @@ void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
// unlikely to change anything. We typically don't want to shrink the
// PIC base register that has lots of uses everywhere.
// Always shrink COPY uses that probably come from live range splitting.
if ((MI->readsVirtualRegister(Reg) &&
(MO.isDef() || TII.isCopyInstr(*MI))) ||
if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MO.isDef())) ||
(MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO))))
ToShrink.insert(&LI);
else if (MO.readsReg())
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/CodeGen/LiveRangeShrink.cpp
Expand Up @@ -23,7 +23,6 @@
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -110,7 +109,6 @@ bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {
return false;

MachineRegisterInfo &MRI = MF.getRegInfo();
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();

LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');

Expand Down Expand Up @@ -199,7 +197,7 @@ bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {
// is because it needs more accurate model to handle register
// pressure correctly.
MachineInstr &DefInstr = *MRI.def_instr_begin(Reg);
if (!TII.isCopyInstr(DefInstr))
if (!DefInstr.isCopy())
NumEligibleUse++;
Insert = FindDominatedInstruction(DefInstr, Insert, IOM);
} else {
Expand Down
21 changes: 9 additions & 12 deletions llvm/lib/CodeGen/RegAllocGreedy.cpp
Expand Up @@ -1282,12 +1282,10 @@ static LaneBitmask getInstReadLaneMask(const MachineRegisterInfo &MRI,
/// VirtReg.
static bool readsLaneSubset(const MachineRegisterInfo &MRI,
const MachineInstr *MI, const LiveInterval &VirtReg,
const TargetRegisterInfo *TRI, SlotIndex Use,
const TargetInstrInfo *TII) {
const TargetRegisterInfo *TRI, SlotIndex Use) {
// Early check the common case.
auto DestSrc = TII->isCopyInstr(*MI);
if (DestSrc &&
DestSrc->Destination->getSubReg() == DestSrc->Source->getSubReg())
if (MI->isCopy() &&
MI->getOperand(0).getSubReg() == MI->getOperand(1).getSubReg())
return false;

// FIXME: We're only considering uses, but should be consider defs too?
Expand Down Expand Up @@ -1346,14 +1344,14 @@ unsigned RAGreedy::tryInstructionSplit(const LiveInterval &VirtReg,
// the allocation.
for (const SlotIndex Use : Uses) {
if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Use)) {
if (TII->isFullCopyInstr(*MI) ||
if (MI->isFullCopy() ||
(SplitSubClass &&
SuperRCNumAllocatableRegs ==
getNumAllocatableRegsForConstraints(MI, VirtReg.reg(), SuperRC,
TII, TRI, RegClassInfo)) ||
// TODO: Handle split for subranges with subclass constraints?
(!SplitSubClass && VirtReg.hasSubRanges() &&
!readsLaneSubset(*MRI, MI, VirtReg, TRI, Use, TII))) {
!readsLaneSubset(*MRI, MI, VirtReg, TRI, Use))) {
LLVM_DEBUG(dbgs() << " skip:\t" << Use << '\t' << *MI);
continue;
}
Expand Down Expand Up @@ -2140,7 +2138,7 @@ void RAGreedy::initializeCSRCost() {
/// \p Out is not cleared before being populated.
void RAGreedy::collectHintInfo(Register Reg, HintsInfo &Out) {
for (const MachineInstr &Instr : MRI->reg_nodbg_instructions(Reg)) {
if (!TII->isFullCopyInstr(Instr))
if (!Instr.isFullCopy())
continue;
// Look for the other end of the copy.
Register OtherReg = Instr.getOperand(0).getReg();
Expand Down Expand Up @@ -2455,10 +2453,9 @@ RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) {
MI.getOpcode() == TargetOpcode::STATEPOINT;
};
for (MachineInstr &MI : MBB) {
auto DestSrc = TII->isCopyInstr(MI);
if (DestSrc) {
const MachineOperand &Dest = *DestSrc->Destination;
const MachineOperand &Src = *DestSrc->Source;
if (MI.isCopy()) {
const MachineOperand &Dest = MI.getOperand(0);
const MachineOperand &Src = MI.getOperand(1);
Register SrcReg = Src.getReg();
Register DestReg = Dest.getReg();
// Only count `COPY`s with a virtual register as source or destination.
Expand Down
17 changes: 7 additions & 10 deletions llvm/lib/CodeGen/SplitKit.cpp
Expand Up @@ -514,10 +514,10 @@ void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
VFP = ValueForcePair(nullptr, true);
}

SlotIndex SplitEditor::buildSingleSubRegCopy(
Register FromReg, Register ToReg, MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore, unsigned SubIdx,
LiveInterval &DestLI, bool Late, SlotIndex Def, const MCInstrDesc &Desc) {
SlotIndex SplitEditor::buildSingleSubRegCopy(Register FromReg, Register ToReg,
MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
bool FirstCopy = !Def.isValid();
MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
.addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
Expand All @@ -536,8 +536,7 @@ SlotIndex SplitEditor::buildSingleSubRegCopy(
SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
LaneBitmask LaneMask, MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
const MCInstrDesc &Desc =
TII.get(TII.getLiveRangeSplitOpcode(FromReg, *MBB.getParent()));
const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
SlotIndexes &Indexes = *LIS.getSlotIndexes();
if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
// The full vreg is copied.
Expand Down Expand Up @@ -565,7 +564,7 @@ SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
SlotIndex Def;
for (unsigned BestIdx : SubIndexes) {
Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
DestLI, Late, Def, Desc);
DestLI, Late, Def);
}

BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
Expand Down Expand Up @@ -1585,9 +1584,7 @@ bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
if (BI.LiveIn && BI.LiveOut)
return true;
// No point in isolating a copy. It has no register class constraints.
MachineInstr *MI = LIS.getInstructionFromIndex(BI.FirstInstr);
bool copyLike = TII.isCopyInstr(*MI) || MI->isSubregToReg();
if (copyLike)
if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
return false;
// Finally, don't isolate an end point that was created by earlier splits.
return isOriginalEndpoint(BI.FirstInstr);
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/CodeGen/SplitKit.h
Expand Up @@ -428,11 +428,8 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
bool Late, unsigned RegIdx);

SlotIndex buildSingleSubRegCopy(Register FromReg, Register ToReg,
MachineBasicBlock &MB,
MachineBasicBlock::iterator InsertBefore,
unsigned SubIdx, LiveInterval &DestLI,
bool Late, SlotIndex Def,
const MCInstrDesc &Desc);
MachineBasicBlock &MB, MachineBasicBlock::iterator InsertBefore,
unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def);

public:
/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/CodeGen/TargetInstrInfo.cpp
Expand Up @@ -440,9 +440,8 @@ MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
// If the COPY instruction in MI can be folded to a stack operation, return
// the register class to use.
static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
const TargetInstrInfo &TII,
unsigned FoldIdx) {
assert(TII.isCopyInstr(MI) && "MI must be a COPY instruction");
assert(MI.isCopy() && "MI must be a COPY instruction");
if (MI.getNumOperands() != 2)
return nullptr;
assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
Expand Down Expand Up @@ -631,10 +630,10 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
}

// Straight COPY may fold as load/store.
if (!isCopyInstr(MI) || Ops.size() != 1)
if (!MI.isCopy() || Ops.size() != 1)
return nullptr;

const TargetRegisterClass *RC = canFoldCopy(MI, *this, Ops[0]);
const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
if (!RC)
return nullptr;

Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/Target/AMDGPU/AMDGPU.h
Expand Up @@ -41,7 +41,6 @@ FunctionPass *createSIFixControlFlowLiveIntervalsPass();
FunctionPass *createSIOptimizeExecMaskingPreRAPass();
FunctionPass *createSIOptimizeVGPRLiveRangePass();
FunctionPass *createSIFixSGPRCopiesPass();
FunctionPass *createLowerWWMCopiesPass();
FunctionPass *createSIMemoryLegalizerPass();
FunctionPass *createSIInsertWaitcntsPass();
FunctionPass *createSIPreAllocateWWMRegsPass();
Expand Down Expand Up @@ -145,9 +144,6 @@ extern char &SIFixSGPRCopiesID;
void initializeSIFixVGPRCopiesPass(PassRegistry &);
extern char &SIFixVGPRCopiesID;

void initializeSILowerWWMCopiesPass(PassRegistry &);
extern char &SILowerWWMCopiesID;

void initializeSILowerI1CopiesPass(PassRegistry &);
extern char &SILowerI1CopiesID;

Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Expand Up @@ -364,7 +364,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
initializeAMDGPUDAGToDAGISelPass(*PR);
initializeGCNDPPCombinePass(*PR);
initializeSILowerI1CopiesPass(*PR);
initializeSILowerWWMCopiesPass(*PR);
initializeSILowerSGPRSpillsPass(*PR);
initializeSIFixSGPRCopiesPass(*PR);
initializeSIFixVGPRCopiesPass(*PR);
Expand Down Expand Up @@ -1297,7 +1296,6 @@ void GCNPassConfig::addOptimizedRegAlloc() {
}

bool GCNPassConfig::addPreRewrite() {
addPass(&SILowerWWMCopiesID);
if (EnableRegReassign)
addPass(&GCNNSAReassignID);
return true;
Expand Down Expand Up @@ -1352,8 +1350,6 @@ bool GCNPassConfig::addRegAssignAndRewriteFast() {
addPass(&SILowerSGPRSpillsID);

addPass(createVGPRAllocPass(false));

addPass(&SILowerWWMCopiesID);
return true;
}

Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/AMDGPU/CMakeLists.txt
Expand Up @@ -145,7 +145,6 @@ add_llvm_target(AMDGPUCodeGen
SILoadStoreOptimizer.cpp
SILowerControlFlow.cpp
SILowerI1Copies.cpp
SILowerWWMCopies.cpp
SILowerSGPRSpills.cpp
SIMachineFunctionInfo.cpp
SIMachineScheduler.cpp
Expand Down

0 comments on commit a496c8b

Please sign in to comment.