LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineCopyPropagation.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 173 190 91.1 %
Date: 2018-09-23 13:06:45 Functions: 16 18 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This is an extremely simple MachineInstr-level copy propagation pass.
      11             : //
      12             : // This pass forwards the source of COPYs to the users of their destinations
      13             : // when doing so is legal.  For example:
      14             : //
      15             : //   %reg1 = COPY %reg0
      16             : //   ...
      17             : //   ... = OP %reg1
      18             : //
      19             : // If
      20             : //   - %reg0 has not been clobbered by the time of the use of %reg1
      21             : //   - the register class constraints are satisfied
      22             : //   - the COPY def is the only value that reaches OP
      23             : // then this pass replaces the above with:
      24             : //
      25             : //   %reg1 = COPY %reg0
      26             : //   ...
      27             : //   ... = OP %reg0
      28             : //
      29             : // This pass also removes some redundant COPYs.  For example:
      30             : //
      31             : //    %R1 = COPY %R0
      32             : //    ... // No clobber of %R1
      33             : //    %R0 = COPY %R1 <<< Removed
      34             : //
      35             : // or
      36             : //
      37             : //    %R1 = COPY %R0
      38             : //    ... // No clobber of %R0
      39             : //    %R1 = COPY %R0 <<< Removed
      40             : //
      41             : //===----------------------------------------------------------------------===//
      42             : 
      43             : #include "llvm/ADT/DenseMap.h"
      44             : #include "llvm/ADT/STLExtras.h"
      45             : #include "llvm/ADT/SetVector.h"
      46             : #include "llvm/ADT/SmallVector.h"
      47             : #include "llvm/ADT/Statistic.h"
      48             : #include "llvm/ADT/iterator_range.h"
      49             : #include "llvm/CodeGen/MachineBasicBlock.h"
      50             : #include "llvm/CodeGen/MachineFunction.h"
      51             : #include "llvm/CodeGen/MachineFunctionPass.h"
      52             : #include "llvm/CodeGen/MachineInstr.h"
      53             : #include "llvm/CodeGen/MachineOperand.h"
      54             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      55             : #include "llvm/CodeGen/TargetInstrInfo.h"
      56             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      57             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      58             : #include "llvm/MC/MCRegisterInfo.h"
      59             : #include "llvm/Pass.h"
      60             : #include "llvm/Support/Debug.h"
      61             : #include "llvm/Support/DebugCounter.h"
      62             : #include "llvm/Support/raw_ostream.h"
      63             : #include <cassert>
      64             : #include <iterator>
      65             : 
      66             : using namespace llvm;
      67             : 
      68             : #define DEBUG_TYPE "machine-cp"
      69             : 
      70             : STATISTIC(NumDeletes, "Number of dead copies deleted");
      71             : STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
      72             : DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
      73             :               "Controls which register COPYs are forwarded");
      74             : 
      75             : namespace {
      76             : 
      77             : class CopyTracker {
      78             :   using RegList = SmallVector<unsigned, 4>;
      79             :   using SourceMap = DenseMap<unsigned, RegList>;
      80             :   using Reg2MIMap = DenseMap<unsigned, MachineInstr *>;
      81             : 
      82             :   /// Def -> available copies map.
      83             :   Reg2MIMap AvailCopyMap;
      84             : 
      85             :   /// Def -> copies map.
      86             :   Reg2MIMap CopyMap;
      87             : 
      88             :   /// Src -> Def map
      89             :   SourceMap SrcMap;
      90             : 
      91             : public:
      92             :   /// Mark all of the given registers and their subregisters as unavailable for
      93             :   /// copying.
      94      146537 :   void markRegsUnavailable(const RegList &Regs, const TargetRegisterInfo &TRI) {
      95      299670 :     for (unsigned Reg : Regs) {
      96             :       // Source of copy is no longer available for propagation.
      97      727864 :       for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
      98     1149462 :         AvailCopyMap.erase(*SR);
      99             :     }
     100      146537 :   }
     101             : 
     102             :   /// Remove any entry in the tracker's copy maps that is marked clobbered in \p
     103             :   /// RegMask. The map will typically have a lot fewer entries than the regmask
     104             :   /// clobbers, so this is more efficient than iterating the clobbered registers
     105             :   /// and calling ClobberRegister() on them.
     106      254115 :   void removeClobberedRegs(const MachineOperand &RegMask,
     107             :                            const TargetRegisterInfo &TRI) {
     108             :     auto RemoveFromMap = [&RegMask](Reg2MIMap &Map) {
     109             :       for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
     110             :            I = Next) {
     111             :         Next = std::next(I);
     112             :         if (RegMask.clobbersPhysReg(I->first))
     113             :           Map.erase(I);
     114             :       }
     115      254115 :     };
     116      254115 :     RemoveFromMap(AvailCopyMap);
     117      254115 :     RemoveFromMap(CopyMap);
     118             : 
     119      365377 :     for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next; I != E;
     120             :          I = Next) {
     121             :       Next = std::next(I);
     122      222524 :       if (RegMask.clobbersPhysReg(I->first)) {
     123       45033 :         markRegsUnavailable(I->second, TRI);
     124             :         SrcMap.erase(I);
     125             :       }
     126             :     }
     127      254115 :   }
     128             : 
     129             :   /// Clobber a single register, removing it from the tracker's copy maps.
     130     6087221 :   void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
     131    44261213 :     for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) {
     132    76347984 :       CopyMap.erase(*AI);
     133    76347984 :       AvailCopyMap.erase(*AI);
     134             : 
     135    76347984 :       SourceMap::iterator SI = SrcMap.find(*AI);
     136    38173992 :       if (SI != SrcMap.end()) {
     137      101504 :         markRegsUnavailable(SI->second, TRI);
     138             :         SrcMap.erase(SI);
     139             :       }
     140             :     }
     141     6087221 :   }
     142             : 
     143             :   /// Add this copy's registers into the tracker's copy maps.
     144      394422 :   void trackCopy(MachineInstr *Copy, const TargetRegisterInfo &TRI) {
     145             :     assert(Copy->isCopy() && "Tracking non-copy?");
     146             : 
     147      394422 :     unsigned Def = Copy->getOperand(0).getReg();
     148      394422 :     unsigned Src = Copy->getOperand(1).getReg();
     149             : 
     150             :     // Remember Def is defined by the copy.
     151     1691585 :     for (MCSubRegIterator SR(Def, &TRI, /*IncludeSelf=*/true); SR.isValid();
     152             :          ++SR) {
     153     1297163 :       CopyMap[*SR] = Copy;
     154     1297163 :       AvailCopyMap[*SR] = Copy;
     155             :     }
     156             : 
     157             :     // Remember source that's copied to Def. Once it's clobbered, then
     158             :     // it's no longer available for copy propagation.
     159      394422 :     RegList &DestList = SrcMap[Src];
     160      394422 :     if (!is_contained(DestList, Def))
     161      386265 :       DestList.push_back(Def);
     162      394422 :   }
     163             : 
     164             :   bool hasAvailableCopies() { return !AvailCopyMap.empty(); }
     165             : 
     166             :   MachineInstr *findAvailCopy(unsigned Reg) {
     167     1292051 :     auto CI = AvailCopyMap.find(Reg);
     168     1292051 :     if (CI != AvailCopyMap.end())
     169       85164 :       return CI->second;
     170             :     return nullptr;
     171             :   }
     172             : 
     173             :   MachineInstr *findCopy(unsigned Reg) {
     174    66905758 :     auto CI = CopyMap.find(Reg);
     175    66905758 :     if (CI != CopyMap.end())
     176     1630977 :       return CI->second;
     177             :     return nullptr;
     178             :   }
     179             : 
     180      910816 :   void clear() {
     181      910816 :     AvailCopyMap.clear();
     182      910816 :     CopyMap.clear();
     183      910816 :     SrcMap.clear();
     184      910816 :   }
     185             : };
     186             : 
     187             : class MachineCopyPropagation : public MachineFunctionPass {
     188             :   const TargetRegisterInfo *TRI;
     189             :   const TargetInstrInfo *TII;
     190             :   const MachineRegisterInfo *MRI;
     191             : 
     192             : public:
     193             :   static char ID; // Pass identification, replacement for typeid
     194             : 
     195       39106 :   MachineCopyPropagation() : MachineFunctionPass(ID) {
     196       39106 :     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
     197       39106 :   }
     198             : 
     199       38791 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
     200       38791 :     AU.setPreservesCFG();
     201       38791 :     MachineFunctionPass::getAnalysisUsage(AU);
     202       38791 :   }
     203             : 
     204             :   bool runOnMachineFunction(MachineFunction &MF) override;
     205             : 
     206       38797 :   MachineFunctionProperties getRequiredProperties() const override {
     207       38797 :     return MachineFunctionProperties().set(
     208       38797 :         MachineFunctionProperties::Property::NoVRegs);
     209             :   }
     210             : 
     211             : private:
     212             :   void ClobberRegister(unsigned Reg);
     213             :   void ReadRegister(unsigned Reg);
     214             :   void CopyPropagateBlock(MachineBasicBlock &MBB);
     215             :   bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
     216             :   void forwardUses(MachineInstr &MI);
     217             :   bool isForwardableRegClassCopy(const MachineInstr &Copy,
     218             :                                  const MachineInstr &UseI, unsigned UseIdx);
     219             :   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
     220             : 
     221             :   /// Candidates for deletion.
     222             :   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
     223             : 
     224             :   CopyTracker Tracker;
     225             : 
     226             :   bool Changed;
     227             : };
     228             : 
     229             : } // end anonymous namespace
     230             : 
     231             : char MachineCopyPropagation::ID = 0;
     232             : 
     233             : char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
     234             : 
     235      125979 : INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
     236             :                 "Machine Copy Propagation Pass", false, false)
     237             : 
     238     8929427 : void MachineCopyPropagation::ReadRegister(unsigned Reg) {
     239             :   // If 'Reg' is defined by a copy, the copy is no longer a candidate
     240             :   // for elimination.
     241    75835185 :   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
     242    66905758 :     if (MachineInstr *Copy = Tracker.findCopy(*AI)) {
     243             :       LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
     244     1630977 :       MaybeDeadCopies.remove(Copy);
     245             :     }
     246             :   }
     247     8929427 : }
     248             : 
     249             : /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
     250             : /// This fact may have been obscured by sub register usage or may not be true at
     251             : /// all even though Src and Def are subregisters of the registers used in
     252             : /// PreviousCopy. e.g.
     253             : /// isNopCopy("ecx = COPY eax", AX, CX) == true
     254             : /// isNopCopy("ecx = COPY eax", AH, CL) == false
     255       16791 : static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
     256             :                       unsigned Def, const TargetRegisterInfo *TRI) {
     257       16791 :   unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
     258       16791 :   unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
     259       16791 :   if (Src == PreviousSrc) {
     260             :     assert(Def == PreviousDef);
     261             :     return true;
     262             :   }
     263       13943 :   if (!TRI->isSubRegister(PreviousSrc, Src))
     264             :     return false;
     265          73 :   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
     266          73 :   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
     267             : }
     268             : 
     269             : /// Remove instruction \p Copy if there exists a previous copy that copies the
     270             : /// register \p Src to the register \p Def; This may happen indirectly by
     271             : /// copying the super registers.
     272      793140 : bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
     273             :                                               unsigned Def) {
     274             :   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
     275             :   // the value (Example: The sparc zero register is writable but stays zero).
     276     1586280 :   if (MRI->isReserved(Src) || MRI->isReserved(Def))
     277             :     return false;
     278             : 
     279             :   // Search for an existing copy.
     280      757050 :   MachineInstr *PrevCopy = Tracker.findAvailCopy(Def);
     281       16794 :   if (!PrevCopy)
     282      757050 :     return false;
     283             : 
     284             :   // Check that the existing copy uses the correct sub registers.
     285       33588 :   if (PrevCopy->getOperand(0).isDead())
     286             :     return false;
     287       16791 :   if (!isNopCopy(*PrevCopy, Src, Def, TRI))
     288             :     return false;
     289             : 
     290             :   LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
     291             : 
     292             :   // Copy was redundantly redefining either Src or Def. Remove earlier kill
     293             :   // flags between Copy and PrevCopy because the value will be reused now.
     294             :   assert(Copy.isCopy());
     295        2912 :   unsigned CopyDef = Copy.getOperand(0).getReg();
     296             :   assert(CopyDef == Src || CopyDef == Def);
     297             :   for (MachineInstr &MI :
     298       45959 :        make_range(PrevCopy->getIterator(), Copy.getIterator()))
     299       43047 :     MI.clearRegisterKills(CopyDef, TRI);
     300             : 
     301        2912 :   Copy.eraseFromParent();
     302        2912 :   Changed = true;
     303             :   ++NumDeletes;
     304        2912 :   return true;
     305             : }
     306             : 
     307             : /// Decide whether we should forward the source of \param Copy to its use in
     308             : /// \param UseI based on the physical register class constraints of the opcode
     309             : /// and avoiding introducing more cross-class COPYs.
     310           0 : bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
     311             :                                                        const MachineInstr &UseI,
     312             :                                                        unsigned UseIdx) {
     313             : 
     314           0 :   unsigned CopySrcReg = Copy.getOperand(1).getReg();
     315             : 
     316             :   // If the new register meets the opcode register constraints, then allow
     317             :   // forwarding.
     318           0 :   if (const TargetRegisterClass *URC =
     319           0 :           UseI.getRegClassConstraint(UseIdx, TII, TRI))
     320           0 :     return URC->contains(CopySrcReg);
     321             : 
     322           0 :   if (!UseI.isCopy())
     323           0 :     return false;
     324             : 
     325             :   /// COPYs don't have register class constraints, so if the user instruction
     326             :   /// is a COPY, we just try to avoid introducing additional cross-class
     327             :   /// COPYs.  For example:
     328             :   ///
     329             :   ///   RegClassA = COPY RegClassB  // Copy parameter
     330             :   ///   ...
     331             :   ///   RegClassB = COPY RegClassA  // UseI parameter
     332             :   ///
     333             :   /// which after forwarding becomes
     334             :   ///
     335             :   ///   RegClassA = COPY RegClassB
     336             :   ///   ...
     337             :   ///   RegClassB = COPY RegClassB
     338             :   ///
     339             :   /// so we have reduced the number of cross-class COPYs and potentially
     340             :   /// introduced a nop COPY that can be removed.
     341             :   const TargetRegisterClass *UseDstRC =
     342           0 :       TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
     343             : 
     344             :   const TargetRegisterClass *SuperRC = UseDstRC;
     345           0 :   for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
     346           0 :        SuperRC; SuperRC = *SuperRCI++)
     347           0 :     if (SuperRC->contains(CopySrcReg))
     348           0 :       return true;
     349             : 
     350             :   return false;
     351             : }
     352             : 
     353             : /// Check that \p MI does not have implicit uses that overlap with it's \p Use
     354             : /// operand (the register being replaced), since these can sometimes be
     355             : /// implicitly tied to other operands.  For example, on AMDGPU:
     356             : ///
     357             : /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
     358             : ///
     359             : /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
     360             : /// way of knowing we need to update the latter when updating the former.
     361           0 : bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
     362             :                                                 const MachineOperand &Use) {
     363           0 :   for (const MachineOperand &MIUse : MI.uses())
     364           0 :     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
     365           0 :         MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
     366           0 :       return true;
     367             : 
     368             :   return false;
     369             : }
     370             : 
     371             : /// Look for available copies whose destination register is used by \p MI and
     372             : /// replace the use in \p MI with the copy's source register.
     373     6859044 : void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
     374     6859044 :   if (!Tracker.hasAvailableCopies())
     375             :     return;
     376             : 
     377             :   // Look for non-tied explicit vreg uses that have an active COPY
     378             :   // instruction that defines the physical register allocated to them.
     379             :   // Replace the vreg with the source of the active COPY.
     380     5261455 :   for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
     381             :        ++OpIdx) {
     382     4341635 :     MachineOperand &MOUse = MI.getOperand(OpIdx);
     383             :     // Don't forward into undef use operands since doing so can cause problems
     384             :     // with the machine verifier, since it doesn't treat undef reads as reads,
     385             :     // so we can end up with a live range that ends on an undef read, leading to
     386             :     // an error that the live range doesn't end on a read of the live range
     387             :     // register.
     388     4341635 :     if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
     389             :         MOUse.isImplicit())
     390             :       continue;
     391             : 
     392     1317698 :     if (!MOUse.getReg())
     393             :       continue;
     394             : 
     395             :     // Check that the register is marked 'renamable' so we know it is safe to
     396             :     // rename it without violating any constraints that aren't expressed in the
     397             :     // IR (e.g. ABI or opcode requirements).
     398      892661 :     if (!MOUse.isRenamable())
     399             :       continue;
     400             : 
     401      968044 :     MachineInstr *Copy = Tracker.findAvailCopy(MOUse.getReg());
     402       68370 :     if (!Copy)
     403      449837 :       continue;
     404             : 
     405       68370 :     unsigned CopyDstReg = Copy->getOperand(0).getReg();
     406             :     const MachineOperand &CopySrc = Copy->getOperand(1);
     407       68370 :     unsigned CopySrcReg = CopySrc.getReg();
     408             : 
     409             :     // FIXME: Don't handle partial uses of wider COPYs yet.
     410       68370 :     if (MOUse.getReg() != CopyDstReg) {
     411             :       LLVM_DEBUG(
     412             :           dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n  "
     413             :                  << MI);
     414             :       continue;
     415             :     }
     416             : 
     417             :     // Don't forward COPYs of reserved regs unless they are constant.
     418      128336 :     if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
     419             :       continue;
     420             : 
     421       62566 :     if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
     422             :       continue;
     423             : 
     424       11110 :     if (hasImplicitOverlap(MI, MOUse))
     425             :       continue;
     426             : 
     427             :     if (!DebugCounter::shouldExecute(FwdCounter)) {
     428             :       LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
     429             :                         << MI);
     430             :       continue;
     431             :     }
     432             : 
     433             :     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
     434             :                       << "\n     with " << printReg(CopySrcReg, TRI)
     435             :                       << "\n     in " << MI << "     from " << *Copy);
     436             : 
     437       11079 :     MOUse.setReg(CopySrcReg);
     438       11079 :     if (!CopySrc.isRenamable())
     439        9252 :       MOUse.setIsRenamable(false);
     440             : 
     441             :     LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
     442             : 
     443             :     // Clear kill markers that may have been invalidated.
     444             :     for (MachineInstr &KMI :
     445      175752 :          make_range(Copy->getIterator(), std::next(MI.getIterator())))
     446      153594 :       KMI.clearRegisterKills(CopySrcReg, TRI);
     447             : 
     448             :     ++NumCopyForwards;
     449       11079 :     Changed = true;
     450             :   }
     451             : }
     452             : 
     453      910816 : void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
     454             :   LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
     455             : 
     456     7772773 :   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
     457     6861957 :     MachineInstr *MI = &*I;
     458             :     ++I;
     459             : 
     460             :     // Analyze copies (which don't overlap themselves).
     461     7259353 :     if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
     462      397396 :                                           MI->getOperand(1).getReg())) {
     463      397334 :       unsigned Def = MI->getOperand(0).getReg();
     464      397334 :       unsigned Src = MI->getOperand(1).getReg();
     465             : 
     466             :       assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
     467             :              !TargetRegisterInfo::isVirtualRegister(Src) &&
     468             :              "MachineCopyPropagation should be run after register allocation!");
     469             : 
     470             :       // The two copies cancel out and the source of the first copy
     471             :       // hasn't been overridden, eliminate the second one. e.g.
     472             :       //  %ecx = COPY %eax
     473             :       //  ... nothing clobbered eax.
     474             :       //  %eax = COPY %ecx
     475             :       // =>
     476             :       //  %ecx = COPY %eax
     477             :       //
     478             :       // or
     479             :       //
     480             :       //  %ecx = COPY %eax
     481             :       //  ... nothing clobbered eax.
     482             :       //  %ecx = COPY %eax
     483             :       // =>
     484             :       //  %ecx = COPY %eax
     485      397334 :       if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
     486      397334 :         continue;
     487             : 
     488      394422 :       forwardUses(*MI);
     489             : 
     490             :       // Src may have been changed by forwardUses()
     491      394422 :       Src = MI->getOperand(1).getReg();
     492             : 
     493             :       // If Src is defined by a previous copy, the previous copy cannot be
     494             :       // eliminated.
     495      394422 :       ReadRegister(Src);
     496      832139 :       for (const MachineOperand &MO : MI->implicit_operands()) {
     497       43295 :         if (!MO.isReg() || !MO.readsReg())
     498             :           continue;
     499       39378 :         unsigned Reg = MO.getReg();
     500       39378 :         if (!Reg)
     501             :           continue;
     502       39378 :         ReadRegister(Reg);
     503             :       }
     504             : 
     505             :       LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
     506             : 
     507             :       // Copy is now a candidate for deletion.
     508      788844 :       if (!MRI->isReserved(Def))
     509      392468 :         MaybeDeadCopies.insert(MI);
     510             : 
     511             :       // If 'Def' is previously source of another copy, then this earlier copy's
     512             :       // source is no longer available. e.g.
     513             :       // %xmm9 = copy %xmm2
     514             :       // ...
     515             :       // %xmm2 = copy %xmm0
     516             :       // ...
     517             :       // %xmm2 = copy %xmm9
     518      394422 :       Tracker.clobberRegister(Def, *TRI);
     519      832139 :       for (const MachineOperand &MO : MI->implicit_operands()) {
     520       43295 :         if (!MO.isReg() || !MO.isDef())
     521             :           continue;
     522        3917 :         unsigned Reg = MO.getReg();
     523        3917 :         if (!Reg)
     524             :           continue;
     525        3917 :         Tracker.clobberRegister(Reg, *TRI);
     526             :       }
     527             : 
     528      394422 :       Tracker.trackCopy(MI, *TRI);
     529             : 
     530      394422 :       continue;
     531             :     }
     532             : 
     533             :     // Clobber any earlyclobber regs first.
     534    37117181 :     for (const MachineOperand &MO : MI->operands())
     535    30652558 :       if (MO.isReg() && MO.isEarlyClobber()) {
     536      102690 :         unsigned Reg = MO.getReg();
     537             :         // If we have a tied earlyclobber, that means it is also read by this
     538             :         // instruction, so we need to make sure we don't remove it as dead
     539             :         // later.
     540      102690 :         if (MO.isTied())
     541        7248 :           ReadRegister(Reg);
     542      102690 :         Tracker.clobberRegister(Reg, *TRI);
     543             :       }
     544             : 
     545     6464623 :     forwardUses(*MI);
     546             : 
     547             :     // Not a copy.
     548             :     SmallVector<unsigned, 2> Defs;
     549             :     const MachineOperand *RegMask = nullptr;
     550    37117179 :     for (const MachineOperand &MO : MI->operands()) {
     551    30652557 :       if (MO.isRegMask())
     552             :         RegMask = &MO;
     553    30652557 :       if (!MO.isReg())
     554    21854877 :         continue;
     555    19261703 :       unsigned Reg = MO.getReg();
     556    19261703 :       if (!Reg)
     557             :         continue;
     558             : 
     559             :       assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
     560             :              "MachineCopyPropagation should be run after register allocation!");
     561             : 
     562    14383872 :       if (MO.isDef() && !MO.isEarlyClobber()) {
     563     5586192 :         Defs.push_back(Reg);
     564     5586192 :         continue;
     565     8797680 :       } else if (!MO.isDebug() && MO.readsReg())
     566     8488379 :         ReadRegister(Reg);
     567             :     }
     568             : 
     569             :     // The instruction has a register mask operand which means that it clobbers
     570             :     // a large set of registers.  Treat clobbered registers the same way as
     571             :     // defined registers.
     572     6464622 :     if (RegMask) {
     573             :       // Erase any MaybeDeadCopies whose destination register is clobbered.
     574             :       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
     575             :                MaybeDeadCopies.begin();
     576      295564 :            DI != MaybeDeadCopies.end();) {
     577       41449 :         MachineInstr *MaybeDead = *DI;
     578       41449 :         unsigned Reg = MaybeDead->getOperand(0).getReg();
     579             :         assert(!MRI->isReserved(Reg));
     580             : 
     581       41449 :         if (!RegMask->clobbersPhysReg(Reg)) {
     582       41378 :           ++DI;
     583       41378 :           continue;
     584             :         }
     585             : 
     586             :         LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
     587             :                    MaybeDead->dump());
     588             : 
     589             :         // erase() will return the next valid iterator pointing to the next
     590             :         // element after the erased one.
     591          71 :         DI = MaybeDeadCopies.erase(DI);
     592          71 :         MaybeDead->eraseFromParent();
     593          71 :         Changed = true;
     594             :         ++NumDeletes;
     595             :       }
     596             : 
     597      254115 :       Tracker.removeClobberedRegs(*RegMask, *TRI);
     598             :     }
     599             : 
     600             :     // Any previous copy definition or reading the Defs is no longer available.
     601    12050815 :     for (unsigned Reg : Defs)
     602     5586192 :       Tracker.clobberRegister(Reg, *TRI);
     603             :   }
     604             : 
     605             :   // If MBB doesn't have successors, delete the copies whose defs are not used.
     606             :   // If MBB does have successors, then conservative assume the defs are live-out
     607             :   // since we don't want to trust live-in lists.
     608      910816 :   if (MBB.succ_empty()) {
     609      426420 :     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
     610             :       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
     611             :                  MaybeDead->dump());
     612             :       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
     613         191 :       MaybeDead->eraseFromParent();
     614         191 :       Changed = true;
     615             :       ++NumDeletes;
     616             :     }
     617             :   }
     618             : 
     619             :   MaybeDeadCopies.clear();
     620      910816 :   Tracker.clear();
     621      910816 : }
     622             : 
     623      383599 : bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
     624      383599 :   if (skipFunction(MF.getFunction()))
     625             :     return false;
     626             : 
     627      383293 :   Changed = false;
     628             : 
     629      383293 :   TRI = MF.getSubtarget().getRegisterInfo();
     630      383293 :   TII = MF.getSubtarget().getInstrInfo();
     631      383293 :   MRI = &MF.getRegInfo();
     632             : 
     633     1294109 :   for (MachineBasicBlock &MBB : MF)
     634      910816 :     CopyPropagateBlock(MBB);
     635             : 
     636      383293 :   return Changed;
     637             : }

Generated by: LCOV version 1.13