LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineInstrBundle.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 131 132 99.2 %
Date: 2018-10-20 13:21:21 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
       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             : #include "llvm/CodeGen/MachineInstrBundle.h"
      11             : #include "llvm/ADT/SmallSet.h"
      12             : #include "llvm/ADT/SmallVector.h"
      13             : #include "llvm/CodeGen/MachineFunctionPass.h"
      14             : #include "llvm/CodeGen/MachineInstrBuilder.h"
      15             : #include "llvm/CodeGen/Passes.h"
      16             : #include "llvm/CodeGen/TargetInstrInfo.h"
      17             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      18             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      19             : #include "llvm/Target/TargetMachine.h"
      20             : #include <utility>
      21             : using namespace llvm;
      22             : 
      23             : namespace {
      24             :   class UnpackMachineBundles : public MachineFunctionPass {
      25             :   public:
      26             :     static char ID; // Pass identification
      27        2829 :     UnpackMachineBundles(
      28             :         std::function<bool(const MachineFunction &)> Ftor = nullptr)
      29        2829 :         : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
      30        2829 :       initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
      31        2829 :     }
      32             : 
      33             :     bool runOnMachineFunction(MachineFunction &MF) override;
      34             : 
      35             :   private:
      36             :     std::function<bool(const MachineFunction &)> PredicateFtor;
      37             :   };
      38             : } // end anonymous namespace
      39             : 
      40             : char UnpackMachineBundles::ID = 0;
      41             : char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
      42       87976 : INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
      43             :                 "Unpack machine instruction bundles", false, false)
      44             : 
      45       14590 : bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
      46       29180 :   if (PredicateFtor && !PredicateFtor(MF))
      47             :     return false;
      48             : 
      49             :   bool Changed = false;
      50       11794 :   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
      51             :     MachineBasicBlock *MBB = &*I;
      52             : 
      53             :     for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
      54       64632 :            MIE = MBB->instr_end(); MII != MIE; ) {
      55             :       MachineInstr *MI = &*MII;
      56             : 
      57             :       // Remove BUNDLE instruction and the InsideBundle flags from bundled
      58             :       // instructions.
      59       57801 :       if (MI->isBundle()) {
      60        3403 :         while (++MII != MIE && MII->isBundledWithPred()) {
      61        2332 :           MII->unbundleFromPred();
      62       13675 :           for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
      63       11343 :             MachineOperand &MO = MII->getOperand(i);
      64       11343 :             if (MO.isReg() && MO.isInternalRead())
      65             :               MO.setIsInternalRead(false);
      66             :           }
      67             :         }
      68        1071 :         MI->eraseFromParent();
      69             : 
      70             :         Changed = true;
      71        1071 :         continue;
      72             :       }
      73             : 
      74             :       ++MII;
      75             :     }
      76             :   }
      77             : 
      78             :   return Changed;
      79             : }
      80             : 
      81             : FunctionPass *
      82        2829 : llvm::createUnpackMachineBundles(
      83             :     std::function<bool(const MachineFunction &)> Ftor) {
      84        2829 :   return new UnpackMachineBundles(std::move(Ftor));
      85             : }
      86             : 
      87             : namespace {
      88             :   class FinalizeMachineBundles : public MachineFunctionPass {
      89             :   public:
      90             :     static char ID; // Pass identification
      91         282 :     FinalizeMachineBundles() : MachineFunctionPass(ID) {
      92         282 :       initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
      93         282 :     }
      94             : 
      95             :     bool runOnMachineFunction(MachineFunction &MF) override;
      96             :   };
      97             : } // end anonymous namespace
      98             : 
      99             : char FinalizeMachineBundles::ID = 0;
     100             : char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
     101      139078 : INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
     102             :                 "Finalize machine instruction bundles", false, false)
     103             : 
     104        2297 : bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
     105        2297 :   return llvm::finalizeBundles(MF);
     106             : }
     107             : 
     108             : /// Return the first found DebugLoc that has a DILocation, given a range of
     109             : /// instructions. The search range is from FirstMI to LastMI (exclusive). If no
     110             : /// DILocation is found, then an empty location is returned.
     111       24387 : static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI,
     112             :                             MachineBasicBlock::instr_iterator LastMI) {
     113       87786 :   for (auto MII = FirstMI; MII != LastMI; ++MII)
     114       63428 :     if (MII->getDebugLoc().get())
     115             :       return MII->getDebugLoc();
     116       24358 :   return DebugLoc();
     117             : }
     118             : 
     119             : /// finalizeBundle - Finalize a machine instruction bundle which includes
     120             : /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
     121             : /// This routine adds a BUNDLE instruction to represent the bundle, it adds
     122             : /// IsInternalRead markers to MachineOperands which are defined inside the
     123             : /// bundle, and it copies externally visible defs and uses to the BUNDLE
     124             : /// instruction.
     125       24387 : void llvm::finalizeBundle(MachineBasicBlock &MBB,
     126             :                           MachineBasicBlock::instr_iterator FirstMI,
     127             :                           MachineBasicBlock::instr_iterator LastMI) {
     128             :   assert(FirstMI != LastMI && "Empty bundle?");
     129       24387 :   MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
     130             : 
     131       24387 :   MachineFunction &MF = *MBB.getParent();
     132       24387 :   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
     133       24387 :   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
     134             : 
     135             :   MachineInstrBuilder MIB =
     136       73161 :       BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
     137       24387 :   Bundle.prepend(MIB);
     138             : 
     139             :   SmallVector<unsigned, 32> LocalDefs;
     140       24387 :   SmallSet<unsigned, 32> LocalDefSet;
     141       24387 :   SmallSet<unsigned, 8> DeadDefSet;
     142       24387 :   SmallSet<unsigned, 16> KilledDefSet;
     143             :   SmallVector<unsigned, 8> ExternUses;
     144       24387 :   SmallSet<unsigned, 8> ExternUseSet;
     145       24387 :   SmallSet<unsigned, 8> KilledUseSet;
     146       24387 :   SmallSet<unsigned, 8> UndefUseSet;
     147             :   SmallVector<MachineOperand*, 4> Defs;
     148       87859 :   for (auto MII = FirstMI; MII != LastMI; ++MII) {
     149      880558 :     for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
     150      817086 :       MachineOperand &MO = MII->getOperand(i);
     151      817086 :       if (!MO.isReg())
     152      663910 :         continue;
     153      219465 :       if (MO.isDef()) {
     154       65369 :         Defs.push_back(&MO);
     155       65369 :         continue;
     156             :       }
     157             : 
     158      154096 :       unsigned Reg = MO.getReg();
     159      154096 :       if (!Reg)
     160             :         continue;
     161             :       assert(TargetRegisterInfo::isPhysicalRegister(Reg));
     162      153176 :       if (LocalDefSet.count(Reg)) {
     163             :         MO.setIsInternalRead();
     164        7790 :         if (MO.isKill())
     165             :           // Internal def is now killed.
     166        4792 :           KilledDefSet.insert(Reg);
     167             :       } else {
     168      145386 :         if (ExternUseSet.insert(Reg).second) {
     169      102384 :           ExternUses.push_back(Reg);
     170      102384 :           if (MO.isUndef())
     171         240 :             UndefUseSet.insert(Reg);
     172             :         }
     173      145386 :         if (MO.isKill())
     174             :           // External def is now killed.
     175       37836 :           KilledUseSet.insert(Reg);
     176             :       }
     177             :     }
     178             : 
     179      128841 :     for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
     180       65369 :       MachineOperand &MO = *Defs[i];
     181       65369 :       unsigned Reg = MO.getReg();
     182       65369 :       if (!Reg)
     183           0 :         continue;
     184             : 
     185       65369 :       if (LocalDefSet.insert(Reg).second) {
     186       62695 :         LocalDefs.push_back(Reg);
     187       62695 :         if (MO.isDead()) {
     188        3892 :           DeadDefSet.insert(Reg);
     189             :         }
     190             :       } else {
     191             :         // Re-defined inside the bundle, it's no longer killed.
     192        2674 :         KilledDefSet.erase(Reg);
     193        2674 :         if (!MO.isDead())
     194             :           // Previously defined but dead.
     195        2470 :           DeadDefSet.erase(Reg);
     196             :       }
     197             : 
     198       65369 :       if (!MO.isDead()) {
     199      130177 :         for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
     200        7631 :           unsigned SubReg = *SubRegs;
     201        7631 :           if (LocalDefSet.insert(SubReg).second)
     202        7274 :             LocalDefs.push_back(SubReg);
     203             :         }
     204             :       }
     205             :     }
     206             : 
     207             :     Defs.clear();
     208             :   }
     209             : 
     210       24387 :   SmallSet<unsigned, 32> Added;
     211       94356 :   for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
     212       69969 :     unsigned Reg = LocalDefs[i];
     213       69969 :     if (Added.insert(Reg).second) {
     214             :       // If it's not live beyond end of the bundle, mark it dead.
     215       69969 :       bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
     216       69969 :       MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
     217       69969 :                  getImplRegState(true));
     218             :     }
     219             :   }
     220             : 
     221      126771 :   for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
     222      102384 :     unsigned Reg = ExternUses[i];
     223      102384 :     bool isKill = KilledUseSet.count(Reg);
     224      102384 :     bool isUndef = UndefUseSet.count(Reg);
     225      102384 :     MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
     226      102384 :                getImplRegState(true));
     227             :   }
     228             : 
     229             :   // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
     230             :   // the property, then also set it on the bundle.
     231       87859 :   for (auto MII = FirstMI; MII != LastMI; ++MII) {
     232       63472 :     if (MII->getFlag(MachineInstr::FrameSetup))
     233             :       MIB.setMIFlag(MachineInstr::FrameSetup);
     234       63472 :     if (MII->getFlag(MachineInstr::FrameDestroy))
     235             :       MIB.setMIFlag(MachineInstr::FrameDestroy);
     236             :   }
     237       24387 : }
     238             : 
     239             : /// finalizeBundle - Same functionality as the previous finalizeBundle except
     240             : /// the last instruction in the bundle is not provided as an input. This is
     241             : /// used in cases where bundles are pre-determined by marking instructions
     242             : /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
     243             : /// points to the end of the bundle.
     244             : MachineBasicBlock::instr_iterator
     245         693 : llvm::finalizeBundle(MachineBasicBlock &MBB,
     246             :                      MachineBasicBlock::instr_iterator FirstMI) {
     247             :   MachineBasicBlock::instr_iterator E = MBB.instr_end();
     248             :   MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
     249        2161 :   while (LastMI != E && LastMI->isInsideBundle())
     250             :     ++LastMI;
     251         693 :   finalizeBundle(MBB, FirstMI, LastMI);
     252         693 :   return LastMI;
     253             : }
     254             : 
     255             : /// finalizeBundles - Finalize instruction bundles in the specified
     256             : /// MachineFunction. Return true if any bundles are finalized.
     257        2297 : bool llvm::finalizeBundles(MachineFunction &MF) {
     258             :   bool Changed = false;
     259        4594 :   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
     260             :     MachineBasicBlock &MBB = *I;
     261             :     MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
     262             :     MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
     263        2297 :     if (MII == MIE)
     264             :       continue;
     265             :     assert(!MII->isInsideBundle() &&
     266             :            "First instr cannot be inside bundle before finalization!");
     267             : 
     268       61704 :     for (++MII; MII != MIE; ) {
     269       59409 :       if (!MII->isInsideBundle())
     270             :         ++MII;
     271             :       else {
     272          82 :         MII = finalizeBundle(MBB, std::prev(MII));
     273             :         Changed = true;
     274             :       }
     275             :     }
     276             :   }
     277             : 
     278        2297 :   return Changed;
     279             : }
     280             : 
     281             : //===----------------------------------------------------------------------===//
     282             : // MachineOperand iterator
     283             : //===----------------------------------------------------------------------===//
     284             : 
     285             : MachineOperandIteratorBase::VirtRegInfo
     286       89183 : MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
     287             :                     SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
     288             :   VirtRegInfo RI = { false, false, false };
     289      400598 :   for(; isValid(); ++*this) {
     290             :     MachineOperand &MO = deref();
     291      311415 :     if (!MO.isReg() || MO.getReg() != Reg)
     292             :       continue;
     293             : 
     294             :     // Remember each (MI, OpNo) that refers to Reg.
     295       90515 :     if (Ops)
     296      181030 :       Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
     297             : 
     298             :     // Both defs and uses can read virtual registers.
     299             :     if (MO.readsReg()) {
     300             :       RI.Reads = true;
     301       53688 :       if (MO.isDef())
     302             :         RI.Tied = true;
     303             :     }
     304             : 
     305             :     // Only defs can write.
     306       90515 :     if (MO.isDef())
     307             :       RI.Writes = true;
     308       53668 :     else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
     309             :       RI.Tied = true;
     310             :   }
     311       89183 :   return RI;
     312             : }
     313             : 
     314             : MachineOperandIteratorBase::PhysRegInfo
     315        9330 : MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
     316             :                                            const TargetRegisterInfo *TRI) {
     317             :   bool AllDefsDead = true;
     318             :   PhysRegInfo PRI = {false, false, false, false, false, false, false, false};
     319             : 
     320             :   assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
     321             :          "analyzePhysReg not given a physical register!");
     322       67823 :   for (; isValid(); ++*this) {
     323             :     MachineOperand &MO = deref();
     324             : 
     325       58493 :     if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) {
     326             :       PRI.Clobbered = true;
     327             :       continue;
     328             :     }
     329             : 
     330       58222 :     if (!MO.isReg())
     331             :       continue;
     332             : 
     333       35848 :     unsigned MOReg = MO.getReg();
     334       35848 :     if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
     335             :       continue;
     336             : 
     337       12281 :     if (!TRI->regsOverlap(MOReg, Reg))
     338             :       continue;
     339             : 
     340        5989 :     bool Covered = TRI->isSuperRegisterEq(Reg, MOReg);
     341             :     if (MO.readsReg()) {
     342             :       PRI.Read = true;
     343        1019 :       if (Covered) {
     344             :         PRI.FullyRead = true;
     345        1016 :         if (MO.isKill())
     346             :           PRI.Killed = true;
     347             :       }
     348        4970 :     } else if (MO.isDef()) {
     349             :       PRI.Defined = true;
     350        4969 :       if (Covered)
     351             :         PRI.FullyDefined = true;
     352        4969 :       if (!MO.isDead())
     353             :         AllDefsDead = false;
     354             :     }
     355             :   }
     356             : 
     357        9330 :   if (AllDefsDead) {
     358        5549 :     if (PRI.FullyDefined || PRI.Clobbered)
     359             :       PRI.DeadDef = true;
     360        4155 :     else if (PRI.Defined)
     361             :       PRI.PartialDeadDef = true;
     362             :   }
     363             : 
     364        9330 :   return PRI;
     365             : }

Generated by: LCOV version 1.13