1dff0c46cSDimitry Andric //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2dff0c46cSDimitry Andric //
3dff0c46cSDimitry Andric //                     The LLVM Compiler Infrastructure
4dff0c46cSDimitry Andric //
5dff0c46cSDimitry Andric // This file is distributed under the University of Illinois Open Source
6dff0c46cSDimitry Andric // License. See LICENSE.TXT for details.
7dff0c46cSDimitry Andric //
8dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
9dff0c46cSDimitry Andric //
10dff0c46cSDimitry Andric // This is an extremely simple MachineInstr-level copy propagation pass.
11dff0c46cSDimitry Andric //
124ba319b5SDimitry Andric // This pass forwards the source of COPYs to the users of their destinations
134ba319b5SDimitry Andric // when doing so is legal.  For example:
144ba319b5SDimitry Andric //
154ba319b5SDimitry Andric //   %reg1 = COPY %reg0
164ba319b5SDimitry Andric //   ...
174ba319b5SDimitry Andric //   ... = OP %reg1
184ba319b5SDimitry Andric //
194ba319b5SDimitry Andric // If
204ba319b5SDimitry Andric //   - %reg0 has not been clobbered by the time of the use of %reg1
214ba319b5SDimitry Andric //   - the register class constraints are satisfied
224ba319b5SDimitry Andric //   - the COPY def is the only value that reaches OP
234ba319b5SDimitry Andric // then this pass replaces the above with:
244ba319b5SDimitry Andric //
254ba319b5SDimitry Andric //   %reg1 = COPY %reg0
264ba319b5SDimitry Andric //   ...
274ba319b5SDimitry Andric //   ... = OP %reg0
284ba319b5SDimitry Andric //
294ba319b5SDimitry Andric // This pass also removes some redundant COPYs.  For example:
304ba319b5SDimitry Andric //
314ba319b5SDimitry Andric //    %R1 = COPY %R0
324ba319b5SDimitry Andric //    ... // No clobber of %R1
334ba319b5SDimitry Andric //    %R0 = COPY %R1 <<< Removed
344ba319b5SDimitry Andric //
354ba319b5SDimitry Andric // or
364ba319b5SDimitry Andric //
374ba319b5SDimitry Andric //    %R1 = COPY %R0
384ba319b5SDimitry Andric //    ... // No clobber of %R0
394ba319b5SDimitry Andric //    %R1 = COPY %R0 <<< Removed
404ba319b5SDimitry Andric //
41dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
42dff0c46cSDimitry Andric 
43dff0c46cSDimitry Andric #include "llvm/ADT/DenseMap.h"
442cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
45dff0c46cSDimitry Andric #include "llvm/ADT/SetVector.h"
46dff0c46cSDimitry Andric #include "llvm/ADT/SmallVector.h"
47dff0c46cSDimitry Andric #include "llvm/ADT/Statistic.h"
482cab237bSDimitry Andric #include "llvm/ADT/iterator_range.h"
492cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
50139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
51139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
522cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
532cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
54139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
554ba319b5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
562cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
572cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
582cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
59139f7f9bSDimitry Andric #include "llvm/Pass.h"
60139f7f9bSDimitry Andric #include "llvm/Support/Debug.h"
614ba319b5SDimitry Andric #include "llvm/Support/DebugCounter.h"
62139f7f9bSDimitry Andric #include "llvm/Support/raw_ostream.h"
632cab237bSDimitry Andric #include <cassert>
642cab237bSDimitry Andric #include <iterator>
652cab237bSDimitry Andric 
66dff0c46cSDimitry Andric using namespace llvm;
67dff0c46cSDimitry Andric 
68302affcbSDimitry Andric #define DEBUG_TYPE "machine-cp"
6991bc56edSDimitry Andric 
70dff0c46cSDimitry Andric STATISTIC(NumDeletes, "Number of dead copies deleted");
714ba319b5SDimitry Andric STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
724ba319b5SDimitry Andric DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
734ba319b5SDimitry Andric               "Controls which register COPYs are forwarded");
74dff0c46cSDimitry Andric 
75dff0c46cSDimitry Andric namespace {
762cab237bSDimitry Andric 
77*b5893f02SDimitry Andric class CopyTracker {
78*b5893f02SDimitry Andric   struct CopyInfo {
79*b5893f02SDimitry Andric     MachineInstr *MI;
80*b5893f02SDimitry Andric     SmallVector<unsigned, 4> DefRegs;
81*b5893f02SDimitry Andric     bool Avail;
82*b5893f02SDimitry Andric   };
83*b5893f02SDimitry Andric 
84*b5893f02SDimitry Andric   DenseMap<unsigned, CopyInfo> Copies;
85*b5893f02SDimitry Andric 
86*b5893f02SDimitry Andric public:
87*b5893f02SDimitry Andric   /// Mark all of the given registers and their subregisters as unavailable for
88*b5893f02SDimitry Andric   /// copying.
markRegsUnavailable(ArrayRef<unsigned> Regs,const TargetRegisterInfo & TRI)89*b5893f02SDimitry Andric   void markRegsUnavailable(ArrayRef<unsigned> Regs,
90*b5893f02SDimitry Andric                            const TargetRegisterInfo &TRI) {
91*b5893f02SDimitry Andric     for (unsigned Reg : Regs) {
92*b5893f02SDimitry Andric       // Source of copy is no longer available for propagation.
93*b5893f02SDimitry Andric       for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
94*b5893f02SDimitry Andric         auto CI = Copies.find(*RUI);
95*b5893f02SDimitry Andric         if (CI != Copies.end())
96*b5893f02SDimitry Andric           CI->second.Avail = false;
97*b5893f02SDimitry Andric       }
98*b5893f02SDimitry Andric     }
99*b5893f02SDimitry Andric   }
100*b5893f02SDimitry Andric 
101*b5893f02SDimitry Andric   /// Clobber a single register, removing it from the tracker's copy maps.
clobberRegister(unsigned Reg,const TargetRegisterInfo & TRI)102*b5893f02SDimitry Andric   void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
103*b5893f02SDimitry Andric     for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
104*b5893f02SDimitry Andric       auto I = Copies.find(*RUI);
105*b5893f02SDimitry Andric       if (I != Copies.end()) {
106*b5893f02SDimitry Andric         // When we clobber the source of a copy, we need to clobber everything
107*b5893f02SDimitry Andric         // it defined.
108*b5893f02SDimitry Andric         markRegsUnavailable(I->second.DefRegs, TRI);
109*b5893f02SDimitry Andric         // When we clobber the destination of a copy, we need to clobber the
110*b5893f02SDimitry Andric         // whole register it defined.
111*b5893f02SDimitry Andric         if (MachineInstr *MI = I->second.MI)
112*b5893f02SDimitry Andric           markRegsUnavailable({MI->getOperand(0).getReg()}, TRI);
113*b5893f02SDimitry Andric         // Now we can erase the copy.
114*b5893f02SDimitry Andric         Copies.erase(I);
115*b5893f02SDimitry Andric       }
116*b5893f02SDimitry Andric     }
117*b5893f02SDimitry Andric   }
118*b5893f02SDimitry Andric 
119*b5893f02SDimitry Andric   /// Add this copy's registers into the tracker's copy maps.
trackCopy(MachineInstr * MI,const TargetRegisterInfo & TRI)120*b5893f02SDimitry Andric   void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
121*b5893f02SDimitry Andric     assert(MI->isCopy() && "Tracking non-copy?");
122*b5893f02SDimitry Andric 
123*b5893f02SDimitry Andric     unsigned Def = MI->getOperand(0).getReg();
124*b5893f02SDimitry Andric     unsigned Src = MI->getOperand(1).getReg();
125*b5893f02SDimitry Andric 
126*b5893f02SDimitry Andric     // Remember Def is defined by the copy.
127*b5893f02SDimitry Andric     for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
128*b5893f02SDimitry Andric       Copies[*RUI] = {MI, {}, true};
129*b5893f02SDimitry Andric 
130*b5893f02SDimitry Andric     // Remember source that's copied to Def. Once it's clobbered, then
131*b5893f02SDimitry Andric     // it's no longer available for copy propagation.
132*b5893f02SDimitry Andric     for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
133*b5893f02SDimitry Andric       auto I = Copies.insert({*RUI, {nullptr, {}, false}});
134*b5893f02SDimitry Andric       auto &Copy = I.first->second;
135*b5893f02SDimitry Andric       if (!is_contained(Copy.DefRegs, Def))
136*b5893f02SDimitry Andric         Copy.DefRegs.push_back(Def);
137*b5893f02SDimitry Andric     }
138*b5893f02SDimitry Andric   }
139*b5893f02SDimitry Andric 
hasAnyCopies()140*b5893f02SDimitry Andric   bool hasAnyCopies() {
141*b5893f02SDimitry Andric     return !Copies.empty();
142*b5893f02SDimitry Andric   }
143*b5893f02SDimitry Andric 
findCopyForUnit(unsigned RegUnit,const TargetRegisterInfo & TRI,bool MustBeAvailable=false)144*b5893f02SDimitry Andric   MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI,
145*b5893f02SDimitry Andric                          bool MustBeAvailable = false) {
146*b5893f02SDimitry Andric     auto CI = Copies.find(RegUnit);
147*b5893f02SDimitry Andric     if (CI == Copies.end())
148*b5893f02SDimitry Andric       return nullptr;
149*b5893f02SDimitry Andric     if (MustBeAvailable && !CI->second.Avail)
150*b5893f02SDimitry Andric       return nullptr;
151*b5893f02SDimitry Andric     return CI->second.MI;
152*b5893f02SDimitry Andric   }
153*b5893f02SDimitry Andric 
findAvailCopy(MachineInstr & DestCopy,unsigned Reg,const TargetRegisterInfo & TRI)154*b5893f02SDimitry Andric   MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg,
155*b5893f02SDimitry Andric                               const TargetRegisterInfo &TRI) {
156*b5893f02SDimitry Andric     // We check the first RegUnit here, since we'll only be interested in the
157*b5893f02SDimitry Andric     // copy if it copies the entire register anyway.
158*b5893f02SDimitry Andric     MCRegUnitIterator RUI(Reg, &TRI);
159*b5893f02SDimitry Andric     MachineInstr *AvailCopy =
160*b5893f02SDimitry Andric         findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
161*b5893f02SDimitry Andric     if (!AvailCopy ||
162*b5893f02SDimitry Andric         !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
163*b5893f02SDimitry Andric       return nullptr;
164*b5893f02SDimitry Andric 
165*b5893f02SDimitry Andric     // Check that the available copy isn't clobbered by any regmasks between
166*b5893f02SDimitry Andric     // itself and the destination.
167*b5893f02SDimitry Andric     unsigned AvailSrc = AvailCopy->getOperand(1).getReg();
168*b5893f02SDimitry Andric     unsigned AvailDef = AvailCopy->getOperand(0).getReg();
169*b5893f02SDimitry Andric     for (const MachineInstr &MI :
170*b5893f02SDimitry Andric          make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
171*b5893f02SDimitry Andric       for (const MachineOperand &MO : MI.operands())
172*b5893f02SDimitry Andric         if (MO.isRegMask())
173*b5893f02SDimitry Andric           if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
174*b5893f02SDimitry Andric             return nullptr;
175*b5893f02SDimitry Andric 
176*b5893f02SDimitry Andric     return AvailCopy;
177*b5893f02SDimitry Andric   }
178*b5893f02SDimitry Andric 
clear()179*b5893f02SDimitry Andric   void clear() {
180*b5893f02SDimitry Andric     Copies.clear();
181*b5893f02SDimitry Andric   }
182*b5893f02SDimitry Andric };
1833ca95b02SDimitry Andric 
184dff0c46cSDimitry Andric class MachineCopyPropagation : public MachineFunctionPass {
185dff0c46cSDimitry Andric   const TargetRegisterInfo *TRI;
186139f7f9bSDimitry Andric   const TargetInstrInfo *TII;
1873ca95b02SDimitry Andric   const MachineRegisterInfo *MRI;
188dff0c46cSDimitry Andric 
189dff0c46cSDimitry Andric public:
190dff0c46cSDimitry Andric   static char ID; // Pass identification, replacement for typeid
1912cab237bSDimitry Andric 
MachineCopyPropagation()192dff0c46cSDimitry Andric   MachineCopyPropagation() : MachineFunctionPass(ID) {
193dff0c46cSDimitry Andric     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
194dff0c46cSDimitry Andric   }
195dff0c46cSDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1963ca95b02SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1973ca95b02SDimitry Andric     AU.setPreservesCFG();
1983ca95b02SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
1993ca95b02SDimitry Andric   }
2003ca95b02SDimitry Andric 
20191bc56edSDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
202dff0c46cSDimitry Andric 
getRequiredProperties() const2033ca95b02SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
2043ca95b02SDimitry Andric     return MachineFunctionProperties().set(
205d88c1a5aSDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
2063ca95b02SDimitry Andric   }
207dff0c46cSDimitry Andric 
2083ca95b02SDimitry Andric private:
2093ca95b02SDimitry Andric   void ClobberRegister(unsigned Reg);
2105ca5951eSDimitry Andric   void ReadRegister(unsigned Reg);
2113ca95b02SDimitry Andric   void CopyPropagateBlock(MachineBasicBlock &MBB);
2123ca95b02SDimitry Andric   bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
2134ba319b5SDimitry Andric   void forwardUses(MachineInstr &MI);
2144ba319b5SDimitry Andric   bool isForwardableRegClassCopy(const MachineInstr &Copy,
2154ba319b5SDimitry Andric                                  const MachineInstr &UseI, unsigned UseIdx);
2164ba319b5SDimitry Andric   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
2173ca95b02SDimitry Andric 
2183ca95b02SDimitry Andric   /// Candidates for deletion.
2193ca95b02SDimitry Andric   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
2202cab237bSDimitry Andric 
221*b5893f02SDimitry Andric   CopyTracker Tracker;
2222cab237bSDimitry Andric 
2233ca95b02SDimitry Andric   bool Changed;
224dff0c46cSDimitry Andric };
2252cab237bSDimitry Andric 
2262cab237bSDimitry Andric } // end anonymous namespace
2272cab237bSDimitry Andric 
228dff0c46cSDimitry Andric char MachineCopyPropagation::ID = 0;
2292cab237bSDimitry Andric 
230dff0c46cSDimitry Andric char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
231dff0c46cSDimitry Andric 
232302affcbSDimitry Andric INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
233dff0c46cSDimitry Andric                 "Machine Copy Propagation Pass", false, false)
234dff0c46cSDimitry Andric 
ReadRegister(unsigned Reg)2355ca5951eSDimitry Andric void MachineCopyPropagation::ReadRegister(unsigned Reg) {
2365ca5951eSDimitry Andric   // If 'Reg' is defined by a copy, the copy is no longer a candidate
2375ca5951eSDimitry Andric   // for elimination.
238*b5893f02SDimitry Andric   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
239*b5893f02SDimitry Andric     if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
240*b5893f02SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
241*b5893f02SDimitry Andric       MaybeDeadCopies.remove(Copy);
2425ca5951eSDimitry Andric     }
2435ca5951eSDimitry Andric   }
2445ca5951eSDimitry Andric }
2455ca5951eSDimitry Andric 
2463ca95b02SDimitry Andric /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
2473ca95b02SDimitry Andric /// This fact may have been obscured by sub register usage or may not be true at
2483ca95b02SDimitry Andric /// all even though Src and Def are subregisters of the registers used in
2493ca95b02SDimitry Andric /// PreviousCopy. e.g.
2503ca95b02SDimitry Andric /// isNopCopy("ecx = COPY eax", AX, CX) == true
2513ca95b02SDimitry Andric /// isNopCopy("ecx = COPY eax", AH, CL) == false
isNopCopy(const MachineInstr & PreviousCopy,unsigned Src,unsigned Def,const TargetRegisterInfo * TRI)2523ca95b02SDimitry Andric static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
2533ca95b02SDimitry Andric                       unsigned Def, const TargetRegisterInfo *TRI) {
2543ca95b02SDimitry Andric   unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
2553ca95b02SDimitry Andric   unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
2563ca95b02SDimitry Andric   if (Src == PreviousSrc) {
2573ca95b02SDimitry Andric     assert(Def == PreviousDef);
2583ca95b02SDimitry Andric     return true;
259dff0c46cSDimitry Andric   }
2603ca95b02SDimitry Andric   if (!TRI->isSubRegister(PreviousSrc, Src))
2613ca95b02SDimitry Andric     return false;
2623ca95b02SDimitry Andric   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
2633ca95b02SDimitry Andric   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
2643ca95b02SDimitry Andric }
2653ca95b02SDimitry Andric 
2663ca95b02SDimitry Andric /// Remove instruction \p Copy if there exists a previous copy that copies the
2673ca95b02SDimitry Andric /// register \p Src to the register \p Def; This may happen indirectly by
2683ca95b02SDimitry Andric /// copying the super registers.
eraseIfRedundant(MachineInstr & Copy,unsigned Src,unsigned Def)2693ca95b02SDimitry Andric bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
2703ca95b02SDimitry Andric                                               unsigned Def) {
2713ca95b02SDimitry Andric   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
2723ca95b02SDimitry Andric   // the value (Example: The sparc zero register is writable but stays zero).
2733ca95b02SDimitry Andric   if (MRI->isReserved(Src) || MRI->isReserved(Def))
2743ca95b02SDimitry Andric     return false;
2753ca95b02SDimitry Andric 
2763ca95b02SDimitry Andric   // Search for an existing copy.
277*b5893f02SDimitry Andric   MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
278*b5893f02SDimitry Andric   if (!PrevCopy)
2793ca95b02SDimitry Andric     return false;
2803ca95b02SDimitry Andric 
2813ca95b02SDimitry Andric   // Check that the existing copy uses the correct sub registers.
282*b5893f02SDimitry Andric   if (PrevCopy->getOperand(0).isDead())
2832cab237bSDimitry Andric     return false;
284*b5893f02SDimitry Andric   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
2853ca95b02SDimitry Andric     return false;
2863ca95b02SDimitry Andric 
2874ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
2883ca95b02SDimitry Andric 
2893ca95b02SDimitry Andric   // Copy was redundantly redefining either Src or Def. Remove earlier kill
2903ca95b02SDimitry Andric   // flags between Copy and PrevCopy because the value will be reused now.
2913ca95b02SDimitry Andric   assert(Copy.isCopy());
2923ca95b02SDimitry Andric   unsigned CopyDef = Copy.getOperand(0).getReg();
2933ca95b02SDimitry Andric   assert(CopyDef == Src || CopyDef == Def);
2943ca95b02SDimitry Andric   for (MachineInstr &MI :
295*b5893f02SDimitry Andric        make_range(PrevCopy->getIterator(), Copy.getIterator()))
2963ca95b02SDimitry Andric     MI.clearRegisterKills(CopyDef, TRI);
2973ca95b02SDimitry Andric 
2983ca95b02SDimitry Andric   Copy.eraseFromParent();
2993ca95b02SDimitry Andric   Changed = true;
3003ca95b02SDimitry Andric   ++NumDeletes;
301dff0c46cSDimitry Andric   return true;
302dff0c46cSDimitry Andric }
303dff0c46cSDimitry Andric 
3044ba319b5SDimitry Andric /// Decide whether we should forward the source of \param Copy to its use in
3054ba319b5SDimitry Andric /// \param UseI based on the physical register class constraints of the opcode
3064ba319b5SDimitry Andric /// and avoiding introducing more cross-class COPYs.
isForwardableRegClassCopy(const MachineInstr & Copy,const MachineInstr & UseI,unsigned UseIdx)3074ba319b5SDimitry Andric bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
3084ba319b5SDimitry Andric                                                        const MachineInstr &UseI,
3094ba319b5SDimitry Andric                                                        unsigned UseIdx) {
3104ba319b5SDimitry Andric 
3114ba319b5SDimitry Andric   unsigned CopySrcReg = Copy.getOperand(1).getReg();
3124ba319b5SDimitry Andric 
3134ba319b5SDimitry Andric   // If the new register meets the opcode register constraints, then allow
3144ba319b5SDimitry Andric   // forwarding.
3154ba319b5SDimitry Andric   if (const TargetRegisterClass *URC =
3164ba319b5SDimitry Andric           UseI.getRegClassConstraint(UseIdx, TII, TRI))
3174ba319b5SDimitry Andric     return URC->contains(CopySrcReg);
3184ba319b5SDimitry Andric 
3194ba319b5SDimitry Andric   if (!UseI.isCopy())
3204ba319b5SDimitry Andric     return false;
3214ba319b5SDimitry Andric 
3224ba319b5SDimitry Andric   /// COPYs don't have register class constraints, so if the user instruction
3234ba319b5SDimitry Andric   /// is a COPY, we just try to avoid introducing additional cross-class
3244ba319b5SDimitry Andric   /// COPYs.  For example:
3254ba319b5SDimitry Andric   ///
3264ba319b5SDimitry Andric   ///   RegClassA = COPY RegClassB  // Copy parameter
3274ba319b5SDimitry Andric   ///   ...
3284ba319b5SDimitry Andric   ///   RegClassB = COPY RegClassA  // UseI parameter
3294ba319b5SDimitry Andric   ///
3304ba319b5SDimitry Andric   /// which after forwarding becomes
3314ba319b5SDimitry Andric   ///
3324ba319b5SDimitry Andric   ///   RegClassA = COPY RegClassB
3334ba319b5SDimitry Andric   ///   ...
3344ba319b5SDimitry Andric   ///   RegClassB = COPY RegClassB
3354ba319b5SDimitry Andric   ///
3364ba319b5SDimitry Andric   /// so we have reduced the number of cross-class COPYs and potentially
3374ba319b5SDimitry Andric   /// introduced a nop COPY that can be removed.
3384ba319b5SDimitry Andric   const TargetRegisterClass *UseDstRC =
3394ba319b5SDimitry Andric       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
3404ba319b5SDimitry Andric 
3414ba319b5SDimitry Andric   const TargetRegisterClass *SuperRC = UseDstRC;
3424ba319b5SDimitry Andric   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
3434ba319b5SDimitry Andric        SuperRC; SuperRC = *SuperRCI++)
3444ba319b5SDimitry Andric     if (SuperRC->contains(CopySrcReg))
3454ba319b5SDimitry Andric       return true;
3464ba319b5SDimitry Andric 
3474ba319b5SDimitry Andric   return false;
3484ba319b5SDimitry Andric }
3494ba319b5SDimitry Andric 
3504ba319b5SDimitry Andric /// Check that \p MI does not have implicit uses that overlap with it's \p Use
3514ba319b5SDimitry Andric /// operand (the register being replaced), since these can sometimes be
3524ba319b5SDimitry Andric /// implicitly tied to other operands.  For example, on AMDGPU:
3534ba319b5SDimitry Andric ///
3544ba319b5SDimitry Andric /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
3554ba319b5SDimitry Andric ///
3564ba319b5SDimitry Andric /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
3574ba319b5SDimitry Andric /// way of knowing we need to update the latter when updating the former.
hasImplicitOverlap(const MachineInstr & MI,const MachineOperand & Use)3584ba319b5SDimitry Andric bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
3594ba319b5SDimitry Andric                                                 const MachineOperand &Use) {
3604ba319b5SDimitry Andric   for (const MachineOperand &MIUse : MI.uses())
3614ba319b5SDimitry Andric     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
3624ba319b5SDimitry Andric         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
3634ba319b5SDimitry Andric       return true;
3644ba319b5SDimitry Andric 
3654ba319b5SDimitry Andric   return false;
3664ba319b5SDimitry Andric }
3674ba319b5SDimitry Andric 
3684ba319b5SDimitry Andric /// Look for available copies whose destination register is used by \p MI and
3694ba319b5SDimitry Andric /// replace the use in \p MI with the copy's source register.
forwardUses(MachineInstr & MI)3704ba319b5SDimitry Andric void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
371*b5893f02SDimitry Andric   if (!Tracker.hasAnyCopies())
3724ba319b5SDimitry Andric     return;
3734ba319b5SDimitry Andric 
3744ba319b5SDimitry Andric   // Look for non-tied explicit vreg uses that have an active COPY
3754ba319b5SDimitry Andric   // instruction that defines the physical register allocated to them.
3764ba319b5SDimitry Andric   // Replace the vreg with the source of the active COPY.
3774ba319b5SDimitry Andric   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
3784ba319b5SDimitry Andric        ++OpIdx) {
3794ba319b5SDimitry Andric     MachineOperand &MOUse = MI.getOperand(OpIdx);
3804ba319b5SDimitry Andric     // Don't forward into undef use operands since doing so can cause problems
3814ba319b5SDimitry Andric     // with the machine verifier, since it doesn't treat undef reads as reads,
3824ba319b5SDimitry Andric     // so we can end up with a live range that ends on an undef read, leading to
3834ba319b5SDimitry Andric     // an error that the live range doesn't end on a read of the live range
3844ba319b5SDimitry Andric     // register.
3854ba319b5SDimitry Andric     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
3864ba319b5SDimitry Andric         MOUse.isImplicit())
3874ba319b5SDimitry Andric       continue;
3884ba319b5SDimitry Andric 
3894ba319b5SDimitry Andric     if (!MOUse.getReg())
3904ba319b5SDimitry Andric       continue;
3914ba319b5SDimitry Andric 
3924ba319b5SDimitry Andric     // Check that the register is marked 'renamable' so we know it is safe to
3934ba319b5SDimitry Andric     // rename it without violating any constraints that aren't expressed in the
3944ba319b5SDimitry Andric     // IR (e.g. ABI or opcode requirements).
3954ba319b5SDimitry Andric     if (!MOUse.isRenamable())
3964ba319b5SDimitry Andric       continue;
3974ba319b5SDimitry Andric 
398*b5893f02SDimitry Andric     MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI);
399*b5893f02SDimitry Andric     if (!Copy)
4004ba319b5SDimitry Andric       continue;
4014ba319b5SDimitry Andric 
402*b5893f02SDimitry Andric     unsigned CopyDstReg = Copy->getOperand(0).getReg();
403*b5893f02SDimitry Andric     const MachineOperand &CopySrc = Copy->getOperand(1);
4044ba319b5SDimitry Andric     unsigned CopySrcReg = CopySrc.getReg();
4054ba319b5SDimitry Andric 
4064ba319b5SDimitry Andric     // FIXME: Don't handle partial uses of wider COPYs yet.
4074ba319b5SDimitry Andric     if (MOUse.getReg() != CopyDstReg) {
4084ba319b5SDimitry Andric       LLVM_DEBUG(
4094ba319b5SDimitry Andric           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
4104ba319b5SDimitry Andric                  << MI);
4114ba319b5SDimitry Andric       continue;
4124ba319b5SDimitry Andric     }
4134ba319b5SDimitry Andric 
4144ba319b5SDimitry Andric     // Don't forward COPYs of reserved regs unless they are constant.
4154ba319b5SDimitry Andric     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
4164ba319b5SDimitry Andric       continue;
4174ba319b5SDimitry Andric 
418*b5893f02SDimitry Andric     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
4194ba319b5SDimitry Andric       continue;
4204ba319b5SDimitry Andric 
4214ba319b5SDimitry Andric     if (hasImplicitOverlap(MI, MOUse))
4224ba319b5SDimitry Andric       continue;
4234ba319b5SDimitry Andric 
4244ba319b5SDimitry Andric     if (!DebugCounter::shouldExecute(FwdCounter)) {
4254ba319b5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
4264ba319b5SDimitry Andric                         << MI);
4274ba319b5SDimitry Andric       continue;
4284ba319b5SDimitry Andric     }
4294ba319b5SDimitry Andric 
4304ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
4314ba319b5SDimitry Andric                       << "\n     with " << printReg(CopySrcReg, TRI)
432*b5893f02SDimitry Andric                       << "\n     in " << MI << "     from " << *Copy);
4334ba319b5SDimitry Andric 
4344ba319b5SDimitry Andric     MOUse.setReg(CopySrcReg);
4354ba319b5SDimitry Andric     if (!CopySrc.isRenamable())
4364ba319b5SDimitry Andric       MOUse.setIsRenamable(false);
4374ba319b5SDimitry Andric 
4384ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
4394ba319b5SDimitry Andric 
4404ba319b5SDimitry Andric     // Clear kill markers that may have been invalidated.
4414ba319b5SDimitry Andric     for (MachineInstr &KMI :
442*b5893f02SDimitry Andric          make_range(Copy->getIterator(), std::next(MI.getIterator())))
4434ba319b5SDimitry Andric       KMI.clearRegisterKills(CopySrcReg, TRI);
4444ba319b5SDimitry Andric 
4454ba319b5SDimitry Andric     ++NumCopyForwards;
4464ba319b5SDimitry Andric     Changed = true;
4474ba319b5SDimitry Andric   }
4484ba319b5SDimitry Andric }
4494ba319b5SDimitry Andric 
CopyPropagateBlock(MachineBasicBlock & MBB)4503ca95b02SDimitry Andric void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
4514ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
45291bc56edSDimitry Andric 
453dff0c46cSDimitry Andric   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
454dff0c46cSDimitry Andric     MachineInstr *MI = &*I;
455dff0c46cSDimitry Andric     ++I;
456dff0c46cSDimitry Andric 
4574ba319b5SDimitry Andric     // Analyze copies (which don't overlap themselves).
4584ba319b5SDimitry Andric     if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
4594ba319b5SDimitry Andric                                           MI->getOperand(1).getReg())) {
460dff0c46cSDimitry Andric       unsigned Def = MI->getOperand(0).getReg();
461dff0c46cSDimitry Andric       unsigned Src = MI->getOperand(1).getReg();
462dff0c46cSDimitry Andric 
4633ca95b02SDimitry Andric       assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
4643ca95b02SDimitry Andric              !TargetRegisterInfo::isVirtualRegister(Src) &&
4653ca95b02SDimitry Andric              "MachineCopyPropagation should be run after register allocation!");
466dff0c46cSDimitry Andric 
467dff0c46cSDimitry Andric       // The two copies cancel out and the source of the first copy
468dff0c46cSDimitry Andric       // hasn't been overridden, eliminate the second one. e.g.
4692cab237bSDimitry Andric       //  %ecx = COPY %eax
4702cab237bSDimitry Andric       //  ... nothing clobbered eax.
4712cab237bSDimitry Andric       //  %eax = COPY %ecx
472dff0c46cSDimitry Andric       // =>
4732cab237bSDimitry Andric       //  %ecx = COPY %eax
474dff0c46cSDimitry Andric       //
4753ca95b02SDimitry Andric       // or
4763ca95b02SDimitry Andric       //
4772cab237bSDimitry Andric       //  %ecx = COPY %eax
4782cab237bSDimitry Andric       //  ... nothing clobbered eax.
4792cab237bSDimitry Andric       //  %ecx = COPY %eax
4803ca95b02SDimitry Andric       // =>
4812cab237bSDimitry Andric       //  %ecx = COPY %eax
4823ca95b02SDimitry Andric       if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
483dff0c46cSDimitry Andric         continue;
484dff0c46cSDimitry Andric 
4854ba319b5SDimitry Andric       forwardUses(*MI);
4864ba319b5SDimitry Andric 
4874ba319b5SDimitry Andric       // Src may have been changed by forwardUses()
4884ba319b5SDimitry Andric       Src = MI->getOperand(1).getReg();
4894ba319b5SDimitry Andric 
4903ca95b02SDimitry Andric       // If Src is defined by a previous copy, the previous copy cannot be
4913ca95b02SDimitry Andric       // eliminated.
4925ca5951eSDimitry Andric       ReadRegister(Src);
4935ca5951eSDimitry Andric       for (const MachineOperand &MO : MI->implicit_operands()) {
4945ca5951eSDimitry Andric         if (!MO.isReg() || !MO.readsReg())
4955ca5951eSDimitry Andric           continue;
4965ca5951eSDimitry Andric         unsigned Reg = MO.getReg();
4975ca5951eSDimitry Andric         if (!Reg)
4985ca5951eSDimitry Andric           continue;
4995ca5951eSDimitry Andric         ReadRegister(Reg);
50091bc56edSDimitry Andric       }
50191bc56edSDimitry Andric 
5024ba319b5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
503dff0c46cSDimitry Andric 
504dff0c46cSDimitry Andric       // Copy is now a candidate for deletion.
5053ca95b02SDimitry Andric       if (!MRI->isReserved(Def))
506dff0c46cSDimitry Andric         MaybeDeadCopies.insert(MI);
507dff0c46cSDimitry Andric 
5083ca95b02SDimitry Andric       // If 'Def' is previously source of another copy, then this earlier copy's
509dff0c46cSDimitry Andric       // source is no longer available. e.g.
5102cab237bSDimitry Andric       // %xmm9 = copy %xmm2
511dff0c46cSDimitry Andric       // ...
5122cab237bSDimitry Andric       // %xmm2 = copy %xmm0
513dff0c46cSDimitry Andric       // ...
5142cab237bSDimitry Andric       // %xmm2 = copy %xmm9
515*b5893f02SDimitry Andric       Tracker.clobberRegister(Def, *TRI);
5165ca5951eSDimitry Andric       for (const MachineOperand &MO : MI->implicit_operands()) {
5175ca5951eSDimitry Andric         if (!MO.isReg() || !MO.isDef())
5185ca5951eSDimitry Andric           continue;
5195ca5951eSDimitry Andric         unsigned Reg = MO.getReg();
5205ca5951eSDimitry Andric         if (!Reg)
5215ca5951eSDimitry Andric           continue;
522*b5893f02SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
5235ca5951eSDimitry Andric       }
524dff0c46cSDimitry Andric 
525*b5893f02SDimitry Andric       Tracker.trackCopy(MI, *TRI);
526dff0c46cSDimitry Andric 
527dff0c46cSDimitry Andric       continue;
528dff0c46cSDimitry Andric     }
529dff0c46cSDimitry Andric 
5304ba319b5SDimitry Andric     // Clobber any earlyclobber regs first.
5314ba319b5SDimitry Andric     for (const MachineOperand &MO : MI->operands())
5324ba319b5SDimitry Andric       if (MO.isReg() && MO.isEarlyClobber()) {
5334ba319b5SDimitry Andric         unsigned Reg = MO.getReg();
5344ba319b5SDimitry Andric         // If we have a tied earlyclobber, that means it is also read by this
5354ba319b5SDimitry Andric         // instruction, so we need to make sure we don't remove it as dead
5364ba319b5SDimitry Andric         // later.
5374ba319b5SDimitry Andric         if (MO.isTied())
5384ba319b5SDimitry Andric           ReadRegister(Reg);
539*b5893f02SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
5404ba319b5SDimitry Andric       }
5414ba319b5SDimitry Andric 
5424ba319b5SDimitry Andric     forwardUses(*MI);
5434ba319b5SDimitry Andric 
544dff0c46cSDimitry Andric     // Not a copy.
545dff0c46cSDimitry Andric     SmallVector<unsigned, 2> Defs;
5463ca95b02SDimitry Andric     const MachineOperand *RegMask = nullptr;
5473ca95b02SDimitry Andric     for (const MachineOperand &MO : MI->operands()) {
548dff0c46cSDimitry Andric       if (MO.isRegMask())
5493ca95b02SDimitry Andric         RegMask = &MO;
550dff0c46cSDimitry Andric       if (!MO.isReg())
551dff0c46cSDimitry Andric         continue;
552dff0c46cSDimitry Andric       unsigned Reg = MO.getReg();
553dff0c46cSDimitry Andric       if (!Reg)
554dff0c46cSDimitry Andric         continue;
555dff0c46cSDimitry Andric 
5563ca95b02SDimitry Andric       assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
5573ca95b02SDimitry Andric              "MachineCopyPropagation should be run after register allocation!");
558dff0c46cSDimitry Andric 
5594ba319b5SDimitry Andric       if (MO.isDef() && !MO.isEarlyClobber()) {
560dff0c46cSDimitry Andric         Defs.push_back(Reg);
5617a7e6055SDimitry Andric         continue;
5624ba319b5SDimitry Andric       } else if (!MO.isDebug() && MO.readsReg())
5635ca5951eSDimitry Andric         ReadRegister(Reg);
564dff0c46cSDimitry Andric     }
565dff0c46cSDimitry Andric 
566dff0c46cSDimitry Andric     // The instruction has a register mask operand which means that it clobbers
5673ca95b02SDimitry Andric     // a large set of registers.  Treat clobbered registers the same way as
5683ca95b02SDimitry Andric     // defined registers.
5693ca95b02SDimitry Andric     if (RegMask) {
570dff0c46cSDimitry Andric       // Erase any MaybeDeadCopies whose destination register is clobbered.
5713ca95b02SDimitry Andric       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
5723ca95b02SDimitry Andric                MaybeDeadCopies.begin();
5733ca95b02SDimitry Andric            DI != MaybeDeadCopies.end();) {
5743ca95b02SDimitry Andric         MachineInstr *MaybeDead = *DI;
5753ca95b02SDimitry Andric         unsigned Reg = MaybeDead->getOperand(0).getReg();
5763ca95b02SDimitry Andric         assert(!MRI->isReserved(Reg));
5773ca95b02SDimitry Andric 
5783ca95b02SDimitry Andric         if (!RegMask->clobbersPhysReg(Reg)) {
5793ca95b02SDimitry Andric           ++DI;
580dff0c46cSDimitry Andric           continue;
5813ca95b02SDimitry Andric         }
5823ca95b02SDimitry Andric 
5834ba319b5SDimitry Andric         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
5843ca95b02SDimitry Andric                    MaybeDead->dump());
5853ca95b02SDimitry Andric 
586*b5893f02SDimitry Andric         // Make sure we invalidate any entries in the copy maps before erasing
587*b5893f02SDimitry Andric         // the instruction.
588*b5893f02SDimitry Andric         Tracker.clobberRegister(Reg, *TRI);
589*b5893f02SDimitry Andric 
5903ca95b02SDimitry Andric         // erase() will return the next valid iterator pointing to the next
5913ca95b02SDimitry Andric         // element after the erased one.
5923ca95b02SDimitry Andric         DI = MaybeDeadCopies.erase(DI);
5933ca95b02SDimitry Andric         MaybeDead->eraseFromParent();
594dff0c46cSDimitry Andric         Changed = true;
595dff0c46cSDimitry Andric         ++NumDeletes;
596dff0c46cSDimitry Andric       }
597dff0c46cSDimitry Andric     }
598dff0c46cSDimitry Andric 
5993ca95b02SDimitry Andric     // Any previous copy definition or reading the Defs is no longer available.
6003ca95b02SDimitry Andric     for (unsigned Reg : Defs)
601*b5893f02SDimitry Andric       Tracker.clobberRegister(Reg, *TRI);
602dff0c46cSDimitry Andric   }
603dff0c46cSDimitry Andric 
604dff0c46cSDimitry Andric   // If MBB doesn't have successors, delete the copies whose defs are not used.
605dff0c46cSDimitry Andric   // If MBB does have successors, then conservative assume the defs are live-out
606dff0c46cSDimitry Andric   // since we don't want to trust live-in lists.
607dff0c46cSDimitry Andric   if (MBB.succ_empty()) {
6083ca95b02SDimitry Andric     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
6094ba319b5SDimitry Andric       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
6104ba319b5SDimitry Andric                  MaybeDead->dump());
6113ca95b02SDimitry Andric       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
612*b5893f02SDimitry Andric 
613*b5893f02SDimitry Andric       // Update matching debug values.
614*b5893f02SDimitry Andric       assert(MaybeDead->isCopy());
615*b5893f02SDimitry Andric       MaybeDead->changeDebugValuesDefReg(MaybeDead->getOperand(1).getReg());
616*b5893f02SDimitry Andric 
6173ca95b02SDimitry Andric       MaybeDead->eraseFromParent();
618dff0c46cSDimitry Andric       Changed = true;
619dff0c46cSDimitry Andric       ++NumDeletes;
620dff0c46cSDimitry Andric     }
621dff0c46cSDimitry Andric   }
622dff0c46cSDimitry Andric 
6233ca95b02SDimitry Andric   MaybeDeadCopies.clear();
624*b5893f02SDimitry Andric   Tracker.clear();
625dff0c46cSDimitry Andric }
626dff0c46cSDimitry Andric 
runOnMachineFunction(MachineFunction & MF)627dff0c46cSDimitry Andric bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
6282cab237bSDimitry Andric   if (skipFunction(MF.getFunction()))
62991bc56edSDimitry Andric     return false;
63091bc56edSDimitry Andric 
6313ca95b02SDimitry Andric   Changed = false;
632dff0c46cSDimitry Andric 
63339d628a0SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
63439d628a0SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
6353861d79fSDimitry Andric   MRI = &MF.getRegInfo();
636dff0c46cSDimitry Andric 
6373ca95b02SDimitry Andric   for (MachineBasicBlock &MBB : MF)
6383ca95b02SDimitry Andric     CopyPropagateBlock(MBB);
639dff0c46cSDimitry Andric 
640dff0c46cSDimitry Andric   return Changed;
641dff0c46cSDimitry Andric }
642