1 //===------------------- StackMaps.h - StackMaps ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CODEGEN_STACKMAPS_H
11 #define LLVM_CODEGEN_STACKMAPS_H
13 #include "llvm/ADT/MapVector.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/MC/MCSymbol.h"
25 /// \brief MI-level stackmap operands.
27 /// MI stackmap operations take the form:
28 /// <id>, <numBytes>, live args...
31 /// Enumerate the meta operands.
32 enum { IDPos, NBytesPos };
35 const MachineInstr* MI;
38 explicit StackMapOpers(const MachineInstr *MI);
40 /// Return the ID for the given stackmap
41 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
43 /// Return the number of patchable bytes the given stackmap should emit.
44 uint32_t getNumPatchBytes() const {
45 return MI->getOperand(NBytesPos).getImm();
48 /// Get the operand index of the variable list of non-argument operands.
49 /// These hold the "live state".
50 unsigned getVarIdx() const {
51 // Skip ID, nShadowBytes.
56 /// \brief MI-level patchpoint operands.
58 /// MI patchpoint operations take the form:
59 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
61 /// IR patchpoint intrinsics do not have the <cc> operand because calling
62 /// convention is part of the subclass data.
64 /// SD patchpoint nodes do not have a def operand because it is part of the
67 /// Patchpoints following the anyregcc convention are handled specially. For
68 /// these, the stack map also records the location of the return value and
70 class PatchPointOpers {
72 /// Enumerate the meta operands.
73 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
76 const MachineInstr *MI;
79 unsigned getMetaIdx(unsigned Pos = 0) const {
80 assert(Pos < MetaEnd && "Meta operand index out of range.");
81 return (HasDef ? 1 : 0) + Pos;
84 const MachineOperand &getMetaOper(unsigned Pos) const {
85 return MI->getOperand(getMetaIdx(Pos));
89 explicit PatchPointOpers(const MachineInstr *MI);
91 bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); }
92 bool hasDef() const { return HasDef; }
94 /// Return the ID for the given patchpoint.
95 uint64_t getID() const { return getMetaOper(IDPos).getImm(); }
97 /// Return the number of patchable bytes the given patchpoint should emit.
98 uint32_t getNumPatchBytes() const {
99 return getMetaOper(NBytesPos).getImm();
102 /// Returns the target of the underlying call.
103 const MachineOperand &getCallTarget() const {
104 return getMetaOper(TargetPos);
107 /// Returns the calling convention
108 CallingConv::ID getCallingConv() const {
109 return getMetaOper(CCPos).getImm();
112 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
114 /// Return the number of call arguments
115 uint32_t getNumCallArgs() const {
116 return MI->getOperand(getMetaIdx(NArgPos)).getImm();
119 /// Get the operand index of the variable list of non-argument operands.
120 /// These hold the "live state".
121 unsigned getVarIdx() const {
122 return getMetaIdx() + MetaEnd + getNumCallArgs();
125 /// Get the index at which stack map locations will be recorded.
126 /// Arguments are not recorded unless the anyregcc convention is used.
127 unsigned getStackMapStartIdx() const {
133 /// \brief Get the next scratch register operand index.
134 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
137 /// MI-level Statepoint operands
139 /// Statepoint operands take the form:
140 /// <id>, <num patch bytes >, <num call arguments>, <call target>,
141 /// [call arguments], <StackMaps::ConstantOp>, <calling convention>,
142 /// <StackMaps::ConstantOp>, <statepoint flags>,
143 /// <StackMaps::ConstantOp>, <num other args>, [other args],
145 class StatepointOpers {
147 // These values are aboolute offsets into the operands of the statepoint
149 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
151 // These values are relative offests from the start of the statepoint meta
152 // arguments (i.e. the end of the call arguments).
153 enum { CCOffset = 1, FlagsOffset = 3, NumVMSArgsOffset = 5 };
156 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
158 /// Get starting index of non call related arguments
159 /// (calling convention, statepoint flags, vm state and gc state).
160 unsigned getVarIdx() const {
161 return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
164 /// Return the ID for the given statepoint.
165 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
167 /// Return the number of patchable bytes the given statepoint should emit.
168 uint32_t getNumPatchBytes() const {
169 return MI->getOperand(NBytesPos).getImm();
172 /// Returns the target of the underlying call.
173 const MachineOperand &getCallTarget() const {
174 return MI->getOperand(CallTargetPos);
178 const MachineInstr *MI;
196 Location() : Type(Unprocessed), Size(0), Reg(0), Offset(0) {}
197 Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
198 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
203 unsigned short DwarfRegNum;
206 LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
207 LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
209 : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
212 // OpTypes are used to encode information about the following logical
213 // operand (which may consist of several MachineOperands) for the
215 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
217 StackMaps(AsmPrinter &AP);
225 /// \brief Generate a stackmap record for a stackmap instruction.
227 /// MI must be a raw STACKMAP, not a PATCHPOINT.
228 void recordStackMap(const MachineInstr &MI);
230 /// \brief Generate a stackmap record for a patchpoint instruction.
231 void recordPatchPoint(const MachineInstr &MI);
233 /// \brief Generate a stackmap record for a statepoint instruction.
234 void recordStatepoint(const MachineInstr &MI);
236 /// If there is any stack map data, create a stack map section and serialize
237 /// the map info into it. This clears the stack map data structures
239 void serializeToStackMapSection();
242 static const char *WSMP;
243 typedef SmallVector<Location, 8> LocationVec;
244 typedef SmallVector<LiveOutReg, 8> LiveOutVec;
245 typedef MapVector<uint64_t, uint64_t> ConstantPool;
247 struct FunctionInfo {
249 uint64_t RecordCount;
250 FunctionInfo() : StackSize(0), RecordCount(1) {}
251 explicit FunctionInfo(uint64_t StackSize) : StackSize(StackSize), RecordCount(1) {}
254 struct CallsiteInfo {
255 const MCExpr *CSOffsetExpr;
257 LocationVec Locations;
259 CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {}
260 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
261 LocationVec &&Locations, LiveOutVec &&LiveOuts)
262 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
263 LiveOuts(std::move(LiveOuts)) {}
266 typedef MapVector<const MCSymbol *, FunctionInfo> FnInfoMap;
267 typedef std::vector<CallsiteInfo> CallsiteInfoList;
270 CallsiteInfoList CSInfos;
271 ConstantPool ConstPool;
274 MachineInstr::const_mop_iterator
275 parseOperand(MachineInstr::const_mop_iterator MOI,
276 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
277 LiveOutVec &LiveOuts) const;
279 /// \brief Create a live-out register record for the given register @p Reg.
280 LiveOutReg createLiveOutReg(unsigned Reg,
281 const TargetRegisterInfo *TRI) const;
283 /// \brief Parse the register live-out mask and return a vector of live-out
284 /// registers that need to be recorded in the stackmap.
285 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
287 /// This should be called by the MC lowering code _immediately_ before
288 /// lowering the MI to an MCInst. It records where the operands for the
289 /// instruction are stored, and outputs a label to record the offset of
290 /// the call from the start of the text section. In special cases (e.g. AnyReg
291 /// calling convention) the return register is also recorded if requested.
292 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
293 MachineInstr::const_mop_iterator MOI,
294 MachineInstr::const_mop_iterator MOE,
295 bool recordResult = false);
297 /// \brief Emit the stackmap header.
298 void emitStackmapHeader(MCStreamer &OS);
300 /// \brief Emit the function frame record for each function.
301 void emitFunctionFrameRecords(MCStreamer &OS);
303 /// \brief Emit the constant pool.
304 void emitConstantPoolEntries(MCStreamer &OS);
306 /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
307 void emitCallsiteEntries(MCStreamer &OS);
309 void print(raw_ostream &OS);
310 void debug() { print(dbgs()); }