xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
10b57cec5SDimitry Andric //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This is an extremely simple MachineInstr-level copy propagation pass.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // This pass forwards the source of COPYs to the users of their destinations
120b57cec5SDimitry Andric // when doing so is legal.  For example:
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //   %reg1 = COPY %reg0
150b57cec5SDimitry Andric //   ...
160b57cec5SDimitry Andric //   ... = OP %reg1
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric // If
190b57cec5SDimitry Andric //   - %reg0 has not been clobbered by the time of the use of %reg1
200b57cec5SDimitry Andric //   - the register class constraints are satisfied
210b57cec5SDimitry Andric //   - the COPY def is the only value that reaches OP
220b57cec5SDimitry Andric // then this pass replaces the above with:
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric //   %reg1 = COPY %reg0
250b57cec5SDimitry Andric //   ...
260b57cec5SDimitry Andric //   ... = OP %reg0
270b57cec5SDimitry Andric //
280b57cec5SDimitry Andric // This pass also removes some redundant COPYs.  For example:
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric //    %R1 = COPY %R0
310b57cec5SDimitry Andric //    ... // No clobber of %R1
320b57cec5SDimitry Andric //    %R0 = COPY %R1 <<< Removed
330b57cec5SDimitry Andric //
340b57cec5SDimitry Andric // or
350b57cec5SDimitry Andric //
360b57cec5SDimitry Andric //    %R1 = COPY %R0
370b57cec5SDimitry Andric //    ... // No clobber of %R0
380b57cec5SDimitry Andric //    %R1 = COPY %R0 <<< Removed
390b57cec5SDimitry Andric //
40*480093f4SDimitry Andric // or
41*480093f4SDimitry Andric //
42*480093f4SDimitry Andric //    $R0 = OP ...
43*480093f4SDimitry Andric //    ... // No read/clobber of $R0 and $R1
44*480093f4SDimitry Andric //    $R1 = COPY $R0 // $R0 is killed
45*480093f4SDimitry Andric // Replace $R0 with $R1 and remove the COPY
46*480093f4SDimitry Andric //    $R1 = OP ...
47*480093f4SDimitry Andric //    ...
48*480093f4SDimitry Andric //
490b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
520b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
530b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
540b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
550b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
560b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
570b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
580b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
590b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
600b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
610b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
620b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
630b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
640b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
650b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
66*480093f4SDimitry Andric #include "llvm/InitializePasses.h"
670b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
680b57cec5SDimitry Andric #include "llvm/Pass.h"
690b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
700b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
710b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
720b57cec5SDimitry Andric #include <cassert>
730b57cec5SDimitry Andric #include <iterator>
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric using namespace llvm;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric #define DEBUG_TYPE "machine-cp"
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric STATISTIC(NumDeletes, "Number of dead copies deleted");
800b57cec5SDimitry Andric STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
81*480093f4SDimitry Andric STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
820b57cec5SDimitry Andric DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
830b57cec5SDimitry Andric               "Controls which register COPYs are forwarded");
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric namespace {
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric class CopyTracker {
880b57cec5SDimitry Andric   struct CopyInfo {
890b57cec5SDimitry Andric     MachineInstr *MI;
900b57cec5SDimitry Andric     SmallVector<unsigned, 4> DefRegs;
910b57cec5SDimitry Andric     bool Avail;
920b57cec5SDimitry Andric   };
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   DenseMap<unsigned, CopyInfo> Copies;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric public:
970b57cec5SDimitry Andric   /// Mark all of the given registers and their subregisters as unavailable for
980b57cec5SDimitry Andric   /// copying.
990b57cec5SDimitry Andric   void markRegsUnavailable(ArrayRef<unsigned> Regs,
1000b57cec5SDimitry Andric                            const TargetRegisterInfo &TRI) {
1010b57cec5SDimitry Andric     for (unsigned Reg : Regs) {
1020b57cec5SDimitry Andric       // Source of copy is no longer available for propagation.
1030b57cec5SDimitry Andric       for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
1040b57cec5SDimitry Andric         auto CI = Copies.find(*RUI);
1050b57cec5SDimitry Andric         if (CI != Copies.end())
1060b57cec5SDimitry Andric           CI->second.Avail = false;
1070b57cec5SDimitry Andric       }
1080b57cec5SDimitry Andric     }
1090b57cec5SDimitry Andric   }
1100b57cec5SDimitry Andric 
111*480093f4SDimitry Andric   /// Remove register from copy maps.
112*480093f4SDimitry Andric   void invalidateRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
113*480093f4SDimitry Andric     // Since Reg might be a subreg of some registers, only invalidate Reg is not
114*480093f4SDimitry Andric     // enough. We have to find the COPY defines Reg or registers defined by Reg
115*480093f4SDimitry Andric     // and invalidate all of them.
116*480093f4SDimitry Andric     DenseSet<unsigned> RegsToInvalidate{Reg};
117*480093f4SDimitry Andric     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
118*480093f4SDimitry Andric       auto I = Copies.find(*RUI);
119*480093f4SDimitry Andric       if (I != Copies.end()) {
120*480093f4SDimitry Andric         if (MachineInstr *MI = I->second.MI) {
121*480093f4SDimitry Andric           RegsToInvalidate.insert(MI->getOperand(0).getReg());
122*480093f4SDimitry Andric           RegsToInvalidate.insert(MI->getOperand(1).getReg());
123*480093f4SDimitry Andric         }
124*480093f4SDimitry Andric         RegsToInvalidate.insert(I->second.DefRegs.begin(),
125*480093f4SDimitry Andric                                 I->second.DefRegs.end());
126*480093f4SDimitry Andric       }
127*480093f4SDimitry Andric     }
128*480093f4SDimitry Andric     for (unsigned InvalidReg : RegsToInvalidate)
129*480093f4SDimitry Andric       for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
130*480093f4SDimitry Andric         Copies.erase(*RUI);
131*480093f4SDimitry Andric   }
132*480093f4SDimitry Andric 
1330b57cec5SDimitry Andric   /// Clobber a single register, removing it from the tracker's copy maps.
1340b57cec5SDimitry Andric   void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
1350b57cec5SDimitry Andric     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
1360b57cec5SDimitry Andric       auto I = Copies.find(*RUI);
1370b57cec5SDimitry Andric       if (I != Copies.end()) {
1380b57cec5SDimitry Andric         // When we clobber the source of a copy, we need to clobber everything
1390b57cec5SDimitry Andric         // it defined.
1400b57cec5SDimitry Andric         markRegsUnavailable(I->second.DefRegs, TRI);
1410b57cec5SDimitry Andric         // When we clobber the destination of a copy, we need to clobber the
1420b57cec5SDimitry Andric         // whole register it defined.
1430b57cec5SDimitry Andric         if (MachineInstr *MI = I->second.MI)
1440b57cec5SDimitry Andric           markRegsUnavailable({MI->getOperand(0).getReg()}, TRI);
1450b57cec5SDimitry Andric         // Now we can erase the copy.
1460b57cec5SDimitry Andric         Copies.erase(I);
1470b57cec5SDimitry Andric       }
1480b57cec5SDimitry Andric     }
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   /// Add this copy's registers into the tracker's copy maps.
1520b57cec5SDimitry Andric   void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
1530b57cec5SDimitry Andric     assert(MI->isCopy() && "Tracking non-copy?");
1540b57cec5SDimitry Andric 
1558bcb0991SDimitry Andric     Register Def = MI->getOperand(0).getReg();
1568bcb0991SDimitry Andric     Register Src = MI->getOperand(1).getReg();
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric     // Remember Def is defined by the copy.
1590b57cec5SDimitry Andric     for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
1600b57cec5SDimitry Andric       Copies[*RUI] = {MI, {}, true};
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric     // Remember source that's copied to Def. Once it's clobbered, then
1630b57cec5SDimitry Andric     // it's no longer available for copy propagation.
1640b57cec5SDimitry Andric     for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
1650b57cec5SDimitry Andric       auto I = Copies.insert({*RUI, {nullptr, {}, false}});
1660b57cec5SDimitry Andric       auto &Copy = I.first->second;
1670b57cec5SDimitry Andric       if (!is_contained(Copy.DefRegs, Def))
1680b57cec5SDimitry Andric         Copy.DefRegs.push_back(Def);
1690b57cec5SDimitry Andric     }
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   bool hasAnyCopies() {
1730b57cec5SDimitry Andric     return !Copies.empty();
1740b57cec5SDimitry Andric   }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI,
1770b57cec5SDimitry Andric                          bool MustBeAvailable = false) {
1780b57cec5SDimitry Andric     auto CI = Copies.find(RegUnit);
1790b57cec5SDimitry Andric     if (CI == Copies.end())
1800b57cec5SDimitry Andric       return nullptr;
1810b57cec5SDimitry Andric     if (MustBeAvailable && !CI->second.Avail)
1820b57cec5SDimitry Andric       return nullptr;
1830b57cec5SDimitry Andric     return CI->second.MI;
1840b57cec5SDimitry Andric   }
1850b57cec5SDimitry Andric 
186*480093f4SDimitry Andric   MachineInstr *findCopyDefViaUnit(unsigned RegUnit,
187*480093f4SDimitry Andric                                     const TargetRegisterInfo &TRI) {
188*480093f4SDimitry Andric     auto CI = Copies.find(RegUnit);
189*480093f4SDimitry Andric     if (CI == Copies.end())
190*480093f4SDimitry Andric       return nullptr;
191*480093f4SDimitry Andric     if (CI->second.DefRegs.size() != 1)
192*480093f4SDimitry Andric       return nullptr;
193*480093f4SDimitry Andric     MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI);
194*480093f4SDimitry Andric     return findCopyForUnit(*RUI, TRI, true);
195*480093f4SDimitry Andric   }
196*480093f4SDimitry Andric 
197*480093f4SDimitry Andric   MachineInstr *findAvailBackwardCopy(MachineInstr &I, unsigned Reg,
198*480093f4SDimitry Andric                                       const TargetRegisterInfo &TRI) {
199*480093f4SDimitry Andric     MCRegUnitIterator RUI(Reg, &TRI);
200*480093f4SDimitry Andric     MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
201*480093f4SDimitry Andric     if (!AvailCopy ||
202*480093f4SDimitry Andric         !TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg))
203*480093f4SDimitry Andric       return nullptr;
204*480093f4SDimitry Andric 
205*480093f4SDimitry Andric     Register AvailSrc = AvailCopy->getOperand(1).getReg();
206*480093f4SDimitry Andric     Register AvailDef = AvailCopy->getOperand(0).getReg();
207*480093f4SDimitry Andric     for (const MachineInstr &MI :
208*480093f4SDimitry Andric          make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
209*480093f4SDimitry Andric       for (const MachineOperand &MO : MI.operands())
210*480093f4SDimitry Andric         if (MO.isRegMask())
211*480093f4SDimitry Andric           // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
212*480093f4SDimitry Andric           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
213*480093f4SDimitry Andric             return nullptr;
214*480093f4SDimitry Andric 
215*480093f4SDimitry Andric     return AvailCopy;
216*480093f4SDimitry Andric   }
217*480093f4SDimitry Andric 
2180b57cec5SDimitry Andric   MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg,
2190b57cec5SDimitry Andric                               const TargetRegisterInfo &TRI) {
2200b57cec5SDimitry Andric     // We check the first RegUnit here, since we'll only be interested in the
2210b57cec5SDimitry Andric     // copy if it copies the entire register anyway.
2220b57cec5SDimitry Andric     MCRegUnitIterator RUI(Reg, &TRI);
2230b57cec5SDimitry Andric     MachineInstr *AvailCopy =
2240b57cec5SDimitry Andric         findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
2250b57cec5SDimitry Andric     if (!AvailCopy ||
2260b57cec5SDimitry Andric         !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
2270b57cec5SDimitry Andric       return nullptr;
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric     // Check that the available copy isn't clobbered by any regmasks between
2300b57cec5SDimitry Andric     // itself and the destination.
2318bcb0991SDimitry Andric     Register AvailSrc = AvailCopy->getOperand(1).getReg();
2328bcb0991SDimitry Andric     Register AvailDef = AvailCopy->getOperand(0).getReg();
2330b57cec5SDimitry Andric     for (const MachineInstr &MI :
2340b57cec5SDimitry Andric          make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
2350b57cec5SDimitry Andric       for (const MachineOperand &MO : MI.operands())
2360b57cec5SDimitry Andric         if (MO.isRegMask())
2370b57cec5SDimitry Andric           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
2380b57cec5SDimitry Andric             return nullptr;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric     return AvailCopy;
2410b57cec5SDimitry Andric   }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   void clear() {
2440b57cec5SDimitry Andric     Copies.clear();
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric };
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric class MachineCopyPropagation : public MachineFunctionPass {
2490b57cec5SDimitry Andric   const TargetRegisterInfo *TRI;
2500b57cec5SDimitry Andric   const TargetInstrInfo *TII;
2510b57cec5SDimitry Andric   const MachineRegisterInfo *MRI;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric public:
2540b57cec5SDimitry Andric   static char ID; // Pass identification, replacement for typeid
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   MachineCopyPropagation() : MachineFunctionPass(ID) {
2570b57cec5SDimitry Andric     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
2610b57cec5SDimitry Andric     AU.setPreservesCFG();
2620b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
2630b57cec5SDimitry Andric   }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
2680b57cec5SDimitry Andric     return MachineFunctionProperties().set(
2690b57cec5SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric private:
2738bcb0991SDimitry Andric   typedef enum { DebugUse = false, RegularUse = true } DebugType;
2748bcb0991SDimitry Andric 
2750b57cec5SDimitry Andric   void ClobberRegister(unsigned Reg);
2768bcb0991SDimitry Andric   void ReadRegister(unsigned Reg, MachineInstr &Reader,
2778bcb0991SDimitry Andric                     DebugType DT);
278*480093f4SDimitry Andric   void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
279*480093f4SDimitry Andric   void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
2800b57cec5SDimitry Andric   bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
2810b57cec5SDimitry Andric   void forwardUses(MachineInstr &MI);
282*480093f4SDimitry Andric   void propagateDefs(MachineInstr &MI);
2830b57cec5SDimitry Andric   bool isForwardableRegClassCopy(const MachineInstr &Copy,
2840b57cec5SDimitry Andric                                  const MachineInstr &UseI, unsigned UseIdx);
285*480093f4SDimitry Andric   bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
286*480093f4SDimitry Andric                                           const MachineInstr &UseI,
287*480093f4SDimitry Andric                                           unsigned UseIdx);
2880b57cec5SDimitry Andric   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   /// Candidates for deletion.
2910b57cec5SDimitry Andric   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
2920b57cec5SDimitry Andric 
2938bcb0991SDimitry Andric   /// Multimap tracking debug users in current BB
2948bcb0991SDimitry Andric   DenseMap<MachineInstr*, SmallVector<MachineInstr*, 2>> CopyDbgUsers;
2958bcb0991SDimitry Andric 
2960b57cec5SDimitry Andric   CopyTracker Tracker;
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   bool Changed;
2990b57cec5SDimitry Andric };
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric } // end anonymous namespace
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric char MachineCopyPropagation::ID = 0;
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
3080b57cec5SDimitry Andric                 "Machine Copy Propagation Pass", false, false)
3090b57cec5SDimitry Andric 
3108bcb0991SDimitry Andric void MachineCopyPropagation::ReadRegister(unsigned Reg, MachineInstr &Reader,
3118bcb0991SDimitry Andric                                           DebugType DT) {
3120b57cec5SDimitry Andric   // If 'Reg' is defined by a copy, the copy is no longer a candidate
3138bcb0991SDimitry Andric   // for elimination. If a copy is "read" by a debug user, record the user
3148bcb0991SDimitry Andric   // for propagation.
3150b57cec5SDimitry Andric   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
3160b57cec5SDimitry Andric     if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
3178bcb0991SDimitry Andric       if (DT == RegularUse) {
3180b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
3190b57cec5SDimitry Andric         MaybeDeadCopies.remove(Copy);
3208bcb0991SDimitry Andric       } else {
3218bcb0991SDimitry Andric         CopyDbgUsers[Copy].push_back(&Reader);
3228bcb0991SDimitry Andric       }
3230b57cec5SDimitry Andric     }
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
3280b57cec5SDimitry Andric /// This fact may have been obscured by sub register usage or may not be true at
3290b57cec5SDimitry Andric /// all even though Src and Def are subregisters of the registers used in
3300b57cec5SDimitry Andric /// PreviousCopy. e.g.
3310b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AX, CX) == true
3320b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AH, CL) == false
3330b57cec5SDimitry Andric static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
3340b57cec5SDimitry Andric                       unsigned Def, const TargetRegisterInfo *TRI) {
3358bcb0991SDimitry Andric   Register PreviousSrc = PreviousCopy.getOperand(1).getReg();
3368bcb0991SDimitry Andric   Register PreviousDef = PreviousCopy.getOperand(0).getReg();
3370b57cec5SDimitry Andric   if (Src == PreviousSrc) {
3380b57cec5SDimitry Andric     assert(Def == PreviousDef);
3390b57cec5SDimitry Andric     return true;
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric   if (!TRI->isSubRegister(PreviousSrc, Src))
3420b57cec5SDimitry Andric     return false;
3430b57cec5SDimitry Andric   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
3440b57cec5SDimitry Andric   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric /// Remove instruction \p Copy if there exists a previous copy that copies the
3480b57cec5SDimitry Andric /// register \p Src to the register \p Def; This may happen indirectly by
3490b57cec5SDimitry Andric /// copying the super registers.
3500b57cec5SDimitry Andric bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
3510b57cec5SDimitry Andric                                               unsigned Def) {
3520b57cec5SDimitry Andric   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
3530b57cec5SDimitry Andric   // the value (Example: The sparc zero register is writable but stays zero).
3540b57cec5SDimitry Andric   if (MRI->isReserved(Src) || MRI->isReserved(Def))
3550b57cec5SDimitry Andric     return false;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // Search for an existing copy.
3580b57cec5SDimitry Andric   MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
3590b57cec5SDimitry Andric   if (!PrevCopy)
3600b57cec5SDimitry Andric     return false;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   // Check that the existing copy uses the correct sub registers.
3630b57cec5SDimitry Andric   if (PrevCopy->getOperand(0).isDead())
3640b57cec5SDimitry Andric     return false;
3650b57cec5SDimitry Andric   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
3660b57cec5SDimitry Andric     return false;
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   // Copy was redundantly redefining either Src or Def. Remove earlier kill
3710b57cec5SDimitry Andric   // flags between Copy and PrevCopy because the value will be reused now.
3720b57cec5SDimitry Andric   assert(Copy.isCopy());
3738bcb0991SDimitry Andric   Register CopyDef = Copy.getOperand(0).getReg();
3740b57cec5SDimitry Andric   assert(CopyDef == Src || CopyDef == Def);
3750b57cec5SDimitry Andric   for (MachineInstr &MI :
3760b57cec5SDimitry Andric        make_range(PrevCopy->getIterator(), Copy.getIterator()))
3770b57cec5SDimitry Andric     MI.clearRegisterKills(CopyDef, TRI);
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   Copy.eraseFromParent();
3800b57cec5SDimitry Andric   Changed = true;
3810b57cec5SDimitry Andric   ++NumDeletes;
3820b57cec5SDimitry Andric   return true;
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
385*480093f4SDimitry Andric bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
386*480093f4SDimitry Andric     const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
387*480093f4SDimitry Andric   Register Def = Copy.getOperand(0).getReg();
388*480093f4SDimitry Andric 
389*480093f4SDimitry Andric   if (const TargetRegisterClass *URC =
390*480093f4SDimitry Andric           UseI.getRegClassConstraint(UseIdx, TII, TRI))
391*480093f4SDimitry Andric     return URC->contains(Def);
392*480093f4SDimitry Andric 
393*480093f4SDimitry Andric   // We don't process further if UseI is a COPY, since forward copy propagation
394*480093f4SDimitry Andric   // should handle that.
395*480093f4SDimitry Andric   return false;
396*480093f4SDimitry Andric }
397*480093f4SDimitry Andric 
3980b57cec5SDimitry Andric /// Decide whether we should forward the source of \param Copy to its use in
3990b57cec5SDimitry Andric /// \param UseI based on the physical register class constraints of the opcode
4000b57cec5SDimitry Andric /// and avoiding introducing more cross-class COPYs.
4010b57cec5SDimitry Andric bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
4020b57cec5SDimitry Andric                                                        const MachineInstr &UseI,
4030b57cec5SDimitry Andric                                                        unsigned UseIdx) {
4040b57cec5SDimitry Andric 
4058bcb0991SDimitry Andric   Register CopySrcReg = Copy.getOperand(1).getReg();
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   // If the new register meets the opcode register constraints, then allow
4080b57cec5SDimitry Andric   // forwarding.
4090b57cec5SDimitry Andric   if (const TargetRegisterClass *URC =
4100b57cec5SDimitry Andric           UseI.getRegClassConstraint(UseIdx, TII, TRI))
4110b57cec5SDimitry Andric     return URC->contains(CopySrcReg);
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   if (!UseI.isCopy())
4140b57cec5SDimitry Andric     return false;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   /// COPYs don't have register class constraints, so if the user instruction
4170b57cec5SDimitry Andric   /// is a COPY, we just try to avoid introducing additional cross-class
4180b57cec5SDimitry Andric   /// COPYs.  For example:
4190b57cec5SDimitry Andric   ///
4200b57cec5SDimitry Andric   ///   RegClassA = COPY RegClassB  // Copy parameter
4210b57cec5SDimitry Andric   ///   ...
4220b57cec5SDimitry Andric   ///   RegClassB = COPY RegClassA  // UseI parameter
4230b57cec5SDimitry Andric   ///
4240b57cec5SDimitry Andric   /// which after forwarding becomes
4250b57cec5SDimitry Andric   ///
4260b57cec5SDimitry Andric   ///   RegClassA = COPY RegClassB
4270b57cec5SDimitry Andric   ///   ...
4280b57cec5SDimitry Andric   ///   RegClassB = COPY RegClassB
4290b57cec5SDimitry Andric   ///
4300b57cec5SDimitry Andric   /// so we have reduced the number of cross-class COPYs and potentially
4310b57cec5SDimitry Andric   /// introduced a nop COPY that can be removed.
4320b57cec5SDimitry Andric   const TargetRegisterClass *UseDstRC =
4330b57cec5SDimitry Andric       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   const TargetRegisterClass *SuperRC = UseDstRC;
4360b57cec5SDimitry Andric   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
4370b57cec5SDimitry Andric        SuperRC; SuperRC = *SuperRCI++)
4380b57cec5SDimitry Andric     if (SuperRC->contains(CopySrcReg))
4390b57cec5SDimitry Andric       return true;
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   return false;
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric /// Check that \p MI does not have implicit uses that overlap with it's \p Use
4450b57cec5SDimitry Andric /// operand (the register being replaced), since these can sometimes be
4460b57cec5SDimitry Andric /// implicitly tied to other operands.  For example, on AMDGPU:
4470b57cec5SDimitry Andric ///
4480b57cec5SDimitry Andric /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
4490b57cec5SDimitry Andric ///
4500b57cec5SDimitry Andric /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
4510b57cec5SDimitry Andric /// way of knowing we need to update the latter when updating the former.
4520b57cec5SDimitry Andric bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
4530b57cec5SDimitry Andric                                                 const MachineOperand &Use) {
4540b57cec5SDimitry Andric   for (const MachineOperand &MIUse : MI.uses())
4550b57cec5SDimitry Andric     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
4560b57cec5SDimitry Andric         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
4570b57cec5SDimitry Andric       return true;
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   return false;
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric /// Look for available copies whose destination register is used by \p MI and
4630b57cec5SDimitry Andric /// replace the use in \p MI with the copy's source register.
4640b57cec5SDimitry Andric void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
4650b57cec5SDimitry Andric   if (!Tracker.hasAnyCopies())
4660b57cec5SDimitry Andric     return;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   // Look for non-tied explicit vreg uses that have an active COPY
4690b57cec5SDimitry Andric   // instruction that defines the physical register allocated to them.
4700b57cec5SDimitry Andric   // Replace the vreg with the source of the active COPY.
4710b57cec5SDimitry Andric   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
4720b57cec5SDimitry Andric        ++OpIdx) {
4730b57cec5SDimitry Andric     MachineOperand &MOUse = MI.getOperand(OpIdx);
4740b57cec5SDimitry Andric     // Don't forward into undef use operands since doing so can cause problems
4750b57cec5SDimitry Andric     // with the machine verifier, since it doesn't treat undef reads as reads,
4760b57cec5SDimitry Andric     // so we can end up with a live range that ends on an undef read, leading to
4770b57cec5SDimitry Andric     // an error that the live range doesn't end on a read of the live range
4780b57cec5SDimitry Andric     // register.
4790b57cec5SDimitry Andric     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
4800b57cec5SDimitry Andric         MOUse.isImplicit())
4810b57cec5SDimitry Andric       continue;
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     if (!MOUse.getReg())
4840b57cec5SDimitry Andric       continue;
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric     // Check that the register is marked 'renamable' so we know it is safe to
4870b57cec5SDimitry Andric     // rename it without violating any constraints that aren't expressed in the
4880b57cec5SDimitry Andric     // IR (e.g. ABI or opcode requirements).
4890b57cec5SDimitry Andric     if (!MOUse.isRenamable())
4900b57cec5SDimitry Andric       continue;
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric     MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI);
4930b57cec5SDimitry Andric     if (!Copy)
4940b57cec5SDimitry Andric       continue;
4950b57cec5SDimitry Andric 
4968bcb0991SDimitry Andric     Register CopyDstReg = Copy->getOperand(0).getReg();
4970b57cec5SDimitry Andric     const MachineOperand &CopySrc = Copy->getOperand(1);
4988bcb0991SDimitry Andric     Register CopySrcReg = CopySrc.getReg();
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric     // FIXME: Don't handle partial uses of wider COPYs yet.
5010b57cec5SDimitry Andric     if (MOUse.getReg() != CopyDstReg) {
5020b57cec5SDimitry Andric       LLVM_DEBUG(
5030b57cec5SDimitry Andric           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
5040b57cec5SDimitry Andric                  << MI);
5050b57cec5SDimitry Andric       continue;
5060b57cec5SDimitry Andric     }
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric     // Don't forward COPYs of reserved regs unless they are constant.
5090b57cec5SDimitry Andric     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
5100b57cec5SDimitry Andric       continue;
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
5130b57cec5SDimitry Andric       continue;
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     if (hasImplicitOverlap(MI, MOUse))
5160b57cec5SDimitry Andric       continue;
5170b57cec5SDimitry Andric 
518*480093f4SDimitry Andric     // Check that the instruction is not a copy that partially overwrites the
519*480093f4SDimitry Andric     // original copy source that we are about to use. The tracker mechanism
520*480093f4SDimitry Andric     // cannot cope with that.
521*480093f4SDimitry Andric     if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
522*480093f4SDimitry Andric         !MI.definesRegister(CopySrcReg)) {
523*480093f4SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
524*480093f4SDimitry Andric       continue;
525*480093f4SDimitry Andric     }
526*480093f4SDimitry Andric 
5270b57cec5SDimitry Andric     if (!DebugCounter::shouldExecute(FwdCounter)) {
5280b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
5290b57cec5SDimitry Andric                         << MI);
5300b57cec5SDimitry Andric       continue;
5310b57cec5SDimitry Andric     }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
5340b57cec5SDimitry Andric                       << "\n     with " << printReg(CopySrcReg, TRI)
5350b57cec5SDimitry Andric                       << "\n     in " << MI << "     from " << *Copy);
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric     MOUse.setReg(CopySrcReg);
5380b57cec5SDimitry Andric     if (!CopySrc.isRenamable())
5390b57cec5SDimitry Andric       MOUse.setIsRenamable(false);
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric     // Clear kill markers that may have been invalidated.
5440b57cec5SDimitry Andric     for (MachineInstr &KMI :
5450b57cec5SDimitry Andric          make_range(Copy->getIterator(), std::next(MI.getIterator())))
5460b57cec5SDimitry Andric       KMI.clearRegisterKills(CopySrcReg, TRI);
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric     ++NumCopyForwards;
5490b57cec5SDimitry Andric     Changed = true;
5500b57cec5SDimitry Andric   }
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric 
553*480093f4SDimitry Andric void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
554*480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
555*480093f4SDimitry Andric                     << "\n");
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
5580b57cec5SDimitry Andric     MachineInstr *MI = &*I;
5590b57cec5SDimitry Andric     ++I;
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric     // Analyze copies (which don't overlap themselves).
5620b57cec5SDimitry Andric     if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
5630b57cec5SDimitry Andric                                           MI->getOperand(1).getReg())) {
5648bcb0991SDimitry Andric       Register Def = MI->getOperand(0).getReg();
5658bcb0991SDimitry Andric       Register Src = MI->getOperand(1).getReg();
5660b57cec5SDimitry Andric 
5678bcb0991SDimitry Andric       assert(!Register::isVirtualRegister(Def) &&
5688bcb0991SDimitry Andric              !Register::isVirtualRegister(Src) &&
5690b57cec5SDimitry Andric              "MachineCopyPropagation should be run after register allocation!");
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric       // The two copies cancel out and the source of the first copy
5720b57cec5SDimitry Andric       // hasn't been overridden, eliminate the second one. e.g.
5730b57cec5SDimitry Andric       //  %ecx = COPY %eax
5740b57cec5SDimitry Andric       //  ... nothing clobbered eax.
5750b57cec5SDimitry Andric       //  %eax = COPY %ecx
5760b57cec5SDimitry Andric       // =>
5770b57cec5SDimitry Andric       //  %ecx = COPY %eax
5780b57cec5SDimitry Andric       //
5790b57cec5SDimitry Andric       // or
5800b57cec5SDimitry Andric       //
5810b57cec5SDimitry Andric       //  %ecx = COPY %eax
5820b57cec5SDimitry Andric       //  ... nothing clobbered eax.
5830b57cec5SDimitry Andric       //  %ecx = COPY %eax
5840b57cec5SDimitry Andric       // =>
5850b57cec5SDimitry Andric       //  %ecx = COPY %eax
5860b57cec5SDimitry Andric       if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
5870b57cec5SDimitry Andric         continue;
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric       forwardUses(*MI);
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric       // Src may have been changed by forwardUses()
5920b57cec5SDimitry Andric       Src = MI->getOperand(1).getReg();
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric       // If Src is defined by a previous copy, the previous copy cannot be
5950b57cec5SDimitry Andric       // eliminated.
5968bcb0991SDimitry Andric       ReadRegister(Src, *MI, RegularUse);
5970b57cec5SDimitry Andric       for (const MachineOperand &MO : MI->implicit_operands()) {
5980b57cec5SDimitry Andric         if (!MO.isReg() || !MO.readsReg())
5990b57cec5SDimitry Andric           continue;
6008bcb0991SDimitry Andric         Register Reg = MO.getReg();
6010b57cec5SDimitry Andric         if (!Reg)
6020b57cec5SDimitry Andric           continue;
6038bcb0991SDimitry Andric         ReadRegister(Reg, *MI, RegularUse);
6040b57cec5SDimitry Andric       }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric       // Copy is now a candidate for deletion.
6090b57cec5SDimitry Andric       if (!MRI->isReserved(Def))
6100b57cec5SDimitry Andric         MaybeDeadCopies.insert(MI);
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric       // If 'Def' is previously source of another copy, then this earlier copy's
6130b57cec5SDimitry Andric       // source is no longer available. e.g.
6140b57cec5SDimitry Andric       // %xmm9 = copy %xmm2
6150b57cec5SDimitry Andric       // ...
6160b57cec5SDimitry Andric       // %xmm2 = copy %xmm0
6170b57cec5SDimitry Andric       // ...
6180b57cec5SDimitry Andric       // %xmm2 = copy %xmm9
6190b57cec5SDimitry Andric       Tracker.clobberRegister(Def, *TRI);
6200b57cec5SDimitry Andric       for (const MachineOperand &MO : MI->implicit_operands()) {
6210b57cec5SDimitry Andric         if (!MO.isReg() || !MO.isDef())
6220b57cec5SDimitry Andric           continue;
6238bcb0991SDimitry Andric         Register Reg = MO.getReg();
6240b57cec5SDimitry Andric         if (!Reg)
6250b57cec5SDimitry Andric           continue;
6260b57cec5SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
6270b57cec5SDimitry Andric       }
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric       Tracker.trackCopy(MI, *TRI);
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric       continue;
6320b57cec5SDimitry Andric     }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric     // Clobber any earlyclobber regs first.
6350b57cec5SDimitry Andric     for (const MachineOperand &MO : MI->operands())
6360b57cec5SDimitry Andric       if (MO.isReg() && MO.isEarlyClobber()) {
6378bcb0991SDimitry Andric         Register Reg = MO.getReg();
6380b57cec5SDimitry Andric         // If we have a tied earlyclobber, that means it is also read by this
6390b57cec5SDimitry Andric         // instruction, so we need to make sure we don't remove it as dead
6400b57cec5SDimitry Andric         // later.
6410b57cec5SDimitry Andric         if (MO.isTied())
6428bcb0991SDimitry Andric           ReadRegister(Reg, *MI, RegularUse);
6430b57cec5SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
6440b57cec5SDimitry Andric       }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric     forwardUses(*MI);
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric     // Not a copy.
6490b57cec5SDimitry Andric     SmallVector<unsigned, 2> Defs;
6500b57cec5SDimitry Andric     const MachineOperand *RegMask = nullptr;
6510b57cec5SDimitry Andric     for (const MachineOperand &MO : MI->operands()) {
6520b57cec5SDimitry Andric       if (MO.isRegMask())
6530b57cec5SDimitry Andric         RegMask = &MO;
6540b57cec5SDimitry Andric       if (!MO.isReg())
6550b57cec5SDimitry Andric         continue;
6568bcb0991SDimitry Andric       Register Reg = MO.getReg();
6570b57cec5SDimitry Andric       if (!Reg)
6580b57cec5SDimitry Andric         continue;
6590b57cec5SDimitry Andric 
6608bcb0991SDimitry Andric       assert(!Register::isVirtualRegister(Reg) &&
6610b57cec5SDimitry Andric              "MachineCopyPropagation should be run after register allocation!");
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric       if (MO.isDef() && !MO.isEarlyClobber()) {
6640b57cec5SDimitry Andric         Defs.push_back(Reg);
6650b57cec5SDimitry Andric         continue;
6668bcb0991SDimitry Andric       } else if (MO.readsReg())
6678bcb0991SDimitry Andric         ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse);
6680b57cec5SDimitry Andric     }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric     // The instruction has a register mask operand which means that it clobbers
6710b57cec5SDimitry Andric     // a large set of registers.  Treat clobbered registers the same way as
6720b57cec5SDimitry Andric     // defined registers.
6730b57cec5SDimitry Andric     if (RegMask) {
6740b57cec5SDimitry Andric       // Erase any MaybeDeadCopies whose destination register is clobbered.
6750b57cec5SDimitry Andric       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
6760b57cec5SDimitry Andric                MaybeDeadCopies.begin();
6770b57cec5SDimitry Andric            DI != MaybeDeadCopies.end();) {
6780b57cec5SDimitry Andric         MachineInstr *MaybeDead = *DI;
6798bcb0991SDimitry Andric         Register Reg = MaybeDead->getOperand(0).getReg();
6800b57cec5SDimitry Andric         assert(!MRI->isReserved(Reg));
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric         if (!RegMask->clobbersPhysReg(Reg)) {
6830b57cec5SDimitry Andric           ++DI;
6840b57cec5SDimitry Andric           continue;
6850b57cec5SDimitry Andric         }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
6880b57cec5SDimitry Andric                    MaybeDead->dump());
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric         // Make sure we invalidate any entries in the copy maps before erasing
6910b57cec5SDimitry Andric         // the instruction.
6920b57cec5SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric         // erase() will return the next valid iterator pointing to the next
6950b57cec5SDimitry Andric         // element after the erased one.
6960b57cec5SDimitry Andric         DI = MaybeDeadCopies.erase(DI);
6970b57cec5SDimitry Andric         MaybeDead->eraseFromParent();
6980b57cec5SDimitry Andric         Changed = true;
6990b57cec5SDimitry Andric         ++NumDeletes;
7000b57cec5SDimitry Andric       }
7010b57cec5SDimitry Andric     }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric     // Any previous copy definition or reading the Defs is no longer available.
7040b57cec5SDimitry Andric     for (unsigned Reg : Defs)
7050b57cec5SDimitry Andric       Tracker.clobberRegister(Reg, *TRI);
7060b57cec5SDimitry Andric   }
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric   // If MBB doesn't have successors, delete the copies whose defs are not used.
7090b57cec5SDimitry Andric   // If MBB does have successors, then conservative assume the defs are live-out
7100b57cec5SDimitry Andric   // since we don't want to trust live-in lists.
7110b57cec5SDimitry Andric   if (MBB.succ_empty()) {
7120b57cec5SDimitry Andric     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
7130b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
7140b57cec5SDimitry Andric                  MaybeDead->dump());
7150b57cec5SDimitry Andric       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
7160b57cec5SDimitry Andric 
7178bcb0991SDimitry Andric       // Update matching debug values, if any.
7180b57cec5SDimitry Andric       assert(MaybeDead->isCopy());
7198bcb0991SDimitry Andric       unsigned SrcReg = MaybeDead->getOperand(1).getReg();
7208bcb0991SDimitry Andric       MRI->updateDbgUsersToReg(SrcReg, CopyDbgUsers[MaybeDead]);
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric       MaybeDead->eraseFromParent();
7230b57cec5SDimitry Andric       Changed = true;
7240b57cec5SDimitry Andric       ++NumDeletes;
7250b57cec5SDimitry Andric     }
7260b57cec5SDimitry Andric   }
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric   MaybeDeadCopies.clear();
7298bcb0991SDimitry Andric   CopyDbgUsers.clear();
7300b57cec5SDimitry Andric   Tracker.clear();
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric 
733*480093f4SDimitry Andric static bool isBackwardPropagatableCopy(MachineInstr &MI,
734*480093f4SDimitry Andric                                        const MachineRegisterInfo &MRI) {
735*480093f4SDimitry Andric   assert(MI.isCopy() && "MI is expected to be a COPY");
736*480093f4SDimitry Andric   Register Def = MI.getOperand(0).getReg();
737*480093f4SDimitry Andric   Register Src = MI.getOperand(1).getReg();
738*480093f4SDimitry Andric 
739*480093f4SDimitry Andric   if (!Def || !Src)
740*480093f4SDimitry Andric     return false;
741*480093f4SDimitry Andric 
742*480093f4SDimitry Andric   if (MRI.isReserved(Def) || MRI.isReserved(Src))
743*480093f4SDimitry Andric     return false;
744*480093f4SDimitry Andric 
745*480093f4SDimitry Andric   return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
746*480093f4SDimitry Andric }
747*480093f4SDimitry Andric 
748*480093f4SDimitry Andric void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
749*480093f4SDimitry Andric   if (!Tracker.hasAnyCopies())
750*480093f4SDimitry Andric     return;
751*480093f4SDimitry Andric 
752*480093f4SDimitry Andric   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
753*480093f4SDimitry Andric        ++OpIdx) {
754*480093f4SDimitry Andric     MachineOperand &MODef = MI.getOperand(OpIdx);
755*480093f4SDimitry Andric 
756*480093f4SDimitry Andric     if (!MODef.isReg() || MODef.isUse())
757*480093f4SDimitry Andric       continue;
758*480093f4SDimitry Andric 
759*480093f4SDimitry Andric     // Ignore non-trivial cases.
760*480093f4SDimitry Andric     if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
761*480093f4SDimitry Andric       continue;
762*480093f4SDimitry Andric 
763*480093f4SDimitry Andric     if (!MODef.getReg())
764*480093f4SDimitry Andric       continue;
765*480093f4SDimitry Andric 
766*480093f4SDimitry Andric     // We only handle if the register comes from a vreg.
767*480093f4SDimitry Andric     if (!MODef.isRenamable())
768*480093f4SDimitry Andric       continue;
769*480093f4SDimitry Andric 
770*480093f4SDimitry Andric     MachineInstr *Copy =
771*480093f4SDimitry Andric         Tracker.findAvailBackwardCopy(MI, MODef.getReg(), *TRI);
772*480093f4SDimitry Andric     if (!Copy)
773*480093f4SDimitry Andric       continue;
774*480093f4SDimitry Andric 
775*480093f4SDimitry Andric     Register Def = Copy->getOperand(0).getReg();
776*480093f4SDimitry Andric     Register Src = Copy->getOperand(1).getReg();
777*480093f4SDimitry Andric 
778*480093f4SDimitry Andric     if (MODef.getReg() != Src)
779*480093f4SDimitry Andric       continue;
780*480093f4SDimitry Andric 
781*480093f4SDimitry Andric     if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
782*480093f4SDimitry Andric       continue;
783*480093f4SDimitry Andric 
784*480093f4SDimitry Andric     if (hasImplicitOverlap(MI, MODef))
785*480093f4SDimitry Andric       continue;
786*480093f4SDimitry Andric 
787*480093f4SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
788*480093f4SDimitry Andric                       << "\n     with " << printReg(Def, TRI) << "\n     in "
789*480093f4SDimitry Andric                       << MI << "     from " << *Copy);
790*480093f4SDimitry Andric 
791*480093f4SDimitry Andric     MODef.setReg(Def);
792*480093f4SDimitry Andric     MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
793*480093f4SDimitry Andric 
794*480093f4SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
795*480093f4SDimitry Andric     MaybeDeadCopies.insert(Copy);
796*480093f4SDimitry Andric     Changed = true;
797*480093f4SDimitry Andric     ++NumCopyBackwardPropagated;
798*480093f4SDimitry Andric   }
799*480093f4SDimitry Andric }
800*480093f4SDimitry Andric 
801*480093f4SDimitry Andric void MachineCopyPropagation::BackwardCopyPropagateBlock(
802*480093f4SDimitry Andric     MachineBasicBlock &MBB) {
803*480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
804*480093f4SDimitry Andric                     << "\n");
805*480093f4SDimitry Andric 
806*480093f4SDimitry Andric   for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
807*480093f4SDimitry Andric        I != E;) {
808*480093f4SDimitry Andric     MachineInstr *MI = &*I;
809*480093f4SDimitry Andric     ++I;
810*480093f4SDimitry Andric 
811*480093f4SDimitry Andric     // Ignore non-trivial COPYs.
812*480093f4SDimitry Andric     if (MI->isCopy() && MI->getNumOperands() == 2 &&
813*480093f4SDimitry Andric         !TRI->regsOverlap(MI->getOperand(0).getReg(),
814*480093f4SDimitry Andric                           MI->getOperand(1).getReg())) {
815*480093f4SDimitry Andric 
816*480093f4SDimitry Andric       Register Def = MI->getOperand(0).getReg();
817*480093f4SDimitry Andric       Register Src = MI->getOperand(1).getReg();
818*480093f4SDimitry Andric 
819*480093f4SDimitry Andric       // Unlike forward cp, we don't invoke propagateDefs here,
820*480093f4SDimitry Andric       // just let forward cp do COPY-to-COPY propagation.
821*480093f4SDimitry Andric       if (isBackwardPropagatableCopy(*MI, *MRI)) {
822*480093f4SDimitry Andric         Tracker.invalidateRegister(Src, *TRI);
823*480093f4SDimitry Andric         Tracker.invalidateRegister(Def, *TRI);
824*480093f4SDimitry Andric         Tracker.trackCopy(MI, *TRI);
825*480093f4SDimitry Andric         continue;
826*480093f4SDimitry Andric       }
827*480093f4SDimitry Andric     }
828*480093f4SDimitry Andric 
829*480093f4SDimitry Andric     // Invalidate any earlyclobber regs first.
830*480093f4SDimitry Andric     for (const MachineOperand &MO : MI->operands())
831*480093f4SDimitry Andric       if (MO.isReg() && MO.isEarlyClobber()) {
832*480093f4SDimitry Andric         Register Reg = MO.getReg();
833*480093f4SDimitry Andric         if (!Reg)
834*480093f4SDimitry Andric           continue;
835*480093f4SDimitry Andric         Tracker.invalidateRegister(Reg, *TRI);
836*480093f4SDimitry Andric       }
837*480093f4SDimitry Andric 
838*480093f4SDimitry Andric     propagateDefs(*MI);
839*480093f4SDimitry Andric     for (const MachineOperand &MO : MI->operands()) {
840*480093f4SDimitry Andric       if (!MO.isReg())
841*480093f4SDimitry Andric         continue;
842*480093f4SDimitry Andric 
843*480093f4SDimitry Andric       if (!MO.getReg())
844*480093f4SDimitry Andric         continue;
845*480093f4SDimitry Andric 
846*480093f4SDimitry Andric       if (MO.isDef())
847*480093f4SDimitry Andric         Tracker.invalidateRegister(MO.getReg(), *TRI);
848*480093f4SDimitry Andric 
849*480093f4SDimitry Andric       if (MO.readsReg())
850*480093f4SDimitry Andric         Tracker.invalidateRegister(MO.getReg(), *TRI);
851*480093f4SDimitry Andric     }
852*480093f4SDimitry Andric   }
853*480093f4SDimitry Andric 
854*480093f4SDimitry Andric   for (auto *Copy : MaybeDeadCopies) {
855*480093f4SDimitry Andric     Copy->eraseFromParent();
856*480093f4SDimitry Andric     ++NumDeletes;
857*480093f4SDimitry Andric   }
858*480093f4SDimitry Andric 
859*480093f4SDimitry Andric   MaybeDeadCopies.clear();
860*480093f4SDimitry Andric   CopyDbgUsers.clear();
861*480093f4SDimitry Andric   Tracker.clear();
862*480093f4SDimitry Andric }
863*480093f4SDimitry Andric 
8640b57cec5SDimitry Andric bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
8650b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
8660b57cec5SDimitry Andric     return false;
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric   Changed = false;
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
8710b57cec5SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
8720b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
8730b57cec5SDimitry Andric 
874*480093f4SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
875*480093f4SDimitry Andric     BackwardCopyPropagateBlock(MBB);
876*480093f4SDimitry Andric     ForwardCopyPropagateBlock(MBB);
877*480093f4SDimitry Andric   }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   return Changed;
8800b57cec5SDimitry Andric }
881