xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision 1bb3a9c6428ea460bcdc2d9eed43866a9a5e2fa8)
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   /// COPYs don't have register class constraints, so if the user instruction
417   /// is a COPY, we just try to avoid introducing additional cross-class
418   /// COPYs.  For example:
419   ///
420   ///   RegClassA = COPY RegClassB  // Copy parameter
421   ///   ...
422   ///   RegClassB = COPY RegClassA  // UseI parameter
423   ///
424   /// which after forwarding becomes
425   ///
426   ///   RegClassA = COPY RegClassB
427   ///   ...
428   ///   RegClassB = COPY RegClassB
429   ///
430   /// so we have reduced the number of cross-class COPYs and potentially
431   /// introduced a nop COPY that can be removed.
432 
433   // Allow forwarding if src and dst belong to any common class, so long as they
434   // don't belong to any (possibly smaller) common class that requires copies to
435   // go via a different class.
436   Register UseDstReg = UseI.getOperand(0).getReg();
437   bool Found = false;
438   bool IsCrossClass = false;
439   for (const TargetRegisterClass *RC : TRI->regclasses()) {
440     if (RC->contains(CopySrcReg) && RC->contains(UseDstReg)) {
441       Found = true;
442       if (TRI->getCrossCopyRegClass(RC) != RC) {
443         IsCrossClass = true;
444         break;
445       }
446     }
447   }
448   if (!Found)
449     return false;
450   if (!IsCrossClass)
451     return true;
452   // The forwarded copy would be cross-class. Only do this if the original copy
453   // was also cross-class.
454   Register CopyDstReg = Copy.getOperand(0).getReg();
455   for (const TargetRegisterClass *RC : TRI->regclasses()) {
456     if (RC->contains(CopySrcReg) && RC->contains(CopyDstReg) &&
457         TRI->getCrossCopyRegClass(RC) != RC)
458       return true;
459   }
460   return false;
461 }
462 
463 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
464 /// operand (the register being replaced), since these can sometimes be
465 /// implicitly tied to other operands.  For example, on AMDGPU:
466 ///
467 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
468 ///
469 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
470 /// way of knowing we need to update the latter when updating the former.
471 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
472                                                 const MachineOperand &Use) {
473   for (const MachineOperand &MIUse : MI.uses())
474     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
475         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
476       return true;
477 
478   return false;
479 }
480 
481 /// For an MI that has multiple definitions, check whether \p MI has
482 /// a definition that overlaps with another of its definitions.
483 /// For example, on ARM: umull   r9, r9, lr, r0
484 /// The umull instruction is unpredictable unless RdHi and RdLo are different.
485 bool MachineCopyPropagation::hasOverlappingMultipleDef(
486     const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
487   for (const MachineOperand &MIDef : MI.defs()) {
488     if ((&MIDef != &MODef) && MIDef.isReg() &&
489         TRI->regsOverlap(Def, MIDef.getReg()))
490       return true;
491   }
492 
493   return false;
494 }
495 
496 /// Look for available copies whose destination register is used by \p MI and
497 /// replace the use in \p MI with the copy's source register.
498 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
499   if (!Tracker.hasAnyCopies())
500     return;
501 
502   // Look for non-tied explicit vreg uses that have an active COPY
503   // instruction that defines the physical register allocated to them.
504   // Replace the vreg with the source of the active COPY.
505   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
506        ++OpIdx) {
507     MachineOperand &MOUse = MI.getOperand(OpIdx);
508     // Don't forward into undef use operands since doing so can cause problems
509     // with the machine verifier, since it doesn't treat undef reads as reads,
510     // so we can end up with a live range that ends on an undef read, leading to
511     // an error that the live range doesn't end on a read of the live range
512     // register.
513     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
514         MOUse.isImplicit())
515       continue;
516 
517     if (!MOUse.getReg())
518       continue;
519 
520     // Check that the register is marked 'renamable' so we know it is safe to
521     // rename it without violating any constraints that aren't expressed in the
522     // IR (e.g. ABI or opcode requirements).
523     if (!MOUse.isRenamable())
524       continue;
525 
526     MachineInstr *Copy =
527         Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), *TRI);
528     if (!Copy)
529       continue;
530 
531     Register CopyDstReg = Copy->getOperand(0).getReg();
532     const MachineOperand &CopySrc = Copy->getOperand(1);
533     Register CopySrcReg = CopySrc.getReg();
534 
535     // FIXME: Don't handle partial uses of wider COPYs yet.
536     if (MOUse.getReg() != CopyDstReg) {
537       LLVM_DEBUG(
538           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
539                  << MI);
540       continue;
541     }
542 
543     // Don't forward COPYs of reserved regs unless they are constant.
544     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
545       continue;
546 
547     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
548       continue;
549 
550     if (hasImplicitOverlap(MI, MOUse))
551       continue;
552 
553     // Check that the instruction is not a copy that partially overwrites the
554     // original copy source that we are about to use. The tracker mechanism
555     // cannot cope with that.
556     if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
557         !MI.definesRegister(CopySrcReg)) {
558       LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
559       continue;
560     }
561 
562     if (!DebugCounter::shouldExecute(FwdCounter)) {
563       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
564                         << MI);
565       continue;
566     }
567 
568     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
569                       << "\n     with " << printReg(CopySrcReg, TRI)
570                       << "\n     in " << MI << "     from " << *Copy);
571 
572     MOUse.setReg(CopySrcReg);
573     if (!CopySrc.isRenamable())
574       MOUse.setIsRenamable(false);
575     MOUse.setIsUndef(CopySrc.isUndef());
576 
577     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
578 
579     // Clear kill markers that may have been invalidated.
580     for (MachineInstr &KMI :
581          make_range(Copy->getIterator(), std::next(MI.getIterator())))
582       KMI.clearRegisterKills(CopySrcReg, TRI);
583 
584     ++NumCopyForwards;
585     Changed = true;
586   }
587 }
588 
589 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
590   LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
591                     << "\n");
592 
593   for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
594     // Analyze copies (which don't overlap themselves).
595     if (MI.isCopy() && !TRI->regsOverlap(MI.getOperand(0).getReg(),
596                                          MI.getOperand(1).getReg())) {
597       assert(MI.getOperand(0).getReg().isPhysical() &&
598              MI.getOperand(1).getReg().isPhysical() &&
599              "MachineCopyPropagation should be run after register allocation!");
600 
601       MCRegister Def = MI.getOperand(0).getReg().asMCReg();
602       MCRegister Src = MI.getOperand(1).getReg().asMCReg();
603 
604       // The two copies cancel out and the source of the first copy
605       // hasn't been overridden, eliminate the second one. e.g.
606       //  %ecx = COPY %eax
607       //  ... nothing clobbered eax.
608       //  %eax = COPY %ecx
609       // =>
610       //  %ecx = COPY %eax
611       //
612       // or
613       //
614       //  %ecx = COPY %eax
615       //  ... nothing clobbered eax.
616       //  %ecx = COPY %eax
617       // =>
618       //  %ecx = COPY %eax
619       if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
620         continue;
621 
622       forwardUses(MI);
623 
624       // Src may have been changed by forwardUses()
625       Src = MI.getOperand(1).getReg().asMCReg();
626 
627       // If Src is defined by a previous copy, the previous copy cannot be
628       // eliminated.
629       ReadRegister(Src, MI, RegularUse);
630       for (const MachineOperand &MO : MI.implicit_operands()) {
631         if (!MO.isReg() || !MO.readsReg())
632           continue;
633         MCRegister Reg = MO.getReg().asMCReg();
634         if (!Reg)
635           continue;
636         ReadRegister(Reg, MI, RegularUse);
637       }
638 
639       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
640 
641       // Copy is now a candidate for deletion.
642       if (!MRI->isReserved(Def))
643         MaybeDeadCopies.insert(&MI);
644 
645       // If 'Def' is previously source of another copy, then this earlier copy's
646       // source is no longer available. e.g.
647       // %xmm9 = copy %xmm2
648       // ...
649       // %xmm2 = copy %xmm0
650       // ...
651       // %xmm2 = copy %xmm9
652       Tracker.clobberRegister(Def, *TRI);
653       for (const MachineOperand &MO : MI.implicit_operands()) {
654         if (!MO.isReg() || !MO.isDef())
655           continue;
656         MCRegister Reg = MO.getReg().asMCReg();
657         if (!Reg)
658           continue;
659         Tracker.clobberRegister(Reg, *TRI);
660       }
661 
662       Tracker.trackCopy(&MI, *TRI);
663 
664       continue;
665     }
666 
667     // Clobber any earlyclobber regs first.
668     for (const MachineOperand &MO : MI.operands())
669       if (MO.isReg() && MO.isEarlyClobber()) {
670         MCRegister Reg = MO.getReg().asMCReg();
671         // If we have a tied earlyclobber, that means it is also read by this
672         // instruction, so we need to make sure we don't remove it as dead
673         // later.
674         if (MO.isTied())
675           ReadRegister(Reg, MI, RegularUse);
676         Tracker.clobberRegister(Reg, *TRI);
677       }
678 
679     forwardUses(MI);
680 
681     // Not a copy.
682     SmallVector<Register, 2> Defs;
683     const MachineOperand *RegMask = nullptr;
684     for (const MachineOperand &MO : MI.operands()) {
685       if (MO.isRegMask())
686         RegMask = &MO;
687       if (!MO.isReg())
688         continue;
689       Register Reg = MO.getReg();
690       if (!Reg)
691         continue;
692 
693       assert(!Reg.isVirtual() &&
694              "MachineCopyPropagation should be run after register allocation!");
695 
696       if (MO.isDef() && !MO.isEarlyClobber()) {
697         Defs.push_back(Reg.asMCReg());
698         continue;
699       } else if (MO.readsReg())
700         ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
701     }
702 
703     // The instruction has a register mask operand which means that it clobbers
704     // a large set of registers.  Treat clobbered registers the same way as
705     // defined registers.
706     if (RegMask) {
707       // Erase any MaybeDeadCopies whose destination register is clobbered.
708       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
709                MaybeDeadCopies.begin();
710            DI != MaybeDeadCopies.end();) {
711         MachineInstr *MaybeDead = *DI;
712         MCRegister Reg = MaybeDead->getOperand(0).getReg().asMCReg();
713         assert(!MRI->isReserved(Reg));
714 
715         if (!RegMask->clobbersPhysReg(Reg)) {
716           ++DI;
717           continue;
718         }
719 
720         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
721                    MaybeDead->dump());
722 
723         // Make sure we invalidate any entries in the copy maps before erasing
724         // the instruction.
725         Tracker.clobberRegister(Reg, *TRI);
726 
727         // erase() will return the next valid iterator pointing to the next
728         // element after the erased one.
729         DI = MaybeDeadCopies.erase(DI);
730         MaybeDead->eraseFromParent();
731         Changed = true;
732         ++NumDeletes;
733       }
734     }
735 
736     // Any previous copy definition or reading the Defs is no longer available.
737     for (MCRegister Reg : Defs)
738       Tracker.clobberRegister(Reg, *TRI);
739   }
740 
741   // If MBB doesn't have successors, delete the copies whose defs are not used.
742   // If MBB does have successors, then conservative assume the defs are live-out
743   // since we don't want to trust live-in lists.
744   if (MBB.succ_empty()) {
745     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
746       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
747                  MaybeDead->dump());
748       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
749 
750       // Update matching debug values, if any.
751       assert(MaybeDead->isCopy());
752       Register SrcReg = MaybeDead->getOperand(1).getReg();
753       Register DestReg = MaybeDead->getOperand(0).getReg();
754       SmallVector<MachineInstr *> MaybeDeadDbgUsers(
755           CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
756       MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
757                                MaybeDeadDbgUsers);
758 
759       MaybeDead->eraseFromParent();
760       Changed = true;
761       ++NumDeletes;
762     }
763   }
764 
765   MaybeDeadCopies.clear();
766   CopyDbgUsers.clear();
767   Tracker.clear();
768 }
769 
770 static bool isBackwardPropagatableCopy(MachineInstr &MI,
771                                        const MachineRegisterInfo &MRI) {
772   assert(MI.isCopy() && "MI is expected to be a COPY");
773   Register Def = MI.getOperand(0).getReg();
774   Register Src = MI.getOperand(1).getReg();
775 
776   if (!Def || !Src)
777     return false;
778 
779   if (MRI.isReserved(Def) || MRI.isReserved(Src))
780     return false;
781 
782   return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
783 }
784 
785 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
786   if (!Tracker.hasAnyCopies())
787     return;
788 
789   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
790        ++OpIdx) {
791     MachineOperand &MODef = MI.getOperand(OpIdx);
792 
793     if (!MODef.isReg() || MODef.isUse())
794       continue;
795 
796     // Ignore non-trivial cases.
797     if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
798       continue;
799 
800     if (!MODef.getReg())
801       continue;
802 
803     // We only handle if the register comes from a vreg.
804     if (!MODef.isRenamable())
805       continue;
806 
807     MachineInstr *Copy =
808         Tracker.findAvailBackwardCopy(MI, MODef.getReg().asMCReg(), *TRI);
809     if (!Copy)
810       continue;
811 
812     Register Def = Copy->getOperand(0).getReg();
813     Register Src = Copy->getOperand(1).getReg();
814 
815     if (MODef.getReg() != Src)
816       continue;
817 
818     if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
819       continue;
820 
821     if (hasImplicitOverlap(MI, MODef))
822       continue;
823 
824     if (hasOverlappingMultipleDef(MI, MODef, Def))
825       continue;
826 
827     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
828                       << "\n     with " << printReg(Def, TRI) << "\n     in "
829                       << MI << "     from " << *Copy);
830 
831     MODef.setReg(Def);
832     MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
833 
834     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
835     MaybeDeadCopies.insert(Copy);
836     Changed = true;
837     ++NumCopyBackwardPropagated;
838   }
839 }
840 
841 void MachineCopyPropagation::BackwardCopyPropagateBlock(
842     MachineBasicBlock &MBB) {
843   LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
844                     << "\n");
845 
846   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
847     // Ignore non-trivial COPYs.
848     if (MI.isCopy() && MI.getNumOperands() == 2 &&
849         !TRI->regsOverlap(MI.getOperand(0).getReg(),
850                           MI.getOperand(1).getReg())) {
851 
852       MCRegister Def = MI.getOperand(0).getReg().asMCReg();
853       MCRegister Src = MI.getOperand(1).getReg().asMCReg();
854 
855       // Unlike forward cp, we don't invoke propagateDefs here,
856       // just let forward cp do COPY-to-COPY propagation.
857       if (isBackwardPropagatableCopy(MI, *MRI)) {
858         Tracker.invalidateRegister(Src, *TRI);
859         Tracker.invalidateRegister(Def, *TRI);
860         Tracker.trackCopy(&MI, *TRI);
861         continue;
862       }
863     }
864 
865     // Invalidate any earlyclobber regs first.
866     for (const MachineOperand &MO : MI.operands())
867       if (MO.isReg() && MO.isEarlyClobber()) {
868         MCRegister Reg = MO.getReg().asMCReg();
869         if (!Reg)
870           continue;
871         Tracker.invalidateRegister(Reg, *TRI);
872       }
873 
874     propagateDefs(MI);
875     for (const MachineOperand &MO : MI.operands()) {
876       if (!MO.isReg())
877         continue;
878 
879       if (!MO.getReg())
880         continue;
881 
882       if (MO.isDef())
883         Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
884 
885       if (MO.readsReg()) {
886         if (MO.isDebug()) {
887           //  Check if the register in the debug instruction is utilized
888           // in a copy instruction, so we can update the debug info if the
889           // register is changed.
890           for (MCRegUnitIterator RUI(MO.getReg().asMCReg(), TRI); RUI.isValid();
891                ++RUI) {
892             if (auto *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
893               CopyDbgUsers[Copy].insert(&MI);
894             }
895           }
896         } else {
897           Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
898         }
899       }
900     }
901   }
902 
903   for (auto *Copy : MaybeDeadCopies) {
904 
905     Register Src = Copy->getOperand(1).getReg();
906     Register Def = Copy->getOperand(0).getReg();
907     SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
908                                                   CopyDbgUsers[Copy].end());
909 
910     MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
911     Copy->eraseFromParent();
912     ++NumDeletes;
913   }
914 
915   MaybeDeadCopies.clear();
916   CopyDbgUsers.clear();
917   Tracker.clear();
918 }
919 
920 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
921   if (skipFunction(MF.getFunction()))
922     return false;
923 
924   Changed = false;
925 
926   TRI = MF.getSubtarget().getRegisterInfo();
927   TII = MF.getSubtarget().getInstrInfo();
928   MRI = &MF.getRegInfo();
929 
930   for (MachineBasicBlock &MBB : MF) {
931     BackwardCopyPropagateBlock(MBB);
932     ForwardCopyPropagateBlock(MBB);
933   }
934 
935   return Changed;
936 }
937