Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [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 | |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Benjamin Kramer | 722ff28 | 2015-03-24 13:20:54 +0000 | [diff] [blame^] | 17 | #include "llvm/CodeGen/LivePhysRegs.h" |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
Benjamin Kramer | 722ff28 | 2015-03-24 13:20:54 +0000 | [diff] [blame^] | 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Passes.h" |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | b85d375 | 2015-03-23 18:45:56 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "stackmaps" |
| 31 | |
Benjamin Kramer | 722ff28 | 2015-03-24 13:20:54 +0000 | [diff] [blame^] | 32 | static cl::opt<bool> EnablePatchPointLiveness( |
| 33 | "enable-patchpoint-liveness", cl::Hidden, cl::init(true), |
| 34 | cl::desc("Enable PatchPoint Liveness Analysis Pass")); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 722ff28 | 2015-03-24 13:20:54 +0000 | [diff] [blame^] | 42 | namespace { |
| 43 | /// \brief This pass calculates the liveness information for each basic block in |
| 44 | /// a function and attaches the register live-out information to a patchpoint |
| 45 | /// intrinsic if present. |
| 46 | /// |
| 47 | /// This pass can be disabled via the -enable-patchpoint-liveness=false flag. |
| 48 | /// The pass skips functions that don't have any patchpoint intrinsics. The |
| 49 | /// information provided by this pass is optional and not required by the |
| 50 | /// aformentioned intrinsic to function. |
| 51 | class StackMapLiveness : public MachineFunctionPass { |
| 52 | MachineFunction *MF; |
| 53 | const TargetRegisterInfo *TRI; |
| 54 | LivePhysRegs LiveRegs; |
| 55 | |
| 56 | public: |
| 57 | static char ID; |
| 58 | |
| 59 | /// \brief Default construct and initialize the pass. |
| 60 | StackMapLiveness(); |
| 61 | |
| 62 | /// \brief Tell the pass manager which passes we depend on and what |
| 63 | /// information we preserve. |
| 64 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 65 | |
| 66 | /// \brief Calculate the liveness information for the given machine function. |
| 67 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 68 | |
| 69 | private: |
| 70 | /// \brief Performs the actual liveness calculation for the function. |
| 71 | bool calculateLiveness(); |
| 72 | |
| 73 | /// \brief Add the current register live set to the instruction. |
| 74 | void addLiveOutSetToMI(MachineInstr &MI); |
| 75 | |
| 76 | /// \brief Create a register mask and initialize it with the registers from |
| 77 | /// the register live set. |
| 78 | uint32_t *createRegisterMask() const; |
| 79 | }; |
| 80 | } // namespace |
| 81 | |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 82 | char StackMapLiveness::ID = 0; |
| 83 | char &llvm::StackMapLivenessID = StackMapLiveness::ID; |
| 84 | INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", |
| 85 | "StackMap Liveness Analysis", false, false) |
| 86 | |
| 87 | /// Default construct and initialize the pass. |
| 88 | StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) { |
| 89 | initializeStackMapLivenessPass(*PassRegistry::getPassRegistry()); |
| 90 | } |
| 91 | |
| 92 | /// Tell the pass manager which passes we depend on and what information we |
| 93 | /// preserve. |
| 94 | void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const { |
| 95 | // We preserve all information. |
| 96 | AU.setPreservesAll(); |
| 97 | AU.setPreservesCFG(); |
| 98 | // Default dependencie for all MachineFunction passes. |
| 99 | AU.addRequired<MachineFunctionAnalysis>(); |
| 100 | } |
| 101 | |
| 102 | /// Calculate the liveness information for the given machine function. |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 103 | bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) { |
Juergen Ributzka | 009bff2 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 104 | if (!EnablePatchPointLiveness) |
| 105 | return false; |
| 106 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 107 | DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName() |
| 108 | << " **********\n"); |
| 109 | this->MF = &MF; |
| 110 | TRI = MF.getSubtarget().getRegisterInfo(); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 111 | ++NumStackMapFuncVisited; |
| 112 | |
Juergen Ributzka | 14871f7 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 113 | // Skip this function if there are no patchpoints to process. |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 114 | if (!MF.getFrameInfo()->hasPatchPoint()) { |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 115 | ++NumStackMapFuncSkipped; |
| 116 | return false; |
| 117 | } |
| 118 | return calculateLiveness(); |
| 119 | } |
| 120 | |
| 121 | /// Performs the actual liveness calculation for the function. |
| 122 | bool StackMapLiveness::calculateLiveness() { |
| 123 | bool HasChanged = false; |
| 124 | // For all basic blocks in the function. |
| 125 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 126 | MBBI != MBBE; ++MBBI) { |
| 127 | DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n"); |
| 128 | LiveRegs.init(TRI); |
| 129 | LiveRegs.addLiveOuts(MBBI); |
| 130 | bool HasStackMap = false; |
| 131 | // Reverse iterate over all instructions and add the current live register |
Juergen Ributzka | 14871f7 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 132 | // set to an instruction if we encounter a patchpoint instruction. |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 133 | for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(), |
| 134 | E = MBBI->rend(); I != E; ++I) { |
Juergen Ributzka | 009bff2 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 135 | if (I->getOpcode() == TargetOpcode::PATCHPOINT) { |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 136 | addLiveOutSetToMI(*I); |
| 137 | HasChanged = true; |
| 138 | HasStackMap = true; |
| 139 | ++NumStackMaps; |
| 140 | } |
Andrew Trick | 326c1f68 | 2014-04-04 23:49:35 +0000 | [diff] [blame] | 141 | DEBUG(dbgs() << " " << LiveRegs << " " << *I); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 142 | LiveRegs.stepBackward(*I); |
| 143 | } |
| 144 | ++NumBBsVisited; |
| 145 | if (!HasStackMap) |
| 146 | ++NumBBsHaveNoStackmap; |
| 147 | } |
| 148 | return HasChanged; |
| 149 | } |
| 150 | |
| 151 | /// Add the current register live set to the instruction. |
| 152 | void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) { |
| 153 | uint32_t *Mask = createRegisterMask(); |
| 154 | MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask); |
| 155 | MI.addOperand(*MF, MO); |
| 156 | } |
| 157 | |
| 158 | /// Create a register mask and initialize it with the registers from the |
| 159 | /// register live set. |
| 160 | uint32_t *StackMapLiveness::createRegisterMask() const { |
| 161 | // The mask is owned and cleaned up by the Machine Function. |
| 162 | uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs()); |
| 163 | for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end(); |
| 164 | RI != RE; ++RI) |
| 165 | Mask[*RI / 32] |= 1U << (*RI % 32); |
Hal Finkel | df87f93 | 2015-01-13 17:47:59 +0000 | [diff] [blame] | 166 | |
| 167 | TRI->adjustStackMapLiveOutMask(Mask); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 168 | return Mask; |
| 169 | } |