LLVM API Documentation

StackMapLivenessAnalysis.cpp
Go to the documentation of this file.
00001 //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the StackMap Liveness analysis pass. The pass calculates
00011 // the liveness for each basic block in a function and attaches the register
00012 // live-out information to a stackmap or patchpoint intrinsic if present.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/ADT/Statistic.h"
00017 #include "llvm/CodeGen/MachineFrameInfo.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
00020 #include "llvm/CodeGen/Passes.h"
00021 #include "llvm/CodeGen/StackMapLivenessAnalysis.h"
00022 #include "llvm/Support/CommandLine.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Target/TargetSubtargetInfo.h"
00025 
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "stackmaps"
00029 
00030 namespace llvm {
00031 cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
00032   cl::Hidden, cl::init(true),
00033   cl::desc("Enable PatchPoint Liveness Analysis Pass"));
00034 }
00035 
00036 STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
00037 STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
00038 STATISTIC(NumBBsVisited,          "Number of basic blocks visited");
00039 STATISTIC(NumBBsHaveNoStackmap,   "Number of basic blocks with no stackmap");
00040 STATISTIC(NumStackMaps,           "Number of StackMaps visited");
00041 
00042 char StackMapLiveness::ID = 0;
00043 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
00044 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
00045                 "StackMap Liveness Analysis", false, false)
00046 
00047 /// Default construct and initialize the pass.
00048 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
00049   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
00050 }
00051 
00052 /// Tell the pass manager which passes we depend on and what information we
00053 /// preserve.
00054 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
00055   // We preserve all information.
00056   AU.setPreservesAll();
00057   AU.setPreservesCFG();
00058   // Default dependencie for all MachineFunction passes.
00059   AU.addRequired<MachineFunctionAnalysis>();
00060 }
00061 
00062 /// Calculate the liveness information for the given machine function.
00063 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
00064   if (!EnablePatchPointLiveness)
00065     return false;
00066 
00067   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
00068                << _MF.getName() << " **********\n");
00069   MF = &_MF;
00070   TRI = MF->getSubtarget().getRegisterInfo();
00071   ++NumStackMapFuncVisited;
00072 
00073   // Skip this function if there are no patchpoints to process.
00074   if (!MF->getFrameInfo()->hasPatchPoint()) {
00075     ++NumStackMapFuncSkipped;
00076     return false;
00077   }
00078   return calculateLiveness();
00079 }
00080 
00081 /// Performs the actual liveness calculation for the function.
00082 bool StackMapLiveness::calculateLiveness() {
00083   bool HasChanged = false;
00084   // For all basic blocks in the function.
00085   for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
00086        MBBI != MBBE; ++MBBI) {
00087     DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
00088     LiveRegs.init(TRI);
00089     LiveRegs.addLiveOuts(MBBI);
00090     bool HasStackMap = false;
00091     // Reverse iterate over all instructions and add the current live register
00092     // set to an instruction if we encounter a patchpoint instruction.
00093     for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
00094          E = MBBI->rend(); I != E; ++I) {
00095       if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
00096         addLiveOutSetToMI(*I);
00097         HasChanged = true;
00098         HasStackMap = true;
00099         ++NumStackMaps;
00100       }
00101       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
00102       LiveRegs.stepBackward(*I);
00103     }
00104     ++NumBBsVisited;
00105     if (!HasStackMap)
00106       ++NumBBsHaveNoStackmap;
00107   }
00108   return HasChanged;
00109 }
00110 
00111 /// Add the current register live set to the instruction.
00112 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
00113   uint32_t *Mask = createRegisterMask();
00114   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
00115   MI.addOperand(*MF, MO);
00116 }
00117 
00118 /// Create a register mask and initialize it with the registers from the
00119 /// register live set.
00120 uint32_t *StackMapLiveness::createRegisterMask() const {
00121   // The mask is owned and cleaned up by the Machine Function.
00122   uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
00123   for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
00124        RI != RE; ++RI)
00125     Mask[*RI / 32] |= 1U << (*RI % 32);
00126   return Mask;
00127 }