xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision e15248362352c549561cda871bd952842e1db620)
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 // This pass forwards the source of COPYs to the users of their destinations
13 // when doing so is legal.  For example:
14 //
15 //   %reg1 = COPY %reg0
16 //   ...
17 //   ... = OP %reg1
18 //
19 // If
20 //   - %reg0 has not been clobbered by the time of the use of %reg1
21 //   - the register class constraints are satisfied
22 //   - the COPY def is the only value that reaches OP
23 // then this pass replaces the above with:
24 //
25 //   %reg1 = COPY %reg0
26 //   ...
27 //   ... = OP %reg0
28 //
29 // This pass also removes some redundant COPYs.  For example:
30 //
31 //    %R1 = COPY %R0
32 //    ... // No clobber of %R1
33 //    %R0 = COPY %R1 <<< Removed
34 //
35 // or
36 //
37 //    %R1 = COPY %R0
38 //    ... // No clobber of %R0
39 //    %R1 = COPY %R0 <<< Removed
40 //
41 //===----------------------------------------------------------------------===//
42 
43 #include "llvm/ADT/DenseMap.h"
44 #include "llvm/ADT/STLExtras.h"
45 #include "llvm/ADT/SetVector.h"
46 #include "llvm/ADT/SmallVector.h"
47 #include "llvm/ADT/Statistic.h"
48 #include "llvm/ADT/iterator_range.h"
49 #include "llvm/CodeGen/MachineBasicBlock.h"
50 #include "llvm/CodeGen/MachineFunction.h"
51 #include "llvm/CodeGen/MachineFunctionPass.h"
52 #include "llvm/CodeGen/MachineInstr.h"
53 #include "llvm/CodeGen/MachineOperand.h"
54 #include "llvm/CodeGen/MachineRegisterInfo.h"
55 #include "llvm/CodeGen/TargetInstrInfo.h"
56 #include "llvm/CodeGen/TargetRegisterInfo.h"
57 #include "llvm/CodeGen/TargetSubtargetInfo.h"
58 #include "llvm/MC/MCRegisterInfo.h"
59 #include "llvm/Pass.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/DebugCounter.h"
62 #include "llvm/Support/raw_ostream.h"
63 #include <cassert>
64 #include <iterator>
65 
66 using namespace llvm;
67 
68 #define DEBUG_TYPE "machine-cp"
69 
70 STATISTIC(NumDeletes, "Number of dead copies deleted");
71 STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
72 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
73               "Controls which register COPYs are forwarded");
74 
75 namespace {
76 
77 class CopyTracker {
78   struct CopyInfo {
79     MachineInstr *MI;
80     SmallVector<unsigned, 4> DefRegs;
81     bool Avail;
82   };
83 
84   DenseMap<unsigned, CopyInfo> Copies;
85 
86 public:
87   /// Mark all of the given registers and their subregisters as unavailable for
88   /// copying.
89   void markRegsUnavailable(ArrayRef<unsigned> Regs,
90                            const TargetRegisterInfo &TRI) {
91     for (unsigned Reg : Regs) {
92       // Source of copy is no longer available for propagation.
93       for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
94         auto CI = Copies.find(*RUI);
95         if (CI != Copies.end())
96           CI->second.Avail = false;
97       }
98     }
99   }
100 
101   /// Clobber a single register, removing it from the tracker's copy maps.
102   void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
103     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
104       auto I = Copies.find(*RUI);
105       if (I != Copies.end()) {
106         markRegsUnavailable(I->second.DefRegs, TRI);
107         Copies.erase(I);
108       }
109     }
110   }
111 
112   /// Add this copy's registers into the tracker's copy maps.
113   void trackCopy(MachineInstr *Copy, const TargetRegisterInfo &TRI) {
114     assert(Copy->isCopy() && "Tracking non-copy?");
115 
116     unsigned Def = Copy->getOperand(0).getReg();
117     unsigned Src = Copy->getOperand(1).getReg();
118 
119     // Remember Def is defined by the copy.
120     for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
121       Copies[*RUI] = {Copy, {}, true};
122 
123     // Remember source that's copied to Def. Once it's clobbered, then
124     // it's no longer available for copy propagation.
125     for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
126       auto I = Copies.insert({*RUI, {nullptr, {}, false}});
127       auto &Copy = I.first->second;
128       if (!is_contained(Copy.DefRegs, Def))
129         Copy.DefRegs.push_back(Def);
130     }
131   }
132 
133   bool hasAnyCopies() {
134     return !Copies.empty();
135   }
136 
137   MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI,
138                          bool MustBeAvailable = false) {
139     auto CI = Copies.find(RegUnit);
140     if (CI == Copies.end())
141       return nullptr;
142     if (MustBeAvailable && !CI->second.Avail)
143       return nullptr;
144     return CI->second.MI;
145   }
146 
147   MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg,
148                               const TargetRegisterInfo &TRI) {
149     // We check the first RegUnit here, since we'll only be interested in the
150     // copy if it copies the entire register anyway.
151     MCRegUnitIterator RUI(Reg, &TRI);
152     MachineInstr *AvailCopy =
153         findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
154     if (!AvailCopy ||
155         !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
156       return nullptr;
157 
158     // Check that the available copy isn't clobbered by any regmasks between
159     // itself and the destination.
160     unsigned AvailSrc = AvailCopy->getOperand(1).getReg();
161     unsigned AvailDef = AvailCopy->getOperand(0).getReg();
162     for (const MachineInstr &MI :
163          make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
164       for (const MachineOperand &MO : MI.operands())
165         if (MO.isRegMask())
166           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
167             return nullptr;
168 
169     return AvailCopy;
170   }
171 
172   void clear() {
173     Copies.clear();
174   }
175 };
176 
177 class MachineCopyPropagation : public MachineFunctionPass {
178   const TargetRegisterInfo *TRI;
179   const TargetInstrInfo *TII;
180   const MachineRegisterInfo *MRI;
181 
182 public:
183   static char ID; // Pass identification, replacement for typeid
184 
185   MachineCopyPropagation() : MachineFunctionPass(ID) {
186     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
187   }
188 
189   void getAnalysisUsage(AnalysisUsage &AU) const override {
190     AU.setPreservesCFG();
191     MachineFunctionPass::getAnalysisUsage(AU);
192   }
193 
194   bool runOnMachineFunction(MachineFunction &MF) override;
195 
196   MachineFunctionProperties getRequiredProperties() const override {
197     return MachineFunctionProperties().set(
198         MachineFunctionProperties::Property::NoVRegs);
199   }
200 
201 private:
202   void ClobberRegister(unsigned Reg);
203   void ReadRegister(unsigned Reg);
204   void CopyPropagateBlock(MachineBasicBlock &MBB);
205   bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
206   void forwardUses(MachineInstr &MI);
207   bool isForwardableRegClassCopy(const MachineInstr &Copy,
208                                  const MachineInstr &UseI, unsigned UseIdx);
209   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
210 
211   /// Candidates for deletion.
212   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
213 
214   CopyTracker Tracker;
215 
216   bool Changed;
217 };
218 
219 } // end anonymous namespace
220 
221 char MachineCopyPropagation::ID = 0;
222 
223 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
224 
225 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
226                 "Machine Copy Propagation Pass", false, false)
227 
228 void MachineCopyPropagation::ReadRegister(unsigned Reg) {
229   // If 'Reg' is defined by a copy, the copy is no longer a candidate
230   // for elimination.
231   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
232     if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
233       LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
234       MaybeDeadCopies.remove(Copy);
235     }
236   }
237 }
238 
239 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
240 /// This fact may have been obscured by sub register usage or may not be true at
241 /// all even though Src and Def are subregisters of the registers used in
242 /// PreviousCopy. e.g.
243 /// isNopCopy("ecx = COPY eax", AX, CX) == true
244 /// isNopCopy("ecx = COPY eax", AH, CL) == false
245 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
246                       unsigned Def, const TargetRegisterInfo *TRI) {
247   unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
248   unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
249   if (Src == PreviousSrc) {
250     assert(Def == PreviousDef);
251     return true;
252   }
253   if (!TRI->isSubRegister(PreviousSrc, Src))
254     return false;
255   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
256   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
257 }
258 
259 /// Remove instruction \p Copy if there exists a previous copy that copies the
260 /// register \p Src to the register \p Def; This may happen indirectly by
261 /// copying the super registers.
262 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
263                                               unsigned Def) {
264   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
265   // the value (Example: The sparc zero register is writable but stays zero).
266   if (MRI->isReserved(Src) || MRI->isReserved(Def))
267     return false;
268 
269   // Search for an existing copy.
270   MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
271   if (!PrevCopy)
272     return false;
273 
274   // Check that the existing copy uses the correct sub registers.
275   if (PrevCopy->getOperand(0).isDead())
276     return false;
277   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
278     return false;
279 
280   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
281 
282   // Copy was redundantly redefining either Src or Def. Remove earlier kill
283   // flags between Copy and PrevCopy because the value will be reused now.
284   assert(Copy.isCopy());
285   unsigned CopyDef = Copy.getOperand(0).getReg();
286   assert(CopyDef == Src || CopyDef == Def);
287   for (MachineInstr &MI :
288        make_range(PrevCopy->getIterator(), Copy.getIterator()))
289     MI.clearRegisterKills(CopyDef, TRI);
290 
291   Copy.eraseFromParent();
292   Changed = true;
293   ++NumDeletes;
294   return true;
295 }
296 
297 /// Decide whether we should forward the source of \param Copy to its use in
298 /// \param UseI based on the physical register class constraints of the opcode
299 /// and avoiding introducing more cross-class COPYs.
300 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
301                                                        const MachineInstr &UseI,
302                                                        unsigned UseIdx) {
303 
304   unsigned CopySrcReg = Copy.getOperand(1).getReg();
305 
306   // If the new register meets the opcode register constraints, then allow
307   // forwarding.
308   if (const TargetRegisterClass *URC =
309           UseI.getRegClassConstraint(UseIdx, TII, TRI))
310     return URC->contains(CopySrcReg);
311 
312   if (!UseI.isCopy())
313     return false;
314 
315   /// COPYs don't have register class constraints, so if the user instruction
316   /// is a COPY, we just try to avoid introducing additional cross-class
317   /// COPYs.  For example:
318   ///
319   ///   RegClassA = COPY RegClassB  // Copy parameter
320   ///   ...
321   ///   RegClassB = COPY RegClassA  // UseI parameter
322   ///
323   /// which after forwarding becomes
324   ///
325   ///   RegClassA = COPY RegClassB
326   ///   ...
327   ///   RegClassB = COPY RegClassB
328   ///
329   /// so we have reduced the number of cross-class COPYs and potentially
330   /// introduced a nop COPY that can be removed.
331   const TargetRegisterClass *UseDstRC =
332       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
333 
334   const TargetRegisterClass *SuperRC = UseDstRC;
335   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
336        SuperRC; SuperRC = *SuperRCI++)
337     if (SuperRC->contains(CopySrcReg))
338       return true;
339 
340   return false;
341 }
342 
343 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
344 /// operand (the register being replaced), since these can sometimes be
345 /// implicitly tied to other operands.  For example, on AMDGPU:
346 ///
347 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
348 ///
349 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
350 /// way of knowing we need to update the latter when updating the former.
351 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
352                                                 const MachineOperand &Use) {
353   for (const MachineOperand &MIUse : MI.uses())
354     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
355         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
356       return true;
357 
358   return false;
359 }
360 
361 /// Look for available copies whose destination register is used by \p MI and
362 /// replace the use in \p MI with the copy's source register.
363 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
364   if (!Tracker.hasAnyCopies())
365     return;
366 
367   // Look for non-tied explicit vreg uses that have an active COPY
368   // instruction that defines the physical register allocated to them.
369   // Replace the vreg with the source of the active COPY.
370   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
371        ++OpIdx) {
372     MachineOperand &MOUse = MI.getOperand(OpIdx);
373     // Don't forward into undef use operands since doing so can cause problems
374     // with the machine verifier, since it doesn't treat undef reads as reads,
375     // so we can end up with a live range that ends on an undef read, leading to
376     // an error that the live range doesn't end on a read of the live range
377     // register.
378     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
379         MOUse.isImplicit())
380       continue;
381 
382     if (!MOUse.getReg())
383       continue;
384 
385     // Check that the register is marked 'renamable' so we know it is safe to
386     // rename it without violating any constraints that aren't expressed in the
387     // IR (e.g. ABI or opcode requirements).
388     if (!MOUse.isRenamable())
389       continue;
390 
391     MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI);
392     if (!Copy)
393       continue;
394 
395     unsigned CopyDstReg = Copy->getOperand(0).getReg();
396     const MachineOperand &CopySrc = Copy->getOperand(1);
397     unsigned CopySrcReg = CopySrc.getReg();
398 
399     // FIXME: Don't handle partial uses of wider COPYs yet.
400     if (MOUse.getReg() != CopyDstReg) {
401       LLVM_DEBUG(
402           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
403                  << MI);
404       continue;
405     }
406 
407     // Don't forward COPYs of reserved regs unless they are constant.
408     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
409       continue;
410 
411     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
412       continue;
413 
414     if (hasImplicitOverlap(MI, MOUse))
415       continue;
416 
417     if (!DebugCounter::shouldExecute(FwdCounter)) {
418       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
419                         << MI);
420       continue;
421     }
422 
423     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
424                       << "\n     with " << printReg(CopySrcReg, TRI)
425                       << "\n     in " << MI << "     from " << *Copy);
426 
427     MOUse.setReg(CopySrcReg);
428     if (!CopySrc.isRenamable())
429       MOUse.setIsRenamable(false);
430 
431     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
432 
433     // Clear kill markers that may have been invalidated.
434     for (MachineInstr &KMI :
435          make_range(Copy->getIterator(), std::next(MI.getIterator())))
436       KMI.clearRegisterKills(CopySrcReg, TRI);
437 
438     ++NumCopyForwards;
439     Changed = true;
440   }
441 }
442 
443 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
444   LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
445 
446   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
447     MachineInstr *MI = &*I;
448     ++I;
449 
450     // Analyze copies (which don't overlap themselves).
451     if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
452                                           MI->getOperand(1).getReg())) {
453       unsigned Def = MI->getOperand(0).getReg();
454       unsigned Src = MI->getOperand(1).getReg();
455 
456       assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
457              !TargetRegisterInfo::isVirtualRegister(Src) &&
458              "MachineCopyPropagation should be run after register allocation!");
459 
460       // The two copies cancel out and the source of the first copy
461       // hasn't been overridden, eliminate the second one. e.g.
462       //  %ecx = COPY %eax
463       //  ... nothing clobbered eax.
464       //  %eax = COPY %ecx
465       // =>
466       //  %ecx = COPY %eax
467       //
468       // or
469       //
470       //  %ecx = COPY %eax
471       //  ... nothing clobbered eax.
472       //  %ecx = COPY %eax
473       // =>
474       //  %ecx = COPY %eax
475       if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
476         continue;
477 
478       forwardUses(*MI);
479 
480       // Src may have been changed by forwardUses()
481       Src = MI->getOperand(1).getReg();
482 
483       // If Src is defined by a previous copy, the previous copy cannot be
484       // eliminated.
485       ReadRegister(Src);
486       for (const MachineOperand &MO : MI->implicit_operands()) {
487         if (!MO.isReg() || !MO.readsReg())
488           continue;
489         unsigned Reg = MO.getReg();
490         if (!Reg)
491           continue;
492         ReadRegister(Reg);
493       }
494 
495       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
496 
497       // Copy is now a candidate for deletion.
498       if (!MRI->isReserved(Def))
499         MaybeDeadCopies.insert(MI);
500 
501       // If 'Def' is previously source of another copy, then this earlier copy's
502       // source is no longer available. e.g.
503       // %xmm9 = copy %xmm2
504       // ...
505       // %xmm2 = copy %xmm0
506       // ...
507       // %xmm2 = copy %xmm9
508       Tracker.clobberRegister(Def, *TRI);
509       for (const MachineOperand &MO : MI->implicit_operands()) {
510         if (!MO.isReg() || !MO.isDef())
511           continue;
512         unsigned Reg = MO.getReg();
513         if (!Reg)
514           continue;
515         Tracker.clobberRegister(Reg, *TRI);
516       }
517 
518       Tracker.trackCopy(MI, *TRI);
519 
520       continue;
521     }
522 
523     // Clobber any earlyclobber regs first.
524     for (const MachineOperand &MO : MI->operands())
525       if (MO.isReg() && MO.isEarlyClobber()) {
526         unsigned Reg = MO.getReg();
527         // If we have a tied earlyclobber, that means it is also read by this
528         // instruction, so we need to make sure we don't remove it as dead
529         // later.
530         if (MO.isTied())
531           ReadRegister(Reg);
532         Tracker.clobberRegister(Reg, *TRI);
533       }
534 
535     forwardUses(*MI);
536 
537     // Not a copy.
538     SmallVector<unsigned, 2> Defs;
539     const MachineOperand *RegMask = nullptr;
540     for (const MachineOperand &MO : MI->operands()) {
541       if (MO.isRegMask())
542         RegMask = &MO;
543       if (!MO.isReg())
544         continue;
545       unsigned Reg = MO.getReg();
546       if (!Reg)
547         continue;
548 
549       assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
550              "MachineCopyPropagation should be run after register allocation!");
551 
552       if (MO.isDef() && !MO.isEarlyClobber()) {
553         Defs.push_back(Reg);
554         continue;
555       } else if (!MO.isDebug() && MO.readsReg())
556         ReadRegister(Reg);
557     }
558 
559     // The instruction has a register mask operand which means that it clobbers
560     // a large set of registers.  Treat clobbered registers the same way as
561     // defined registers.
562     if (RegMask) {
563       // Erase any MaybeDeadCopies whose destination register is clobbered.
564       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
565                MaybeDeadCopies.begin();
566            DI != MaybeDeadCopies.end();) {
567         MachineInstr *MaybeDead = *DI;
568         unsigned Reg = MaybeDead->getOperand(0).getReg();
569         assert(!MRI->isReserved(Reg));
570 
571         if (!RegMask->clobbersPhysReg(Reg)) {
572           ++DI;
573           continue;
574         }
575 
576         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
577                    MaybeDead->dump());
578 
579         // Make sure we invalidate any entries in the copy maps before erasing
580         // the instruction.
581         Tracker.clobberRegister(Reg, *TRI);
582 
583         // erase() will return the next valid iterator pointing to the next
584         // element after the erased one.
585         DI = MaybeDeadCopies.erase(DI);
586         MaybeDead->eraseFromParent();
587         Changed = true;
588         ++NumDeletes;
589       }
590     }
591 
592     // Any previous copy definition or reading the Defs is no longer available.
593     for (unsigned Reg : Defs)
594       Tracker.clobberRegister(Reg, *TRI);
595   }
596 
597   // If MBB doesn't have successors, delete the copies whose defs are not used.
598   // If MBB does have successors, then conservative assume the defs are live-out
599   // since we don't want to trust live-in lists.
600   if (MBB.succ_empty()) {
601     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
602       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
603                  MaybeDead->dump());
604       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
605       MaybeDead->eraseFromParent();
606       Changed = true;
607       ++NumDeletes;
608     }
609   }
610 
611   MaybeDeadCopies.clear();
612   Tracker.clear();
613 }
614 
615 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
616   if (skipFunction(MF.getFunction()))
617     return false;
618 
619   Changed = false;
620 
621   TRI = MF.getSubtarget().getRegisterInfo();
622   TII = MF.getSubtarget().getInstrInfo();
623   MRI = &MF.getRegInfo();
624 
625   for (MachineBasicBlock &MBB : MF)
626     CopyPropagateBlock(MBB);
627 
628   return Changed;
629 }
630