LLVM  6.0.0svn
MachineCopyPropagation.cpp
Go to the documentation of this file.
1 //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is an extremely simple MachineInstr-level copy propagation pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/Debug.h"
33 #include <cassert>
34 #include <iterator>
35 
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "machine-cp"
39 
40 STATISTIC(NumDeletes, "Number of dead copies deleted");
41 
42 namespace {
43 
44 using RegList = SmallVector<unsigned, 4>;
45 using SourceMap = DenseMap<unsigned, RegList>;
46 using Reg2MIMap = DenseMap<unsigned, MachineInstr *>;
47 
48  class MachineCopyPropagation : public MachineFunctionPass {
49  const TargetRegisterInfo *TRI;
50  const TargetInstrInfo *TII;
51  const MachineRegisterInfo *MRI;
52 
53  public:
54  static char ID; // Pass identification, replacement for typeid
55 
56  MachineCopyPropagation() : MachineFunctionPass(ID) {
58  }
59 
60  void getAnalysisUsage(AnalysisUsage &AU) const override {
61  AU.setPreservesCFG();
63  }
64 
65  bool runOnMachineFunction(MachineFunction &MF) override;
66 
67  MachineFunctionProperties getRequiredProperties() const override {
70  }
71 
72  private:
73  void ClobberRegister(unsigned Reg);
74  void ReadRegister(unsigned Reg);
75  void CopyPropagateBlock(MachineBasicBlock &MBB);
76  bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
77 
78  /// Candidates for deletion.
79  SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
80 
81  /// Def -> available copies map.
82  Reg2MIMap AvailCopyMap;
83 
84  /// Def -> copies map.
85  Reg2MIMap CopyMap;
86 
87  /// Src -> Def map
88  SourceMap SrcMap;
89 
90  bool Changed;
91  };
92 
93 } // end anonymous namespace
94 
96 
98 
99 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
100  "Machine Copy Propagation Pass", false, false)
101 
102 /// Remove any entry in \p Map where the register is a subregister or equal to
103 /// a register contained in \p Regs.
104 static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
105  const TargetRegisterInfo &TRI) {
106  for (unsigned Reg : Regs) {
107  // Source of copy is no longer available for propagation.
108  for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
109  Map.erase(*SR);
110  }
111 }
112 
113 /// Remove any entry in \p Map that is marked clobbered in \p RegMask.
114 /// The map will typically have a lot fewer entries than the regmask clobbers,
115 /// so this is more efficient than iterating the clobbered registers and calling
116 /// ClobberRegister() on them.
117 static void removeClobberedRegsFromMap(Reg2MIMap &Map,
118  const MachineOperand &RegMask) {
119  for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
120  I = Next) {
121  Next = std::next(I);
122  unsigned Reg = I->first;
123  if (RegMask.clobbersPhysReg(Reg))
124  Map.erase(I);
125  }
126 }
127 
128 void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
129  for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
130  CopyMap.erase(*AI);
131  AvailCopyMap.erase(*AI);
132 
133  SourceMap::iterator SI = SrcMap.find(*AI);
134  if (SI != SrcMap.end()) {
135  removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
136  SrcMap.erase(SI);
137  }
138  }
139 }
140 
141 void MachineCopyPropagation::ReadRegister(unsigned Reg) {
142  // If 'Reg' is defined by a copy, the copy is no longer a candidate
143  // for elimination.
144  for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
145  Reg2MIMap::iterator CI = CopyMap.find(*AI);
146  if (CI != CopyMap.end()) {
147  DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
148  MaybeDeadCopies.remove(CI->second);
149  }
150  }
151 }
152 
153 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
154 /// This fact may have been obscured by sub register usage or may not be true at
155 /// all even though Src and Def are subregisters of the registers used in
156 /// PreviousCopy. e.g.
157 /// isNopCopy("ecx = COPY eax", AX, CX) == true
158 /// isNopCopy("ecx = COPY eax", AH, CL) == false
159 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
160  unsigned Def, const TargetRegisterInfo *TRI) {
161  unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
162  unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
163  if (Src == PreviousSrc) {
164  assert(Def == PreviousDef);
165  return true;
166  }
167  if (!TRI->isSubRegister(PreviousSrc, Src))
168  return false;
169  unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
170  return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
171 }
172 
173 /// Remove instruction \p Copy if there exists a previous copy that copies the
174 /// register \p Src to the register \p Def; This may happen indirectly by
175 /// copying the super registers.
176 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
177  unsigned Def) {
178  // Avoid eliminating a copy from/to a reserved registers as we cannot predict
179  // the value (Example: The sparc zero register is writable but stays zero).
180  if (MRI->isReserved(Src) || MRI->isReserved(Def))
181  return false;
182 
183  // Search for an existing copy.
184  Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
185  if (CI == AvailCopyMap.end())
186  return false;
187 
188  // Check that the existing copy uses the correct sub registers.
189  MachineInstr &PrevCopy = *CI->second;
190  if (PrevCopy.getOperand(0).isDead())
191  return false;
192  if (!isNopCopy(PrevCopy, Src, Def, TRI))
193  return false;
194 
195  DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
196 
197  // Copy was redundantly redefining either Src or Def. Remove earlier kill
198  // flags between Copy and PrevCopy because the value will be reused now.
199  assert(Copy.isCopy());
200  unsigned CopyDef = Copy.getOperand(0).getReg();
201  assert(CopyDef == Src || CopyDef == Def);
202  for (MachineInstr &MI :
203  make_range(PrevCopy.getIterator(), Copy.getIterator()))
204  MI.clearRegisterKills(CopyDef, TRI);
205 
206  Copy.eraseFromParent();
207  Changed = true;
208  ++NumDeletes;
209  return true;
210 }
211 
212 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
213  DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
214 
215  for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
216  MachineInstr *MI = &*I;
217  ++I;
218 
219  if (MI->isCopy()) {
220  unsigned Def = MI->getOperand(0).getReg();
221  unsigned Src = MI->getOperand(1).getReg();
222 
225  "MachineCopyPropagation should be run after register allocation!");
226 
227  // The two copies cancel out and the source of the first copy
228  // hasn't been overridden, eliminate the second one. e.g.
229  // %ECX<def> = COPY %EAX
230  // ... nothing clobbered EAX.
231  // %EAX<def> = COPY %ECX
232  // =>
233  // %ECX<def> = COPY %EAX
234  //
235  // or
236  //
237  // %ECX<def> = COPY %EAX
238  // ... nothing clobbered EAX.
239  // %ECX<def> = COPY %EAX
240  // =>
241  // %ECX<def> = COPY %EAX
242  if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
243  continue;
244 
245  // If Src is defined by a previous copy, the previous copy cannot be
246  // eliminated.
247  ReadRegister(Src);
248  for (const MachineOperand &MO : MI->implicit_operands()) {
249  if (!MO.isReg() || !MO.readsReg())
250  continue;
251  unsigned Reg = MO.getReg();
252  if (!Reg)
253  continue;
254  ReadRegister(Reg);
255  }
256 
257  DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
258 
259  // Copy is now a candidate for deletion.
260  if (!MRI->isReserved(Def))
261  MaybeDeadCopies.insert(MI);
262 
263  // If 'Def' is previously source of another copy, then this earlier copy's
264  // source is no longer available. e.g.
265  // %xmm9<def> = copy %xmm2
266  // ...
267  // %xmm2<def> = copy %xmm0
268  // ...
269  // %xmm2<def> = copy %xmm9
270  ClobberRegister(Def);
271  for (const MachineOperand &MO : MI->implicit_operands()) {
272  if (!MO.isReg() || !MO.isDef())
273  continue;
274  unsigned Reg = MO.getReg();
275  if (!Reg)
276  continue;
277  ClobberRegister(Reg);
278  }
279 
280  // Remember Def is defined by the copy.
281  for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
282  ++SR) {
283  CopyMap[*SR] = MI;
284  AvailCopyMap[*SR] = MI;
285  }
286 
287  // Remember source that's copied to Def. Once it's clobbered, then
288  // it's no longer available for copy propagation.
289  RegList &DestList = SrcMap[Src];
290  if (!is_contained(DestList, Def))
291  DestList.push_back(Def);
292 
293  continue;
294  }
295 
296  // Not a copy.
298  const MachineOperand *RegMask = nullptr;
299  for (const MachineOperand &MO : MI->operands()) {
300  if (MO.isRegMask())
301  RegMask = &MO;
302  if (!MO.isReg())
303  continue;
304  unsigned Reg = MO.getReg();
305  if (!Reg)
306  continue;
307 
309  "MachineCopyPropagation should be run after register allocation!");
310 
311  if (MO.isDef()) {
312  Defs.push_back(Reg);
313  continue;
314  } else if (MO.readsReg())
315  ReadRegister(Reg);
316  }
317 
318  // The instruction has a register mask operand which means that it clobbers
319  // a large set of registers. Treat clobbered registers the same way as
320  // defined registers.
321  if (RegMask) {
322  // Erase any MaybeDeadCopies whose destination register is clobbered.
324  MaybeDeadCopies.begin();
325  DI != MaybeDeadCopies.end();) {
326  MachineInstr *MaybeDead = *DI;
327  unsigned Reg = MaybeDead->getOperand(0).getReg();
328  assert(!MRI->isReserved(Reg));
329 
330  if (!RegMask->clobbersPhysReg(Reg)) {
331  ++DI;
332  continue;
333  }
334 
335  DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
336  MaybeDead->dump());
337 
338  // erase() will return the next valid iterator pointing to the next
339  // element after the erased one.
340  DI = MaybeDeadCopies.erase(DI);
341  MaybeDead->eraseFromParent();
342  Changed = true;
343  ++NumDeletes;
344  }
345 
346  removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
347  removeClobberedRegsFromMap(CopyMap, *RegMask);
348  for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
349  I != E; I = Next) {
350  Next = std::next(I);
351  if (RegMask->clobbersPhysReg(I->first)) {
352  removeRegsFromMap(AvailCopyMap, I->second, *TRI);
353  SrcMap.erase(I);
354  }
355  }
356  }
357 
358  // Any previous copy definition or reading the Defs is no longer available.
359  for (unsigned Reg : Defs)
360  ClobberRegister(Reg);
361  }
362 
363  // If MBB doesn't have successors, delete the copies whose defs are not used.
364  // If MBB does have successors, then conservative assume the defs are live-out
365  // since we don't want to trust live-in lists.
366  if (MBB.succ_empty()) {
367  for (MachineInstr *MaybeDead : MaybeDeadCopies) {
368  assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
369  MaybeDead->eraseFromParent();
370  Changed = true;
371  ++NumDeletes;
372  }
373  }
374 
375  MaybeDeadCopies.clear();
376  AvailCopyMap.clear();
377  CopyMap.clear();
378  SrcMap.clear();
379 }
380 
381 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
382  if (skipFunction(*MF.getFunction()))
383  return false;
384 
385  Changed = false;
386 
387  TRI = MF.getSubtarget().getRegisterInfo();
388  TII = MF.getSubtarget().getInstrInfo();
389  MRI = &MF.getRegInfo();
390 
391  for (MachineBasicBlock &MBB : MF)
392  CopyPropagateBlock(MBB);
393 
394  return Changed;
395 }
void push_back(const T &Elt)
Definition: SmallVector.h:212
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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")
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:332
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink &#39;this&#39; 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.
Reg
All possible values of the reg field in the ModR/M byte.
const RegList & Regs
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:83
virtual const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo - Interface to description of machine instruction set.
#define DEBUG_TYPE
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()
Definition: ilist_node.h:82
char & MachineCopyPropagationID
MachineCopyPropagation - This pass performs copy propagation on machine instructions.
bool isCopy() const
Definition: MachineInstr.h:857
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.
Definition: SetVector.h:298
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:285
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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.
iterator_range< mop_iterator > implicit_operands()
Definition: MachineInstr.h:346
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.
Definition: MachineInstr.h:59
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
#define I(x, y, z)
Definition: MD5.cpp:58
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
#define DEBUG(X)
Definition: Debug.h:118
IRTranslator LLVM IR MI
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
Definition: MachineInstr.h:295
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.
Definition: STLExtras.h:867