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" |
| 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/StackMapLivenessAnalysis.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | b85d375 | 2015-03-23 18:45:56 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetSubtargetInfo.h" |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "stackmaps" |
| 30 | |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 31 | namespace llvm { |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 32 | cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness", |
Juergen Ributzka | 009bff2 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 33 | 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 | |
| 37 | STATISTIC(NumStackMapFuncVisited, "Number of functions visited"); |
| 38 | STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped"); |
| 39 | STATISTIC(NumBBsVisited, "Number of basic blocks visited"); |
| 40 | STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap"); |
| 41 | STATISTIC(NumStackMaps, "Number of StackMaps visited"); |
| 42 | |
| 43 | char StackMapLiveness::ID = 0; |
| 44 | char &llvm::StackMapLivenessID = StackMapLiveness::ID; |
| 45 | INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", |
| 46 | "StackMap Liveness Analysis", false, false) |
| 47 | |
| 48 | /// Default construct and initialize the pass. |
| 49 | StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) { |
| 50 | initializeStackMapLivenessPass(*PassRegistry::getPassRegistry()); |
| 51 | } |
| 52 | |
| 53 | /// Tell the pass manager which passes we depend on and what information we |
| 54 | /// preserve. |
| 55 | void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const { |
| 56 | // We preserve all information. |
| 57 | AU.setPreservesAll(); |
| 58 | AU.setPreservesCFG(); |
| 59 | // Default dependencie for all MachineFunction passes. |
| 60 | AU.addRequired<MachineFunctionAnalysis>(); |
| 61 | } |
| 62 | |
| 63 | /// Calculate the liveness information for the given machine function. |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 64 | bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) { |
Juergen Ributzka | 009bff2 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 65 | if (!EnablePatchPointLiveness) |
| 66 | return false; |
| 67 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 68 | DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName() |
| 69 | << " **********\n"); |
| 70 | this->MF = &MF; |
| 71 | TRI = MF.getSubtarget().getRegisterInfo(); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 72 | ++NumStackMapFuncVisited; |
| 73 | |
Juergen Ributzka | 14871f7 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 74 | // Skip this function if there are no patchpoints to process. |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 75 | if (!MF.getFrameInfo()->hasPatchPoint()) { |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 76 | ++NumStackMapFuncSkipped; |
| 77 | return false; |
| 78 | } |
| 79 | return calculateLiveness(); |
| 80 | } |
| 81 | |
| 82 | /// Performs the actual liveness calculation for the function. |
| 83 | bool StackMapLiveness::calculateLiveness() { |
| 84 | bool HasChanged = false; |
| 85 | // For all basic blocks in the function. |
| 86 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 87 | MBBI != MBBE; ++MBBI) { |
| 88 | DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n"); |
| 89 | LiveRegs.init(TRI); |
| 90 | LiveRegs.addLiveOuts(MBBI); |
| 91 | bool HasStackMap = false; |
| 92 | // Reverse iterate over all instructions and add the current live register |
Juergen Ributzka | 14871f7 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 93 | // set to an instruction if we encounter a patchpoint instruction. |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 94 | for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(), |
| 95 | E = MBBI->rend(); I != E; ++I) { |
Juergen Ributzka | 009bff2 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 96 | if (I->getOpcode() == TargetOpcode::PATCHPOINT) { |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 97 | addLiveOutSetToMI(*I); |
| 98 | HasChanged = true; |
| 99 | HasStackMap = true; |
| 100 | ++NumStackMaps; |
| 101 | } |
Andrew Trick | 326c1f68 | 2014-04-04 23:49:35 +0000 | [diff] [blame] | 102 | DEBUG(dbgs() << " " << LiveRegs << " " << *I); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 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); |
Hal Finkel | df87f93 | 2015-01-13 17:47:59 +0000 | [diff] [blame] | 127 | |
| 128 | TRI->adjustStackMapLiveOutMask(Mask); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 129 | return Mask; |
| 130 | } |