LLVM  9.0.0svn
StackMapLivenessAnalysis.cpp
Go to the documentation of this file.
1 //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the StackMap Liveness analysis pass. The pass calculates
10 // the liveness for each basic block in a function and attaches the register
11 // live-out information to a stackmap or patchpoint intrinsic if present.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Support/Debug.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "stackmaps"
29 
31  "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
32  cl::desc("Enable PatchPoint Liveness Analysis Pass"));
33 
34 STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
35 STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
36 STATISTIC(NumBBsVisited, "Number of basic blocks visited");
37 STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
38 STATISTIC(NumStackMaps, "Number of StackMaps visited");
39 
40 namespace {
41 /// This pass calculates the liveness information for each basic block in
42 /// a function and attaches the register live-out information to a patchpoint
43 /// intrinsic if present.
44 ///
45 /// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
46 /// The pass skips functions that don't have any patchpoint intrinsics. The
47 /// information provided by this pass is optional and not required by the
48 /// aformentioned intrinsic to function.
49 class StackMapLiveness : public MachineFunctionPass {
50  const TargetRegisterInfo *TRI;
51  LivePhysRegs LiveRegs;
52 
53 public:
54  static char ID;
55 
56  /// Default construct and initialize the pass.
57  StackMapLiveness();
58 
59  /// Tell the pass manager which passes we depend on and what
60  /// information we preserve.
61  void getAnalysisUsage(AnalysisUsage &AU) const override;
62 
63  MachineFunctionProperties getRequiredProperties() const override {
66  }
67 
68  /// Calculate the liveness information for the given machine function.
69  bool runOnMachineFunction(MachineFunction &MF) override;
70 
71 private:
72  /// Performs the actual liveness calculation for the function.
73  bool calculateLiveness(MachineFunction &MF);
74 
75  /// Add the current register live set to the instruction.
76  void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
77 
78  /// Create a register mask and initialize it with the registers from
79  /// the register live set.
80  uint32_t *createRegisterMask(MachineFunction &MF) const;
81 };
82 } // namespace
83 
84 char StackMapLiveness::ID = 0;
86 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
87  "StackMap Liveness Analysis", false, false)
88 
89 /// Default construct and initialize the pass.
90 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
92 }
93 
94 /// Tell the pass manager which passes we depend on and what information we
95 /// preserve.
96 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
97  // We preserve all information.
98  AU.setPreservesAll();
99  AU.setPreservesCFG();
101 }
102 
103 /// Calculate the liveness information for the given machine function.
104 bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
106  return false;
107 
108  LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
109  << MF.getName() << " **********\n");
111  ++NumStackMapFuncVisited;
112 
113  // Skip this function if there are no patchpoints to process.
114  if (!MF.getFrameInfo().hasPatchPoint()) {
115  ++NumStackMapFuncSkipped;
116  return false;
117  }
118  return calculateLiveness(MF);
119 }
120 
121 /// Performs the actual liveness calculation for the function.
122 bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
123  bool HasChanged = false;
124  // For all basic blocks in the function.
125  for (auto &MBB : MF) {
126  LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
127  LiveRegs.init(*TRI);
128  // FIXME: This should probably be addLiveOuts().
129  LiveRegs.addLiveOutsNoPristines(MBB);
130  bool HasStackMap = false;
131  // Reverse iterate over all instructions and add the current live register
132  // set to an instruction if we encounter a patchpoint instruction.
133  for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
134  if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
135  addLiveOutSetToMI(MF, *I);
136  HasChanged = true;
137  HasStackMap = true;
138  ++NumStackMaps;
139  }
140  LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << *I);
141  LiveRegs.stepBackward(*I);
142  }
143  ++NumBBsVisited;
144  if (!HasStackMap)
145  ++NumBBsHaveNoStackmap;
146  }
147  return HasChanged;
148 }
149 
150 /// Add the current register live set to the instruction.
151 void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
152  MachineInstr &MI) {
153  uint32_t *Mask = createRegisterMask(MF);
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(MachineFunction &MF) const {
161  // The mask is owned and cleaned up by the Machine Function.
162  uint32_t *Mask = MF.allocateRegMask();
163  for (auto Reg : LiveRegs)
164  Mask[Reg / 32] |= 1U << (Reg % 32);
165 
166  // Give the target a chance to adjust the mask.
167  TRI->adjustStackMapLiveOutMask(Mask);
168 
169  return Mask;
170 }
static cl::opt< bool > EnablePatchPointLiveness("enable-patchpoint-liveness", cl::Hidden, cl::init(true), cl::desc("Enable PatchPoint Liveness Analysis Pass"))
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned Reg
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", "StackMap Liveness Analysis", false, false) StackMapLiveness
Default construct and initialize the pass.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void initializeStackMapLivenessPass(PassRegistry &)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
char & StackMapLivenessID
StackMapLiveness - This pass analyses the register live-out set of stackmap/patchpoint intrinsics and...
MachineOperand class - Representation of each machine instruction operand.
uint32_t * allocateRegMask()
Allocate and initialize a register mask with NumRegister bits.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:63
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:48
#define I(x, y, z)
Definition: MD5.cpp:58
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
IRTranslator LLVM IR MI
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Properties which a MachineFunction may have at a given point in time.