xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision 7f230feeeac8a67b335f52bd2e900a05c6098f20)
1 //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is an extremely simple MachineInstr-level copy propagation pass.
10 //
11 // This pass forwards the source of COPYs to the users of their destinations
12 // when doing so is legal.  For example:
13 //
14 //   %reg1 = COPY %reg0
15 //   ...
16 //   ... = OP %reg1
17 //
18 // If
19 //   - %reg0 has not been clobbered by the time of the use of %reg1
20 //   - the register class constraints are satisfied
21 //   - the COPY def is the only value that reaches OP
22 // then this pass replaces the above with:
23 //
24 //   %reg1 = COPY %reg0
25 //   ...
26 //   ... = OP %reg0
27 //
28 // This pass also removes some redundant COPYs.  For example:
29 //
30 //    %R1 = COPY %R0
31 //    ... // No clobber of %R1
32 //    %R0 = COPY %R1 <<< Removed
33 //
34 // or
35 //
36 //    %R1 = COPY %R0
37 //    ... // No clobber of %R0
38 //    %R1 = COPY %R0 <<< Removed
39 //
40 // or
41 //
42 //    $R0 = OP ...
43 //    ... // No read/clobber of $R0 and $R1
44 //    $R1 = COPY $R0 // $R0 is killed
45 // Replace $R0 with $R1 and remove the COPY
46 //    $R1 = OP ...
47 //    ...
48 //
49 //===----------------------------------------------------------------------===//
50 
51 #include "llvm/ADT/DenseMap.h"
52 #include "llvm/ADT/STLExtras.h"
53 #include "llvm/ADT/SetVector.h"
54 #include "llvm/ADT/SmallSet.h"
55 #include "llvm/ADT/SmallVector.h"
56 #include "llvm/ADT/Statistic.h"
57 #include "llvm/ADT/iterator_range.h"
58 #include "llvm/CodeGen/MachineBasicBlock.h"
59 #include "llvm/CodeGen/MachineFunction.h"
60 #include "llvm/CodeGen/MachineFunctionPass.h"
61 #include "llvm/CodeGen/MachineInstr.h"
62 #include "llvm/CodeGen/MachineOperand.h"
63 #include "llvm/CodeGen/MachineRegisterInfo.h"
64 #include "llvm/CodeGen/TargetRegisterInfo.h"
65 #include "llvm/CodeGen/TargetSubtargetInfo.h"
66 #include "llvm/InitializePasses.h"
67 #include "llvm/MC/MCRegisterInfo.h"
68 #include "llvm/Pass.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Support/DebugCounter.h"
71 #include "llvm/Support/raw_ostream.h"
72 #include <cassert>
73 #include <iterator>
74 
75 using namespace llvm;
76 
77 #define DEBUG_TYPE "machine-cp"
78 
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");
82 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
83               "Controls which register COPYs are forwarded");
84 
85 namespace {
86 
87 class CopyTracker {
88   struct CopyInfo {
89     MachineInstr *MI;
90     SmallVector<MCRegister, 4> DefRegs;
91     bool Avail;
92   };
93 
94   DenseMap<MCRegister, CopyInfo> Copies;
95 
96 public:
97   /// Mark all of the given registers and their subregisters as unavailable for
98   /// copying.
99   void markRegsUnavailable(ArrayRef<MCRegister> Regs,
100                            const TargetRegisterInfo &TRI) {
101     for (MCRegister Reg : Regs) {
102       // Source of copy is no longer available for propagation.
103       for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
104         auto CI = Copies.find(*RUI);
105         if (CI != Copies.end())
106           CI->second.Avail = false;
107       }
108     }
109   }
110 
111   /// Remove register from copy maps.
112   void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
113     // Since Reg might be a subreg of some registers, only invalidate Reg is not
114     // enough. We have to find the COPY defines Reg or registers defined by Reg
115     // and invalidate all of them.
116     SmallSet<MCRegister, 8> RegsToInvalidate;
117     RegsToInvalidate.insert(Reg);
118     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
119       auto I = Copies.find(*RUI);
120       if (I != Copies.end()) {
121         if (MachineInstr *MI = I->second.MI) {
122           RegsToInvalidate.insert(MI->getOperand(0).getReg().asMCReg());
123           RegsToInvalidate.insert(MI->getOperand(1).getReg().asMCReg());
124         }
125         RegsToInvalidate.insert(I->second.DefRegs.begin(),
126                                 I->second.DefRegs.end());
127       }
128     }
129     for (MCRegister InvalidReg : RegsToInvalidate)
130       for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
131         Copies.erase(*RUI);
132   }
133 
134   /// Clobber a single register, removing it from the tracker's copy maps.
135   void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
136     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
137       auto I = Copies.find(*RUI);
138       if (I != Copies.end()) {
139         // When we clobber the source of a copy, we need to clobber everything
140         // it defined.
141         markRegsUnavailable(I->second.DefRegs, TRI);
142         // When we clobber the destination of a copy, we need to clobber the
143         // whole register it defined.
144         if (MachineInstr *MI = I->second.MI)
145           markRegsUnavailable({MI->getOperand(0).getReg().asMCReg()}, TRI);
146         // Now we can erase the copy.
147         Copies.erase(I);
148       }
149     }
150   }
151 
152   /// Add this copy's registers into the tracker's copy maps.
153   void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
154     assert(MI->isCopy() && "Tracking non-copy?");
155 
156     MCRegister Def = MI->getOperand(0).getReg().asMCReg();
157     MCRegister Src = MI->getOperand(1).getReg().asMCReg();
158 
159     // Remember Def is defined by the copy.
160     for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
161       Copies[*RUI] = {MI, {}, true};
162 
163     // Remember source that's copied to Def. Once it's clobbered, then
164     // it's no longer available for copy propagation.
165     for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
166       auto I = Copies.insert({*RUI, {nullptr, {}, false}});
167       auto &Copy = I.first->second;
168       if (!is_contained(Copy.DefRegs, Def))
169         Copy.DefRegs.push_back(Def);
170     }
171   }
172 
173   bool hasAnyCopies() {
174     return !Copies.empty();
175   }
176 
177   MachineInstr *findCopyForUnit(MCRegister RegUnit,
178                                 const TargetRegisterInfo &TRI,
179                                 bool MustBeAvailable = false) {
180     auto CI = Copies.find(RegUnit);
181     if (CI == Copies.end())
182       return nullptr;
183     if (MustBeAvailable && !CI->second.Avail)
184       return nullptr;
185     return CI->second.MI;
186   }
187 
188   MachineInstr *findCopyDefViaUnit(MCRegister RegUnit,
189                                    const TargetRegisterInfo &TRI) {
190     auto CI = Copies.find(RegUnit);
191     if (CI == Copies.end())
192       return nullptr;
193     if (CI->second.DefRegs.size() != 1)
194       return nullptr;
195     MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI);
196     return findCopyForUnit(*RUI, TRI, true);
197   }
198 
199   MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg,
200                                       const TargetRegisterInfo &TRI) {
201     MCRegUnitIterator RUI(Reg, &TRI);
202     MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
203     if (!AvailCopy ||
204         !TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg))
205       return nullptr;
206 
207     Register AvailSrc = AvailCopy->getOperand(1).getReg();
208     Register AvailDef = AvailCopy->getOperand(0).getReg();
209     for (const MachineInstr &MI :
210          make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
211       for (const MachineOperand &MO : MI.operands())
212         if (MO.isRegMask())
213           // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
214           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
215             return nullptr;
216 
217     return AvailCopy;
218   }
219 
220   MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg,
221                               const TargetRegisterInfo &TRI) {
222     // We check the first RegUnit here, since we'll only be interested in the
223     // copy if it copies the entire register anyway.
224     MCRegUnitIterator RUI(Reg, &TRI);
225     MachineInstr *AvailCopy =
226         findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
227     if (!AvailCopy ||
228         !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
229       return nullptr;
230 
231     // Check that the available copy isn't clobbered by any regmasks between
232     // itself and the destination.
233     Register AvailSrc = AvailCopy->getOperand(1).getReg();
234     Register AvailDef = AvailCopy->getOperand(0).getReg();
235     for (const MachineInstr &MI :
236          make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
237       for (const MachineOperand &MO : MI.operands())
238         if (MO.isRegMask())
239           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
240             return nullptr;
241 
242     return AvailCopy;
243   }
244 
245   void clear() {
246     Copies.clear();
247   }
248 };
249 
250 class MachineCopyPropagation : public MachineFunctionPass {
251   const TargetRegisterInfo *TRI;
252   const TargetInstrInfo *TII;
253   const MachineRegisterInfo *MRI;
254 
255 public:
256   static char ID; // Pass identification, replacement for typeid
257 
258   MachineCopyPropagation() : MachineFunctionPass(ID) {
259     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
260   }
261 
262   void getAnalysisUsage(AnalysisUsage &AU) const override {
263     AU.setPreservesCFG();
264     MachineFunctionPass::getAnalysisUsage(AU);
265   }
266 
267   bool runOnMachineFunction(MachineFunction &MF) override;
268 
269   MachineFunctionProperties getRequiredProperties() const override {
270     return MachineFunctionProperties().set(
271         MachineFunctionProperties::Property::NoVRegs);
272   }
273 
274 private:
275   typedef enum { DebugUse = false, RegularUse = true } DebugType;
276 
277   void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT);
278   void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
279   void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
280   bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def);
281   void forwardUses(MachineInstr &MI);
282   void propagateDefs(MachineInstr &MI);
283   bool isForwardableRegClassCopy(const MachineInstr &Copy,
284                                  const MachineInstr &UseI, unsigned UseIdx);
285   bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
286                                           const MachineInstr &UseI,
287                                           unsigned UseIdx);
288   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
289   bool hasOverlappingMultipleDef(const MachineInstr &MI,
290                                  const MachineOperand &MODef, Register Def);
291 
292   /// Candidates for deletion.
293   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
294 
295   /// Multimap tracking debug users in current BB
296   DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers;
297 
298   CopyTracker Tracker;
299 
300   bool Changed;
301 };
302 
303 } // end anonymous namespace
304 
305 char MachineCopyPropagation::ID = 0;
306 
307 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
308 
309 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
310                 "Machine Copy Propagation Pass", false, false)
311 
312 void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
313                                           DebugType DT) {
314   // If 'Reg' is defined by a copy, the copy is no longer a candidate
315   // for elimination. If a copy is "read" by a debug user, record the user
316   // for propagation.
317   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
318     if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
319       if (DT == RegularUse) {
320         LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
321         MaybeDeadCopies.remove(Copy);
322       } else {
323         CopyDbgUsers[Copy].insert(&Reader);
324       }
325     }
326   }
327 }
328 
329 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
330 /// This fact may have been obscured by sub register usage or may not be true at
331 /// all even though Src and Def are subregisters of the registers used in
332 /// PreviousCopy. e.g.
333 /// isNopCopy("ecx = COPY eax", AX, CX) == true
334 /// isNopCopy("ecx = COPY eax", AH, CL) == false
335 static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
336                       MCRegister Def, const TargetRegisterInfo *TRI) {
337   MCRegister PreviousSrc = PreviousCopy.getOperand(1).getReg().asMCReg();
338   MCRegister PreviousDef = PreviousCopy.getOperand(0).getReg().asMCReg();
339   if (Src == PreviousSrc && Def == PreviousDef)
340     return true;
341   if (!TRI->isSubRegister(PreviousSrc, Src))
342     return false;
343   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
344   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
345 }
346 
347 /// Remove instruction \p Copy if there exists a previous copy that copies the
348 /// register \p Src to the register \p Def; This may happen indirectly by
349 /// copying the super registers.
350 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
351                                               MCRegister Src, MCRegister Def) {
352   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
353   // the value (Example: The sparc zero register is writable but stays zero).
354   if (MRI->isReserved(Src) || MRI->isReserved(Def))
355     return false;
356 
357   // Search for an existing copy.
358   MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
359   if (!PrevCopy)
360     return false;
361 
362   // Check that the existing copy uses the correct sub registers.
363   if (PrevCopy->getOperand(0).isDead())
364     return false;
365   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
366     return false;
367 
368   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
369 
370   // Copy was redundantly redefining either Src or Def. Remove earlier kill
371   // flags between Copy and PrevCopy because the value will be reused now.
372   assert(Copy.isCopy());
373   Register CopyDef = Copy.getOperand(0).getReg();
374   assert(CopyDef == Src || CopyDef == Def);
375   for (MachineInstr &MI :
376        make_range(PrevCopy->getIterator(), Copy.getIterator()))
377     MI.clearRegisterKills(CopyDef, TRI);
378 
379   Copy.eraseFromParent();
380   Changed = true;
381   ++NumDeletes;
382   return true;
383 }
384 
385 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
386     const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
387   Register Def = Copy.getOperand(0).getReg();
388 
389   if (const TargetRegisterClass *URC =
390           UseI.getRegClassConstraint(UseIdx, TII, TRI))
391     return URC->contains(Def);
392 
393   // We don't process further if UseI is a COPY, since forward copy propagation
394   // should handle that.
395   return false;
396 }
397 
398 /// Decide whether we should forward the source of \param Copy to its use in
399 /// \param UseI based on the physical register class constraints of the opcode
400 /// and avoiding introducing more cross-class COPYs.
401 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
402                                                        const MachineInstr &UseI,
403                                                        unsigned UseIdx) {
404 
405   Register CopySrcReg = Copy.getOperand(1).getReg();
406 
407   // If the new register meets the opcode register constraints, then allow
408   // forwarding.
409   if (const TargetRegisterClass *URC =
410           UseI.getRegClassConstraint(UseIdx, TII, TRI))
411     return URC->contains(CopySrcReg);
412 
413   if (!UseI.isCopy())
414     return false;
415 
416   const TargetRegisterClass *CopySrcRC =
417       TRI->getMinimalPhysRegClass(CopySrcReg);
418   const TargetRegisterClass *UseDstRC =
419       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
420   const TargetRegisterClass *CrossCopyRC = TRI->getCrossCopyRegClass(CopySrcRC);
421 
422   // If cross copy register class is not the same as copy source register class
423   // then it is not possible to copy the register directly and requires a cross
424   // register class copy. Fowarding this copy without checking register class of
425   // UseDst may create additional cross register copies when expanding the copy
426   // instruction in later passes.
427   if (CopySrcRC != CrossCopyRC) {
428     const TargetRegisterClass *CopyDstRC =
429         TRI->getMinimalPhysRegClass(Copy.getOperand(0).getReg());
430 
431     // Check if UseDstRC matches the necessary register class to copy from
432     // CopySrc's register class. If so then forwarding the copy will not
433     // introduce any cross-class copys. Else if CopyDstRC matches then keep the
434     // copy and do not forward. If neither UseDstRC or CopyDstRC matches then
435     // we may need a cross register copy later but we do not worry about it
436     // here.
437     if (UseDstRC != CrossCopyRC && CopyDstRC == CrossCopyRC)
438       return false;
439   }
440 
441   /// COPYs don't have register class constraints, so if the user instruction
442   /// is a COPY, we just try to avoid introducing additional cross-class
443   /// COPYs.  For example:
444   ///
445   ///   RegClassA = COPY RegClassB  // Copy parameter
446   ///   ...
447   ///   RegClassB = COPY RegClassA  // UseI parameter
448   ///
449   /// which after forwarding becomes
450   ///
451   ///   RegClassA = COPY RegClassB
452   ///   ...
453   ///   RegClassB = COPY RegClassB
454   ///
455   /// so we have reduced the number of cross-class COPYs and potentially
456   /// introduced a nop COPY that can be removed.
457   const TargetRegisterClass *SuperRC = UseDstRC;
458   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
459        SuperRC; SuperRC = *SuperRCI++)
460     if (SuperRC->contains(CopySrcReg))
461       return true;
462 
463   return false;
464 }
465 
466 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
467 /// operand (the register being replaced), since these can sometimes be
468 /// implicitly tied to other operands.  For example, on AMDGPU:
469 ///
470 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
471 ///
472 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
473 /// way of knowing we need to update the latter when updating the former.
474 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
475                                                 const MachineOperand &Use) {
476   for (const MachineOperand &MIUse : MI.uses())
477     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
478         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
479       return true;
480 
481   return false;
482 }
483 
484 /// For an MI that has multiple definitions, check whether \p MI has
485 /// a definition that overlaps with another of its definitions.
486 /// For example, on ARM: umull   r9, r9, lr, r0
487 /// The umull instruction is unpredictable unless RdHi and RdLo are different.
488 bool MachineCopyPropagation::hasOverlappingMultipleDef(
489     const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
490   for (const MachineOperand &MIDef : MI.defs()) {
491     if ((&MIDef != &MODef) && MIDef.isReg() &&
492         TRI->regsOverlap(Def, MIDef.getReg()))
493       return true;
494   }
495 
496   return false;
497 }
498 
499 /// Look for available copies whose destination register is used by \p MI and
500 /// replace the use in \p MI with the copy's source register.
501 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
502   if (!Tracker.hasAnyCopies())
503     return;
504 
505   // Look for non-tied explicit vreg uses that have an active COPY
506   // instruction that defines the physical register allocated to them.
507   // Replace the vreg with the source of the active COPY.
508   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
509        ++OpIdx) {
510     MachineOperand &MOUse = MI.getOperand(OpIdx);
511     // Don't forward into undef use operands since doing so can cause problems
512     // with the machine verifier, since it doesn't treat undef reads as reads,
513     // so we can end up with a live range that ends on an undef read, leading to
514     // an error that the live range doesn't end on a read of the live range
515     // register.
516     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
517         MOUse.isImplicit())
518       continue;
519 
520     if (!MOUse.getReg())
521       continue;
522 
523     // Check that the register is marked 'renamable' so we know it is safe to
524     // rename it without violating any constraints that aren't expressed in the
525     // IR (e.g. ABI or opcode requirements).
526     if (!MOUse.isRenamable())
527       continue;
528 
529     MachineInstr *Copy =
530         Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), *TRI);
531     if (!Copy)
532       continue;
533 
534     Register CopyDstReg = Copy->getOperand(0).getReg();
535     const MachineOperand &CopySrc = Copy->getOperand(1);
536     Register CopySrcReg = CopySrc.getReg();
537 
538     // FIXME: Don't handle partial uses of wider COPYs yet.
539     if (MOUse.getReg() != CopyDstReg) {
540       LLVM_DEBUG(
541           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
542                  << MI);
543       continue;
544     }
545 
546     // Don't forward COPYs of reserved regs unless they are constant.
547     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
548       continue;
549 
550     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
551       continue;
552 
553     if (hasImplicitOverlap(MI, MOUse))
554       continue;
555 
556     // Check that the instruction is not a copy that partially overwrites the
557     // original copy source that we are about to use. The tracker mechanism
558     // cannot cope with that.
559     if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
560         !MI.definesRegister(CopySrcReg)) {
561       LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
562       continue;
563     }
564 
565     if (!DebugCounter::shouldExecute(FwdCounter)) {
566       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
567                         << MI);
568       continue;
569     }
570 
571     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
572                       << "\n     with " << printReg(CopySrcReg, TRI)
573                       << "\n     in " << MI << "     from " << *Copy);
574 
575     MOUse.setReg(CopySrcReg);
576     if (!CopySrc.isRenamable())
577       MOUse.setIsRenamable(false);
578     MOUse.setIsUndef(CopySrc.isUndef());
579 
580     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
581 
582     // Clear kill markers that may have been invalidated.
583     for (MachineInstr &KMI :
584          make_range(Copy->getIterator(), std::next(MI.getIterator())))
585       KMI.clearRegisterKills(CopySrcReg, TRI);
586 
587     ++NumCopyForwards;
588     Changed = true;
589   }
590 }
591 
592 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
593   LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
594                     << "\n");
595 
596   for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
597     // Analyze copies (which don't overlap themselves).
598     if (MI.isCopy() && !TRI->regsOverlap(MI.getOperand(0).getReg(),
599                                          MI.getOperand(1).getReg())) {
600       assert(MI.getOperand(0).getReg().isPhysical() &&
601              MI.getOperand(1).getReg().isPhysical() &&
602              "MachineCopyPropagation should be run after register allocation!");
603 
604       MCRegister Def = MI.getOperand(0).getReg().asMCReg();
605       MCRegister Src = MI.getOperand(1).getReg().asMCReg();
606 
607       // The two copies cancel out and the source of the first copy
608       // hasn't been overridden, eliminate the second one. e.g.
609       //  %ecx = COPY %eax
610       //  ... nothing clobbered eax.
611       //  %eax = COPY %ecx
612       // =>
613       //  %ecx = COPY %eax
614       //
615       // or
616       //
617       //  %ecx = COPY %eax
618       //  ... nothing clobbered eax.
619       //  %ecx = COPY %eax
620       // =>
621       //  %ecx = COPY %eax
622       if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
623         continue;
624 
625       forwardUses(MI);
626 
627       // Src may have been changed by forwardUses()
628       Src = MI.getOperand(1).getReg().asMCReg();
629 
630       // If Src is defined by a previous copy, the previous copy cannot be
631       // eliminated.
632       ReadRegister(Src, MI, RegularUse);
633       for (const MachineOperand &MO : MI.implicit_operands()) {
634         if (!MO.isReg() || !MO.readsReg())
635           continue;
636         MCRegister Reg = MO.getReg().asMCReg();
637         if (!Reg)
638           continue;
639         ReadRegister(Reg, MI, RegularUse);
640       }
641 
642       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
643 
644       // Copy is now a candidate for deletion.
645       if (!MRI->isReserved(Def))
646         MaybeDeadCopies.insert(&MI);
647 
648       // If 'Def' is previously source of another copy, then this earlier copy's
649       // source is no longer available. e.g.
650       // %xmm9 = copy %xmm2
651       // ...
652       // %xmm2 = copy %xmm0
653       // ...
654       // %xmm2 = copy %xmm9
655       Tracker.clobberRegister(Def, *TRI);
656       for (const MachineOperand &MO : MI.implicit_operands()) {
657         if (!MO.isReg() || !MO.isDef())
658           continue;
659         MCRegister Reg = MO.getReg().asMCReg();
660         if (!Reg)
661           continue;
662         Tracker.clobberRegister(Reg, *TRI);
663       }
664 
665       Tracker.trackCopy(&MI, *TRI);
666 
667       continue;
668     }
669 
670     // Clobber any earlyclobber regs first.
671     for (const MachineOperand &MO : MI.operands())
672       if (MO.isReg() && MO.isEarlyClobber()) {
673         MCRegister Reg = MO.getReg().asMCReg();
674         // If we have a tied earlyclobber, that means it is also read by this
675         // instruction, so we need to make sure we don't remove it as dead
676         // later.
677         if (MO.isTied())
678           ReadRegister(Reg, MI, RegularUse);
679         Tracker.clobberRegister(Reg, *TRI);
680       }
681 
682     forwardUses(MI);
683 
684     // Not a copy.
685     SmallVector<Register, 2> Defs;
686     const MachineOperand *RegMask = nullptr;
687     for (const MachineOperand &MO : MI.operands()) {
688       if (MO.isRegMask())
689         RegMask = &MO;
690       if (!MO.isReg())
691         continue;
692       Register Reg = MO.getReg();
693       if (!Reg)
694         continue;
695 
696       assert(!Reg.isVirtual() &&
697              "MachineCopyPropagation should be run after register allocation!");
698 
699       if (MO.isDef() && !MO.isEarlyClobber()) {
700         Defs.push_back(Reg.asMCReg());
701         continue;
702       } else if (MO.readsReg())
703         ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
704     }
705 
706     // The instruction has a register mask operand which means that it clobbers
707     // a large set of registers.  Treat clobbered registers the same way as
708     // defined registers.
709     if (RegMask) {
710       // Erase any MaybeDeadCopies whose destination register is clobbered.
711       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
712                MaybeDeadCopies.begin();
713            DI != MaybeDeadCopies.end();) {
714         MachineInstr *MaybeDead = *DI;
715         MCRegister Reg = MaybeDead->getOperand(0).getReg().asMCReg();
716         assert(!MRI->isReserved(Reg));
717 
718         if (!RegMask->clobbersPhysReg(Reg)) {
719           ++DI;
720           continue;
721         }
722 
723         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
724                    MaybeDead->dump());
725 
726         // Make sure we invalidate any entries in the copy maps before erasing
727         // the instruction.
728         Tracker.clobberRegister(Reg, *TRI);
729 
730         // erase() will return the next valid iterator pointing to the next
731         // element after the erased one.
732         DI = MaybeDeadCopies.erase(DI);
733         MaybeDead->eraseFromParent();
734         Changed = true;
735         ++NumDeletes;
736       }
737     }
738 
739     // Any previous copy definition or reading the Defs is no longer available.
740     for (MCRegister Reg : Defs)
741       Tracker.clobberRegister(Reg, *TRI);
742   }
743 
744   // If MBB doesn't have successors, delete the copies whose defs are not used.
745   // If MBB does have successors, then conservative assume the defs are live-out
746   // since we don't want to trust live-in lists.
747   if (MBB.succ_empty()) {
748     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
749       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
750                  MaybeDead->dump());
751       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
752 
753       // Update matching debug values, if any.
754       assert(MaybeDead->isCopy());
755       Register SrcReg = MaybeDead->getOperand(1).getReg();
756       Register DestReg = MaybeDead->getOperand(0).getReg();
757       SmallVector<MachineInstr *> MaybeDeadDbgUsers(
758           CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
759       MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
760                                MaybeDeadDbgUsers);
761 
762       MaybeDead->eraseFromParent();
763       Changed = true;
764       ++NumDeletes;
765     }
766   }
767 
768   MaybeDeadCopies.clear();
769   CopyDbgUsers.clear();
770   Tracker.clear();
771 }
772 
773 static bool isBackwardPropagatableCopy(MachineInstr &MI,
774                                        const MachineRegisterInfo &MRI) {
775   assert(MI.isCopy() && "MI is expected to be a COPY");
776   Register Def = MI.getOperand(0).getReg();
777   Register Src = MI.getOperand(1).getReg();
778 
779   if (!Def || !Src)
780     return false;
781 
782   if (MRI.isReserved(Def) || MRI.isReserved(Src))
783     return false;
784 
785   return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
786 }
787 
788 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
789   if (!Tracker.hasAnyCopies())
790     return;
791 
792   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
793        ++OpIdx) {
794     MachineOperand &MODef = MI.getOperand(OpIdx);
795 
796     if (!MODef.isReg() || MODef.isUse())
797       continue;
798 
799     // Ignore non-trivial cases.
800     if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
801       continue;
802 
803     if (!MODef.getReg())
804       continue;
805 
806     // We only handle if the register comes from a vreg.
807     if (!MODef.isRenamable())
808       continue;
809 
810     MachineInstr *Copy =
811         Tracker.findAvailBackwardCopy(MI, MODef.getReg().asMCReg(), *TRI);
812     if (!Copy)
813       continue;
814 
815     Register Def = Copy->getOperand(0).getReg();
816     Register Src = Copy->getOperand(1).getReg();
817 
818     if (MODef.getReg() != Src)
819       continue;
820 
821     if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
822       continue;
823 
824     if (hasImplicitOverlap(MI, MODef))
825       continue;
826 
827     if (hasOverlappingMultipleDef(MI, MODef, Def))
828       continue;
829 
830     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
831                       << "\n     with " << printReg(Def, TRI) << "\n     in "
832                       << MI << "     from " << *Copy);
833 
834     MODef.setReg(Def);
835     MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
836 
837     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
838     MaybeDeadCopies.insert(Copy);
839     Changed = true;
840     ++NumCopyBackwardPropagated;
841   }
842 }
843 
844 void MachineCopyPropagation::BackwardCopyPropagateBlock(
845     MachineBasicBlock &MBB) {
846   LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
847                     << "\n");
848 
849   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
850     // Ignore non-trivial COPYs.
851     if (MI.isCopy() && MI.getNumOperands() == 2 &&
852         !TRI->regsOverlap(MI.getOperand(0).getReg(),
853                           MI.getOperand(1).getReg())) {
854 
855       MCRegister Def = MI.getOperand(0).getReg().asMCReg();
856       MCRegister Src = MI.getOperand(1).getReg().asMCReg();
857 
858       // Unlike forward cp, we don't invoke propagateDefs here,
859       // just let forward cp do COPY-to-COPY propagation.
860       if (isBackwardPropagatableCopy(MI, *MRI)) {
861         Tracker.invalidateRegister(Src, *TRI);
862         Tracker.invalidateRegister(Def, *TRI);
863         Tracker.trackCopy(&MI, *TRI);
864         continue;
865       }
866     }
867 
868     // Invalidate any earlyclobber regs first.
869     for (const MachineOperand &MO : MI.operands())
870       if (MO.isReg() && MO.isEarlyClobber()) {
871         MCRegister Reg = MO.getReg().asMCReg();
872         if (!Reg)
873           continue;
874         Tracker.invalidateRegister(Reg, *TRI);
875       }
876 
877     propagateDefs(MI);
878     for (const MachineOperand &MO : MI.operands()) {
879       if (!MO.isReg())
880         continue;
881 
882       if (!MO.getReg())
883         continue;
884 
885       if (MO.isDef())
886         Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
887 
888       if (MO.readsReg()) {
889         if (MO.isDebug()) {
890           //  Check if the register in the debug instruction is utilized
891           // in a copy instruction, so we can update the debug info if the
892           // register is changed.
893           for (MCRegUnitIterator RUI(MO.getReg().asMCReg(), TRI); RUI.isValid();
894                ++RUI) {
895             if (auto *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
896               CopyDbgUsers[Copy].insert(&MI);
897             }
898           }
899         } else {
900           Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
901         }
902       }
903     }
904   }
905 
906   for (auto *Copy : MaybeDeadCopies) {
907 
908     Register Src = Copy->getOperand(1).getReg();
909     Register Def = Copy->getOperand(0).getReg();
910     SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
911                                                   CopyDbgUsers[Copy].end());
912 
913     MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
914     Copy->eraseFromParent();
915     ++NumDeletes;
916   }
917 
918   MaybeDeadCopies.clear();
919   CopyDbgUsers.clear();
920   Tracker.clear();
921 }
922 
923 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
924   if (skipFunction(MF.getFunction()))
925     return false;
926 
927   Changed = false;
928 
929   TRI = MF.getSubtarget().getRegisterInfo();
930   TII = MF.getSubtarget().getInstrInfo();
931   MRI = &MF.getRegInfo();
932 
933   for (MachineBasicBlock &MBB : MF) {
934     BackwardCopyPropagateBlock(MBB);
935     ForwardCopyPropagateBlock(MBB);
936   }
937 
938   return Changed;
939 }
940