LLVM 23.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"
22#include "llvm/Pass.h"
24#include "llvm/Support/Debug.h"
26
27using 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
35STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
36STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
37STATISTIC(NumBBsVisited, "Number of basic blocks visited");
38STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
39STATISTIC(NumStackMaps, "Number of StackMaps visited");
40
41namespace {
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.
50class StackMapLiveness : public MachineFunctionPass {
51 const TargetRegisterInfo *TRI = nullptr;
53
54public:
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 {
65 return MachineFunctionProperties().setNoVRegs();
66 }
67
68 /// Calculate the liveness information for the given machine function.
69 bool runOnMachineFunction(MachineFunction &MF) override;
70
71private:
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
84char StackMapLiveness::ID = 0;
85char &llvm::StackMapLivenessID = StackMapLiveness::ID;
86INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
87 "StackMap Liveness Analysis", false, false)
88
89/// Default construct and initialize the pass.
90StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {}
91
92/// Tell the pass manager which passes we depend on and what information we
93/// preserve.
94void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
95 // We preserve all information.
96 AU.setPreservesAll();
97 AU.setPreservesCFG();
99}
100
101/// Calculate the liveness information for the given machine function.
102bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
104 return false;
105
107 ++NumStackMapFuncVisited;
108
109 // Skip this function if there are no patchpoints to process.
110 if (!MF.getFrameInfo().hasPatchPoint()) {
111 ++NumStackMapFuncSkipped;
112 return false;
113 }
114 return calculateLiveness(MF);
115}
116
117/// Performs the actual liveness calculation for the function.
118bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
119 LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
120 << MF.getName() << " **********\n");
121 bool HasChanged = false;
122 // For all basic blocks in the function.
123 for (auto &MBB : MF) {
124 LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
125 LiveRegs.init(*TRI);
126 LiveRegs.addLiveOuts(MBB);
127 bool HasStackMap = false;
128 // Reverse iterate over all instructions and add the current live register
129 // set to an instruction if we encounter a patchpoint instruction.
130 for (MachineInstr &MI : llvm::reverse(MBB)) {
131 if (MI.getOpcode() == TargetOpcode::PATCHPOINT) {
132 addLiveOutSetToMI(MF, MI);
133 HasChanged = true;
134 HasStackMap = true;
135 ++NumStackMaps;
136 }
137 LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << MI);
138 LiveRegs.stepBackward(MI);
139 }
140 ++NumBBsVisited;
141 if (!HasStackMap)
142 ++NumBBsHaveNoStackmap;
143 }
144 return HasChanged;
145}
146
147/// Add the current register live set to the instruction.
148void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
149 MachineInstr &MI) {
150 uint32_t *Mask = createRegisterMask(MF);
151 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
152 MI.addOperand(MF, MO);
153}
154
155/// Create a register mask and initialize it with the registers from the
156/// register live set.
157uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
158 // The mask is owned and cleaned up by the Machine Function.
159 uint32_t *Mask = MF.allocateRegMask();
160 for (auto Reg : LiveRegs)
161 Mask[Reg / 32] |= 1U << (Reg % 32);
162
163 // Give the target a chance to adjust the mask.
164 TRI->adjustStackMapLiveOutMask(Mask);
165
166 return Mask;
167}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
Register Reg
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static cl::opt< bool > EnablePatchPointLiveness("enable-patchpoint-liveness", cl::Hidden, cl::init(true), cl::desc("Enable PatchPoint Liveness Analysis Pass"))
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
void setPreservesAll()
Set by analyses that do not transform their input at all.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
uint32_t * allocateRegMask()
Allocate and initialize a register mask with NumRegister bits.
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
constexpr 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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI char & StackMapLivenessID
StackMapLiveness - This pass analyses the register live-out set of stackmap/patchpoint intrinsics and...