30 #define DEBUG_TYPE "codegen-cp"
32 STATISTIC(NumDeletes,
"Number of dead copies deleted");
63 void ClobberRegister(
unsigned Reg);
64 void ReadRegister(
unsigned Reg);
71 Reg2MIMap AvailCopyMap;
83 "Machine Copy Propagation Pass",
false,
false)
87 static
void removeRegsFromMap(Reg2MIMap &Map,
const RegList &
Regs,
89 for (
unsigned Reg : Regs) {
100 static void removeClobberedRegsFromMap(Reg2MIMap &Map,
102 for (Reg2MIMap::iterator
I = Map.begin(),
E = Map.end(), Next;
I !=
E;
105 unsigned Reg =
I->first;
111 void MachineCopyPropagation::ClobberRegister(
unsigned Reg) {
114 AvailCopyMap.erase(*AI);
116 SourceMap::iterator
SI = SrcMap.find(*AI);
117 if (SI != SrcMap.end()) {
118 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
124 void MachineCopyPropagation::ReadRegister(
unsigned Reg) {
128 Reg2MIMap::iterator CI = CopyMap.find(*AI);
129 if (CI != CopyMap.end()) {
130 DEBUG(
dbgs() <<
"MCP: Copy is used - not dead: "; CI->second->dump());
131 MaybeDeadCopies.remove(CI->second);
142 static bool isNopCopy(
const MachineInstr &PreviousCopy,
unsigned Src,
146 if (Src == PreviousSrc) {
147 assert(Def == PreviousDef);
159 bool MachineCopyPropagation::eraseIfRedundant(
MachineInstr &Copy,
unsigned Src,
163 if (
MRI->isReserved(Src) ||
MRI->isReserved(Def))
167 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
168 if (CI == AvailCopyMap.end())
173 if (!isNopCopy(PrevCopy, Src, Def, TRI))
176 DEBUG(
dbgs() <<
"MCP: copy is a NOP, removing: "; Copy.
dump());
182 assert(CopyDef == Src || CopyDef == Def);
185 MI.clearRegisterKills(CopyDef, TRI);
206 "MachineCopyPropagation should be run after register allocation!");
223 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
230 if (!MO.isReg() || !MO.readsReg())
232 unsigned Reg = MO.getReg();
238 DEBUG(
dbgs() <<
"MCP: Copy is a deletion candidate: "; MI->
dump());
241 if (!
MRI->isReserved(Def))
242 MaybeDeadCopies.insert(MI);
251 ClobberRegister(Def);
253 if (!MO.isReg() || !MO.isDef())
255 unsigned Reg = MO.getReg();
258 ClobberRegister(Reg);
265 AvailCopyMap[*SR] =
MI;
270 RegList &DestList = SrcMap[Src];
272 DestList.push_back(Def);
285 unsigned Reg = MO.getReg();
290 "MachineCopyPropagation should be run after register allocation!");
313 MaybeDeadCopies.
begin();
314 DI != MaybeDeadCopies.end();) {
324 DEBUG(
dbgs() <<
"MCP: Removing copy due to regmask clobbering: ";
329 DI = MaybeDeadCopies.erase(DI);
335 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
336 removeClobberedRegsFromMap(CopyMap, *RegMask);
337 for (SourceMap::iterator
I = SrcMap.begin(),
E = SrcMap.end(), Next;
341 removeRegsFromMap(AvailCopyMap,
I->second, *TRI);
348 for (
unsigned Reg : Defs)
349 ClobberRegister(Reg);
364 MaybeDeadCopies.clear();
365 AvailCopyMap.clear();
370 bool MachineCopyPropagation::runOnMachineFunction(
MachineFunction &MF) {
381 CopyPropagateBlock(MBB);
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool isSubRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a sub-register of RegA.
iterator_range< mop_iterator > operands()
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 ...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
iterator begin()
Get an iterator to the beginning of the SetVector.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
TargetInstrInfo - Interface to description of machine instruction set.
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineOperand & getOperand(unsigned i) const
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
self_iterator getIterator()
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.
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.
void dump(const TargetInstrInfo *TII=nullptr) const
void setPreservesCFG()
This function should be called by the pass, iff they do not:
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.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or "(null)".
INITIALIZE_PASS(MachineCopyPropagation,"machine-cp","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...
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.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Properties which a MachineFunction may have at a given point in time.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.