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/Support/Debug.h"
26 /// \brief MI-level patchpoint operands.
28 /// MI patchpoint operations take the form:
29 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
31 /// IR patchpoint intrinsics do not have the <cc> operand because calling
32 /// convention is part of the subclass data.
34 /// SD patchpoint nodes do not have a def operand because it is part of the
37 /// Patchpoints following the anyregcc convention are handled specially. For
38 /// these, the stack map also records the location of the return value and
40 class PatchPointOpers {
42 /// Enumerate the meta operands.
43 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
46 const MachineInstr *MI;
51 explicit PatchPointOpers(const MachineInstr *MI);
53 bool isAnyReg() const { return IsAnyReg; }
54 bool hasDef() const { return HasDef; }
56 unsigned getMetaIdx(unsigned Pos = 0) const {
57 assert(Pos < MetaEnd && "Meta operand index out of range.");
58 return (HasDef ? 1 : 0) + Pos;
61 const MachineOperand &getMetaOper(unsigned Pos) {
62 return MI->getOperand(getMetaIdx(Pos));
65 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
67 /// Get the operand index of the variable list of non-argument operands.
68 /// These hold the "live state".
69 unsigned getVarIdx() const {
70 return getMetaIdx() + MetaEnd +
71 MI->getOperand(getMetaIdx(NArgPos)).getImm();
74 /// Get the index at which stack map locations will be recorded.
75 /// Arguments are not recorded unless the anyregcc convention is used.
76 unsigned getStackMapStartIdx() const {
82 /// \brief Get the next scratch register operand index.
83 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
86 /// MI-level Statepoint operands
88 /// Statepoint operands take the form:
89 /// <id>, <num patch bytes >, <num call arguments>, <call target>,
90 /// [call arguments], <StackMaps::ConstantOp>, <calling convention>,
91 /// <StackMaps::ConstantOp>, <statepoint flags>,
92 /// <StackMaps::ConstantOp>, <num other args>, [other args],
94 class StatepointOpers {
96 // These values are aboolute offsets into the operands of the statepoint
98 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
100 // These values are relative offests from the start of the statepoint meta
101 // arguments (i.e. the end of the call arguments).
102 enum { CCOffset = 1, FlagsOffset = 3, NumVMSArgsOffset = 5 };
105 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
107 /// Get starting index of non call related arguments
108 /// (calling convention, statepoint flags, vm state and gc state).
109 unsigned getVarIdx() const {
110 return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
113 /// Return the ID for the given statepoint.
114 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
116 /// Return the number of patchable bytes the given statepoint should emit.
117 uint32_t getNumPatchBytes() const {
118 return MI->getOperand(NBytesPos).getImm();
121 /// Returns the target of the underlying call.
122 const MachineOperand &getCallTarget() const {
123 return MI->getOperand(CallTargetPos);
127 const MachineInstr *MI;
145 Location() : Type(Unprocessed), Size(0), Reg(0), Offset(0) {}
146 Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
147 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
152 unsigned short DwarfRegNum;
155 LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
156 LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
158 : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
161 // OpTypes are used to encode information about the following logical
162 // operand (which may consist of several MachineOperands) for the
164 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
166 StackMaps(AsmPrinter &AP);
174 /// \brief Generate a stackmap record for a stackmap instruction.
176 /// MI must be a raw STACKMAP, not a PATCHPOINT.
177 void recordStackMap(const MachineInstr &MI);
179 /// \brief Generate a stackmap record for a patchpoint instruction.
180 void recordPatchPoint(const MachineInstr &MI);
182 /// \brief Generate a stackmap record for a statepoint instruction.
183 void recordStatepoint(const MachineInstr &MI);
185 /// If there is any stack map data, create a stack map section and serialize
186 /// the map info into it. This clears the stack map data structures
188 void serializeToStackMapSection();
191 static const char *WSMP;
192 typedef SmallVector<Location, 8> LocationVec;
193 typedef SmallVector<LiveOutReg, 8> LiveOutVec;
194 typedef MapVector<uint64_t, uint64_t> ConstantPool;
195 typedef MapVector<const MCSymbol *, uint64_t> FnStackSizeMap;
197 struct CallsiteInfo {
198 const MCExpr *CSOffsetExpr;
200 LocationVec Locations;
202 CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {}
203 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
204 LocationVec &&Locations, LiveOutVec &&LiveOuts)
205 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
206 LiveOuts(std::move(LiveOuts)) {}
209 typedef std::vector<CallsiteInfo> CallsiteInfoList;
212 CallsiteInfoList CSInfos;
213 ConstantPool ConstPool;
214 FnStackSizeMap FnStackSize;
216 MachineInstr::const_mop_iterator
217 parseOperand(MachineInstr::const_mop_iterator MOI,
218 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
219 LiveOutVec &LiveOuts) const;
221 /// \brief Create a live-out register record for the given register @p Reg.
222 LiveOutReg createLiveOutReg(unsigned Reg,
223 const TargetRegisterInfo *TRI) const;
225 /// \brief Parse the register live-out mask and return a vector of live-out
226 /// registers that need to be recorded in the stackmap.
227 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
229 /// This should be called by the MC lowering code _immediately_ before
230 /// lowering the MI to an MCInst. It records where the operands for the
231 /// instruction are stored, and outputs a label to record the offset of
232 /// the call from the start of the text section. In special cases (e.g. AnyReg
233 /// calling convention) the return register is also recorded if requested.
234 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
235 MachineInstr::const_mop_iterator MOI,
236 MachineInstr::const_mop_iterator MOE,
237 bool recordResult = false);
239 /// \brief Emit the stackmap header.
240 void emitStackmapHeader(MCStreamer &OS);
242 /// \brief Emit the function frame record for each function.
243 void emitFunctionFrameRecords(MCStreamer &OS);
245 /// \brief Emit the constant pool.
246 void emitConstantPoolEntries(MCStreamer &OS);
248 /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
249 void emitCallsiteEntries(MCStreamer &OS);
251 void print(raw_ostream &OS);
252 void debug() { print(dbgs()); }