xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision 6a05c6bfc8d363a6d3d6cd7015d0e6fc01a91ce2)
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 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
84               "Controls which register COPYs are forwarded");
85 
86 namespace {
87 
88 class CopyTracker {
89   struct CopyInfo {
90     MachineInstr *MI;
91     SmallVector<unsigned, 4> DefRegs;
92     bool Avail;
93   };
94 
95   DenseMap<unsigned, CopyInfo> Copies;
96 
97 public:
98   /// Mark all of the given registers and their subregisters as unavailable for
99   /// copying.
100   void markRegsUnavailable(ArrayRef<unsigned> Regs,
101                            const TargetRegisterInfo &TRI) {
102     for (unsigned Reg : Regs) {
103       // Source of copy is no longer available for propagation.
104       for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
105         auto CI = Copies.find(*RUI);
106         if (CI != Copies.end())
107           CI->second.Avail = false;
108       }
109     }
110   }
111 
112   /// Remove register from copy maps.
113   void invalidateRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
114     // Since Reg might be a subreg of some registers, only invalidate Reg is not
115     // enough. We have to find the COPY defines Reg or registers defined by Reg
116     // and invalidate all of them.
117     SmallSet<unsigned, 8> RegsToInvalidate;
118     RegsToInvalidate.insert(Reg);
119     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
120       auto I = Copies.find(*RUI);
121       if (I != Copies.end()) {
122         if (MachineInstr *MI = I->second.MI) {
123           RegsToInvalidate.insert(MI->getOperand(0).getReg());
124           RegsToInvalidate.insert(MI->getOperand(1).getReg());
125         }
126         RegsToInvalidate.insert(I->second.DefRegs.begin(),
127                                 I->second.DefRegs.end());
128       }
129     }
130     for (unsigned InvalidReg : RegsToInvalidate)
131       for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
132         Copies.erase(*RUI);
133   }
134 
135   /// Clobber a single register, removing it from the tracker's copy maps.
136   void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
137     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
138       auto I = Copies.find(*RUI);
139       if (I != Copies.end()) {
140         // When we clobber the source of a copy, we need to clobber everything
141         // it defined.
142         markRegsUnavailable(I->second.DefRegs, TRI);
143         // When we clobber the destination of a copy, we need to clobber the
144         // whole register it defined.
145         if (MachineInstr *MI = I->second.MI)
146           markRegsUnavailable({MI->getOperand(0).getReg()}, TRI);
147         // Now we can erase the copy.
148         Copies.erase(I);
149       }
150     }
151   }
152 
153   /// Add this copy's registers into the tracker's copy maps.
154   void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
155     assert(MI->isCopy() && "Tracking non-copy?");
156 
157     Register Def = MI->getOperand(0).getReg();
158     Register Src = MI->getOperand(1).getReg();
159 
160     // Remember Def is defined by the copy.
161     for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
162       Copies[*RUI] = {MI, {}, true};
163 
164     // Remember source that's copied to Def. Once it's clobbered, then
165     // it's no longer available for copy propagation.
166     for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
167       auto I = Copies.insert({*RUI, {nullptr, {}, false}});
168       auto &Copy = I.first->second;
169       if (!is_contained(Copy.DefRegs, Def))
170         Copy.DefRegs.push_back(Def);
171     }
172   }
173 
174   bool hasAnyCopies() {
175     return !Copies.empty();
176   }
177 
178   MachineInstr *findCopyForUnit(unsigned RegUnit, 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(unsigned 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, unsigned 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, unsigned 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 ClobberRegister(unsigned Reg);
278   void ReadRegister(unsigned Reg, MachineInstr &Reader,
279                     DebugType DT);
280   void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
281   void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
282   bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
283   void forwardUses(MachineInstr &MI);
284   void propagateDefs(MachineInstr &MI);
285   bool isForwardableRegClassCopy(const MachineInstr &Copy,
286                                  const MachineInstr &UseI, unsigned UseIdx);
287   bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
288                                           const MachineInstr &UseI,
289                                           unsigned UseIdx);
290   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
291   bool hasOverlappingMultipleDef(const MachineInstr &MI,
292                                  const MachineOperand &MODef, Register Def);
293 
294   /// Candidates for deletion.
295   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
296 
297   /// Multimap tracking debug users in current BB
298   DenseMap<MachineInstr*, SmallVector<MachineInstr*, 2>> CopyDbgUsers;
299 
300   CopyTracker Tracker;
301 
302   bool Changed;
303 };
304 
305 } // end anonymous namespace
306 
307 char MachineCopyPropagation::ID = 0;
308 
309 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
310 
311 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
312                 "Machine Copy Propagation Pass", false, false)
313 
314 void MachineCopyPropagation::ReadRegister(unsigned Reg, MachineInstr &Reader,
315                                           DebugType DT) {
316   // If 'Reg' is defined by a copy, the copy is no longer a candidate
317   // for elimination. If a copy is "read" by a debug user, record the user
318   // for propagation.
319   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
320     if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
321       if (DT == RegularUse) {
322         LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
323         MaybeDeadCopies.remove(Copy);
324       } else {
325         CopyDbgUsers[Copy].push_back(&Reader);
326       }
327     }
328   }
329 }
330 
331 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
332 /// This fact may have been obscured by sub register usage or may not be true at
333 /// all even though Src and Def are subregisters of the registers used in
334 /// PreviousCopy. e.g.
335 /// isNopCopy("ecx = COPY eax", AX, CX) == true
336 /// isNopCopy("ecx = COPY eax", AH, CL) == false
337 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
338                       unsigned Def, const TargetRegisterInfo *TRI) {
339   Register PreviousSrc = PreviousCopy.getOperand(1).getReg();
340   Register PreviousDef = PreviousCopy.getOperand(0).getReg();
341   if (Src == PreviousSrc) {
342     assert(Def == PreviousDef);
343     return true;
344   }
345   if (!TRI->isSubRegister(PreviousSrc, Src))
346     return false;
347   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
348   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
349 }
350 
351 /// Remove instruction \p Copy if there exists a previous copy that copies the
352 /// register \p Src to the register \p Def; This may happen indirectly by
353 /// copying the super registers.
354 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
355                                               unsigned Def) {
356   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
357   // the value (Example: The sparc zero register is writable but stays zero).
358   if (MRI->isReserved(Src) || MRI->isReserved(Def))
359     return false;
360 
361   // Search for an existing copy.
362   MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
363   if (!PrevCopy)
364     return false;
365 
366   // Check that the existing copy uses the correct sub registers.
367   if (PrevCopy->getOperand(0).isDead())
368     return false;
369   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
370     return false;
371 
372   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
373 
374   // Copy was redundantly redefining either Src or Def. Remove earlier kill
375   // flags between Copy and PrevCopy because the value will be reused now.
376   assert(Copy.isCopy());
377   Register CopyDef = Copy.getOperand(0).getReg();
378   assert(CopyDef == Src || CopyDef == Def);
379   for (MachineInstr &MI :
380        make_range(PrevCopy->getIterator(), Copy.getIterator()))
381     MI.clearRegisterKills(CopyDef, TRI);
382 
383   Copy.eraseFromParent();
384   Changed = true;
385   ++NumDeletes;
386   return true;
387 }
388 
389 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
390     const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
391   Register Def = Copy.getOperand(0).getReg();
392 
393   if (const TargetRegisterClass *URC =
394           UseI.getRegClassConstraint(UseIdx, TII, TRI))
395     return URC->contains(Def);
396 
397   // We don't process further if UseI is a COPY, since forward copy propagation
398   // should handle that.
399   return false;
400 }
401 
402 /// Decide whether we should forward the source of \param Copy to its use in
403 /// \param UseI based on the physical register class constraints of the opcode
404 /// and avoiding introducing more cross-class COPYs.
405 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
406                                                        const MachineInstr &UseI,
407                                                        unsigned UseIdx) {
408 
409   Register CopySrcReg = Copy.getOperand(1).getReg();
410 
411   // If the new register meets the opcode register constraints, then allow
412   // forwarding.
413   if (const TargetRegisterClass *URC =
414           UseI.getRegClassConstraint(UseIdx, TII, TRI))
415     return URC->contains(CopySrcReg);
416 
417   if (!UseI.isCopy())
418     return false;
419 
420   /// COPYs don't have register class constraints, so if the user instruction
421   /// is a COPY, we just try to avoid introducing additional cross-class
422   /// COPYs.  For example:
423   ///
424   ///   RegClassA = COPY RegClassB  // Copy parameter
425   ///   ...
426   ///   RegClassB = COPY RegClassA  // UseI parameter
427   ///
428   /// which after forwarding becomes
429   ///
430   ///   RegClassA = COPY RegClassB
431   ///   ...
432   ///   RegClassB = COPY RegClassB
433   ///
434   /// so we have reduced the number of cross-class COPYs and potentially
435   /// introduced a nop COPY that can be removed.
436   const TargetRegisterClass *UseDstRC =
437       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
438 
439   const TargetRegisterClass *SuperRC = UseDstRC;
440   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
441        SuperRC; SuperRC = *SuperRCI++)
442     if (SuperRC->contains(CopySrcReg))
443       return true;
444 
445   return false;
446 }
447 
448 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
449 /// operand (the register being replaced), since these can sometimes be
450 /// implicitly tied to other operands.  For example, on AMDGPU:
451 ///
452 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
453 ///
454 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
455 /// way of knowing we need to update the latter when updating the former.
456 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
457                                                 const MachineOperand &Use) {
458   for (const MachineOperand &MIUse : MI.uses())
459     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
460         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
461       return true;
462 
463   return false;
464 }
465 
466 /// For an MI that has multiple definitions, check whether \p MI has
467 /// a definition that overlaps with another of its definitions.
468 /// For example, on ARM: umull   r9, r9, lr, r0
469 /// The umull instruction is unpredictable unless RdHi and RdLo are different.
470 bool MachineCopyPropagation::hasOverlappingMultipleDef(
471     const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
472   for (const MachineOperand &MIDef : MI.defs()) {
473     if ((&MIDef != &MODef) && MIDef.isReg() &&
474         TRI->regsOverlap(Def, MIDef.getReg()))
475       return true;
476   }
477 
478   return false;
479 }
480 
481 /// Look for available copies whose destination register is used by \p MI and
482 /// replace the use in \p MI with the copy's source register.
483 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
484   if (!Tracker.hasAnyCopies())
485     return;
486 
487   // Look for non-tied explicit vreg uses that have an active COPY
488   // instruction that defines the physical register allocated to them.
489   // Replace the vreg with the source of the active COPY.
490   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
491        ++OpIdx) {
492     MachineOperand &MOUse = MI.getOperand(OpIdx);
493     // Don't forward into undef use operands since doing so can cause problems
494     // with the machine verifier, since it doesn't treat undef reads as reads,
495     // so we can end up with a live range that ends on an undef read, leading to
496     // an error that the live range doesn't end on a read of the live range
497     // register.
498     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
499         MOUse.isImplicit())
500       continue;
501 
502     if (!MOUse.getReg())
503       continue;
504 
505     // Check that the register is marked 'renamable' so we know it is safe to
506     // rename it without violating any constraints that aren't expressed in the
507     // IR (e.g. ABI or opcode requirements).
508     if (!MOUse.isRenamable())
509       continue;
510 
511     MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI);
512     if (!Copy)
513       continue;
514 
515     Register CopyDstReg = Copy->getOperand(0).getReg();
516     const MachineOperand &CopySrc = Copy->getOperand(1);
517     Register CopySrcReg = CopySrc.getReg();
518 
519     // FIXME: Don't handle partial uses of wider COPYs yet.
520     if (MOUse.getReg() != CopyDstReg) {
521       LLVM_DEBUG(
522           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
523                  << MI);
524       continue;
525     }
526 
527     // Don't forward COPYs of reserved regs unless they are constant.
528     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
529       continue;
530 
531     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
532       continue;
533 
534     if (hasImplicitOverlap(MI, MOUse))
535       continue;
536 
537     // Check that the instruction is not a copy that partially overwrites the
538     // original copy source that we are about to use. The tracker mechanism
539     // cannot cope with that.
540     if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
541         !MI.definesRegister(CopySrcReg)) {
542       LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
543       continue;
544     }
545 
546     if (!DebugCounter::shouldExecute(FwdCounter)) {
547       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
548                         << MI);
549       continue;
550     }
551 
552     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
553                       << "\n     with " << printReg(CopySrcReg, TRI)
554                       << "\n     in " << MI << "     from " << *Copy);
555 
556     MOUse.setReg(CopySrcReg);
557     if (!CopySrc.isRenamable())
558       MOUse.setIsRenamable(false);
559 
560     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
561 
562     // Clear kill markers that may have been invalidated.
563     for (MachineInstr &KMI :
564          make_range(Copy->getIterator(), std::next(MI.getIterator())))
565       KMI.clearRegisterKills(CopySrcReg, TRI);
566 
567     ++NumCopyForwards;
568     Changed = true;
569   }
570 }
571 
572 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
573   LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
574                     << "\n");
575 
576   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
577     MachineInstr *MI = &*I;
578     ++I;
579 
580     // Analyze copies (which don't overlap themselves).
581     if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
582                                           MI->getOperand(1).getReg())) {
583       Register Def = MI->getOperand(0).getReg();
584       Register Src = MI->getOperand(1).getReg();
585 
586       assert(!Register::isVirtualRegister(Def) &&
587              !Register::isVirtualRegister(Src) &&
588              "MachineCopyPropagation should be run after register allocation!");
589 
590       // The two copies cancel out and the source of the first copy
591       // hasn't been overridden, eliminate the second one. e.g.
592       //  %ecx = COPY %eax
593       //  ... nothing clobbered eax.
594       //  %eax = COPY %ecx
595       // =>
596       //  %ecx = COPY %eax
597       //
598       // or
599       //
600       //  %ecx = COPY %eax
601       //  ... nothing clobbered eax.
602       //  %ecx = COPY %eax
603       // =>
604       //  %ecx = COPY %eax
605       if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
606         continue;
607 
608       forwardUses(*MI);
609 
610       // Src may have been changed by forwardUses()
611       Src = MI->getOperand(1).getReg();
612 
613       // If Src is defined by a previous copy, the previous copy cannot be
614       // eliminated.
615       ReadRegister(Src, *MI, RegularUse);
616       for (const MachineOperand &MO : MI->implicit_operands()) {
617         if (!MO.isReg() || !MO.readsReg())
618           continue;
619         Register Reg = MO.getReg();
620         if (!Reg)
621           continue;
622         ReadRegister(Reg, *MI, RegularUse);
623       }
624 
625       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
626 
627       // Copy is now a candidate for deletion.
628       if (!MRI->isReserved(Def))
629         MaybeDeadCopies.insert(MI);
630 
631       // If 'Def' is previously source of another copy, then this earlier copy's
632       // source is no longer available. e.g.
633       // %xmm9 = copy %xmm2
634       // ...
635       // %xmm2 = copy %xmm0
636       // ...
637       // %xmm2 = copy %xmm9
638       Tracker.clobberRegister(Def, *TRI);
639       for (const MachineOperand &MO : MI->implicit_operands()) {
640         if (!MO.isReg() || !MO.isDef())
641           continue;
642         Register Reg = MO.getReg();
643         if (!Reg)
644           continue;
645         Tracker.clobberRegister(Reg, *TRI);
646       }
647 
648       Tracker.trackCopy(MI, *TRI);
649 
650       continue;
651     }
652 
653     // Clobber any earlyclobber regs first.
654     for (const MachineOperand &MO : MI->operands())
655       if (MO.isReg() && MO.isEarlyClobber()) {
656         Register Reg = MO.getReg();
657         // If we have a tied earlyclobber, that means it is also read by this
658         // instruction, so we need to make sure we don't remove it as dead
659         // later.
660         if (MO.isTied())
661           ReadRegister(Reg, *MI, RegularUse);
662         Tracker.clobberRegister(Reg, *TRI);
663       }
664 
665     forwardUses(*MI);
666 
667     // Not a copy.
668     SmallVector<unsigned, 2> Defs;
669     const MachineOperand *RegMask = nullptr;
670     for (const MachineOperand &MO : MI->operands()) {
671       if (MO.isRegMask())
672         RegMask = &MO;
673       if (!MO.isReg())
674         continue;
675       Register Reg = MO.getReg();
676       if (!Reg)
677         continue;
678 
679       assert(!Register::isVirtualRegister(Reg) &&
680              "MachineCopyPropagation should be run after register allocation!");
681 
682       if (MO.isDef() && !MO.isEarlyClobber()) {
683         Defs.push_back(Reg);
684         continue;
685       } else if (MO.readsReg())
686         ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse);
687     }
688 
689     // The instruction has a register mask operand which means that it clobbers
690     // a large set of registers.  Treat clobbered registers the same way as
691     // defined registers.
692     if (RegMask) {
693       // Erase any MaybeDeadCopies whose destination register is clobbered.
694       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
695                MaybeDeadCopies.begin();
696            DI != MaybeDeadCopies.end();) {
697         MachineInstr *MaybeDead = *DI;
698         Register Reg = MaybeDead->getOperand(0).getReg();
699         assert(!MRI->isReserved(Reg));
700 
701         if (!RegMask->clobbersPhysReg(Reg)) {
702           ++DI;
703           continue;
704         }
705 
706         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
707                    MaybeDead->dump());
708 
709         // Make sure we invalidate any entries in the copy maps before erasing
710         // the instruction.
711         Tracker.clobberRegister(Reg, *TRI);
712 
713         // erase() will return the next valid iterator pointing to the next
714         // element after the erased one.
715         DI = MaybeDeadCopies.erase(DI);
716         MaybeDead->eraseFromParent();
717         Changed = true;
718         ++NumDeletes;
719       }
720     }
721 
722     // Any previous copy definition or reading the Defs is no longer available.
723     for (unsigned Reg : Defs)
724       Tracker.clobberRegister(Reg, *TRI);
725   }
726 
727   // If MBB doesn't have successors, delete the copies whose defs are not used.
728   // If MBB does have successors, then conservative assume the defs are live-out
729   // since we don't want to trust live-in lists.
730   if (MBB.succ_empty()) {
731     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
732       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
733                  MaybeDead->dump());
734       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
735 
736       // Update matching debug values, if any.
737       assert(MaybeDead->isCopy());
738       unsigned SrcReg = MaybeDead->getOperand(1).getReg();
739       MRI->updateDbgUsersToReg(SrcReg, CopyDbgUsers[MaybeDead]);
740 
741       MaybeDead->eraseFromParent();
742       Changed = true;
743       ++NumDeletes;
744     }
745   }
746 
747   MaybeDeadCopies.clear();
748   CopyDbgUsers.clear();
749   Tracker.clear();
750 }
751 
752 static bool isBackwardPropagatableCopy(MachineInstr &MI,
753                                        const MachineRegisterInfo &MRI) {
754   assert(MI.isCopy() && "MI is expected to be a COPY");
755   Register Def = MI.getOperand(0).getReg();
756   Register Src = MI.getOperand(1).getReg();
757 
758   if (!Def || !Src)
759     return false;
760 
761   if (MRI.isReserved(Def) || MRI.isReserved(Src))
762     return false;
763 
764   return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
765 }
766 
767 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
768   if (!Tracker.hasAnyCopies())
769     return;
770 
771   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
772        ++OpIdx) {
773     MachineOperand &MODef = MI.getOperand(OpIdx);
774 
775     if (!MODef.isReg() || MODef.isUse())
776       continue;
777 
778     // Ignore non-trivial cases.
779     if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
780       continue;
781 
782     if (!MODef.getReg())
783       continue;
784 
785     // We only handle if the register comes from a vreg.
786     if (!MODef.isRenamable())
787       continue;
788 
789     MachineInstr *Copy =
790         Tracker.findAvailBackwardCopy(MI, MODef.getReg(), *TRI);
791     if (!Copy)
792       continue;
793 
794     Register Def = Copy->getOperand(0).getReg();
795     Register Src = Copy->getOperand(1).getReg();
796 
797     if (MODef.getReg() != Src)
798       continue;
799 
800     if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
801       continue;
802 
803     if (hasImplicitOverlap(MI, MODef))
804       continue;
805 
806     if (hasOverlappingMultipleDef(MI, MODef, Def))
807       continue;
808 
809     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
810                       << "\n     with " << printReg(Def, TRI) << "\n     in "
811                       << MI << "     from " << *Copy);
812 
813     MODef.setReg(Def);
814     MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
815 
816     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
817     MaybeDeadCopies.insert(Copy);
818     Changed = true;
819     ++NumCopyBackwardPropagated;
820   }
821 }
822 
823 void MachineCopyPropagation::BackwardCopyPropagateBlock(
824     MachineBasicBlock &MBB) {
825   LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
826                     << "\n");
827 
828   for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
829        I != E;) {
830     MachineInstr *MI = &*I;
831     ++I;
832 
833     // Ignore non-trivial COPYs.
834     if (MI->isCopy() && MI->getNumOperands() == 2 &&
835         !TRI->regsOverlap(MI->getOperand(0).getReg(),
836                           MI->getOperand(1).getReg())) {
837 
838       Register Def = MI->getOperand(0).getReg();
839       Register Src = MI->getOperand(1).getReg();
840 
841       // Unlike forward cp, we don't invoke propagateDefs here,
842       // just let forward cp do COPY-to-COPY propagation.
843       if (isBackwardPropagatableCopy(*MI, *MRI)) {
844         Tracker.invalidateRegister(Src, *TRI);
845         Tracker.invalidateRegister(Def, *TRI);
846         Tracker.trackCopy(MI, *TRI);
847         continue;
848       }
849     }
850 
851     // Invalidate any earlyclobber regs first.
852     for (const MachineOperand &MO : MI->operands())
853       if (MO.isReg() && MO.isEarlyClobber()) {
854         Register Reg = MO.getReg();
855         if (!Reg)
856           continue;
857         Tracker.invalidateRegister(Reg, *TRI);
858       }
859 
860     propagateDefs(*MI);
861     for (const MachineOperand &MO : MI->operands()) {
862       if (!MO.isReg())
863         continue;
864 
865       if (!MO.getReg())
866         continue;
867 
868       if (MO.isDef())
869         Tracker.invalidateRegister(MO.getReg(), *TRI);
870 
871       if (MO.readsReg())
872         Tracker.invalidateRegister(MO.getReg(), *TRI);
873     }
874   }
875 
876   for (auto *Copy : MaybeDeadCopies) {
877     Copy->eraseFromParent();
878     ++NumDeletes;
879   }
880 
881   MaybeDeadCopies.clear();
882   CopyDbgUsers.clear();
883   Tracker.clear();
884 }
885 
886 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
887   if (skipFunction(MF.getFunction()))
888     return false;
889 
890   Changed = false;
891 
892   TRI = MF.getSubtarget().getRegisterInfo();
893   TII = MF.getSubtarget().getInstrInfo();
894   MRI = &MF.getRegInfo();
895 
896   for (MachineBasicBlock &MBB : MF) {
897     BackwardCopyPropagateBlock(MBB);
898     ForwardCopyPropagateBlock(MBB);
899   }
900 
901   return Changed;
902 }
903