LLVM 22.0.0git
StackMaps.h
Go to the documentation of this file.
1//===- StackMaps.h - StackMaps ----------------------------------*- C++ -*-===//
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#ifndef LLVM_CODEGEN_STACKMAPS_H
10#define LLVM_CODEGEN_STACKMAPS_H
11
12#include "llvm/ADT/MapVector.h"
15#include "llvm/IR/CallingConv.h"
17#include "llvm/Support/Debug.h"
18#include <algorithm>
19#include <cassert>
20#include <cstdint>
21#include <vector>
22
23namespace llvm {
24
25class AsmPrinter;
26class MCSymbol;
27class MCExpr;
28class MCStreamer;
29class raw_ostream;
30class TargetRegisterInfo;
31
32/// MI-level stackmap operands.
33///
34/// MI stackmap operations take the form:
35/// <id>, <numBytes>, live args...
37public:
38 /// Enumerate the meta operands.
39 enum { IDPos, NBytesPos };
40
41private:
42 const MachineInstr* MI;
43
44public:
45 LLVM_ABI explicit StackMapOpers(const MachineInstr *MI);
46
47 /// Return the ID for the given stackmap
48 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
49
50 /// Return the number of patchable bytes the given stackmap should emit.
52 return MI->getOperand(NBytesPos).getImm();
53 }
54
55 /// Get the operand index of the variable list of non-argument operands.
56 /// These hold the "live state".
57 unsigned getVarIdx() const {
58 // Skip ID, nShadowBytes.
59 return 2;
60 }
61};
62
63/// MI-level patchpoint operands.
64///
65/// MI patchpoint operations take the form:
66/// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
67///
68/// IR patchpoint intrinsics do not have the <cc> operand because calling
69/// convention is part of the subclass data.
70///
71/// SD patchpoint nodes do not have a def operand because it is part of the
72/// SDValue.
73///
74/// Patchpoints following the anyregcc convention are handled specially. For
75/// these, the stack map also records the location of the return value and
76/// arguments.
78public:
79 /// Enumerate the meta operands.
81
82private:
83 const MachineInstr *MI;
84 bool HasDef;
85
86 unsigned getMetaIdx(unsigned Pos = 0) const {
87 assert(Pos < MetaEnd && "Meta operand index out of range.");
88 return (HasDef ? 1 : 0) + Pos;
89 }
90
91 const MachineOperand &getMetaOper(unsigned Pos) const {
92 return MI->getOperand(getMetaIdx(Pos));
93 }
94
95public:
96 LLVM_ABI explicit PatchPointOpers(const MachineInstr *MI);
97
98 bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); }
99 bool hasDef() const { return HasDef; }
100
101 /// Return the ID for the given patchpoint.
102 uint64_t getID() const { return getMetaOper(IDPos).getImm(); }
103
104 /// Return the number of patchable bytes the given patchpoint should emit.
106 return getMetaOper(NBytesPos).getImm();
107 }
108
109 /// Returns the target of the underlying call.
111 return getMetaOper(TargetPos);
112 }
113
114 /// Returns the calling convention
116 return getMetaOper(CCPos).getImm();
117 }
118
119 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
120
121 /// Return the number of call arguments
123 return MI->getOperand(getMetaIdx(NArgPos)).getImm();
124 }
125
126 /// Get the operand index of the variable list of non-argument operands.
127 /// These hold the "live state".
128 unsigned getVarIdx() const {
129 return getMetaIdx() + MetaEnd + getNumCallArgs();
130 }
131
132 /// Get the index at which stack map locations will be recorded.
133 /// Arguments are not recorded unless the anyregcc convention is used.
134 unsigned getStackMapStartIdx() const {
135 if (isAnyReg())
136 return getArgIdx();
137 return getVarIdx();
138 }
139
140 /// Get the next scratch register operand index.
141 LLVM_ABI unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
142};
143
144/// MI-level Statepoint operands
145///
146/// Statepoint operands take the form:
147/// <id>, <num patch bytes >, <num call arguments>, <call target>,
148/// [call arguments...],
149/// <StackMaps::ConstantOp>, <calling convention>,
150/// <StackMaps::ConstantOp>, <statepoint flags>,
151/// <StackMaps::ConstantOp>, <num deopt args>, [deopt args...],
152/// <StackMaps::ConstantOp>, <num gc pointer args>, [gc pointer args...],
153/// <StackMaps::ConstantOp>, <num gc allocas>, [gc allocas args...],
154/// <StackMaps::ConstantOp>, <num entries in gc map>, [base/derived pairs]
155/// base/derived pairs in gc map are logical indices into <gc pointer args>
156/// section.
157/// All gc pointers assigned to VRegs produce new value (in form of MI Def
158/// operand) and are tied to it.
160 // TODO:: we should change the STATEPOINT representation so that CC and
161 // Flags should be part of meta operands, with args and deopt operands, and
162 // gc operands all prefixed by their length and a type code. This would be
163 // much more consistent.
164
165 // These values are absolute offsets into the operands of the statepoint
166 // instruction.
167 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
168
169 // These values are relative offsets from the start of the statepoint meta
170 // arguments (i.e. the end of the call arguments).
171 enum { CCOffset = 1, FlagsOffset = 3, NumDeoptOperandsOffset = 5 };
172
173public:
174 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {
175 NumDefs = MI->getNumDefs();
176 }
177
178 /// Get index of statepoint ID operand.
179 unsigned getIDPos() const { return NumDefs + IDPos; }
180
181 /// Get index of Num Patch Bytes operand.
182 unsigned getNBytesPos() const { return NumDefs + NBytesPos; }
183
184 /// Get index of Num Call Arguments operand.
185 unsigned getNCallArgsPos() const { return NumDefs + NCallArgsPos; }
186
187 /// Get starting index of non call related arguments
188 /// (calling convention, statepoint flags, vm state and gc state).
189 unsigned getVarIdx() const {
190 return MI->getOperand(NumDefs + NCallArgsPos).getImm() + MetaEnd + NumDefs;
191 }
192
193 /// Get index of Calling Convention operand.
194 unsigned getCCIdx() const { return getVarIdx() + CCOffset; }
195
196 /// Get index of Flags operand.
197 unsigned getFlagsIdx() const { return getVarIdx() + FlagsOffset; }
198
199 /// Get index of Number Deopt Arguments operand.
200 unsigned getNumDeoptArgsIdx() const {
201 return getVarIdx() + NumDeoptOperandsOffset;
202 }
203
204 /// Return the ID for the given statepoint.
205 uint64_t getID() const { return MI->getOperand(NumDefs + IDPos).getImm(); }
206
207 /// Return the number of patchable bytes the given statepoint should emit.
209 return MI->getOperand(NumDefs + NBytesPos).getImm();
210 }
211
212 /// Return the target of the underlying call.
214 return MI->getOperand(NumDefs + CallTargetPos);
215 }
216
217 /// Return the calling convention.
219 return MI->getOperand(getCCIdx()).getImm();
220 }
221
222 /// Return the statepoint flags.
223 uint64_t getFlags() const { return MI->getOperand(getFlagsIdx()).getImm(); }
224
226 return MI->getOperand(getNumDeoptArgsIdx()).getImm();
227 }
228
229 /// Get index of number of gc map entries.
231
232 /// Get index of number of gc allocas.
233 LLVM_ABI unsigned getNumAllocaIdx();
234
235 /// Get index of number of GC pointers.
236 LLVM_ABI unsigned getNumGCPtrIdx();
237
238 /// Get index of first GC pointer operand of -1 if there are none.
240
241 /// Get vector of base/derived pairs from statepoint.
242 /// Elements are indices into GC Pointer operand list (logical).
243 /// Returns number of elements in GCMap.
244 LLVM_ABI unsigned
245 getGCPointerMap(SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap);
246
247 /// Return true if Reg is used only in operands which can be folded to
248 /// stack usage.
250
251 /// Return true if Reg is used only in operands of MI which can be folded to
252 /// stack usage and MI is a statepoint instruction.
253 LLVM_ABI static bool isFoldableReg(const MachineInstr *MI, Register Reg);
254
255private:
256 const MachineInstr *MI;
257 unsigned NumDefs;
258};
259
261public:
262 struct Location {
270 };
274 int32_t Offset = 0;
275
276 Location() = default;
278 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
279 };
280
281 struct LiveOutReg {
285
286 LiveOutReg() = default;
289 };
290
291 // OpTypes are used to encode information about the following logical
292 // operand (which may consist of several MachineOperands) for the
293 // OpParser.
294 using OpType = enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp };
295
297
298 /// Get index of next meta operand.
299 /// Similar to parseOperand, but does not actually parses operand meaning.
300 LLVM_ABI static unsigned getNextMetaArgIdx(const MachineInstr *MI,
301 unsigned CurIdx);
302
303 void reset() {
304 CSInfos.clear();
305 ConstPool.clear();
306 FnInfos.clear();
307 }
308
312
316
317 FunctionInfo() = default;
319 };
320
322 const MCExpr *CSOffsetExpr = nullptr;
326
327 CallsiteInfo() = default;
332 };
333
335 using CallsiteInfoList = std::vector<CallsiteInfo>;
336
337 /// Generate a stackmap record for a stackmap instruction.
338 ///
339 /// MI must be a raw STACKMAP, not a PATCHPOINT.
340 LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI);
341
342 /// Generate a stackmap record for a patchpoint instruction.
343 LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI);
344
345 /// Generate a stackmap record for a statepoint instruction.
346 LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI);
347
348 /// If there is any stack map data, create a stack map section and serialize
349 /// the map info into it. This clears the stack map data structures
350 /// afterwards.
352
353 /// Get call site info.
354 CallsiteInfoList &getCSInfos() { return CSInfos; }
355
356 /// Get function info.
357 FnInfoMap &getFnInfos() { return FnInfos; }
358
359private:
360 static const char *WSMP;
361
362 AsmPrinter &AP;
363 CallsiteInfoList CSInfos;
364 ConstantPool ConstPool;
365 FnInfoMap FnInfos;
366
368 parseOperand(MachineInstr::const_mop_iterator MOI,
370 LiveOutVec &LiveOuts);
371
372 /// Specialized parser of statepoint operands.
373 /// They do not directly correspond to StackMap record entries.
374 void parseStatepointOpers(const MachineInstr &MI,
377 LocationVec &Locations, LiveOutVec &LiveOuts);
378
379 /// Create a live-out register record for the given register @p Reg.
380 LiveOutReg createLiveOutReg(unsigned Reg,
381 const TargetRegisterInfo *TRI) const;
382
383 /// Parse the register live-out mask and return a vector of live-out
384 /// registers that need to be recorded in the stackmap.
385 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
386
387 /// Record the locations of the operands of the provided instruction in a
388 /// record keyed by the provided label. For instructions w/AnyReg calling
389 /// convention the return register is also recorded if requested. For
390 /// STACKMAP, and PATCHPOINT the label is expected to immediately *preceed*
391 /// lowering of the MI to MCInsts. For STATEPOINT, it expected to
392 /// immediately *follow*. It's not clear this difference was intentional,
393 /// but it exists today.
394 void recordStackMapOpers(const MCSymbol &L,
395 const MachineInstr &MI, uint64_t ID,
398 bool recordResult = false);
399
400 /// Emit the stackmap header.
401 void emitStackmapHeader(MCStreamer &OS);
402
403 /// Emit the function frame record for each function.
404 void emitFunctionFrameRecords(MCStreamer &OS);
405
406 /// Emit the constant pool.
407 void emitConstantPoolEntries(MCStreamer &OS);
408
409 /// Emit the callsite info for each stackmap/patchpoint intrinsic call.
410 void emitCallsiteEntries(MCStreamer &OS);
411
412 LLVM_ABI void print(raw_ostream &OS);
413 void debug() { print(dbgs()); }
414};
415
416} // end namespace llvm
417
418#endif // LLVM_CODEGEN_STACKMAPS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
raw_pwrite_stream & OS
This file defines the SmallVector class.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:90
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Representation of each machine instruction.
Definition: MachineInstr.h:72
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
void clear()
Definition: MapVector.h:84
MI-level patchpoint operands.
Definition: StackMaps.h:77
uint32_t getNumCallArgs() const
Return the number of call arguments.
Definition: StackMaps.h:122
LLVM_ABI PatchPointOpers(const MachineInstr *MI)
Definition: StackMaps.cpp:61
LLVM_ABI unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
Definition: StackMaps.cpp:76
unsigned getArgIdx() const
Definition: StackMaps.h:119
uint64_t getID() const
Return the ID for the given patchpoint.
Definition: StackMaps.h:102
bool isAnyReg() const
Definition: StackMaps.h:98
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given patchpoint should emit.
Definition: StackMaps.h:105
CallingConv::ID getCallingConv() const
Returns the calling convention.
Definition: StackMaps.h:115
const MachineOperand & getCallTarget() const
Returns the target of the underlying call.
Definition: StackMaps.h:110
unsigned getStackMapStartIdx() const
Get the index at which stack map locations will be recorded.
Definition: StackMaps.h:134
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition: StackMaps.h:128
bool hasDef() const
Definition: StackMaps.h:99
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
MI-level stackmap operands.
Definition: StackMaps.h:36
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given stackmap should emit.
Definition: StackMaps.h:51
uint64_t getID() const
Return the ID for the given stackmap.
Definition: StackMaps.h:48
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition: StackMaps.h:57
static LLVM_ABI unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx)
Get index of next meta operand.
Definition: StackMaps.cpp:170
enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType
Definition: StackMaps.h:294
LLVM_ABI void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it.
Definition: StackMaps.cpp:713
SmallVector< LiveOutReg, 8 > LiveOutVec
Definition: StackMaps.h:310
MapVector< const MCSymbol *, FunctionInfo > FnInfoMap
Definition: StackMaps.h:334
SmallVector< Location, 8 > LocationVec
Definition: StackMaps.h:309
LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
Definition: StackMaps.cpp:560
std::vector< CallsiteInfo > CallsiteInfoList
Definition: StackMaps.h:335
LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
Definition: StackMaps.cpp:539
FnInfoMap & getFnInfos()
Get function info.
Definition: StackMaps.h:357
CallsiteInfoList & getCSInfos()
Get call site info.
Definition: StackMaps.h:354
LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Definition: StackMaps.cpp:529
MI-level Statepoint operands.
Definition: StackMaps.h:159
StatepointOpers(const MachineInstr *MI)
Definition: StackMaps.h:174
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given statepoint should emit.
Definition: StackMaps.h:208
unsigned getIDPos() const
Get index of statepoint ID operand.
Definition: StackMaps.h:179
CallingConv::ID getCallingConv() const
Return the calling convention.
Definition: StackMaps.h:218
unsigned getCCIdx() const
Get index of Calling Convention operand.
Definition: StackMaps.h:194
LLVM_ABI unsigned getGCPointerMap(SmallVectorImpl< std::pair< unsigned, unsigned > > &GCMap)
Get vector of base/derived pairs from statepoint.
Definition: StackMaps.cpp:134
unsigned getNCallArgsPos() const
Get index of Num Call Arguments operand.
Definition: StackMaps.h:185
LLVM_ABI unsigned getNumAllocaIdx()
Get index of number of gc allocas.
Definition: StackMaps.cpp:103
LLVM_ABI unsigned getNumGcMapEntriesIdx()
Get index of number of gc map entries.
Definition: StackMaps.cpp:93
LLVM_ABI int getFirstGCPtrIdx()
Get index of first GC pointer operand of -1 if there are none.
Definition: StackMaps.cpp:124
const MachineOperand & getCallTarget() const
Return the target of the underlying call.
Definition: StackMaps.h:213
unsigned getNumDeoptArgsIdx() const
Get index of Number Deopt Arguments operand.
Definition: StackMaps.h:200
unsigned getNBytesPos() const
Get index of Num Patch Bytes operand.
Definition: StackMaps.h:182
uint64_t getNumDeoptArgs() const
Definition: StackMaps.h:225
uint64_t getFlags() const
Return the statepoint flags.
Definition: StackMaps.h:223
unsigned getFlagsIdx() const
Get index of Flags operand.
Definition: StackMaps.h:197
uint64_t getID() const
Return the ID for the given statepoint.
Definition: StackMaps.h:205
LLVM_ABI bool isFoldableReg(Register Reg) const
Return true if Reg is used only in operands which can be folded to stack usage.
Definition: StackMaps.cpp:148
unsigned getVarIdx() const
Get starting index of non call related arguments (calling convention, statepoint flags,...
Definition: StackMaps.h:189
LLVM_ABI unsigned getNumGCPtrIdx()
Get index of number of GC pointers.
Definition: StackMaps.cpp:113
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition: CallingConv.h:60
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID, LocationVec &&Locations, LiveOutVec &&LiveOuts)
Definition: StackMaps.h:328
const MCExpr * CSOffsetExpr
Definition: StackMaps.h:322
FunctionInfo(uint64_t StackSize)
Definition: StackMaps.h:318
LiveOutReg(uint16_t Reg, uint16_t DwarfRegNum, uint16_t Size)
Definition: StackMaps.h:287
Location(LocationType Type, uint16_t Size, uint16_t Reg, int32_t Offset)
Definition: StackMaps.h:277