xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision da7892f7295f31b46486418e2abf15334db96cbb)
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/TargetInstrInfo.h"
65 #include "llvm/CodeGen/TargetRegisterInfo.h"
66 #include "llvm/CodeGen/TargetSubtargetInfo.h"
67 #include "llvm/InitializePasses.h"
68 #include "llvm/MC/MCRegisterInfo.h"
69 #include "llvm/Pass.h"
70 #include "llvm/Support/Debug.h"
71 #include "llvm/Support/DebugCounter.h"
72 #include "llvm/Support/raw_ostream.h"
73 #include <cassert>
74 #include <iterator>
75 
76 using namespace llvm;
77 
78 #define DEBUG_TYPE "machine-cp"
79 
80 STATISTIC(NumDeletes, "Number of dead copies deleted");
81 STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
82 STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
83 STATISTIC(SpillageChainsLength, "Length of spillage chains");
84 STATISTIC(NumSpillageChains, "Number of spillage chains");
85 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
86               "Controls which register COPYs are forwarded");
87 
88 static cl::opt<bool> MCPUseCopyInstr("mcp-use-is-copy-instr", cl::init(false),
89                                      cl::Hidden);
90 static cl::opt<cl::boolOrDefault>
91     EnableSpillageCopyElimination("enable-spill-copy-elim", cl::Hidden);
92 
93 namespace {
94 
95 static std::optional<DestSourcePair> isCopyInstr(const MachineInstr &MI,
96                                                  const TargetInstrInfo &TII,
97                                                  bool UseCopyInstr) {
98   if (UseCopyInstr)
99     return TII.isCopyInstr(MI);
100 
101   if (MI.isCopy())
102     return std::optional<DestSourcePair>(
103         DestSourcePair{MI.getOperand(0), MI.getOperand(1)});
104 
105   return std::nullopt;
106 }
107 
108 class CopyTracker {
109   struct CopyInfo {
110     MachineInstr *MI, *LastSeenUseInCopy;
111     SmallVector<MCRegister, 4> DefRegs;
112     bool Avail;
113   };
114 
115   DenseMap<MCRegister, CopyInfo> Copies;
116 
117 public:
118   /// Mark all of the given registers and their subregisters as unavailable for
119   /// copying.
120   void markRegsUnavailable(ArrayRef<MCRegister> Regs,
121                            const TargetRegisterInfo &TRI) {
122     for (MCRegister Reg : Regs) {
123       // Source of copy is no longer available for propagation.
124       for (MCRegUnit Unit : TRI.regunits(Reg)) {
125         auto CI = Copies.find(Unit);
126         if (CI != Copies.end())
127           CI->second.Avail = false;
128       }
129     }
130   }
131 
132   /// Remove register from copy maps.
133   void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI,
134                           const TargetInstrInfo &TII, bool UseCopyInstr) {
135     // Since Reg might be a subreg of some registers, only invalidate Reg is not
136     // enough. We have to find the COPY defines Reg or registers defined by Reg
137     // and invalidate all of them.
138     SmallSet<MCRegister, 8> RegsToInvalidate;
139     RegsToInvalidate.insert(Reg);
140     for (MCRegUnit Unit : TRI.regunits(Reg)) {
141       auto I = Copies.find(Unit);
142       if (I != Copies.end()) {
143         if (MachineInstr *MI = I->second.MI) {
144           std::optional<DestSourcePair> CopyOperands =
145               isCopyInstr(*MI, TII, UseCopyInstr);
146           assert(CopyOperands && "Expect copy");
147 
148           RegsToInvalidate.insert(
149               CopyOperands->Destination->getReg().asMCReg());
150           RegsToInvalidate.insert(CopyOperands->Source->getReg().asMCReg());
151         }
152         RegsToInvalidate.insert(I->second.DefRegs.begin(),
153                                 I->second.DefRegs.end());
154       }
155     }
156     for (MCRegister InvalidReg : RegsToInvalidate)
157       for (MCRegUnit Unit : TRI.regunits(InvalidReg))
158         Copies.erase(Unit);
159   }
160 
161   /// Clobber a single register, removing it from the tracker's copy maps.
162   void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI,
163                        const TargetInstrInfo &TII, bool UseCopyInstr) {
164     for (MCRegUnit Unit : TRI.regunits(Reg)) {
165       auto I = Copies.find(Unit);
166       if (I != Copies.end()) {
167         // When we clobber the source of a copy, we need to clobber everything
168         // it defined.
169         markRegsUnavailable(I->second.DefRegs, TRI);
170         // When we clobber the destination of a copy, we need to clobber the
171         // whole register it defined.
172         if (MachineInstr *MI = I->second.MI) {
173           std::optional<DestSourcePair> CopyOperands =
174               isCopyInstr(*MI, TII, UseCopyInstr);
175           markRegsUnavailable({CopyOperands->Destination->getReg().asMCReg()},
176                               TRI);
177         }
178         // Now we can erase the copy.
179         Copies.erase(I);
180       }
181     }
182   }
183 
184   /// Add this copy's registers into the tracker's copy maps.
185   void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI,
186                  const TargetInstrInfo &TII, bool UseCopyInstr) {
187     std::optional<DestSourcePair> CopyOperands =
188         isCopyInstr(*MI, TII, UseCopyInstr);
189     assert(CopyOperands && "Tracking non-copy?");
190 
191     MCRegister Src = CopyOperands->Source->getReg().asMCReg();
192     MCRegister Def = CopyOperands->Destination->getReg().asMCReg();
193 
194     // Remember Def is defined by the copy.
195     for (MCRegUnit Unit : TRI.regunits(Def))
196       Copies[Unit] = {MI, nullptr, {}, true};
197 
198     // Remember source that's copied to Def. Once it's clobbered, then
199     // it's no longer available for copy propagation.
200     for (MCRegUnit Unit : TRI.regunits(Src)) {
201       auto I = Copies.insert({Unit, {nullptr, nullptr, {}, false}});
202       auto &Copy = I.first->second;
203       if (!is_contained(Copy.DefRegs, Def))
204         Copy.DefRegs.push_back(Def);
205       Copy.LastSeenUseInCopy = MI;
206     }
207   }
208 
209   bool hasAnyCopies() {
210     return !Copies.empty();
211   }
212 
213   MachineInstr *findCopyForUnit(MCRegister RegUnit,
214                                 const TargetRegisterInfo &TRI,
215                                 bool MustBeAvailable = false) {
216     auto CI = Copies.find(RegUnit);
217     if (CI == Copies.end())
218       return nullptr;
219     if (MustBeAvailable && !CI->second.Avail)
220       return nullptr;
221     return CI->second.MI;
222   }
223 
224   MachineInstr *findCopyDefViaUnit(MCRegister RegUnit,
225                                    const TargetRegisterInfo &TRI) {
226     auto CI = Copies.find(RegUnit);
227     if (CI == Copies.end())
228       return nullptr;
229     if (CI->second.DefRegs.size() != 1)
230       return nullptr;
231     MCRegUnit RU = *TRI.regunits(CI->second.DefRegs[0]).begin();
232     return findCopyForUnit(RU, TRI, true);
233   }
234 
235   MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg,
236                                       const TargetRegisterInfo &TRI,
237                                       const TargetInstrInfo &TII,
238                                       bool UseCopyInstr) {
239     MCRegUnit RU = *TRI.regunits(Reg).begin();
240     MachineInstr *AvailCopy = findCopyDefViaUnit(RU, TRI);
241 
242     if (!AvailCopy)
243       return nullptr;
244 
245     std::optional<DestSourcePair> CopyOperands =
246         isCopyInstr(*AvailCopy, TII, UseCopyInstr);
247     Register AvailSrc = CopyOperands->Source->getReg();
248     Register AvailDef = CopyOperands->Destination->getReg();
249     if (!TRI.isSubRegisterEq(AvailSrc, Reg))
250       return nullptr;
251 
252     for (const MachineInstr &MI :
253          make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
254       for (const MachineOperand &MO : MI.operands())
255         if (MO.isRegMask())
256           // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
257           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
258             return nullptr;
259 
260     return AvailCopy;
261   }
262 
263   MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg,
264                               const TargetRegisterInfo &TRI,
265                               const TargetInstrInfo &TII, bool UseCopyInstr) {
266     // We check the first RegUnit here, since we'll only be interested in the
267     // copy if it copies the entire register anyway.
268     MCRegUnit RU = *TRI.regunits(Reg).begin();
269     MachineInstr *AvailCopy =
270         findCopyForUnit(RU, TRI, /*MustBeAvailable=*/true);
271 
272     if (!AvailCopy)
273       return nullptr;
274 
275     std::optional<DestSourcePair> CopyOperands =
276         isCopyInstr(*AvailCopy, TII, UseCopyInstr);
277     Register AvailSrc = CopyOperands->Source->getReg();
278     Register AvailDef = CopyOperands->Destination->getReg();
279     if (!TRI.isSubRegisterEq(AvailDef, Reg))
280       return nullptr;
281 
282     // Check that the available copy isn't clobbered by any regmasks between
283     // itself and the destination.
284     for (const MachineInstr &MI :
285          make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
286       for (const MachineOperand &MO : MI.operands())
287         if (MO.isRegMask())
288           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
289             return nullptr;
290 
291     return AvailCopy;
292   }
293 
294   // Find last COPY that defines Reg before Current MachineInstr.
295   MachineInstr *findLastSeenDefInCopy(const MachineInstr &Current,
296                                       MCRegister Reg,
297                                       const TargetRegisterInfo &TRI,
298                                       const TargetInstrInfo &TII,
299                                       bool UseCopyInstr) {
300     MCRegUnit RU = *TRI.regunits(Reg).begin();
301     auto CI = Copies.find(RU);
302     if (CI == Copies.end() || !CI->second.Avail)
303       return nullptr;
304 
305     MachineInstr *DefCopy = CI->second.MI;
306     std::optional<DestSourcePair> CopyOperands =
307         isCopyInstr(*DefCopy, TII, UseCopyInstr);
308     Register Def = CopyOperands->Destination->getReg();
309     if (!TRI.isSubRegisterEq(Def, Reg))
310       return nullptr;
311 
312     for (const MachineInstr &MI :
313          make_range(static_cast<const MachineInstr *>(DefCopy)->getIterator(),
314                     Current.getIterator()))
315       for (const MachineOperand &MO : MI.operands())
316         if (MO.isRegMask())
317           if (MO.clobbersPhysReg(Def)) {
318             LLVM_DEBUG(dbgs() << "MCP: Removed tracking of "
319                               << printReg(Def, &TRI) << "\n");
320             return nullptr;
321           }
322 
323     return DefCopy;
324   }
325 
326   // Find last COPY that uses Reg.
327   MachineInstr *findLastSeenUseInCopy(MCRegister Reg,
328                                       const TargetRegisterInfo &TRI) {
329     MCRegUnit RU = *TRI.regunits(Reg).begin();
330     auto CI = Copies.find(RU);
331     if (CI == Copies.end())
332       return nullptr;
333     return CI->second.LastSeenUseInCopy;
334   }
335 
336   void clear() {
337     Copies.clear();
338   }
339 };
340 
341 class MachineCopyPropagation : public MachineFunctionPass {
342   const TargetRegisterInfo *TRI = nullptr;
343   const TargetInstrInfo *TII = nullptr;
344   const MachineRegisterInfo *MRI = nullptr;
345 
346   // Return true if this is a copy instruction and false otherwise.
347   bool UseCopyInstr;
348 
349 public:
350   static char ID; // Pass identification, replacement for typeid
351 
352   MachineCopyPropagation(bool CopyInstr = false)
353       : MachineFunctionPass(ID), UseCopyInstr(CopyInstr || MCPUseCopyInstr) {
354     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
355   }
356 
357   void getAnalysisUsage(AnalysisUsage &AU) const override {
358     AU.setPreservesCFG();
359     MachineFunctionPass::getAnalysisUsage(AU);
360   }
361 
362   bool runOnMachineFunction(MachineFunction &MF) override;
363 
364   MachineFunctionProperties getRequiredProperties() const override {
365     return MachineFunctionProperties().set(
366         MachineFunctionProperties::Property::NoVRegs);
367   }
368 
369 private:
370   typedef enum { DebugUse = false, RegularUse = true } DebugType;
371 
372   void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT);
373   void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
374   void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
375   void EliminateSpillageCopies(MachineBasicBlock &MBB);
376   bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def);
377   void forwardUses(MachineInstr &MI);
378   void propagateDefs(MachineInstr &MI);
379   bool isForwardableRegClassCopy(const MachineInstr &Copy,
380                                  const MachineInstr &UseI, unsigned UseIdx);
381   bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
382                                           const MachineInstr &UseI,
383                                           unsigned UseIdx);
384   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
385   bool hasOverlappingMultipleDef(const MachineInstr &MI,
386                                  const MachineOperand &MODef, Register Def);
387 
388   /// Candidates for deletion.
389   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
390 
391   /// Multimap tracking debug users in current BB
392   DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers;
393 
394   CopyTracker Tracker;
395 
396   bool Changed = false;
397 };
398 
399 } // end anonymous namespace
400 
401 char MachineCopyPropagation::ID = 0;
402 
403 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
404 
405 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
406                 "Machine Copy Propagation Pass", false, false)
407 
408 void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
409                                           DebugType DT) {
410   // If 'Reg' is defined by a copy, the copy is no longer a candidate
411   // for elimination. If a copy is "read" by a debug user, record the user
412   // for propagation.
413   for (MCRegUnit Unit : TRI->regunits(Reg)) {
414     if (MachineInstr *Copy = Tracker.findCopyForUnit(Unit, *TRI)) {
415       if (DT == RegularUse) {
416         LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
417         MaybeDeadCopies.remove(Copy);
418       } else {
419         CopyDbgUsers[Copy].insert(&Reader);
420       }
421     }
422   }
423 }
424 
425 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
426 /// This fact may have been obscured by sub register usage or may not be true at
427 /// all even though Src and Def are subregisters of the registers used in
428 /// PreviousCopy. e.g.
429 /// isNopCopy("ecx = COPY eax", AX, CX) == true
430 /// isNopCopy("ecx = COPY eax", AH, CL) == false
431 static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
432                       MCRegister Def, const TargetRegisterInfo *TRI,
433                       const TargetInstrInfo *TII, bool UseCopyInstr) {
434 
435   std::optional<DestSourcePair> CopyOperands =
436       isCopyInstr(PreviousCopy, *TII, UseCopyInstr);
437   MCRegister PreviousSrc = CopyOperands->Source->getReg().asMCReg();
438   MCRegister PreviousDef = CopyOperands->Destination->getReg().asMCReg();
439   if (Src == PreviousSrc && Def == PreviousDef)
440     return true;
441   if (!TRI->isSubRegister(PreviousSrc, Src))
442     return false;
443   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
444   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
445 }
446 
447 /// Remove instruction \p Copy if there exists a previous copy that copies the
448 /// register \p Src to the register \p Def; This may happen indirectly by
449 /// copying the super registers.
450 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
451                                               MCRegister Src, MCRegister Def) {
452   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
453   // the value (Example: The sparc zero register is writable but stays zero).
454   if (MRI->isReserved(Src) || MRI->isReserved(Def))
455     return false;
456 
457   // Search for an existing copy.
458   MachineInstr *PrevCopy =
459       Tracker.findAvailCopy(Copy, Def, *TRI, *TII, UseCopyInstr);
460   if (!PrevCopy)
461     return false;
462 
463   auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr);
464   // Check that the existing copy uses the correct sub registers.
465   if (PrevCopyOperands->Destination->isDead() ||
466       PrevCopyOperands->Source->isUndef())
467     return false;
468   if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr))
469     return false;
470 
471   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
472 
473   // Copy was redundantly redefining either Src or Def. Remove earlier kill
474   // flags between Copy and PrevCopy because the value will be reused now.
475   std::optional<DestSourcePair> CopyOperands =
476       isCopyInstr(Copy, *TII, UseCopyInstr);
477   assert(CopyOperands);
478 
479   Register CopyDef = CopyOperands->Destination->getReg();
480   assert(CopyDef == Src || CopyDef == Def);
481   for (MachineInstr &MI :
482        make_range(PrevCopy->getIterator(), Copy.getIterator()))
483     MI.clearRegisterKills(CopyDef, TRI);
484 
485   Copy.eraseFromParent();
486   Changed = true;
487   ++NumDeletes;
488   return true;
489 }
490 
491 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
492     const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
493   std::optional<DestSourcePair> CopyOperands =
494       isCopyInstr(Copy, *TII, UseCopyInstr);
495   Register Def = CopyOperands->Destination->getReg();
496 
497   if (const TargetRegisterClass *URC =
498           UseI.getRegClassConstraint(UseIdx, TII, TRI))
499     return URC->contains(Def);
500 
501   // We don't process further if UseI is a COPY, since forward copy propagation
502   // should handle that.
503   return false;
504 }
505 
506 /// Decide whether we should forward the source of \param Copy to its use in
507 /// \param UseI based on the physical register class constraints of the opcode
508 /// and avoiding introducing more cross-class COPYs.
509 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
510                                                        const MachineInstr &UseI,
511                                                        unsigned UseIdx) {
512   std::optional<DestSourcePair> CopyOperands =
513       isCopyInstr(Copy, *TII, UseCopyInstr);
514   Register CopySrcReg = CopyOperands->Source->getReg();
515 
516   // If the new register meets the opcode register constraints, then allow
517   // forwarding.
518   if (const TargetRegisterClass *URC =
519           UseI.getRegClassConstraint(UseIdx, TII, TRI))
520     return URC->contains(CopySrcReg);
521 
522   auto UseICopyOperands = isCopyInstr(UseI, *TII, UseCopyInstr);
523   if (!UseICopyOperands)
524     return false;
525 
526   /// COPYs don't have register class constraints, so if the user instruction
527   /// is a COPY, we just try to avoid introducing additional cross-class
528   /// COPYs.  For example:
529   ///
530   ///   RegClassA = COPY RegClassB  // Copy parameter
531   ///   ...
532   ///   RegClassB = COPY RegClassA  // UseI parameter
533   ///
534   /// which after forwarding becomes
535   ///
536   ///   RegClassA = COPY RegClassB
537   ///   ...
538   ///   RegClassB = COPY RegClassB
539   ///
540   /// so we have reduced the number of cross-class COPYs and potentially
541   /// introduced a nop COPY that can be removed.
542 
543   // Allow forwarding if src and dst belong to any common class, so long as they
544   // don't belong to any (possibly smaller) common class that requires copies to
545   // go via a different class.
546   Register UseDstReg = UseICopyOperands->Destination->getReg();
547   bool Found = false;
548   bool IsCrossClass = false;
549   for (const TargetRegisterClass *RC : TRI->regclasses()) {
550     if (RC->contains(CopySrcReg) && RC->contains(UseDstReg)) {
551       Found = true;
552       if (TRI->getCrossCopyRegClass(RC) != RC) {
553         IsCrossClass = true;
554         break;
555       }
556     }
557   }
558   if (!Found)
559     return false;
560   if (!IsCrossClass)
561     return true;
562   // The forwarded copy would be cross-class. Only do this if the original copy
563   // was also cross-class.
564   Register CopyDstReg = CopyOperands->Destination->getReg();
565   for (const TargetRegisterClass *RC : TRI->regclasses()) {
566     if (RC->contains(CopySrcReg) && RC->contains(CopyDstReg) &&
567         TRI->getCrossCopyRegClass(RC) != RC)
568       return true;
569   }
570   return false;
571 }
572 
573 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
574 /// operand (the register being replaced), since these can sometimes be
575 /// implicitly tied to other operands.  For example, on AMDGPU:
576 ///
577 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
578 ///
579 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
580 /// way of knowing we need to update the latter when updating the former.
581 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
582                                                 const MachineOperand &Use) {
583   for (const MachineOperand &MIUse : MI.uses())
584     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
585         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
586       return true;
587 
588   return false;
589 }
590 
591 /// For an MI that has multiple definitions, check whether \p MI has
592 /// a definition that overlaps with another of its definitions.
593 /// For example, on ARM: umull   r9, r9, lr, r0
594 /// The umull instruction is unpredictable unless RdHi and RdLo are different.
595 bool MachineCopyPropagation::hasOverlappingMultipleDef(
596     const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
597   for (const MachineOperand &MIDef : MI.defs()) {
598     if ((&MIDef != &MODef) && MIDef.isReg() &&
599         TRI->regsOverlap(Def, MIDef.getReg()))
600       return true;
601   }
602 
603   return false;
604 }
605 
606 /// Look for available copies whose destination register is used by \p MI and
607 /// replace the use in \p MI with the copy's source register.
608 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
609   if (!Tracker.hasAnyCopies())
610     return;
611 
612   // Look for non-tied explicit vreg uses that have an active COPY
613   // instruction that defines the physical register allocated to them.
614   // Replace the vreg with the source of the active COPY.
615   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
616        ++OpIdx) {
617     MachineOperand &MOUse = MI.getOperand(OpIdx);
618     // Don't forward into undef use operands since doing so can cause problems
619     // with the machine verifier, since it doesn't treat undef reads as reads,
620     // so we can end up with a live range that ends on an undef read, leading to
621     // an error that the live range doesn't end on a read of the live range
622     // register.
623     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
624         MOUse.isImplicit())
625       continue;
626 
627     if (!MOUse.getReg())
628       continue;
629 
630     // Check that the register is marked 'renamable' so we know it is safe to
631     // rename it without violating any constraints that aren't expressed in the
632     // IR (e.g. ABI or opcode requirements).
633     if (!MOUse.isRenamable())
634       continue;
635 
636     MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(),
637                                                *TRI, *TII, UseCopyInstr);
638     if (!Copy)
639       continue;
640 
641     std::optional<DestSourcePair> CopyOperands =
642         isCopyInstr(*Copy, *TII, UseCopyInstr);
643     Register CopyDstReg = CopyOperands->Destination->getReg();
644     const MachineOperand &CopySrc = *CopyOperands->Source;
645     Register CopySrcReg = CopySrc.getReg();
646 
647     Register ForwardedReg = CopySrcReg;
648     // MI might use a sub-register of the Copy destination, in which case the
649     // forwarded register is the matching sub-register of the Copy source.
650     if (MOUse.getReg() != CopyDstReg) {
651       unsigned SubRegIdx = TRI->getSubRegIndex(CopyDstReg, MOUse.getReg());
652       assert(SubRegIdx &&
653              "MI source is not a sub-register of Copy destination");
654       ForwardedReg = TRI->getSubReg(CopySrcReg, SubRegIdx);
655       if (!ForwardedReg) {
656         LLVM_DEBUG(dbgs() << "MCP: Copy source does not have sub-register "
657                           << TRI->getSubRegIndexName(SubRegIdx) << '\n');
658         continue;
659       }
660     }
661 
662     // Don't forward COPYs of reserved regs unless they are constant.
663     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
664       continue;
665 
666     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
667       continue;
668 
669     if (hasImplicitOverlap(MI, MOUse))
670       continue;
671 
672     // Check that the instruction is not a copy that partially overwrites the
673     // original copy source that we are about to use. The tracker mechanism
674     // cannot cope with that.
675     if (isCopyInstr(MI, *TII, UseCopyInstr) &&
676         MI.modifiesRegister(CopySrcReg, TRI) &&
677         !MI.definesRegister(CopySrcReg)) {
678       LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
679       continue;
680     }
681 
682     if (!DebugCounter::shouldExecute(FwdCounter)) {
683       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
684                         << MI);
685       continue;
686     }
687 
688     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
689                       << "\n     with " << printReg(ForwardedReg, TRI)
690                       << "\n     in " << MI << "     from " << *Copy);
691 
692     MOUse.setReg(ForwardedReg);
693 
694     if (!CopySrc.isRenamable())
695       MOUse.setIsRenamable(false);
696     MOUse.setIsUndef(CopySrc.isUndef());
697 
698     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
699 
700     // Clear kill markers that may have been invalidated.
701     for (MachineInstr &KMI :
702          make_range(Copy->getIterator(), std::next(MI.getIterator())))
703       KMI.clearRegisterKills(CopySrcReg, TRI);
704 
705     ++NumCopyForwards;
706     Changed = true;
707   }
708 }
709 
710 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
711   LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
712                     << "\n");
713 
714   for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
715     // Analyze copies (which don't overlap themselves).
716     std::optional<DestSourcePair> CopyOperands =
717         isCopyInstr(MI, *TII, UseCopyInstr);
718     if (CopyOperands) {
719 
720       Register RegSrc = CopyOperands->Source->getReg();
721       Register RegDef = CopyOperands->Destination->getReg();
722 
723       if (!TRI->regsOverlap(RegDef, RegSrc)) {
724         assert(RegDef.isPhysical() && RegSrc.isPhysical() &&
725               "MachineCopyPropagation should be run after register allocation!");
726 
727         MCRegister Def = RegDef.asMCReg();
728         MCRegister Src = RegSrc.asMCReg();
729 
730         // The two copies cancel out and the source of the first copy
731         // hasn't been overridden, eliminate the second one. e.g.
732         //  %ecx = COPY %eax
733         //  ... nothing clobbered eax.
734         //  %eax = COPY %ecx
735         // =>
736         //  %ecx = COPY %eax
737         //
738         // or
739         //
740         //  %ecx = COPY %eax
741         //  ... nothing clobbered eax.
742         //  %ecx = COPY %eax
743         // =>
744         //  %ecx = COPY %eax
745         if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
746           continue;
747 
748         forwardUses(MI);
749 
750         // Src may have been changed by forwardUses()
751         CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
752         Src = CopyOperands->Source->getReg().asMCReg();
753 
754         // If Src is defined by a previous copy, the previous copy cannot be
755         // eliminated.
756         ReadRegister(Src, MI, RegularUse);
757         for (const MachineOperand &MO : MI.implicit_operands()) {
758           if (!MO.isReg() || !MO.readsReg())
759             continue;
760           MCRegister Reg = MO.getReg().asMCReg();
761           if (!Reg)
762             continue;
763           ReadRegister(Reg, MI, RegularUse);
764         }
765 
766         LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
767 
768         // Copy is now a candidate for deletion.
769         if (!MRI->isReserved(Def))
770           MaybeDeadCopies.insert(&MI);
771 
772         // If 'Def' is previously source of another copy, then this earlier copy's
773         // source is no longer available. e.g.
774         // %xmm9 = copy %xmm2
775         // ...
776         // %xmm2 = copy %xmm0
777         // ...
778         // %xmm2 = copy %xmm9
779         Tracker.clobberRegister(Def, *TRI, *TII, UseCopyInstr);
780         for (const MachineOperand &MO : MI.implicit_operands()) {
781           if (!MO.isReg() || !MO.isDef())
782             continue;
783           MCRegister Reg = MO.getReg().asMCReg();
784           if (!Reg)
785             continue;
786           Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
787         }
788 
789         Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
790 
791         continue;
792       }
793     }
794 
795     // Clobber any earlyclobber regs first.
796     for (const MachineOperand &MO : MI.operands())
797       if (MO.isReg() && MO.isEarlyClobber()) {
798         MCRegister Reg = MO.getReg().asMCReg();
799         // If we have a tied earlyclobber, that means it is also read by this
800         // instruction, so we need to make sure we don't remove it as dead
801         // later.
802         if (MO.isTied())
803           ReadRegister(Reg, MI, RegularUse);
804         Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
805       }
806 
807     forwardUses(MI);
808 
809     // Not a copy.
810     SmallVector<Register, 2> Defs;
811     const MachineOperand *RegMask = nullptr;
812     for (const MachineOperand &MO : MI.operands()) {
813       if (MO.isRegMask())
814         RegMask = &MO;
815       if (!MO.isReg())
816         continue;
817       Register Reg = MO.getReg();
818       if (!Reg)
819         continue;
820 
821       assert(!Reg.isVirtual() &&
822              "MachineCopyPropagation should be run after register allocation!");
823 
824       if (MO.isDef() && !MO.isEarlyClobber()) {
825         Defs.push_back(Reg.asMCReg());
826         continue;
827       } else if (MO.readsReg())
828         ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
829     }
830 
831     // The instruction has a register mask operand which means that it clobbers
832     // a large set of registers.  Treat clobbered registers the same way as
833     // defined registers.
834     if (RegMask) {
835       // Erase any MaybeDeadCopies whose destination register is clobbered.
836       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
837                MaybeDeadCopies.begin();
838            DI != MaybeDeadCopies.end();) {
839         MachineInstr *MaybeDead = *DI;
840         std::optional<DestSourcePair> CopyOperands =
841             isCopyInstr(*MaybeDead, *TII, UseCopyInstr);
842         MCRegister Reg = CopyOperands->Destination->getReg().asMCReg();
843         assert(!MRI->isReserved(Reg));
844 
845         if (!RegMask->clobbersPhysReg(Reg)) {
846           ++DI;
847           continue;
848         }
849 
850         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
851                    MaybeDead->dump());
852 
853         // Make sure we invalidate any entries in the copy maps before erasing
854         // the instruction.
855         Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
856 
857         // erase() will return the next valid iterator pointing to the next
858         // element after the erased one.
859         DI = MaybeDeadCopies.erase(DI);
860         MaybeDead->eraseFromParent();
861         Changed = true;
862         ++NumDeletes;
863       }
864     }
865 
866     // Any previous copy definition or reading the Defs is no longer available.
867     for (MCRegister Reg : Defs)
868       Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
869   }
870 
871   // If MBB doesn't have successors, delete the copies whose defs are not used.
872   // If MBB does have successors, then conservative assume the defs are live-out
873   // since we don't want to trust live-in lists.
874   if (MBB.succ_empty()) {
875     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
876       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
877                  MaybeDead->dump());
878 
879       std::optional<DestSourcePair> CopyOperands =
880           isCopyInstr(*MaybeDead, *TII, UseCopyInstr);
881       assert(CopyOperands);
882 
883       Register SrcReg = CopyOperands->Source->getReg();
884       Register DestReg = CopyOperands->Destination->getReg();
885       assert(!MRI->isReserved(DestReg));
886 
887       // Update matching debug values, if any.
888       SmallVector<MachineInstr *> MaybeDeadDbgUsers(
889           CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
890       MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
891                                MaybeDeadDbgUsers);
892 
893       MaybeDead->eraseFromParent();
894       Changed = true;
895       ++NumDeletes;
896     }
897   }
898 
899   MaybeDeadCopies.clear();
900   CopyDbgUsers.clear();
901   Tracker.clear();
902 }
903 
904 static bool isBackwardPropagatableCopy(const DestSourcePair &CopyOperands,
905                                        const MachineRegisterInfo &MRI,
906                                        const TargetInstrInfo &TII) {
907   Register Def = CopyOperands.Destination->getReg();
908   Register Src = CopyOperands.Source->getReg();
909 
910   if (!Def || !Src)
911     return false;
912 
913   if (MRI.isReserved(Def) || MRI.isReserved(Src))
914     return false;
915 
916   return CopyOperands.Source->isRenamable() && CopyOperands.Source->isKill();
917 }
918 
919 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
920   if (!Tracker.hasAnyCopies())
921     return;
922 
923   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
924        ++OpIdx) {
925     MachineOperand &MODef = MI.getOperand(OpIdx);
926 
927     if (!MODef.isReg() || MODef.isUse())
928       continue;
929 
930     // Ignore non-trivial cases.
931     if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
932       continue;
933 
934     if (!MODef.getReg())
935       continue;
936 
937     // We only handle if the register comes from a vreg.
938     if (!MODef.isRenamable())
939       continue;
940 
941     MachineInstr *Copy = Tracker.findAvailBackwardCopy(
942         MI, MODef.getReg().asMCReg(), *TRI, *TII, UseCopyInstr);
943     if (!Copy)
944       continue;
945 
946     std::optional<DestSourcePair> CopyOperands =
947         isCopyInstr(*Copy, *TII, UseCopyInstr);
948     Register Def = CopyOperands->Destination->getReg();
949     Register Src = CopyOperands->Source->getReg();
950 
951     if (MODef.getReg() != Src)
952       continue;
953 
954     if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
955       continue;
956 
957     if (hasImplicitOverlap(MI, MODef))
958       continue;
959 
960     if (hasOverlappingMultipleDef(MI, MODef, Def))
961       continue;
962 
963     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
964                       << "\n     with " << printReg(Def, TRI) << "\n     in "
965                       << MI << "     from " << *Copy);
966 
967     MODef.setReg(Def);
968     MODef.setIsRenamable(CopyOperands->Destination->isRenamable());
969 
970     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
971     MaybeDeadCopies.insert(Copy);
972     Changed = true;
973     ++NumCopyBackwardPropagated;
974   }
975 }
976 
977 void MachineCopyPropagation::BackwardCopyPropagateBlock(
978     MachineBasicBlock &MBB) {
979   LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
980                     << "\n");
981 
982   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
983     // Ignore non-trivial COPYs.
984     std::optional<DestSourcePair> CopyOperands =
985         isCopyInstr(MI, *TII, UseCopyInstr);
986     if (CopyOperands && MI.getNumOperands() == 2) {
987       Register DefReg = CopyOperands->Destination->getReg();
988       Register SrcReg = CopyOperands->Source->getReg();
989 
990       if (!TRI->regsOverlap(DefReg, SrcReg)) {
991         // Unlike forward cp, we don't invoke propagateDefs here,
992         // just let forward cp do COPY-to-COPY propagation.
993         if (isBackwardPropagatableCopy(*CopyOperands, *MRI, *TII)) {
994           Tracker.invalidateRegister(SrcReg.asMCReg(), *TRI, *TII,
995                                      UseCopyInstr);
996           Tracker.invalidateRegister(DefReg.asMCReg(), *TRI, *TII,
997                                      UseCopyInstr);
998           Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
999           continue;
1000         }
1001       }
1002     }
1003 
1004     // Invalidate any earlyclobber regs first.
1005     for (const MachineOperand &MO : MI.operands())
1006       if (MO.isReg() && MO.isEarlyClobber()) {
1007         MCRegister Reg = MO.getReg().asMCReg();
1008         if (!Reg)
1009           continue;
1010         Tracker.invalidateRegister(Reg, *TRI, *TII, UseCopyInstr);
1011       }
1012 
1013     propagateDefs(MI);
1014     for (const MachineOperand &MO : MI.operands()) {
1015       if (!MO.isReg())
1016         continue;
1017 
1018       if (!MO.getReg())
1019         continue;
1020 
1021       if (MO.isDef())
1022         Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII,
1023                                    UseCopyInstr);
1024 
1025       if (MO.readsReg()) {
1026         if (MO.isDebug()) {
1027           //  Check if the register in the debug instruction is utilized
1028           // in a copy instruction, so we can update the debug info if the
1029           // register is changed.
1030           for (MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) {
1031             if (auto *Copy = Tracker.findCopyDefViaUnit(Unit, *TRI)) {
1032               CopyDbgUsers[Copy].insert(&MI);
1033             }
1034           }
1035         } else {
1036           Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII,
1037                                      UseCopyInstr);
1038         }
1039       }
1040     }
1041   }
1042 
1043   for (auto *Copy : MaybeDeadCopies) {
1044     std::optional<DestSourcePair> CopyOperands =
1045         isCopyInstr(*Copy, *TII, UseCopyInstr);
1046     Register Src = CopyOperands->Source->getReg();
1047     Register Def = CopyOperands->Destination->getReg();
1048     SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
1049                                                   CopyDbgUsers[Copy].end());
1050 
1051     MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
1052     Copy->eraseFromParent();
1053     ++NumDeletes;
1054   }
1055 
1056   MaybeDeadCopies.clear();
1057   CopyDbgUsers.clear();
1058   Tracker.clear();
1059 }
1060 
1061 static void LLVM_ATTRIBUTE_UNUSED printSpillReloadChain(
1062     DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &SpillChain,
1063     DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &ReloadChain,
1064     MachineInstr *Leader) {
1065   auto &SC = SpillChain[Leader];
1066   auto &RC = ReloadChain[Leader];
1067   for (auto I = SC.rbegin(), E = SC.rend(); I != E; ++I)
1068     (*I)->dump();
1069   for (MachineInstr *MI : RC)
1070     MI->dump();
1071 }
1072 
1073 // Remove spill-reload like copy chains. For example
1074 // r0 = COPY r1
1075 // r1 = COPY r2
1076 // r2 = COPY r3
1077 // r3 = COPY r4
1078 // <def-use r4>
1079 // r4 = COPY r3
1080 // r3 = COPY r2
1081 // r2 = COPY r1
1082 // r1 = COPY r0
1083 // will be folded into
1084 // r0 = COPY r1
1085 // r1 = COPY r4
1086 // <def-use r4>
1087 // r4 = COPY r1
1088 // r1 = COPY r0
1089 // TODO: Currently we don't track usage of r0 outside the chain, so we
1090 // conservatively keep its value as it was before the rewrite.
1091 //
1092 // The algorithm is trying to keep
1093 // property#1: No Def of spill COPY in the chain is used or defined until the
1094 // paired reload COPY in the chain uses the Def.
1095 //
1096 // property#2: NO Source of COPY in the chain is used or defined until the next
1097 // COPY in the chain defines the Source, except the innermost spill-reload
1098 // pair.
1099 //
1100 // The algorithm is conducted by checking every COPY inside the MBB, assuming
1101 // the COPY is a reload COPY, then try to find paired spill COPY by searching
1102 // the COPY defines the Src of the reload COPY backward. If such pair is found,
1103 // it either belongs to an existing chain or a new chain depends on
1104 // last available COPY uses the Def of the reload COPY.
1105 // Implementation notes, we use CopyTracker::findLastDefCopy(Reg, ...) to find
1106 // out last COPY that defines Reg; we use CopyTracker::findLastUseCopy(Reg, ...)
1107 // to find out last COPY that uses Reg. When we are encountered with a Non-COPY
1108 // instruction, we check registers in the operands of this instruction. If this
1109 // Reg is defined by a COPY, we untrack this Reg via
1110 // CopyTracker::clobberRegister(Reg, ...).
1111 void MachineCopyPropagation::EliminateSpillageCopies(MachineBasicBlock &MBB) {
1112   // ChainLeader maps MI inside a spill-reload chain to its innermost reload COPY.
1113   // Thus we can track if a MI belongs to an existing spill-reload chain.
1114   DenseMap<MachineInstr *, MachineInstr *> ChainLeader;
1115   // SpillChain maps innermost reload COPY of a spill-reload chain to a sequence
1116   // of COPYs that forms spills of a spill-reload chain.
1117   // ReloadChain maps innermost reload COPY of a spill-reload chain to a
1118   // sequence of COPYs that forms reloads of a spill-reload chain.
1119   DenseMap<MachineInstr *, SmallVector<MachineInstr *>> SpillChain, ReloadChain;
1120   // If a COPY's Source has use or def until next COPY defines the Source,
1121   // we put the COPY in this set to keep property#2.
1122   DenseSet<const MachineInstr *> CopySourceInvalid;
1123 
1124   auto TryFoldSpillageCopies =
1125       [&, this](const SmallVectorImpl<MachineInstr *> &SC,
1126                 const SmallVectorImpl<MachineInstr *> &RC) {
1127         assert(SC.size() == RC.size() && "Spill-reload should be paired");
1128 
1129         // We need at least 3 pairs of copies for the transformation to apply,
1130         // because the first outermost pair cannot be removed since we don't
1131         // recolor outside of the chain and that we need at least one temporary
1132         // spill slot to shorten the chain. If we only have a chain of two
1133         // pairs, we already have the shortest sequence this code can handle:
1134         // the outermost pair for the temporary spill slot, and the pair that
1135         // use that temporary spill slot for the other end of the chain.
1136         // TODO: We might be able to simplify to one spill-reload pair if collecting
1137         // more infomation about the outermost COPY.
1138         if (SC.size() <= 2)
1139           return;
1140 
1141         // If violate property#2, we don't fold the chain.
1142         for (const MachineInstr *Spill : make_range(SC.begin() + 1, SC.end()))
1143           if (CopySourceInvalid.count(Spill))
1144             return;
1145 
1146         for (const MachineInstr *Reload : make_range(RC.begin(), RC.end() - 1))
1147           if (CopySourceInvalid.count(Reload))
1148             return;
1149 
1150         auto CheckCopyConstraint = [this](Register Def, Register Src) {
1151           for (const TargetRegisterClass *RC : TRI->regclasses()) {
1152             if (RC->contains(Def) && RC->contains(Src))
1153               return true;
1154           }
1155           return false;
1156         };
1157 
1158         auto UpdateReg = [](MachineInstr *MI, const MachineOperand *Old,
1159                             const MachineOperand *New) {
1160           for (MachineOperand &MO : MI->operands()) {
1161             if (&MO == Old)
1162               MO.setReg(New->getReg());
1163           }
1164         };
1165 
1166         std::optional<DestSourcePair> InnerMostSpillCopy =
1167             isCopyInstr(*SC[0], *TII, UseCopyInstr);
1168         std::optional<DestSourcePair> OuterMostSpillCopy =
1169             isCopyInstr(*SC.back(), *TII, UseCopyInstr);
1170         std::optional<DestSourcePair> InnerMostReloadCopy =
1171             isCopyInstr(*RC[0], *TII, UseCopyInstr);
1172         std::optional<DestSourcePair> OuterMostReloadCopy =
1173             isCopyInstr(*RC.back(), *TII, UseCopyInstr);
1174         if (!CheckCopyConstraint(OuterMostSpillCopy->Source->getReg(),
1175                                  InnerMostSpillCopy->Source->getReg()) ||
1176             !CheckCopyConstraint(InnerMostReloadCopy->Destination->getReg(),
1177                                  OuterMostReloadCopy->Destination->getReg()))
1178           return;
1179 
1180         SpillageChainsLength += SC.size() + RC.size();
1181         NumSpillageChains += 1;
1182         UpdateReg(SC[0], InnerMostSpillCopy->Destination,
1183                   OuterMostSpillCopy->Source);
1184         UpdateReg(RC[0], InnerMostReloadCopy->Source,
1185                   OuterMostReloadCopy->Destination);
1186 
1187         for (size_t I = 1; I < SC.size() - 1; ++I) {
1188           SC[I]->eraseFromParent();
1189           RC[I]->eraseFromParent();
1190           NumDeletes += 2;
1191         }
1192       };
1193 
1194   auto IsFoldableCopy = [this](const MachineInstr &MaybeCopy) {
1195     if (MaybeCopy.getNumImplicitOperands() > 0)
1196       return false;
1197     std::optional<DestSourcePair> CopyOperands =
1198         isCopyInstr(MaybeCopy, *TII, UseCopyInstr);
1199     if (!CopyOperands)
1200       return false;
1201     Register Src = CopyOperands->Source->getReg();
1202     Register Def = CopyOperands->Destination->getReg();
1203     return Src && Def && !TRI->regsOverlap(Src, Def) &&
1204            CopyOperands->Source->isRenamable() &&
1205            CopyOperands->Destination->isRenamable();
1206   };
1207 
1208   auto IsSpillReloadPair = [&, this](const MachineInstr &Spill,
1209                                      const MachineInstr &Reload) {
1210     if (!IsFoldableCopy(Spill) || !IsFoldableCopy(Reload))
1211       return false;
1212     std::optional<DestSourcePair> SpillCopy =
1213         isCopyInstr(Spill, *TII, UseCopyInstr);
1214     std::optional<DestSourcePair> ReloadCopy =
1215         isCopyInstr(Reload, *TII, UseCopyInstr);
1216     if (!SpillCopy || !ReloadCopy)
1217       return false;
1218     return SpillCopy->Source->getReg() == ReloadCopy->Destination->getReg() &&
1219            SpillCopy->Destination->getReg() == ReloadCopy->Source->getReg();
1220   };
1221 
1222   auto IsChainedCopy = [&, this](const MachineInstr &Prev,
1223                                  const MachineInstr &Current) {
1224     if (!IsFoldableCopy(Prev) || !IsFoldableCopy(Current))
1225       return false;
1226     std::optional<DestSourcePair> PrevCopy =
1227         isCopyInstr(Prev, *TII, UseCopyInstr);
1228     std::optional<DestSourcePair> CurrentCopy =
1229         isCopyInstr(Current, *TII, UseCopyInstr);
1230     if (!PrevCopy || !CurrentCopy)
1231       return false;
1232     return PrevCopy->Source->getReg() == CurrentCopy->Destination->getReg();
1233   };
1234 
1235   for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
1236     std::optional<DestSourcePair> CopyOperands =
1237         isCopyInstr(MI, *TII, UseCopyInstr);
1238 
1239     // Update track information via non-copy instruction.
1240     SmallSet<Register, 8> RegsToClobber;
1241     if (!CopyOperands) {
1242       for (const MachineOperand &MO : MI.operands()) {
1243         if (!MO.isReg())
1244           continue;
1245         Register Reg = MO.getReg();
1246         if (!Reg)
1247           continue;
1248         MachineInstr *LastUseCopy =
1249             Tracker.findLastSeenUseInCopy(Reg.asMCReg(), *TRI);
1250         if (LastUseCopy) {
1251           LLVM_DEBUG(dbgs() << "MCP: Copy source of\n");
1252           LLVM_DEBUG(LastUseCopy->dump());
1253           LLVM_DEBUG(dbgs() << "might be invalidated by\n");
1254           LLVM_DEBUG(MI.dump());
1255           CopySourceInvalid.insert(LastUseCopy);
1256         }
1257         // Must be noted Tracker.clobberRegister(Reg, ...) removes tracking of
1258         // Reg, i.e, COPY that defines Reg is removed from the mapping as well
1259         // as marking COPYs that uses Reg unavailable.
1260         // We don't invoke CopyTracker::clobberRegister(Reg, ...) if Reg is not
1261         // defined by a previous COPY, since we don't want to make COPYs uses
1262         // Reg unavailable.
1263         if (Tracker.findLastSeenDefInCopy(MI, Reg.asMCReg(), *TRI, *TII,
1264                                     UseCopyInstr))
1265           // Thus we can keep the property#1.
1266           RegsToClobber.insert(Reg);
1267       }
1268       for (Register Reg : RegsToClobber) {
1269         Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
1270         LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Reg, TRI)
1271                           << "\n");
1272       }
1273       continue;
1274     }
1275 
1276     Register Src = CopyOperands->Source->getReg();
1277     Register Def = CopyOperands->Destination->getReg();
1278     // Check if we can find a pair spill-reload copy.
1279     LLVM_DEBUG(dbgs() << "MCP: Searching paired spill for reload: ");
1280     LLVM_DEBUG(MI.dump());
1281     MachineInstr *MaybeSpill =
1282         Tracker.findLastSeenDefInCopy(MI, Src.asMCReg(), *TRI, *TII, UseCopyInstr);
1283     bool MaybeSpillIsChained = ChainLeader.count(MaybeSpill);
1284     if (!MaybeSpillIsChained && MaybeSpill &&
1285         IsSpillReloadPair(*MaybeSpill, MI)) {
1286       // Check if we already have an existing chain. Now we have a
1287       // spill-reload pair.
1288       // L2: r2 = COPY r3
1289       // L5: r3 = COPY r2
1290       // Looking for a valid COPY before L5 which uses r3.
1291       // This can be serverial cases.
1292       // Case #1:
1293       // No COPY is found, which can be r3 is def-use between (L2, L5), we
1294       // create a new chain for L2 and L5.
1295       // Case #2:
1296       // L2: r2 = COPY r3
1297       // L5: r3 = COPY r2
1298       // Such COPY is found and is L2, we create a new chain for L2 and L5.
1299       // Case #3:
1300       // L2: r2 = COPY r3
1301       // L3: r1 = COPY r3
1302       // L5: r3 = COPY r2
1303       // we create a new chain for L2 and L5.
1304       // Case #4:
1305       // L2: r2 = COPY r3
1306       // L3: r1 = COPY r3
1307       // L4: r3 = COPY r1
1308       // L5: r3 = COPY r2
1309       // Such COPY won't be found since L4 defines r3. we create a new chain
1310       // for L2 and L5.
1311       // Case #5:
1312       // L2: r2 = COPY r3
1313       // L3: r3 = COPY r1
1314       // L4: r1 = COPY r3
1315       // L5: r3 = COPY r2
1316       // COPY is found and is L4 which belongs to an existing chain, we add
1317       // L2 and L5 to this chain.
1318       LLVM_DEBUG(dbgs() << "MCP: Found spill: ");
1319       LLVM_DEBUG(MaybeSpill->dump());
1320       MachineInstr *MaybePrevReload =
1321           Tracker.findLastSeenUseInCopy(Def.asMCReg(), *TRI);
1322       auto Leader = ChainLeader.find(MaybePrevReload);
1323       MachineInstr *L = nullptr;
1324       if (Leader == ChainLeader.end() ||
1325           (MaybePrevReload && !IsChainedCopy(*MaybePrevReload, MI))) {
1326         L = &MI;
1327         assert(!SpillChain.count(L) &&
1328                "SpillChain should not have contained newly found chain");
1329       } else {
1330         assert(MaybePrevReload &&
1331                "Found a valid leader through nullptr should not happend");
1332         L = Leader->second;
1333         assert(SpillChain[L].size() > 0 &&
1334                "Existing chain's length should be larger than zero");
1335       }
1336       assert(!ChainLeader.count(&MI) && !ChainLeader.count(MaybeSpill) &&
1337              "Newly found paired spill-reload should not belong to any chain "
1338              "at this point");
1339       ChainLeader.insert({MaybeSpill, L});
1340       ChainLeader.insert({&MI, L});
1341       SpillChain[L].push_back(MaybeSpill);
1342       ReloadChain[L].push_back(&MI);
1343       LLVM_DEBUG(dbgs() << "MCP: Chain " << L << " now is:\n");
1344       LLVM_DEBUG(printSpillReloadChain(SpillChain, ReloadChain, L));
1345     } else if (MaybeSpill && !MaybeSpillIsChained) {
1346       // MaybeSpill is unable to pair with MI. That's to say adding MI makes
1347       // the chain invalid.
1348       // The COPY defines Src is no longer considered as a candidate of a
1349       // valid chain. Since we expect the Def of a spill copy isn't used by
1350       // any COPY instruction until a reload copy. For example:
1351       // L1: r1 = COPY r2
1352       // L2: r3 = COPY r1
1353       // If we later have
1354       // L1: r1 = COPY r2
1355       // L2: r3 = COPY r1
1356       // L3: r2 = COPY r1
1357       // L1 and L3 can't be a valid spill-reload pair.
1358       // Thus we keep the property#1.
1359       LLVM_DEBUG(dbgs() << "MCP: Not paired spill-reload:\n");
1360       LLVM_DEBUG(MaybeSpill->dump());
1361       LLVM_DEBUG(MI.dump());
1362       Tracker.clobberRegister(Src.asMCReg(), *TRI, *TII, UseCopyInstr);
1363       LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Src, TRI)
1364                         << "\n");
1365     }
1366     Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
1367   }
1368 
1369   for (auto I = SpillChain.begin(), E = SpillChain.end(); I != E; ++I) {
1370     auto &SC = I->second;
1371     assert(ReloadChain.count(I->first) &&
1372            "Reload chain of the same leader should exist");
1373     auto &RC = ReloadChain[I->first];
1374     TryFoldSpillageCopies(SC, RC);
1375   }
1376 
1377   MaybeDeadCopies.clear();
1378   CopyDbgUsers.clear();
1379   Tracker.clear();
1380 }
1381 
1382 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
1383   if (skipFunction(MF.getFunction()))
1384     return false;
1385 
1386   bool isSpillageCopyElimEnabled = false;
1387   switch (EnableSpillageCopyElimination) {
1388   case cl::BOU_UNSET:
1389     isSpillageCopyElimEnabled =
1390         MF.getSubtarget().enableSpillageCopyElimination();
1391     break;
1392   case cl::BOU_TRUE:
1393     isSpillageCopyElimEnabled = true;
1394     break;
1395   case cl::BOU_FALSE:
1396     isSpillageCopyElimEnabled = false;
1397     break;
1398   }
1399 
1400   Changed = false;
1401 
1402   TRI = MF.getSubtarget().getRegisterInfo();
1403   TII = MF.getSubtarget().getInstrInfo();
1404   MRI = &MF.getRegInfo();
1405 
1406   for (MachineBasicBlock &MBB : MF) {
1407     if (isSpillageCopyElimEnabled)
1408       EliminateSpillageCopies(MBB);
1409     BackwardCopyPropagateBlock(MBB);
1410     ForwardCopyPropagateBlock(MBB);
1411   }
1412 
1413   return Changed;
1414 }
1415 
1416 MachineFunctionPass *
1417 llvm::createMachineCopyPropagationPass(bool UseCopyInstr = false) {
1418   return new MachineCopyPropagation(UseCopyInstr);
1419 }
1420