Juergen Ributzka | aaecc0f | 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 | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LivePhysRegs.h" |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | eef4a33 | 2015-03-23 18:45:56 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "stackmaps" |
| 30 | |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 31 | static cl::opt<bool> EnablePatchPointLiveness( |
| 32 | "enable-patchpoint-liveness", cl::Hidden, cl::init(true), |
| 33 | cl::desc("Enable PatchPoint Liveness Analysis Pass")); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 41 | namespace { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 42 | /// This pass calculates the liveness information for each basic block in |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 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 | class StackMapLiveness : public MachineFunctionPass { |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 51 | const TargetRegisterInfo *TRI; |
| 52 | LivePhysRegs LiveRegs; |
| 53 | |
| 54 | public: |
| 55 | static char ID; |
| 56 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 57 | /// Default construct and initialize the pass. |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 58 | StackMapLiveness(); |
| 59 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 60 | /// Tell the pass manager which passes we depend on and what |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 61 | /// information we preserve. |
| 62 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 63 | |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 64 | MachineFunctionProperties getRequiredProperties() const override { |
| 65 | return MachineFunctionProperties().set( |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 66 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 69 | /// Calculate the liveness information for the given machine function. |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 70 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 71 | |
| 72 | private: |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 73 | /// Performs the actual liveness calculation for the function. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 74 | bool calculateLiveness(MachineFunction &MF); |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 75 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 76 | /// Add the current register live set to the instruction. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 77 | void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI); |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 78 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 79 | /// Create a register mask and initialize it with the registers from |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 80 | /// the register live set. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 81 | uint32_t *createRegisterMask(MachineFunction &MF) const; |
Benjamin Kramer | 1989d62 | 2015-03-24 13:20:54 +0000 | [diff] [blame] | 82 | }; |
| 83 | } // namespace |
| 84 | |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 85 | char StackMapLiveness::ID = 0; |
| 86 | char &llvm::StackMapLivenessID = StackMapLiveness::ID; |
| 87 | INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", |
| 88 | "StackMap Liveness Analysis", false, false) |
| 89 | |
| 90 | /// Default construct and initialize the pass. |
| 91 | StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) { |
| 92 | initializeStackMapLivenessPass(*PassRegistry::getPassRegistry()); |
| 93 | } |
| 94 | |
| 95 | /// Tell the pass manager which passes we depend on and what information we |
| 96 | /// preserve. |
| 97 | void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const { |
| 98 | // We preserve all information. |
| 99 | AU.setPreservesAll(); |
| 100 | AU.setPreservesCFG(); |
Juergen Ributzka | b062a9b | 2015-07-07 02:05:18 +0000 | [diff] [blame] | 101 | MachineFunctionPass::getAnalysisUsage(AU); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// Calculate the liveness information for the given machine function. |
David Blaikie | 7610ba7 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 105 | bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) { |
Juergen Ributzka | b1b6d10 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 106 | if (!EnablePatchPointLiveness) |
| 107 | return false; |
| 108 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 109 | LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " |
| 110 | << MF.getName() << " **********\n"); |
David Blaikie | 7610ba7 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 111 | TRI = MF.getSubtarget().getRegisterInfo(); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 112 | ++NumStackMapFuncVisited; |
| 113 | |
Juergen Ributzka | 307a644 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 114 | // Skip this function if there are no patchpoints to process. |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 115 | if (!MF.getFrameInfo().hasPatchPoint()) { |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 116 | ++NumStackMapFuncSkipped; |
| 117 | return false; |
| 118 | } |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 119 | return calculateLiveness(MF); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /// Performs the actual liveness calculation for the function. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 123 | bool StackMapLiveness::calculateLiveness(MachineFunction &MF) { |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 124 | bool HasChanged = false; |
| 125 | // For all basic blocks in the function. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 126 | for (auto &MBB : MF) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 127 | LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n"); |
Matthias Braun | f3e629e | 2016-12-08 00:15:51 +0000 | [diff] [blame] | 128 | LiveRegs.init(*TRI); |
Matthias Braun | b4756d6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 129 | // FIXME: This should probably be addLiveOuts(). |
Matthias Braun | 02073cb | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 130 | LiveRegs.addLiveOutsNoPristines(MBB); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 131 | bool HasStackMap = false; |
| 132 | // Reverse iterate over all instructions and add the current live register |
Juergen Ributzka | 307a644 | 2014-06-26 23:39:44 +0000 | [diff] [blame] | 133 | // set to an instruction if we encounter a patchpoint instruction. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 134 | for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) { |
Juergen Ributzka | b1b6d10 | 2014-06-26 23:39:52 +0000 | [diff] [blame] | 135 | if (I->getOpcode() == TargetOpcode::PATCHPOINT) { |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 136 | addLiveOutSetToMI(MF, *I); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 137 | HasChanged = true; |
| 138 | HasStackMap = true; |
| 139 | ++NumStackMaps; |
| 140 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 141 | LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << *I); |
Juergen Ributzka | aaecc0f | 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. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 152 | void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF, |
| 153 | MachineInstr &MI) { |
| 154 | uint32_t *Mask = createRegisterMask(MF); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 155 | MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask); |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 156 | MI.addOperand(MF, MO); |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /// Create a register mask and initialize it with the registers from the |
| 160 | /// register live set. |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 161 | uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const { |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 162 | // The mask is owned and cleaned up by the Machine Function. |
Matthias Braun | ee571f2 | 2018-07-26 00:27:47 +0000 | [diff] [blame] | 163 | uint32_t *Mask = MF.allocateRegMask(); |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 164 | for (auto Reg : LiveRegs) |
| 165 | Mask[Reg / 32] |= 1U << (Reg % 32); |
Hal Finkel | 5e50885 | 2015-01-13 17:47:59 +0000 | [diff] [blame] | 166 | |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 167 | // Give the target a chance to adjust the mask. |
Hal Finkel | 5e50885 | 2015-01-13 17:47:59 +0000 | [diff] [blame] | 168 | TRI->adjustStackMapLiveOutMask(Mask); |
Juergen Ributzka | 083b791 | 2015-07-07 02:05:15 +0000 | [diff] [blame] | 169 | |
Juergen Ributzka | aaecc0f | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 170 | return Mask; |
| 171 | } |