LCOV - code coverage report
Current view: top level - lib/CodeGen - StackMapLivenessAnalysis.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 48 48 100.0 %
Date: 2017-09-14 15:23:50 Functions: 12 13 92.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
       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 StackMap Liveness analysis pass. The pass calculates
      11             : // the liveness for each basic block in a function and attaches the register
      12             : // live-out information to a stackmap or patchpoint intrinsic if present.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/ADT/Statistic.h"
      17             : #include "llvm/CodeGen/LivePhysRegs.h"
      18             : #include "llvm/CodeGen/MachineFrameInfo.h"
      19             : #include "llvm/CodeGen/MachineFunction.h"
      20             : #include "llvm/CodeGen/MachineFunctionPass.h"
      21             : #include "llvm/CodeGen/Passes.h"
      22             : #include "llvm/Support/CommandLine.h"
      23             : #include "llvm/Support/Debug.h"
      24             : #include "llvm/Support/raw_ostream.h"
      25             : #include "llvm/Target/TargetSubtargetInfo.h"
      26             : 
      27             : using namespace llvm;
      28             : 
      29             : #define DEBUG_TYPE "stackmaps"
      30             : 
      31       72306 : static cl::opt<bool> EnablePatchPointLiveness(
      32      216918 :     "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
      33      289224 :     cl::desc("Enable PatchPoint Liveness Analysis Pass"));
      34             : 
      35             : STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
      36             : STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
      37             : STATISTIC(NumBBsVisited,          "Number of basic blocks visited");
      38             : STATISTIC(NumBBsHaveNoStackmap,   "Number of basic blocks with no stackmap");
      39             : STATISTIC(NumStackMaps,           "Number of StackMaps visited");
      40             : 
      41             : namespace {
      42             : /// \brief This pass calculates the liveness information for each basic block in
      43             : /// a function and attaches the register live-out information to a patchpoint
      44             : /// intrinsic if present.
      45             : ///
      46             : /// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
      47             : /// The pass skips functions that don't have any patchpoint intrinsics. The
      48             : /// information provided by this pass is optional and not required by the
      49             : /// aformentioned intrinsic to function.
      50       29792 : class StackMapLiveness : public MachineFunctionPass {
      51             :   const TargetRegisterInfo *TRI;
      52             :   LivePhysRegs LiveRegs;
      53             : 
      54             : public:
      55             :   static char ID;
      56             : 
      57             :   /// \brief Default construct and initialize the pass.
      58             :   StackMapLiveness();
      59             : 
      60             :   /// \brief Tell the pass manager which passes we depend on and what
      61             :   /// information we preserve.
      62             :   void getAnalysisUsage(AnalysisUsage &AU) const override;
      63             : 
      64       14930 :   MachineFunctionProperties getRequiredProperties() const override {
      65       44790 :     return MachineFunctionProperties().set(
      66       44790 :         MachineFunctionProperties::Property::NoVRegs);
      67             :   }
      68             : 
      69             :   /// \brief Calculate the liveness information for the given machine function.
      70             :   bool runOnMachineFunction(MachineFunction &MF) override;
      71             : 
      72             : private:
      73             :   /// \brief Performs the actual liveness calculation for the function.
      74             :   bool calculateLiveness(MachineFunction &MF);
      75             : 
      76             :   /// \brief Add the current register live set to the instruction.
      77             :   void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
      78             : 
      79             :   /// \brief Create a register mask and initialize it with the registers from
      80             :   /// the register live set.
      81             :   uint32_t *createRegisterMask(MachineFunction &MF) const;
      82             : };
      83             : } // namespace
      84             : 
      85             : char StackMapLiveness::ID = 0;
      86             : char &llvm::StackMapLivenessID = StackMapLiveness::ID;
      87      196148 : INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
      88             :                 "StackMap Liveness Analysis", false, false)
      89             : 
      90             : /// Default construct and initialize the pass.
      91       29966 : StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
      92       14983 :   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
      93       14983 : }
      94             : 
      95             : /// Tell the pass manager which passes we depend on and what information we
      96             : /// preserve.
      97       14926 : void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
      98             :   // We preserve all information.
      99       14926 :   AU.setPreservesAll();
     100       14926 :   AU.setPreservesCFG();
     101       14926 :   MachineFunctionPass::getAnalysisUsage(AU);
     102       14926 : }
     103             : 
     104             : /// Calculate the liveness information for the given machine function.
     105      124369 : bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
     106      124369 :   if (!EnablePatchPointLiveness)
     107             :     return false;
     108             : 
     109             :   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
     110             :                << " **********\n");
     111      124367 :   TRI = MF.getSubtarget().getRegisterInfo();
     112      124367 :   ++NumStackMapFuncVisited;
     113             : 
     114             :   // Skip this function if there are no patchpoints to process.
     115      124367 :   if (!MF.getFrameInfo().hasPatchPoint()) {
     116             :     ++NumStackMapFuncSkipped;
     117             :     return false;
     118             :   }
     119         122 :   return calculateLiveness(MF);
     120             : }
     121             : 
     122             : /// Performs the actual liveness calculation for the function.
     123         122 : bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
     124         122 :   bool HasChanged = false;
     125             :   // For all basic blocks in the function.
     126         497 :   for (auto &MBB : MF) {
     127             :     DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
     128         262 :     LiveRegs.init(*TRI);
     129             :     // FIXME: This should probably be addLiveOuts().
     130         131 :     LiveRegs.addLiveOutsNoPristines(MBB);
     131         131 :     bool HasStackMap = false;
     132             :     // Reverse iterate over all instructions and add the current live register
     133             :     // set to an instruction if we encounter a patchpoint instruction.
     134        2678 :     for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
     135        4570 :       if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
     136         148 :         addLiveOutSetToMI(MF, *I);
     137         148 :         HasChanged = true;
     138         148 :         HasStackMap = true;
     139             :         ++NumStackMaps;
     140             :       }
     141             :       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
     142        4570 :       LiveRegs.stepBackward(*I);
     143             :     }
     144         131 :     ++NumBBsVisited;
     145             :     if (!HasStackMap)
     146             :       ++NumBBsHaveNoStackmap;
     147             :   }
     148         122 :   return HasChanged;
     149             : }
     150             : 
     151             : /// Add the current register live set to the instruction.
     152         148 : void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
     153             :                                          MachineInstr &MI) {
     154         148 :   uint32_t *Mask = createRegisterMask(MF);
     155         148 :   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
     156         148 :   MI.addOperand(MF, MO);
     157         148 : }
     158             : 
     159             : /// Create a register mask and initialize it with the registers from the
     160             : /// register live set.
     161         148 : uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
     162             :   // The mask is owned and cleaned up by the Machine Function.
     163         148 :   uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
     164        1307 :   for (auto Reg : LiveRegs)
     165         863 :     Mask[Reg / 32] |= 1U << (Reg % 32);
     166             : 
     167             :   // Give the target a chance to adjust the mask.
     168         148 :   TRI->adjustStackMapLiveOutMask(Mask);
     169             : 
     170         148 :   return Mask;
     171      216918 : }

Generated by: LCOV version 1.13