68 #define DEBUG_TYPE "machine-cp" 70 STATISTIC(NumDeletes,
"Number of dead copies deleted");
71 STATISTIC(NumCopyForwards,
"Number of copy uses forwarded");
73 "Controls which register COPYs are forwarded");
106 void ClobberRegister(
unsigned Reg);
107 void ReadRegister(
unsigned Reg);
111 bool isForwardableRegClassCopy(
const MachineInstr &Copy,
119 Reg2MIMap AvailCopyMap;
137 "Machine Copy Propagation Pass",
false,
false)
141 static
void removeRegsFromMap(Reg2MIMap &Map,
const RegList &
Regs,
143 for (
unsigned Reg : Regs) {
154 static void removeClobberedRegsFromMap(Reg2MIMap &Map,
156 for (Reg2MIMap::iterator
I = Map.begin(),
E = Map.end(), Next;
I !=
E;
159 unsigned Reg =
I->first;
165 void MachineCopyPropagation::ClobberRegister(
unsigned Reg) {
168 AvailCopyMap.erase(*AI);
170 SourceMap::iterator
SI = SrcMap.find(*AI);
171 if (SI != SrcMap.end()) {
172 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
178 void MachineCopyPropagation::ReadRegister(
unsigned Reg) {
182 Reg2MIMap::iterator CI = CopyMap.find(*AI);
183 if (CI != CopyMap.end()) {
186 MaybeDeadCopies.remove(CI->second);
197 static bool isNopCopy(
const MachineInstr &PreviousCopy,
unsigned Src,
201 if (Src == PreviousSrc) {
202 assert(Def == PreviousDef);
214 bool MachineCopyPropagation::eraseIfRedundant(
MachineInstr &Copy,
unsigned Src,
222 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
223 if (CI == AvailCopyMap.end())
230 if (!isNopCopy(PrevCopy, Src, Def, TRI))
239 assert(CopyDef == Src || CopyDef == Def);
242 MI.clearRegisterKills(CopyDef, TRI);
253 bool MachineCopyPropagation::isForwardableRegClassCopy(
const MachineInstr &Copy,
263 return URC->contains(CopySrcReg);
289 SuperRC; SuperRC = *SuperRCI++)
304 bool MachineCopyPropagation::hasImplicitOverlap(
const MachineInstr &
MI,
307 if (&MIUse != &Use && MIUse.
isReg() && MIUse.isImplicit() &&
316 void MachineCopyPropagation::forwardUses(
MachineInstr &MI) {
317 if (AvailCopyMap.empty())
323 for (
unsigned OpIdx = 0, OpEnd = MI.
getNumOperands(); OpIdx < OpEnd;
344 auto CI = AvailCopyMap.find(MOUse.
getReg());
345 if (CI == AvailCopyMap.end())
351 unsigned CopySrcReg = CopySrc.
getReg();
354 if (MOUse.
getReg() != CopyDstReg) {
356 dbgs() <<
"MCP: FIXME! Not forwarding COPY to sub-register use:\n " 365 if (!isForwardableRegClassCopy(Copy, MI, OpIdx))
368 if (hasImplicitOverlap(MI, MOUse))
372 LLVM_DEBUG(
dbgs() <<
"MCP: Skipping forwarding due to debug counter:\n " 378 <<
"\n with " <<
printReg(CopySrcReg, TRI)
379 <<
"\n in " << MI <<
" from " << Copy);
390 KMI.clearRegisterKills(CopySrcReg, TRI);
412 "MachineCopyPropagation should be run after register allocation!");
429 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
441 if (!MO.isReg() || !MO.readsReg())
443 unsigned Reg = MO.getReg();
453 MaybeDeadCopies.insert(MI);
462 ClobberRegister(Def);
464 if (!MO.isReg() || !MO.isDef())
466 unsigned Reg = MO.getReg();
469 ClobberRegister(Reg);
476 AvailCopyMap[*SR] =
MI;
481 RegList &DestList = SrcMap[Src];
483 DestList.push_back(Def);
490 if (MO.isReg() && MO.isEarlyClobber()) {
491 unsigned Reg = MO.getReg();
497 ClobberRegister(Reg);
510 unsigned Reg = MO.getReg();
515 "MachineCopyPropagation should be run after register allocation!");
517 if (MO.isDef() && !MO.isEarlyClobber()) {
520 }
else if (!MO.isDebug() && MO.readsReg())
530 MaybeDeadCopies.
begin();
531 DI != MaybeDeadCopies.end();) {
541 LLVM_DEBUG(
dbgs() <<
"MCP: Removing copy due to regmask clobbering: ";
546 DI = MaybeDeadCopies.erase(DI);
552 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
553 removeClobberedRegsFromMap(CopyMap, *RegMask);
554 for (SourceMap::iterator
I = SrcMap.begin(),
E = SrcMap.end(), Next;
558 removeRegsFromMap(AvailCopyMap,
I->second, *TRI);
565 for (
unsigned Reg : Defs)
566 ClobberRegister(Reg);
574 LLVM_DEBUG(
dbgs() <<
"MCP: Removing copy due to no live-out succ: ";
577 MaybeDead->eraseFromParent();
583 MaybeDeadCopies.clear();
584 AvailCopyMap.clear();
589 bool MachineCopyPropagation::runOnMachineFunction(
MachineFunction &MF) {
600 CopyPropagateBlock(MBB);
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool contains(unsigned Reg) const
Return true if the specified register is included in this register class.
Compute iterated dominance frontiers using a linear time algorithm.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
iterator_range< mop_iterator > operands()
void setIsRenamable(bool Val=true)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isSubRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a sub-register of RegA.
This file provides an implementation of debug counters.
const TargetRegisterClass *const * sc_iterator
DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", "Controls which register COPYs are forwarded")
iterator begin()
Get an iterator to the beginning of the SetVector.
virtual const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo - Interface to description of machine instruction set.
sc_iterator getSuperClasses() const
Returns a NULL-terminated list of super-classes.
unsigned const MachineRegisterInfo * MRI
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
self_iterator getIterator()
static bool shouldExecute(unsigned CounterName)
char & MachineCopyPropagationID
MachineCopyPropagation - This pass performs copy propagation on machine instructions.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
bool isConstantPhysReg(unsigned PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
void initializeMachineCopyPropagationPass(PassRegistry &)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
A SetVector that performs no allocations if smaller than a certain size.
bool regsOverlap(unsigned regA, unsigned regB) const
Returns true if the two registers are equal or alias each other.
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
bool isRenamable() const
isRenamable - Returns true if this register may be renamed, i.e.
iterator_range< mop_iterator > implicit_operands()
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, "Machine Copy Propagation Pass", false, false) static void removeRegsFromMap(Reg2MIMap &Map
Remove any entry in Map where the register is a subregister or equal to a register contained in Regs...
const MachineOperand & getOperand(unsigned i) const
Properties which a MachineFunction may have at a given point in time.
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.