Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 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 | #define DEBUG_TYPE "stackmaps" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 21 | #include "llvm/CodeGen/Passes.h" |
| 22 | #include "llvm/CodeGen/StackMapLivenessAnalysis.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace llvm { |
| 30 | cl::opt<bool> EnableStackMapLiveness("enable-stackmap-liveness", |
| 31 | cl::Hidden, cl::desc("Enable StackMap Liveness Analysis Pass")); |
| 32 | cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness", |
| 33 | cl::Hidden, cl::desc("Enable PatchPoint Liveness Analysis Pass")); |
| 34 | } |
| 35 | |
| 36 | STATISTIC(NumStackMapFuncVisited, "Number of functions visited"); |
| 37 | STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped"); |
| 38 | STATISTIC(NumBBsVisited, "Number of basic blocks visited"); |
| 39 | STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap"); |
| 40 | STATISTIC(NumStackMaps, "Number of StackMaps visited"); |
| 41 | |
| 42 | char StackMapLiveness::ID = 0; |
| 43 | char &llvm::StackMapLivenessID = StackMapLiveness::ID; |
| 44 | INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", |
| 45 | "StackMap Liveness Analysis", false, false) |
| 46 | |
| 47 | /// Default construct and initialize the pass. |
| 48 | StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) { |
| 49 | initializeStackMapLivenessPass(*PassRegistry::getPassRegistry()); |
| 50 | } |
| 51 | |
| 52 | /// Tell the pass manager which passes we depend on and what information we |
| 53 | /// preserve. |
| 54 | void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const { |
| 55 | // We preserve all information. |
| 56 | AU.setPreservesAll(); |
| 57 | AU.setPreservesCFG(); |
| 58 | // Default dependencie for all MachineFunction passes. |
| 59 | AU.addRequired<MachineFunctionAnalysis>(); |
| 60 | } |
| 61 | |
| 62 | /// Calculate the liveness information for the given machine function. |
| 63 | bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) { |
| 64 | DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " |
| 65 | << _MF.getName() << " **********\n"); |
| 66 | MF = &_MF; |
| 67 | TRI = MF->getTarget().getRegisterInfo(); |
| 68 | ++NumStackMapFuncVisited; |
| 69 | |
| 70 | // Skip this function if there are no stackmaps or patchpoints to process. |
| 71 | if (!((MF->getFrameInfo()->hasStackMap() && EnableStackMapLiveness) || |
| 72 | (MF->getFrameInfo()->hasPatchPoint() && EnablePatchPointLiveness))) { |
| 73 | ++NumStackMapFuncSkipped; |
| 74 | return false; |
| 75 | } |
| 76 | return calculateLiveness(); |
| 77 | } |
| 78 | |
| 79 | /// Performs the actual liveness calculation for the function. |
| 80 | bool StackMapLiveness::calculateLiveness() { |
| 81 | bool HasChanged = false; |
| 82 | // For all basic blocks in the function. |
| 83 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 84 | MBBI != MBBE; ++MBBI) { |
| 85 | DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n"); |
| 86 | LiveRegs.init(TRI); |
| 87 | LiveRegs.addLiveOuts(MBBI); |
| 88 | bool HasStackMap = false; |
| 89 | // Reverse iterate over all instructions and add the current live register |
| 90 | // set to an instruction if we encounter a stackmap or patchpoint |
| 91 | // instruction. |
| 92 | for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(), |
| 93 | E = MBBI->rend(); I != E; ++I) { |
| 94 | int Opc = I->getOpcode(); |
| 95 | if ((EnableStackMapLiveness && (Opc == TargetOpcode::STACKMAP)) || |
| 96 | (EnablePatchPointLiveness && (Opc == TargetOpcode::PATCHPOINT))) { |
| 97 | addLiveOutSetToMI(*I); |
| 98 | HasChanged = true; |
| 99 | HasStackMap = true; |
| 100 | ++NumStackMaps; |
| 101 | } |
| 102 | DEBUG(dbgs() << " " << *I << " " << LiveRegs); |
| 103 | LiveRegs.stepBackward(*I); |
| 104 | } |
| 105 | ++NumBBsVisited; |
| 106 | if (!HasStackMap) |
| 107 | ++NumBBsHaveNoStackmap; |
| 108 | } |
| 109 | return HasChanged; |
| 110 | } |
| 111 | |
| 112 | /// Add the current register live set to the instruction. |
| 113 | void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) { |
| 114 | uint32_t *Mask = createRegisterMask(); |
| 115 | MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask); |
| 116 | MI.addOperand(*MF, MO); |
| 117 | } |
| 118 | |
| 119 | /// Create a register mask and initialize it with the registers from the |
| 120 | /// register live set. |
| 121 | uint32_t *StackMapLiveness::createRegisterMask() const { |
| 122 | // The mask is owned and cleaned up by the Machine Function. |
| 123 | uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs()); |
| 124 | for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end(); |
| 125 | RI != RE; ++RI) |
| 126 | Mask[*RI / 32] |= 1U << (*RI % 32); |
| 127 | return Mask; |
| 128 | } |