LLVM  13.0.0git
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"
22 #include "llvm/InitializePasses.h"
24 #include "llvm/Support/Debug.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "stackmaps"
30 
32  "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
33  cl::desc("Enable PatchPoint Liveness Analysis Pass"));
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 
41 namespace {
42 /// This pass calculates the liveness information for each basic block in
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 {
51  const TargetRegisterInfo *TRI;
52  LivePhysRegs LiveRegs;
53 
54 public:
55  static char ID;
56 
57  /// Default construct and initialize the pass.
58  StackMapLiveness();
59 
60  /// Tell the pass manager which passes we depend on and what
61  /// information we preserve.
62  void getAnalysisUsage(AnalysisUsage &AU) const override;
63 
64  MachineFunctionProperties getRequiredProperties() const override {
67  }
68 
69  /// Calculate the liveness information for the given machine function.
70  bool runOnMachineFunction(MachineFunction &MF) override;
71 
72 private:
73  /// Performs the actual liveness calculation for the function.
74  bool calculateLiveness(MachineFunction &MF);
75 
76  /// Add the current register live set to the instruction.
77  void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
78 
79  /// Create a register mask and initialize it with the registers from
80  /// the register live set.
81  uint32_t *createRegisterMask(MachineFunction &MF) const;
82 };
83 } // namespace
84 
85 char StackMapLiveness::ID = 0;
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) {
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();
102 }
103 
104 /// Calculate the liveness information for the given machine function.
105 bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
107  return false;
108 
109  LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
110  << MF.getName() << " **********\n");
112  ++NumStackMapFuncVisited;
113 
114  // Skip this function if there are no patchpoints to process.
115  if (!MF.getFrameInfo().hasPatchPoint()) {
116  ++NumStackMapFuncSkipped;
117  return false;
118  }
119  return calculateLiveness(MF);
120 }
121 
122 /// Performs the actual liveness calculation for the function.
123 bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
124  bool HasChanged = false;
125  // For all basic blocks in the function.
126  for (auto &MBB : MF) {
127  LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
128  LiveRegs.init(*TRI);
129  // FIXME: This should probably be addLiveOuts().
130  LiveRegs.addLiveOutsNoPristines(MBB);
131  bool HasStackMap = false;
132  // Reverse iterate over all instructions and add the current live register
133  // set to an instruction if we encounter a patchpoint instruction.
134  for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
135  if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
136  addLiveOutSetToMI(MF, *I);
137  HasChanged = true;
138  HasStackMap = true;
139  ++NumStackMaps;
140  }
141  LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << *I);
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(MachineFunction &MF,
153  MachineInstr &MI) {
154  uint32_t *Mask = createRegisterMask(MF);
156  MI.addOperand(MF, MO);
157 }
158 
159 /// Create a register mask and initialize it with the registers from the
160 /// register live set.
161 uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
162  // The mask is owned and cleaned up by the Machine Function.
163  uint32_t *Mask = MF.allocateRegMask();
164  for (auto Reg : LiveRegs)
165  Mask[Reg / 32] |= 1U << (Reg % 32);
166 
167  // Give the target a chance to adjust the mask.
169 
170  return Mask;
171 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::MachineFunction::allocateRegMask
uint32_t * allocateRegMask()
Allocate and initialize a register mask with NumRegister bits.
Definition: MachineFunction.cpp:521
Statistic.h
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::LivePhysRegs
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:48
llvm::TargetSubtargetInfo::getRegisterInfo
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Definition: TargetSubtargetInfo.h:124
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:231
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:111
llvm::BitmaskEnumDetail::Mask
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
INITIALIZE_PASS
INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness", "StackMap Liveness Analysis", false, false) StackMapLiveness
Default construct and initialize the pass.
Definition: StackMapLivenessAnalysis.cpp:87
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:169
llvm::MachineBasicBlock::rend
reverse_iterator rend()
Definition: MachineBasicBlock.h:278
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::StackMapLivenessID
char & StackMapLivenessID
StackMapLiveness - This pass analyses the register live-out set of stackmap/patchpoint intrinsics and...
Definition: StackMapLivenessAnalysis.cpp:86
llvm::MachineFunctionProperties::Property::NoVRegs
@ NoVRegs
Passes.h
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:622
llvm::cl::opt< bool >
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
MachineFunctionPass.h
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:541
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:638
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::MachineFrameInfo::hasPatchPoint
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
Definition: MachineFrameInfo.h:384
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::MachineBasicBlock::rbegin
reverse_iterator rbegin()
Definition: MachineBasicBlock.h:272
uint32_t
TargetSubtargetInfo.h
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
MachineFrameInfo.h
EnablePatchPointLiveness
static cl::opt< bool > EnablePatchPointLiveness("enable-patchpoint-liveness", cl::Hidden, cl::init(true), cl::desc("Enable PatchPoint Liveness Analysis Pass"))
llvm::initializeStackMapLivenessPass
void initializeStackMapLivenessPass(PassRegistry &)
llvm::TargetRegisterInfo::adjustStackMapLiveOutMask
virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const
Prior to adding the live-out mask to a stackmap or patchpoint instruction, provide the target the opp...
Definition: TargetRegisterInfo.h:571
llvm::cl::desc
Definition: CommandLine.h:414
raw_ostream.h
MachineFunction.h
InitializePasses.h
llvm::MachineBasicBlock::getName
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Definition: MachineBasicBlock.cpp:314
Debug.h
llvm::MachineOperand::CreateRegLiveOut
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
Definition: MachineOperand.h:892
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
LivePhysRegs.h