77 #define DEBUG_TYPE "machine-cp" 79 STATISTIC(NumDeletes,
"Number of dead copies deleted");
80 STATISTIC(NumCopyForwards,
"Number of copy uses forwarded");
81 STATISTIC(NumCopyBackwardPropagated,
"Number of copy defs backward propagated");
83 "Controls which register COPYs are forwarded");
101 for (
unsigned Reg : Regs) {
104 auto CI = Copies.
find(*RUI);
105 if (CI != Copies.
end())
106 CI->second.Avail =
false;
118 auto I = Copies.
find(*RUI);
119 if (
I != Copies.
end()) {
121 RegsToInvalidate.insert(
MI->getOperand(0).getReg());
122 RegsToInvalidate.insert(
MI->getOperand(1).getReg());
124 RegsToInvalidate.insert(
I->second.DefRegs.begin(),
125 I->second.DefRegs.end());
128 for (
unsigned InvalidReg : RegsToInvalidate)
136 auto I = Copies.
find(*RUI);
137 if (
I != Copies.
end()) {
140 markRegsUnavailable(
I->second.DefRegs, TRI);
144 markRegsUnavailable({
MI->getOperand(0).getReg()},
TRI);
160 Copies[*RUI] = {
MI, {},
true};
165 auto I = Copies.
insert({*RUI, {
nullptr, {},
false}});
166 auto &
Copy =
I.first->second;
168 Copy.DefRegs.push_back(Def);
172 bool hasAnyCopies() {
173 return !Copies.
empty();
177 bool MustBeAvailable =
false) {
178 auto CI = Copies.
find(RegUnit);
179 if (CI == Copies.
end())
181 if (MustBeAvailable && !CI->second.Avail)
183 return CI->second.MI;
188 auto CI = Copies.
find(RegUnit);
189 if (CI == Copies.
end())
191 if (CI->second.DefRegs.size() != 1)
194 return findCopyForUnit(*RUI, TRI,
true);
200 MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
212 if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
224 findCopyForUnit(*RUI, TRI,
true);
237 if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
273 typedef enum { DebugUse =
false, RegularUse =
true }
DebugType;
275 void ClobberRegister(
unsigned Reg);
280 bool eraseIfRedundant(
MachineInstr &Copy,
unsigned Src,
unsigned Def);
283 bool isForwardableRegClassCopy(
const MachineInstr &Copy,
285 bool isBackwardPropagatableRegClassCopy(
const MachineInstr &Copy,
308 "Machine Copy Propagation Pass",
false,
false)
310 void MachineCopyPropagation::ReadRegister(
unsigned Reg,
MachineInstr &Reader,
316 if (
MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
317 if (DT == RegularUse) {
318 LLVM_DEBUG(
dbgs() <<
"MCP: Copy is used - not dead: "; Copy->dump());
319 MaybeDeadCopies.remove(Copy);
321 CopyDbgUsers[Copy].push_back(&Reader);
337 if (Src == PreviousSrc) {
338 assert(Def == PreviousDef);
350 bool MachineCopyPropagation::eraseIfRedundant(
MachineInstr &Copy,
unsigned Src,
358 MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
365 if (!
isNopCopy(*PrevCopy, Src, Def, TRI))
374 assert(CopyDef == Src || CopyDef == Def);
385 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
391 return URC->contains(Def);
401 bool MachineCopyPropagation::isForwardableRegClassCopy(
const MachineInstr &Copy,
411 return URC->contains(CopySrcReg);
437 SuperRC; SuperRC = *SuperRCI++)
452 bool MachineCopyPropagation::hasImplicitOverlap(
const MachineInstr &MI,
455 if (&MIUse != &Use && MIUse.
isReg() && MIUse.isImplicit() &&
464 void MachineCopyPropagation::forwardUses(
MachineInstr &MI) {
465 if (!Tracker.hasAnyCopies())
471 for (
unsigned OpIdx = 0, OpEnd = MI.
getNumOperands(); OpIdx < OpEnd;
501 if (MOUse.
getReg() != CopyDstReg) {
503 dbgs() <<
"MCP: FIXME! Not forwarding COPY to sub-register use:\n " 512 if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
515 if (hasImplicitOverlap(MI, MOUse))
523 LLVM_DEBUG(
dbgs() <<
"MCP: Copy source overlap with dest in " << MI);
528 LLVM_DEBUG(
dbgs() <<
"MCP: Skipping forwarding due to debug counter:\n " 534 <<
"\n with " <<
printReg(CopySrcReg, TRI)
535 <<
"\n in " << MI <<
" from " << *Copy);
546 KMI.clearRegisterKills(CopySrcReg, TRI);
569 "MachineCopyPropagation should be run after register allocation!");
586 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
596 ReadRegister(Src, *MI, RegularUse);
598 if (!MO.isReg() || !MO.readsReg())
603 ReadRegister(Reg, *MI, RegularUse);
610 MaybeDeadCopies.insert(MI);
619 Tracker.clobberRegister(Def, *TRI);
621 if (!MO.isReg() || !MO.isDef())
626 Tracker.clobberRegister(Reg, *TRI);
629 Tracker.trackCopy(MI, *TRI);
636 if (MO.isReg() && MO.isEarlyClobber()) {
642 ReadRegister(Reg, *MI, RegularUse);
643 Tracker.clobberRegister(Reg, *TRI);
661 "MachineCopyPropagation should be run after register allocation!");
663 if (MO.isDef() && !MO.isEarlyClobber()) {
666 }
else if (MO.readsReg())
667 ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse);
676 MaybeDeadCopies.
begin();
677 DI != MaybeDeadCopies.end();) {
687 LLVM_DEBUG(
dbgs() <<
"MCP: Removing copy due to regmask clobbering: ";
692 Tracker.clobberRegister(Reg, *TRI);
696 DI = MaybeDeadCopies.erase(DI);
704 for (
unsigned Reg : Defs)
705 Tracker.clobberRegister(Reg, *TRI);
713 LLVM_DEBUG(
dbgs() <<
"MCP: Removing copy due to no live-out succ: ";
718 assert(MaybeDead->isCopy());
719 unsigned SrcReg = MaybeDead->getOperand(1).getReg();
722 MaybeDead->eraseFromParent();
728 MaybeDeadCopies.clear();
729 CopyDbgUsers.clear();
748 void MachineCopyPropagation::propagateDefs(
MachineInstr &MI) {
749 if (!Tracker.hasAnyCopies())
752 for (
unsigned OpIdx = 0, OpEnd = MI.
getNumOperands(); OpIdx != OpEnd;
771 Tracker.findAvailBackwardCopy(MI, MODef.
getReg(), *
TRI);
778 if (MODef.
getReg() != Src)
781 if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
784 if (hasImplicitOverlap(MI, MODef))
788 <<
"\n with " <<
printReg(Def, TRI) <<
"\n in " 789 << MI <<
" from " << *Copy);
795 MaybeDeadCopies.insert(Copy);
797 ++NumCopyBackwardPropagated;
801 void MachineCopyPropagation::BackwardCopyPropagateBlock(
822 Tracker.invalidateRegister(Src, *TRI);
823 Tracker.invalidateRegister(Def, *TRI);
824 Tracker.trackCopy(MI, *TRI);
831 if (MO.isReg() && MO.isEarlyClobber()) {
835 Tracker.invalidateRegister(Reg, *TRI);
847 Tracker.invalidateRegister(MO.getReg(), *
TRI);
850 Tracker.invalidateRegister(MO.getReg(), *
TRI);
854 for (
auto *Copy : MaybeDeadCopies) {
859 MaybeDeadCopies.clear();
860 CopyDbgUsers.clear();
864 bool MachineCopyPropagation::runOnMachineFunction(
MachineFunction &MF) {
875 BackwardCopyPropagateBlock(MBB);
876 ForwardCopyPropagateBlock(MBB);
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.
bool isReserved(Register PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
This class represents lattice values for constants.
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.
void push_back(const T &Elt)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
iterator_range< mop_iterator > operands()
void setIsRenamable(bool Val=true)
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, "Machine Copy Propagation Pass", false, false) void MachineCopyPropagation
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
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
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
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")
bool regsOverlap(Register regA, Register regB) const
Returns true if the two registers are equal or alias each other.
unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
iterator begin()
Get an iterator to the beginning of the SetVector.
void setReg(Register Reg)
Change the register this operand corresponds to.
virtual const TargetInstrInfo * getInstrInfo() const
reverse_iterator rbegin()
TargetInstrInfo - Interface to description of machine instruction set.
iterator find(const_arg_type_t< KeyT > Val)
sc_iterator getSuperClasses() const
Returns a NULL-terminated list of super-classes.
bool erase(const KeyT &Val)
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")
Represent the analysis usage information of a pass.
bool definesRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
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...
bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA or if RegB == RegA.
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.
MachineOperand class - Representation of each machine instruction operand.
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register...
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 isSubRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA.
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()
static void clear(coro::Shape &Shape)
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
static bool isBackwardPropagatableCopy(MachineInstr &MI, const MachineRegisterInfo &MRI)
Representation of each machine instruction.
reverse_self_iterator getReverseIterator()
void updateDbgUsersToReg(unsigned Reg, ArrayRef< MachineInstr *> Users) const
updateDbgUsersToReg - Update a collection of DBG_VALUE instructions to refer to the designated regist...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
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())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src, unsigned Def, const TargetRegisterInfo *TRI)
Return true if PreviousCopy did copy register Src to register Def.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo)
Clear all kill flags affecting Reg.
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.