2012-01-07 11:02:36 +08:00
|
|
|
//===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-10-04 00:59:13 +08:00
|
|
|
// This is an extremely simple MachineInstr-level copy propagation pass.
|
2012-01-07 11:02:36 +08:00
|
|
|
//
|
2018-02-28 00:59:10 +08:00
|
|
|
// This pass forwards the source of COPYs to the users of their destinations
|
|
|
|
// when doing so is legal. For example:
|
|
|
|
//
|
|
|
|
// %reg1 = COPY %reg0
|
|
|
|
// ...
|
|
|
|
// ... = OP %reg1
|
|
|
|
//
|
|
|
|
// If
|
|
|
|
// - %reg0 has not been clobbered by the time of the use of %reg1
|
|
|
|
// - the register class constraints are satisfied
|
|
|
|
// - the COPY def is the only value that reaches OP
|
|
|
|
// then this pass replaces the above with:
|
|
|
|
//
|
|
|
|
// %reg1 = COPY %reg0
|
|
|
|
// ...
|
|
|
|
// ... = OP %reg0
|
|
|
|
//
|
|
|
|
// This pass also removes some redundant COPYs. For example:
|
|
|
|
//
|
|
|
|
// %R1 = COPY %R0
|
|
|
|
// ... // No clobber of %R1
|
|
|
|
// %R0 = COPY %R1 <<< Removed
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// %R1 = COPY %R0
|
|
|
|
// ... // No clobber of %R0
|
|
|
|
// %R1 = COPY %R0 <<< Removed
|
|
|
|
//
|
2012-01-07 11:02:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-10-16 05:57:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2018-02-28 00:59:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-02-28 00:59:10 +08:00
|
|
|
#include "llvm/Support/DebugCounter.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
#define DEBUG_TYPE "machine-cp"
|
2014-04-22 10:02:50 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
STATISTIC(NumDeletes, "Number of dead copies deleted");
|
2018-02-28 00:59:10 +08:00
|
|
|
STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
|
|
|
|
DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
|
|
|
|
"Controls which register COPYs are forwarded");
|
2012-01-07 11:02:36 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-08-30 06:32:07 +08:00
|
|
|
|
|
|
|
using RegList = SmallVector<unsigned, 4>;
|
|
|
|
using SourceMap = DenseMap<unsigned, RegList>;
|
|
|
|
using Reg2MIMap = DenseMap<unsigned, MachineInstr *>;
|
2016-02-26 11:18:50 +08:00
|
|
|
|
2018-09-21 08:08:33 +08:00
|
|
|
class MachineCopyPropagation : public MachineFunctionPass {
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
MachineCopyPropagation() : MachineFunctionPass(ID) {
|
|
|
|
initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ClobberRegister(unsigned Reg);
|
|
|
|
void ReadRegister(unsigned Reg);
|
|
|
|
void CopyPropagateBlock(MachineBasicBlock &MBB);
|
|
|
|
bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
|
|
|
|
void forwardUses(MachineInstr &MI);
|
|
|
|
bool isForwardableRegClassCopy(const MachineInstr &Copy,
|
|
|
|
const MachineInstr &UseI, unsigned UseIdx);
|
|
|
|
bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
|
|
|
|
|
|
|
|
/// Candidates for deletion.
|
|
|
|
SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
|
|
|
|
|
|
|
|
/// Def -> available copies map.
|
|
|
|
Reg2MIMap AvailCopyMap;
|
|
|
|
|
|
|
|
/// Def -> copies map.
|
|
|
|
Reg2MIMap CopyMap;
|
|
|
|
|
|
|
|
/// Src -> Def map
|
|
|
|
SourceMap SrcMap;
|
|
|
|
|
|
|
|
bool Changed;
|
|
|
|
};
|
2017-08-30 06:32:07 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
char MachineCopyPropagation::ID = 0;
|
2017-08-30 06:32:07 +08:00
|
|
|
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
|
2012-01-07 11:02:36 +08:00
|
|
|
"Machine Copy Propagation Pass", false, false)
|
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
/// Remove any entry in \p Map where the register is a subregister or equal to
|
|
|
|
/// a register contained in \p Regs.
|
|
|
|
static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
|
|
|
|
const TargetRegisterInfo &TRI) {
|
|
|
|
for (unsigned Reg : Regs) {
|
|
|
|
// Source of copy is no longer available for propagation.
|
|
|
|
for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
|
|
|
|
Map.erase(*SR);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
/// Remove any entry in \p Map that is marked clobbered in \p RegMask.
|
|
|
|
/// The map will typically have a lot fewer entries than the regmask clobbers,
|
|
|
|
/// so this is more efficient than iterating the clobbered registers and calling
|
|
|
|
/// ClobberRegister() on them.
|
|
|
|
static void removeClobberedRegsFromMap(Reg2MIMap &Map,
|
|
|
|
const MachineOperand &RegMask) {
|
|
|
|
for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
|
|
|
|
I = Next) {
|
|
|
|
Next = std::next(I);
|
|
|
|
unsigned Reg = I->first;
|
|
|
|
if (RegMask.clobbersPhysReg(Reg))
|
|
|
|
Map.erase(I);
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 03:52:28 +08:00
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
|
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
|
|
|
|
CopyMap.erase(*AI);
|
|
|
|
AvailCopyMap.erase(*AI);
|
|
|
|
|
|
|
|
SourceMap::iterator SI = SrcMap.find(*AI);
|
|
|
|
if (SI != SrcMap.end()) {
|
|
|
|
removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
|
|
|
|
SrcMap.erase(SI);
|
|
|
|
}
|
2012-01-09 03:52:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 10:27:20 +08:00
|
|
|
void MachineCopyPropagation::ReadRegister(unsigned Reg) {
|
|
|
|
// If 'Reg' is defined by a copy, the copy is no longer a candidate
|
|
|
|
// for elimination.
|
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
|
|
|
|
Reg2MIMap::iterator CI = CopyMap.find(*AI);
|
|
|
|
if (CI != CopyMap.end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: ";
|
|
|
|
CI->second->dump());
|
2017-02-04 10:27:20 +08:00
|
|
|
MaybeDeadCopies.remove(CI->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
|
|
|
|
/// This fact may have been obscured by sub register usage or may not be true at
|
|
|
|
/// all even though Src and Def are subregisters of the registers used in
|
|
|
|
/// PreviousCopy. e.g.
|
|
|
|
/// isNopCopy("ecx = COPY eax", AX, CX) == true
|
|
|
|
/// isNopCopy("ecx = COPY eax", AH, CL) == false
|
|
|
|
static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
|
|
|
|
unsigned Def, const TargetRegisterInfo *TRI) {
|
|
|
|
unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
|
|
|
|
unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
|
|
|
|
if (Src == PreviousSrc) {
|
|
|
|
assert(Def == PreviousDef);
|
2012-02-21 07:28:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-26 11:18:55 +08:00
|
|
|
if (!TRI->isSubRegister(PreviousSrc, Src))
|
|
|
|
return false;
|
|
|
|
unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
|
|
|
|
return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove instruction \p Copy if there exists a previous copy that copies the
|
|
|
|
/// register \p Src to the register \p Def; This may happen indirectly by
|
|
|
|
/// copying the super registers.
|
|
|
|
bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
|
|
|
|
unsigned Def) {
|
|
|
|
// Avoid eliminating a copy from/to a reserved registers as we cannot predict
|
|
|
|
// the value (Example: The sparc zero register is writable but stays zero).
|
|
|
|
if (MRI->isReserved(Src) || MRI->isReserved(Def))
|
|
|
|
return false;
|
2012-02-21 07:28:17 +08:00
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
// Search for an existing copy.
|
|
|
|
Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
|
|
|
|
if (CI == AvailCopyMap.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the existing copy uses the correct sub registers.
|
|
|
|
MachineInstr &PrevCopy = *CI->second;
|
2017-11-10 20:21:10 +08:00
|
|
|
if (PrevCopy.getOperand(0).isDead())
|
|
|
|
return false;
|
2016-02-26 11:18:55 +08:00
|
|
|
if (!isNopCopy(PrevCopy, Src, Def, TRI))
|
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
|
2016-02-26 11:18:55 +08:00
|
|
|
|
|
|
|
// Copy was redundantly redefining either Src or Def. Remove earlier kill
|
|
|
|
// flags between Copy and PrevCopy because the value will be reused now.
|
|
|
|
assert(Copy.isCopy());
|
|
|
|
unsigned CopyDef = Copy.getOperand(0).getReg();
|
|
|
|
assert(CopyDef == Src || CopyDef == Def);
|
|
|
|
for (MachineInstr &MI :
|
|
|
|
make_range(PrevCopy.getIterator(), Copy.getIterator()))
|
|
|
|
MI.clearRegisterKills(CopyDef, TRI);
|
|
|
|
|
|
|
|
Copy.eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
|
|
|
return true;
|
2012-02-21 07:28:17 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
/// Decide whether we should forward the source of \param Copy to its use in
|
|
|
|
/// \param UseI based on the physical register class constraints of the opcode
|
|
|
|
/// and avoiding introducing more cross-class COPYs.
|
|
|
|
bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
|
|
|
|
const MachineInstr &UseI,
|
|
|
|
unsigned UseIdx) {
|
|
|
|
|
|
|
|
unsigned CopySrcReg = Copy.getOperand(1).getReg();
|
|
|
|
|
|
|
|
// If the new register meets the opcode register constraints, then allow
|
|
|
|
// forwarding.
|
|
|
|
if (const TargetRegisterClass *URC =
|
|
|
|
UseI.getRegClassConstraint(UseIdx, TII, TRI))
|
|
|
|
return URC->contains(CopySrcReg);
|
|
|
|
|
|
|
|
if (!UseI.isCopy())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// COPYs don't have register class constraints, so if the user instruction
|
|
|
|
/// is a COPY, we just try to avoid introducing additional cross-class
|
|
|
|
/// COPYs. For example:
|
|
|
|
///
|
|
|
|
/// RegClassA = COPY RegClassB // Copy parameter
|
|
|
|
/// ...
|
|
|
|
/// RegClassB = COPY RegClassA // UseI parameter
|
|
|
|
///
|
|
|
|
/// which after forwarding becomes
|
|
|
|
///
|
|
|
|
/// RegClassA = COPY RegClassB
|
|
|
|
/// ...
|
|
|
|
/// RegClassB = COPY RegClassB
|
|
|
|
///
|
|
|
|
/// so we have reduced the number of cross-class COPYs and potentially
|
|
|
|
/// introduced a nop COPY that can be removed.
|
|
|
|
const TargetRegisterClass *UseDstRC =
|
|
|
|
TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
|
|
|
|
|
|
|
|
const TargetRegisterClass *SuperRC = UseDstRC;
|
|
|
|
for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
|
|
|
|
SuperRC; SuperRC = *SuperRCI++)
|
|
|
|
if (SuperRC->contains(CopySrcReg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that \p MI does not have implicit uses that overlap with it's \p Use
|
|
|
|
/// operand (the register being replaced), since these can sometimes be
|
|
|
|
/// implicitly tied to other operands. For example, on AMDGPU:
|
|
|
|
///
|
|
|
|
/// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
|
|
|
|
///
|
|
|
|
/// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
|
|
|
|
/// way of knowing we need to update the latter when updating the former.
|
|
|
|
bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
|
|
|
|
const MachineOperand &Use) {
|
|
|
|
for (const MachineOperand &MIUse : MI.uses())
|
|
|
|
if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
|
|
|
|
MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Look for available copies whose destination register is used by \p MI and
|
|
|
|
/// replace the use in \p MI with the copy's source register.
|
|
|
|
void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
|
|
|
|
if (AvailCopyMap.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Look for non-tied explicit vreg uses that have an active COPY
|
|
|
|
// instruction that defines the physical register allocated to them.
|
|
|
|
// Replace the vreg with the source of the active COPY.
|
|
|
|
for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
|
|
|
|
++OpIdx) {
|
|
|
|
MachineOperand &MOUse = MI.getOperand(OpIdx);
|
|
|
|
// Don't forward into undef use operands since doing so can cause problems
|
|
|
|
// with the machine verifier, since it doesn't treat undef reads as reads,
|
|
|
|
// so we can end up with a live range that ends on an undef read, leading to
|
|
|
|
// an error that the live range doesn't end on a read of the live range
|
|
|
|
// register.
|
|
|
|
if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
|
|
|
|
MOUse.isImplicit())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!MOUse.getReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that the register is marked 'renamable' so we know it is safe to
|
|
|
|
// rename it without violating any constraints that aren't expressed in the
|
|
|
|
// IR (e.g. ABI or opcode requirements).
|
|
|
|
if (!MOUse.isRenamable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto CI = AvailCopyMap.find(MOUse.getReg());
|
|
|
|
if (CI == AvailCopyMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MachineInstr &Copy = *CI->second;
|
|
|
|
unsigned CopyDstReg = Copy.getOperand(0).getReg();
|
|
|
|
const MachineOperand &CopySrc = Copy.getOperand(1);
|
|
|
|
unsigned CopySrcReg = CopySrc.getReg();
|
|
|
|
|
|
|
|
// FIXME: Don't handle partial uses of wider COPYs yet.
|
|
|
|
if (MOUse.getReg() != CopyDstReg) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
|
|
|
|
<< MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't forward COPYs of reserved regs unless they are constant.
|
|
|
|
if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!isForwardableRegClassCopy(Copy, MI, OpIdx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hasImplicitOverlap(MI, MOUse))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!DebugCounter::shouldExecute(FwdCounter)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
|
|
|
|
<< MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
|
|
|
|
<< "\n with " << printReg(CopySrcReg, TRI)
|
|
|
|
<< "\n in " << MI << " from " << Copy);
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
MOUse.setReg(CopySrcReg);
|
|
|
|
if (!CopySrc.isRenamable())
|
|
|
|
MOUse.setIsRenamable(false);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
// Clear kill markers that may have been invalidated.
|
|
|
|
for (MachineInstr &KMI :
|
|
|
|
make_range(Copy.getIterator(), std::next(MI.getIterator())))
|
|
|
|
KMI.clearRegisterKills(CopySrcReg, TRI);
|
|
|
|
|
|
|
|
++NumCopyForwards;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:56:39 +08:00
|
|
|
void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
|
MachineCopyPropagation has special logic for removing COPY instructions. It will remove plain COPYs using eraseFromParent(), but if the COPY has imp-defs/imp-uses it will convert it to a KILL, to keep the imp-def around.
This actually totally breaks and causes the machine verifier to cry in several cases, one of which being:
%RAX<def> = COPY %RCX<kill>
%ECX<def> = COPY %EAX<kill>, %RAX<imp-use,kill>
These subregister copies are together identified as noops, so are both removed. However, the second one as it has an imp-use gets converted into a kill:
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
As the original COPY has been removed, the verifier goes into tears at the use of undefined EAX and RAX.
There are several hacky solutions to this hacky problem (which is all to do with imp-use/def weirdnesses), but the least hacky I've come up with is to *always* remove COPYs by converting to KILLs. KILLs are no-ops to the code generator so the generated code doesn't change (which is why they were partially used in the first place), but using them also keeps the def/use and imp-def/imp-use chains alive:
%RAX<def> = KILL %RCX<kill>
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
The patch passes all test cases including the ones that check the removal of MOVs in this circumstance, along with an extra test I added to check subregister behaviour (which made the machine verifier fall over before my patch).
The patch also adds some DEBUG() statements because the file hadn't got any.
llvm-svn: 199797
2014-01-22 17:12:27 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
++I;
|
|
|
|
|
2018-03-30 08:56:03 +08:00
|
|
|
// Analyze copies (which don't overlap themselves).
|
|
|
|
if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
|
|
|
|
MI->getOperand(1).getReg())) {
|
2017-10-04 00:59:13 +08:00
|
|
|
unsigned Def = MI->getOperand(0).getReg();
|
|
|
|
unsigned Src = MI->getOperand(1).getReg();
|
|
|
|
|
|
|
|
assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(Src) &&
|
|
|
|
"MachineCopyPropagation should be run after register allocation!");
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
// The two copies cancel out and the source of the first copy
|
|
|
|
// hasn't been overridden, eliminate the second one. e.g.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2017-11-29 01:15:09 +08:00
|
|
|
// ... nothing clobbered eax.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %eax = COPY %ecx
|
2016-02-26 11:18:55 +08:00
|
|
|
// =>
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2016-02-26 11:18:55 +08:00
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2017-11-29 01:15:09 +08:00
|
|
|
// ... nothing clobbered eax.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2016-02-26 11:18:55 +08:00
|
|
|
// =>
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2017-10-04 00:59:13 +08:00
|
|
|
if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
|
|
|
|
continue;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
forwardUses(*MI);
|
|
|
|
|
|
|
|
// Src may have been changed by forwardUses()
|
|
|
|
Src = MI->getOperand(1).getReg();
|
|
|
|
|
2016-02-03 23:56:27 +08:00
|
|
|
// If Src is defined by a previous copy, the previous copy cannot be
|
|
|
|
// eliminated.
|
2017-02-04 10:27:20 +08:00
|
|
|
ReadRegister(Src);
|
|
|
|
for (const MachineOperand &MO : MI->implicit_operands()) {
|
|
|
|
if (!MO.isReg() || !MO.readsReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
ReadRegister(Reg);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
|
MachineCopyPropagation has special logic for removing COPY instructions. It will remove plain COPYs using eraseFromParent(), but if the COPY has imp-defs/imp-uses it will convert it to a KILL, to keep the imp-def around.
This actually totally breaks and causes the machine verifier to cry in several cases, one of which being:
%RAX<def> = COPY %RCX<kill>
%ECX<def> = COPY %EAX<kill>, %RAX<imp-use,kill>
These subregister copies are together identified as noops, so are both removed. However, the second one as it has an imp-use gets converted into a kill:
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
As the original COPY has been removed, the verifier goes into tears at the use of undefined EAX and RAX.
There are several hacky solutions to this hacky problem (which is all to do with imp-use/def weirdnesses), but the least hacky I've come up with is to *always* remove COPYs by converting to KILLs. KILLs are no-ops to the code generator so the generated code doesn't change (which is why they were partially used in the first place), but using them also keeps the def/use and imp-def/imp-use chains alive:
%RAX<def> = KILL %RCX<kill>
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
The patch passes all test cases including the ones that check the removal of MOVs in this circumstance, along with an extra test I added to check subregister behaviour (which made the machine verifier fall over before my patch).
The patch also adds some DEBUG() statements because the file hadn't got any.
llvm-svn: 199797
2014-01-22 17:12:27 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
// Copy is now a candidate for deletion.
|
2017-10-04 00:59:13 +08:00
|
|
|
if (!MRI->isReserved(Def))
|
2016-02-20 11:56:36 +08:00
|
|
|
MaybeDeadCopies.insert(MI);
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-03 23:56:27 +08:00
|
|
|
// If 'Def' is previously source of another copy, then this earlier copy's
|
2012-01-07 11:02:36 +08:00
|
|
|
// source is no longer available. e.g.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm9 = copy %xmm2
|
2012-01-07 11:02:36 +08:00
|
|
|
// ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm2 = copy %xmm0
|
2012-01-07 11:02:36 +08:00
|
|
|
// ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm2 = copy %xmm9
|
2017-10-04 00:59:13 +08:00
|
|
|
ClobberRegister(Def);
|
2017-02-04 10:27:20 +08:00
|
|
|
for (const MachineOperand &MO : MI->implicit_operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
|
|
|
continue;
|
2017-10-04 00:59:13 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2017-02-04 10:27:20 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
ClobberRegister(Reg);
|
|
|
|
}
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2017-10-17 00:57:37 +08:00
|
|
|
// Remember Def is defined by the copy.
|
|
|
|
for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
|
|
|
|
++SR) {
|
|
|
|
CopyMap[*SR] = MI;
|
|
|
|
AvailCopyMap[*SR] = MI;
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:57:37 +08:00
|
|
|
// Remember source that's copied to Def. Once it's clobbered, then
|
|
|
|
// it's no longer available for copy propagation.
|
|
|
|
RegList &DestList = SrcMap[Src];
|
|
|
|
if (!is_contained(DestList, Def))
|
|
|
|
DestList.push_back(Def);
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
// Clobber any earlyclobber regs first.
|
|
|
|
for (const MachineOperand &MO : MI->operands())
|
|
|
|
if (MO.isReg() && MO.isEarlyClobber()) {
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
// If we have a tied earlyclobber, that means it is also read by this
|
|
|
|
// instruction, so we need to make sure we don't remove it as dead
|
|
|
|
// later.
|
|
|
|
if (MO.isTied())
|
|
|
|
ReadRegister(Reg);
|
|
|
|
ClobberRegister(Reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardUses(*MI);
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
// Not a copy.
|
|
|
|
SmallVector<unsigned, 2> Defs;
|
2016-02-20 11:56:36 +08:00
|
|
|
const MachineOperand *RegMask = nullptr;
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2012-02-09 06:37:35 +08:00
|
|
|
if (MO.isRegMask())
|
2016-02-20 11:56:36 +08:00
|
|
|
RegMask = &MO;
|
2012-01-07 11:02:36 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
2017-10-04 00:59:13 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-01-07 11:02:36 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
|
2017-10-04 00:59:13 +08:00
|
|
|
assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
|
|
|
|
"MachineCopyPropagation should be run after register allocation!");
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
if (MO.isDef() && !MO.isEarlyClobber()) {
|
2012-01-07 11:02:36 +08:00
|
|
|
Defs.push_back(Reg);
|
|
|
|
continue;
|
2018-07-11 21:30:27 +08:00
|
|
|
} else if (!MO.isDebug() && MO.readsReg())
|
2017-02-04 10:27:20 +08:00
|
|
|
ReadRegister(Reg);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 06:37:35 +08:00
|
|
|
// The instruction has a register mask operand which means that it clobbers
|
2016-02-26 11:18:50 +08:00
|
|
|
// a large set of registers. Treat clobbered registers the same way as
|
|
|
|
// defined registers.
|
2016-02-20 11:56:36 +08:00
|
|
|
if (RegMask) {
|
2012-02-09 08:19:08 +08:00
|
|
|
// Erase any MaybeDeadCopies whose destination register is clobbered.
|
2016-03-26 05:15:35 +08:00
|
|
|
for (SmallSetVector<MachineInstr *, 8>::iterator DI =
|
|
|
|
MaybeDeadCopies.begin();
|
|
|
|
DI != MaybeDeadCopies.end();) {
|
|
|
|
MachineInstr *MaybeDead = *DI;
|
2016-02-20 11:56:36 +08:00
|
|
|
unsigned Reg = MaybeDead->getOperand(0).getReg();
|
|
|
|
assert(!MRI->isReserved(Reg));
|
2016-03-26 05:15:35 +08:00
|
|
|
|
|
|
|
if (!RegMask->clobbersPhysReg(Reg)) {
|
|
|
|
++DI;
|
2012-02-09 08:19:08 +08:00
|
|
|
continue;
|
2016-03-26 05:15:35 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
|
|
|
|
MaybeDead->dump());
|
2016-03-26 05:15:35 +08:00
|
|
|
|
|
|
|
// erase() will return the next valid iterator pointing to the next
|
|
|
|
// element after the erased one.
|
|
|
|
DI = MaybeDeadCopies.erase(DI);
|
2016-02-20 11:56:36 +08:00
|
|
|
MaybeDead->eraseFromParent();
|
2012-02-09 08:19:08 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
|
|
|
}
|
2012-02-09 06:37:35 +08:00
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
|
|
|
|
removeClobberedRegsFromMap(CopyMap, *RegMask);
|
|
|
|
for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
|
|
|
|
I != E; I = Next) {
|
|
|
|
Next = std::next(I);
|
|
|
|
if (RegMask->clobbersPhysReg(I->first)) {
|
|
|
|
removeRegsFromMap(AvailCopyMap, I->second, *TRI);
|
|
|
|
SrcMap.erase(I);
|
|
|
|
}
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
2016-02-26 11:18:50 +08:00
|
|
|
}
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
// Any previous copy definition or reading the Defs is no longer available.
|
2016-02-26 11:18:55 +08:00
|
|
|
for (unsigned Reg : Defs)
|
2016-02-26 11:18:50 +08:00
|
|
|
ClobberRegister(Reg);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If MBB doesn't have successors, delete the copies whose defs are not used.
|
|
|
|
// If MBB does have successors, then conservative assume the defs are live-out
|
|
|
|
// since we don't want to trust live-in lists.
|
|
|
|
if (MBB.succ_empty()) {
|
2016-02-20 11:56:36 +08:00
|
|
|
for (MachineInstr *MaybeDead : MaybeDeadCopies) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
|
|
|
|
MaybeDead->dump());
|
2016-02-20 11:56:36 +08:00
|
|
|
assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
|
|
|
|
MaybeDead->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:56:39 +08:00
|
|
|
MaybeDeadCopies.clear();
|
|
|
|
AvailCopyMap.clear();
|
|
|
|
CopyMap.clear();
|
|
|
|
SrcMap.clear();
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2016-02-20 11:56:39 +08:00
|
|
|
Changed = false;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2012-10-16 05:57:41 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-20 11:56:36 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF)
|
2016-02-20 11:56:39 +08:00
|
|
|
CopyPropagateBlock(MBB);
|
2012-01-07 11:02:36 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|