• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/LivePhysRegs.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetSubtargetInfo.h"
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "stackmaps"
31 
32 static cl::opt<bool> EnablePatchPointLiveness(
33     "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
34     cl::desc("Enable PatchPoint Liveness Analysis Pass"));
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 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   const TargetRegisterInfo *TRI;
53   LivePhysRegs LiveRegs;
54 
55 public:
56   static char ID;
57 
58   /// \brief Default construct and initialize the pass.
59   StackMapLiveness();
60 
61   /// \brief Tell the pass manager which passes we depend on and what
62   /// information we preserve.
63   void getAnalysisUsage(AnalysisUsage &AU) const override;
64 
getRequiredProperties() const65   MachineFunctionProperties getRequiredProperties() const override {
66     return MachineFunctionProperties().set(
67         MachineFunctionProperties::Property::AllVRegsAllocated);
68   }
69 
70   /// \brief Calculate the liveness information for the given machine function.
71   bool runOnMachineFunction(MachineFunction &MF) override;
72 
73 private:
74   /// \brief Performs the actual liveness calculation for the function.
75   bool calculateLiveness(MachineFunction &MF);
76 
77   /// \brief Add the current register live set to the instruction.
78   void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
79 
80   /// \brief Create a register mask and initialize it with the registers from
81   /// the register live set.
82   uint32_t *createRegisterMask(MachineFunction &MF) const;
83 };
84 } // namespace
85 
86 char StackMapLiveness::ID = 0;
87 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
88 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
89                 "StackMap Liveness Analysis", false, false)
90 
91 /// Default construct and initialize the pass.
StackMapLiveness()92 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
93   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
94 }
95 
96 /// Tell the pass manager which passes we depend on and what information we
97 /// preserve.
getAnalysisUsage(AnalysisUsage & AU) const98 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
99   // We preserve all information.
100   AU.setPreservesAll();
101   AU.setPreservesCFG();
102   MachineFunctionPass::getAnalysisUsage(AU);
103 }
104 
105 /// Calculate the liveness information for the given machine function.
runOnMachineFunction(MachineFunction & MF)106 bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
107   if (!EnablePatchPointLiveness)
108     return false;
109 
110   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
111                << " **********\n");
112   TRI = MF.getSubtarget().getRegisterInfo();
113   ++NumStackMapFuncVisited;
114 
115   // Skip this function if there are no patchpoints to process.
116   if (!MF.getFrameInfo()->hasPatchPoint()) {
117     ++NumStackMapFuncSkipped;
118     return false;
119   }
120   return calculateLiveness(MF);
121 }
122 
123 /// Performs the actual liveness calculation for the function.
calculateLiveness(MachineFunction & MF)124 bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
125   bool HasChanged = false;
126   // For all basic blocks in the function.
127   for (auto &MBB : MF) {
128     DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
129     LiveRegs.init(TRI);
130     // FIXME: This should probably be addLiveOuts().
131     LiveRegs.addLiveOutsNoPristines(MBB);
132     bool HasStackMap = false;
133     // Reverse iterate over all instructions and add the current live register
134     // set to an instruction if we encounter a patchpoint instruction.
135     for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
136       if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
137         addLiveOutSetToMI(MF, *I);
138         HasChanged = true;
139         HasStackMap = true;
140         ++NumStackMaps;
141       }
142       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
143       LiveRegs.stepBackward(*I);
144     }
145     ++NumBBsVisited;
146     if (!HasStackMap)
147       ++NumBBsHaveNoStackmap;
148   }
149   return HasChanged;
150 }
151 
152 /// Add the current register live set to the instruction.
addLiveOutSetToMI(MachineFunction & MF,MachineInstr & MI)153 void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
154                                          MachineInstr &MI) {
155   uint32_t *Mask = createRegisterMask(MF);
156   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
157   MI.addOperand(MF, MO);
158 }
159 
160 /// Create a register mask and initialize it with the registers from the
161 /// register live set.
createRegisterMask(MachineFunction & MF) const162 uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
163   // The mask is owned and cleaned up by the Machine Function.
164   uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
165   for (auto Reg : LiveRegs)
166     Mask[Reg / 32] |= 1U << (Reg % 32);
167 
168   // Give the target a chance to adjust the mask.
169   TRI->adjustStackMapLiveOutMask(Mask);
170 
171   return Mask;
172 }
173