LCOV - code coverage report
Current view: top level - lib/CodeGen - VirtRegMap.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 174 186 93.5 %
Date: 2018-05-20 00:06:23 Functions: 23 25 92.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===//
       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 file implements the VirtRegMap class.
      11             : //
      12             : // It also contains implementations of the Spiller interface, which, given a
      13             : // virtual register map and a machine function, eliminates all virtual
      14             : // references by replacing them with physical register references - adding spill
      15             : // code as necessary.
      16             : //
      17             : //===----------------------------------------------------------------------===//
      18             : 
      19             : #include "llvm/CodeGen/VirtRegMap.h"
      20             : #include "LiveDebugVariables.h"
      21             : #include "llvm/ADT/SmallVector.h"
      22             : #include "llvm/ADT/Statistic.h"
      23             : #include "llvm/CodeGen/LiveInterval.h"
      24             : #include "llvm/CodeGen/LiveIntervals.h"
      25             : #include "llvm/CodeGen/LiveStacks.h"
      26             : #include "llvm/CodeGen/MachineBasicBlock.h"
      27             : #include "llvm/CodeGen/MachineFrameInfo.h"
      28             : #include "llvm/CodeGen/MachineFunction.h"
      29             : #include "llvm/CodeGen/MachineFunctionPass.h"
      30             : #include "llvm/CodeGen/MachineInstr.h"
      31             : #include "llvm/CodeGen/MachineOperand.h"
      32             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      33             : #include "llvm/CodeGen/SlotIndexes.h"
      34             : #include "llvm/CodeGen/TargetInstrInfo.h"
      35             : #include "llvm/CodeGen/TargetOpcodes.h"
      36             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      37             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      38             : #include "llvm/Config/llvm-config.h"
      39             : #include "llvm/MC/LaneBitmask.h"
      40             : #include "llvm/Pass.h"
      41             : #include "llvm/Support/Compiler.h"
      42             : #include "llvm/Support/Debug.h"
      43             : #include "llvm/Support/raw_ostream.h"
      44             : #include <cassert>
      45             : #include <iterator>
      46             : #include <utility>
      47             : 
      48             : using namespace llvm;
      49             : 
      50             : #define DEBUG_TYPE "regalloc"
      51             : 
      52             : STATISTIC(NumSpillSlots, "Number of spill slots allocated");
      53             : STATISTIC(NumIdCopies,   "Number of identity moves eliminated after rewriting");
      54             : 
      55             : //===----------------------------------------------------------------------===//
      56             : //  VirtRegMap implementation
      57             : //===----------------------------------------------------------------------===//
      58             : 
      59             : char VirtRegMap::ID = 0;
      60             : 
      61      351950 : INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false)
      62             : 
      63      169122 : bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
      64      169122 :   MRI = &mf.getRegInfo();
      65      169122 :   TII = mf.getSubtarget().getInstrInfo();
      66      169122 :   TRI = mf.getSubtarget().getRegisterInfo();
      67      169122 :   MF = &mf;
      68             : 
      69             :   Virt2PhysMap.clear();
      70             :   Virt2StackSlotMap.clear();
      71             :   Virt2SplitMap.clear();
      72             : 
      73      169122 :   grow();
      74      169122 :   return false;
      75             : }
      76             : 
      77      461701 : void VirtRegMap::grow() {
      78      461701 :   unsigned NumRegs = MF->getRegInfo().getNumVirtRegs();
      79      461701 :   Virt2PhysMap.resize(NumRegs);
      80             :   Virt2StackSlotMap.resize(NumRegs);
      81             :   Virt2SplitMap.resize(NumRegs);
      82      461701 : }
      83             : 
      84     1342414 : void VirtRegMap::assignVirt2Phys(unsigned virtReg, MCPhysReg physReg) {
      85             :   assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
      86             :          TargetRegisterInfo::isPhysicalRegister(physReg));
      87             :   assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
      88             :          "attempt to assign physical register to already mapped "
      89             :          "virtual register");
      90             :   assert(!getRegInfo().isReserved(physReg) &&
      91             :          "Attempt to map virtReg to a reserved physReg");
      92     1342414 :   Virt2PhysMap[virtReg] = physReg;
      93     1342414 : }
      94             : 
      95       13868 : unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) {
      96       13868 :   unsigned Size = TRI->getSpillSize(*RC);
      97             :   unsigned Align = TRI->getSpillAlignment(*RC);
      98       13868 :   int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Align);
      99             :   ++NumSpillSlots;
     100       13868 :   return SS;
     101             : }
     102             : 
     103      682305 : bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) {
     104      682305 :   unsigned Hint = MRI->getSimpleHint(VirtReg);
     105      682274 :   if (!Hint)
     106             :     return false;
     107      365238 :   if (TargetRegisterInfo::isVirtualRegister(Hint))
     108             :     Hint = getPhys(Hint);
     109      365238 :   return getPhys(VirtReg) == Hint;
     110             : }
     111             : 
     112     1380103 : bool VirtRegMap::hasKnownPreference(unsigned VirtReg) {
     113     1380103 :   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg);
     114     1380103 :   if (TargetRegisterInfo::isPhysicalRegister(Hint.second))
     115             :     return true;
     116      795904 :   if (TargetRegisterInfo::isVirtualRegister(Hint.second))
     117      135139 :     return hasPhys(Hint.second);
     118             :   return false;
     119             : }
     120             : 
     121       13868 : int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
     122             :   assert(TargetRegisterInfo::isVirtualRegister(virtReg));
     123             :   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
     124             :          "attempt to assign stack slot to already spilled register");
     125       13868 :   const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
     126       27736 :   return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
     127             : }
     128             : 
     129       10049 : void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
     130             :   assert(TargetRegisterInfo::isVirtualRegister(virtReg));
     131             :   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
     132             :          "attempt to assign stack slot to already spilled register");
     133             :   assert((SS >= 0 ||
     134             :           (SS >= MF->getFrameInfo().getObjectIndexBegin())) &&
     135             :          "illegal fixed frame index");
     136       10049 :   Virt2StackSlotMap[virtReg] = SS;
     137       10049 : }
     138             : 
     139           0 : void VirtRegMap::print(raw_ostream &OS, const Module*) const {
     140           0 :   OS << "********** REGISTER MAP **********\n";
     141           0 :   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
     142             :     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
     143           0 :     if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
     144           0 :       OS << '[' << printReg(Reg, TRI) << " -> "
     145           0 :          << printReg(Virt2PhysMap[Reg], TRI) << "] "
     146           0 :          << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
     147             :     }
     148             :   }
     149             : 
     150           0 :   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
     151             :     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
     152           0 :     if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
     153           0 :       OS << '[' << printReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
     154           0 :          << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
     155             :     }
     156             :   }
     157             :   OS << '\n';
     158           0 : }
     159             : 
     160             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     161             : LLVM_DUMP_METHOD void VirtRegMap::dump() const {
     162             :   print(dbgs());
     163             : }
     164             : #endif
     165             : 
     166             : //===----------------------------------------------------------------------===//
     167             : //                              VirtRegRewriter
     168             : //===----------------------------------------------------------------------===//
     169             : //
     170             : // The VirtRegRewriter is the last of the register allocator passes.
     171             : // It rewrites virtual registers to physical registers as specified in the
     172             : // VirtRegMap analysis. It also updates live-in information on basic blocks
     173             : // according to LiveIntervals.
     174             : //
     175             : namespace {
     176             : 
     177       18215 : class VirtRegRewriter : public MachineFunctionPass {
     178             :   MachineFunction *MF;
     179             :   const TargetRegisterInfo *TRI;
     180             :   const TargetInstrInfo *TII;
     181             :   MachineRegisterInfo *MRI;
     182             :   SlotIndexes *Indexes;
     183             :   LiveIntervals *LIS;
     184             :   VirtRegMap *VRM;
     185             : 
     186             :   void rewrite();
     187             :   void addMBBLiveIns();
     188             :   bool readsUndefSubreg(const MachineOperand &MO) const;
     189             :   void addLiveInsForSubRanges(const LiveInterval &LI, unsigned PhysReg) const;
     190             :   void handleIdentityCopy(MachineInstr &MI) const;
     191             :   void expandCopyBundle(MachineInstr &MI) const;
     192             :   bool subRegLiveThrough(const MachineInstr &MI, unsigned SuperPhysReg) const;
     193             : 
     194             : public:
     195             :   static char ID;
     196             : 
     197       18308 :   VirtRegRewriter() : MachineFunctionPass(ID) {}
     198             : 
     199             :   void getAnalysisUsage(AnalysisUsage &AU) const override;
     200             : 
     201             :   bool runOnMachineFunction(MachineFunction&) override;
     202             : 
     203       18173 :   MachineFunctionProperties getSetProperties() const override {
     204       36346 :     return MachineFunctionProperties().set(
     205       18173 :         MachineFunctionProperties::Property::NoVRegs);
     206             :   }
     207             : };
     208             : 
     209             : } // end anonymous namespace
     210             : 
     211             : char VirtRegRewriter::ID = 0;
     212             : 
     213             : char &llvm::VirtRegRewriterID = VirtRegRewriter::ID;
     214             : 
     215       26016 : INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
     216             :                       "Virtual Register Rewriter", false, false)
     217       26016 : INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
     218       26016 : INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
     219       26016 : INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
     220       26016 : INITIALIZE_PASS_DEPENDENCY(LiveStacks)
     221       26016 : INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
     222      143808 : INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter",
     223             :                     "Virtual Register Rewriter", false, false)
     224             : 
     225       18169 : void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
     226       18169 :   AU.setPreservesCFG();
     227             :   AU.addRequired<LiveIntervals>();
     228             :   AU.addRequired<SlotIndexes>();
     229             :   AU.addPreserved<SlotIndexes>();
     230             :   AU.addRequired<LiveDebugVariables>();
     231             :   AU.addRequired<LiveStacks>();
     232             :   AU.addPreserved<LiveStacks>();
     233             :   AU.addRequired<VirtRegMap>();
     234       18169 :   MachineFunctionPass::getAnalysisUsage(AU);
     235       18169 : }
     236             : 
     237      169101 : bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
     238      169101 :   MF = &fn;
     239      169101 :   TRI = MF->getSubtarget().getRegisterInfo();
     240      169101 :   TII = MF->getSubtarget().getInstrInfo();
     241      169101 :   MRI = &MF->getRegInfo();
     242      169101 :   Indexes = &getAnalysis<SlotIndexes>();
     243      169101 :   LIS = &getAnalysis<LiveIntervals>();
     244      169101 :   VRM = &getAnalysis<VirtRegMap>();
     245             :   LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
     246             :                     << "********** Function: " << MF->getName() << '\n');
     247             :   LLVM_DEBUG(VRM->dump());
     248             : 
     249             :   // Add kill flags while we still have virtual registers.
     250      169101 :   LIS->addKillFlags(VRM);
     251             : 
     252             :   // Live-in lists on basic blocks are required for physregs.
     253      169101 :   addMBBLiveIns();
     254             : 
     255             :   // Rewrite virtual registers.
     256      169101 :   rewrite();
     257             : 
     258             :   // Write out new DBG_VALUE instructions.
     259      169100 :   getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
     260             : 
     261             :   // All machine operands and other references to virtual registers have been
     262             :   // replaced. Remove the virtual registers and release all the transient data.
     263      169101 :   VRM->clearAllVirt();
     264      169101 :   MRI->clearVirtRegs();
     265      169101 :   return true;
     266             : }
     267             : 
     268         834 : void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI,
     269             :                                              unsigned PhysReg) const {
     270             :   assert(!LI.empty());
     271             :   assert(LI.hasSubRanges());
     272             : 
     273             :   using SubRangeIteratorPair =
     274             :       std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>;
     275             : 
     276             :   SmallVector<SubRangeIteratorPair, 4> SubRanges;
     277             :   SlotIndex First;
     278             :   SlotIndex Last;
     279        3467 :   for (const LiveInterval::SubRange &SR : LI.subranges()) {
     280        2633 :     SubRanges.push_back(std::make_pair(&SR, SR.begin()));
     281        4432 :     if (!First.isValid() || SR.segments.front().start < First)
     282        1035 :       First = SR.segments.front().start;
     283        4432 :     if (!Last.isValid() || SR.segments.back().end > Last)
     284         980 :       Last = SR.segments.back().end;
     285             :   }
     286             : 
     287             :   // Check all mbb start positions between First and Last while
     288             :   // simulatenously advancing an iterator for each subrange.
     289        3588 :   for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First);
     290       10182 :        MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) {
     291             :     SlotIndex MBBBegin = MBBI->first;
     292             :     // Advance all subrange iterators so that their end position is just
     293             :     // behind MBBBegin (or the iterator is at the end).
     294             :     LaneBitmask LaneMask;
     295       19780 :     for (auto &RangeIterPair : SubRanges) {
     296        8513 :       const LiveInterval::SubRange *SR = RangeIterPair.first;
     297             :       LiveInterval::const_iterator &SRI = RangeIterPair.second;
     298       31090 :       while (SRI != SR->end() && SRI->end <= MBBBegin)
     299        2207 :         ++SRI;
     300        9583 :       if (SRI == SR->end())
     301             :         continue;
     302        7443 :       if (SRI->start <= MBBBegin)
     303             :         LaneMask |= SR->LaneMask;
     304             :     }
     305        2754 :     if (LaneMask.none())
     306             :       continue;
     307        2391 :     MachineBasicBlock *MBB = MBBI->second;
     308        2391 :     MBB->addLiveIn(PhysReg, LaneMask);
     309             :   }
     310         834 : }
     311             : 
     312             : // Compute MBB live-in lists from virtual register live ranges and their
     313             : // assignments.
     314      169101 : void VirtRegRewriter::addMBBLiveIns() {
     315     5570248 :   for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
     316             :     unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx);
     317     5232046 :     if (MRI->reg_nodbg_empty(VirtReg))
     318     1335511 :       continue;
     319     1280512 :     LiveInterval &LI = LIS->getInterval(VirtReg);
     320     1280512 :     if (LI.empty() || LIS->intervalIsInOneMBB(LI))
     321     1191246 :       continue;
     322             :     // This is a virtual register that is live across basic blocks. Its
     323             :     // assigned PhysReg must be marked as live-in to those blocks.
     324       89266 :     unsigned PhysReg = VRM->getPhys(VirtReg);
     325             :     assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register.");
     326             : 
     327       89266 :     if (LI.hasSubRanges()) {
     328         834 :       addLiveInsForSubRanges(LI, PhysReg);
     329             :     } else {
     330             :       // Go over MBB begin positions and see if we have segments covering them.
     331             :       // The following works because segments and the MBBIndex list are both
     332             :       // sorted by slot indexes.
     333       88432 :       SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin();
     334      588748 :       for (const auto &Seg : LI) {
     335      250158 :         I = Indexes->advanceMBBIndex(I, Seg.start);
     336     2262775 :         for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) {
     337      383208 :           MachineBasicBlock *MBB = I->second;
     338      383208 :           MBB->addLiveIn(PhysReg);
     339             :         }
     340             :       }
     341             :     }
     342             :   }
     343             : 
     344             :   // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in
     345             :   // each MBB's LiveIns set before calling addLiveIn on them.
     346      665183 :   for (MachineBasicBlock &MBB : *MF)
     347      326981 :     MBB.sortUniqueLiveIns();
     348      169101 : }
     349             : 
     350             : /// Returns true if the given machine operand \p MO only reads undefined lanes.
     351             : /// The function only works for use operands with a subregister set.
     352       75487 : bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const {
     353             :   // Shortcut if the operand is already marked undef.
     354       75487 :   if (MO.isUndef())
     355             :     return true;
     356             : 
     357       75370 :   unsigned Reg = MO.getReg();
     358       75370 :   const LiveInterval &LI = LIS->getInterval(Reg);
     359       75370 :   const MachineInstr &MI = *MO.getParent();
     360      150740 :   SlotIndex BaseIndex = LIS->getInstructionIndex(MI);
     361             :   // This code is only meant to handle reading undefined subregisters which
     362             :   // we couldn't properly detect before.
     363             :   assert(LI.liveAt(BaseIndex) &&
     364             :          "Reads of completely dead register should be marked undef already");
     365             :   unsigned SubRegIdx = MO.getSubReg();
     366             :   assert(SubRegIdx != 0 && LI.hasSubRanges());
     367       75370 :   LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
     368             :   // See if any of the relevant subregister liveranges is defined at this point.
     369      173622 :   for (const LiveInterval::SubRange &SR : LI.subranges()) {
     370      173622 :     if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex))
     371             :       return false;
     372             :   }
     373             :   return true;
     374             : }
     375             : 
     376     3515395 : void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const {
     377             :   if (!MI.isIdentityCopy())
     378             :     return;
     379             :   LLVM_DEBUG(dbgs() << "Identity copy: " << MI);
     380             :   ++NumIdCopies;
     381             : 
     382             :   // Copies like:
     383             :   //    %r0 = COPY undef %r0
     384             :   //    %al = COPY %al, implicit-def %eax
     385             :   // give us additional liveness information: The target (super-)register
     386             :   // must not be valid before this point. Replace the COPY with a KILL
     387             :   // instruction to maintain this information.
     388      608416 :   if (MI.getOperand(0).isUndef() || MI.getNumOperands() > 2) {
     389       11519 :     MI.setDesc(TII->get(TargetOpcode::KILL));
     390             :     LLVM_DEBUG(dbgs() << "  replace by: " << MI);
     391             :     return;
     392             :   }
     393             : 
     394      596897 :   if (Indexes)
     395      596897 :     Indexes->removeSingleMachineInstrFromMaps(MI);
     396      596897 :   MI.eraseFromBundle();
     397             :   LLVM_DEBUG(dbgs() << "  deleted.\n");
     398             : }
     399             : 
     400             : /// The liverange splitting logic sometimes produces bundles of copies when
     401             : /// subregisters are involved. Expand these into a sequence of copy instructions
     402             : /// after processing the last in the bundle. Does not update LiveIntervals
     403             : /// which we shouldn't need for this instruction anymore.
     404     3515395 : void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const {
     405     3515395 :   if (!MI.isCopy())
     406             :     return;
     407             : 
     408      787849 :   if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) {
     409             :     // Only do this when the complete bundle is made out of COPYs.
     410           1 :     MachineBasicBlock &MBB = *MI.getParent();
     411             :     for (MachineBasicBlock::reverse_instr_iterator I =
     412           1 :          std::next(MI.getReverseIterator()), E = MBB.instr_rend();
     413           4 :          I != E && I->isBundledWithSucc(); ++I) {
     414           1 :       if (!I->isCopy())
     415             :         return;
     416             :     }
     417             : 
     418             :     for (MachineBasicBlock::reverse_instr_iterator I = MI.getReverseIterator();
     419           2 :          I->isBundledWithPred(); ) {
     420             :       MachineInstr &MI = *I;
     421             :       ++I;
     422             : 
     423           1 :       MI.unbundleFromPred();
     424           1 :       if (Indexes)
     425           1 :         Indexes->insertMachineInstrInMaps(MI);
     426             :     }
     427             :   }
     428             : }
     429             : 
     430             : /// Check whether (part of) \p SuperPhysReg is live through \p MI.
     431             : /// \pre \p MI defines a subregister of a virtual register that
     432             : /// has been assigned to \p SuperPhysReg.
     433       35947 : bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI,
     434             :                                         unsigned SuperPhysReg) const {
     435       35947 :   SlotIndex MIIndex = LIS->getInstructionIndex(MI);
     436       35947 :   SlotIndex BeforeMIUses = MIIndex.getBaseIndex();
     437       35947 :   SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex();
     438      145139 :   for (MCRegUnitIterator Unit(SuperPhysReg, TRI); Unit.isValid(); ++Unit) {
     439      147612 :     const LiveRange &UnitRange = LIS->getRegUnit(*Unit);
     440             :     // If the regunit is live both before and after MI,
     441             :     // we assume it is live through.
     442             :     // Generally speaking, this is not true, because something like
     443             :     // "RU = op RU" would match that description.
     444             :     // However, we know that we are trying to assess whether
     445             :     // a def of a virtual reg, vreg, is live at the same time of RU.
     446             :     // If we are in the "RU = op RU" situation, that means that vreg
     447             :     // is defined at the same time as RU (i.e., "vreg, RU = op RU").
     448             :     // Thus, vreg and RU interferes and vreg cannot be assigned to
     449             :     // SuperPhysReg. Therefore, this situation cannot happen.
     450       73806 :     if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses))
     451             :       return true;
     452             :   }
     453             :   return false;
     454             : }
     455             : 
     456      169101 : void VirtRegRewriter::rewrite() {
     457      169101 :   bool NoSubRegLiveness = !MRI->subRegLivenessEnabled();
     458             :   SmallVector<unsigned, 8> SuperDeads;
     459             :   SmallVector<unsigned, 8> SuperDefs;
     460             :   SmallVector<unsigned, 8> SuperKills;
     461             : 
     462      169101 :   for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
     463      496082 :        MBBI != MBBE; ++MBBI) {
     464             :     LLVM_DEBUG(MBBI->print(dbgs(), Indexes));
     465     3515395 :     for (MachineBasicBlock::instr_iterator
     466     3842376 :            MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
     467             :       MachineInstr *MI = &*MII;
     468             :       ++MII;
     469             : 
     470    18762320 :       for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
     471    22277715 :            MOE = MI->operands_end(); MOI != MOE; ++MOI) {
     472             :         MachineOperand &MO = *MOI;
     473             : 
     474             :         // Make sure MRI knows about registers clobbered by regmasks.
     475    15246925 :         if (MO.isRegMask())
     476      152517 :           MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
     477             : 
     478    25441554 :         if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
     479    11246501 :           continue;
     480             :         unsigned VirtReg = MO.getReg();
     481     8000848 :         unsigned PhysReg = VRM->getPhys(VirtReg);
     482             :         assert(PhysReg != VirtRegMap::NO_PHYS_REG &&
     483             :                "Instruction uses unmapped VirtReg");
     484             :         assert(!MRI->isReserved(PhysReg) && "Reserved register assignment");
     485             : 
     486             :         // Preserve semantics of sub-register operands.
     487             :         unsigned SubReg = MO.getSubReg();
     488     4000424 :         if (SubReg != 0) {
     489      298750 :           if (NoSubRegLiveness) {
     490             :             // A virtual register kill refers to the whole register, so we may
     491             :             // have to add implicit killed operands for the super-register.  A
     492             :             // partial redef always kills and redefines the super-register.
     493      185075 :             if ((MO.readsReg() && (MO.isDef() || MO.isKill())) ||
     494       35947 :                 (MO.isDef() && subRegLiveThrough(*MI, PhysReg)))
     495       65489 :               SuperKills.push_back(PhysReg);
     496             : 
     497      117495 :             if (MO.isDef()) {
     498             :               // Also add implicit defs for the super-register.
     499       66323 :               if (MO.isDead())
     500          73 :                 SuperDeads.push_back(PhysReg);
     501             :               else
     502       66250 :                 SuperDefs.push_back(PhysReg);
     503             :             }
     504             :           } else {
     505      181255 :             if (MO.isUse()) {
     506       75487 :               if (readsUndefSubreg(MO))
     507             :                 // We need to add an <undef> flag if the subregister is
     508             :                 // completely undefined (and we are not adding super-register
     509             :                 // defs).
     510             :                 MO.setIsUndef(true);
     511             :             } else if (!MO.isDead()) {
     512             :               assert(MO.isDef());
     513             :             }
     514             :           }
     515             : 
     516             :           // The def undef and def internal flags only make sense for
     517             :           // sub-register defs, and we are substituting a full physreg.  An
     518             :           // implicit killed operand from the SuperKills list will represent the
     519             :           // partial read of the super-register.
     520      298750 :           if (MO.isDef()) {
     521             :             MO.setIsUndef(false);
     522             :             MO.setIsInternalRead(false);
     523             :           }
     524             : 
     525             :           // PhysReg operands cannot have subregister indexes.
     526      298750 :           PhysReg = TRI->getSubReg(PhysReg, SubReg);
     527             :           assert(PhysReg && "Invalid SubReg for physical register");
     528             :           MO.setSubReg(0);
     529             :         }
     530             :         // Rewrite. Note we could have used MachineOperand::substPhysReg(), but
     531             :         // we need the inlining here.
     532     4000424 :         MO.setReg(PhysReg);
     533     4000424 :         MO.setIsRenamable(true);
     534             :       }
     535             : 
     536             :       // Add any missing super-register kills after rewriting the whole
     537             :       // instruction.
     538       65489 :       while (!SuperKills.empty())
     539      130978 :         MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true);
     540             : 
     541          73 :       while (!SuperDeads.empty())
     542         146 :         MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true);
     543             : 
     544       66250 :       while (!SuperDefs.empty())
     545      132500 :         MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI);
     546             : 
     547             :       LLVM_DEBUG(dbgs() << "> " << *MI);
     548             : 
     549     3515395 :       expandCopyBundle(*MI);
     550             : 
     551             :       // We can remove identity copies right now.
     552     3515395 :       handleIdentityCopy(*MI);
     553             :     }
     554             :   }
     555      169101 : }

Generated by: LCOV version 1.13