1 //===------------------- StackMaps.h - StackMaps ----------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_CODEGEN_STACKMAPS_H
12 #define LLVM_CODEGEN_STACKMAPS_H
14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/Support/Debug.h"
27 /// \brief MI-level patchpoint operands.
29 /// MI patchpoint operations take the form:
30 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
32 /// IR patchpoint intrinsics do not have the <cc> operand because calling
33 /// convention is part of the subclass data.
35 /// SD patchpoint nodes do not have a def operand because it is part of the
38 /// Patchpoints following the anyregcc convention are handled specially. For
39 /// these, the stack map also records the location of the return value and
41 class PatchPointOpers {
43 /// Enumerate the meta operands.
44 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
46 const MachineInstr *MI;
50 explicit PatchPointOpers(const MachineInstr *MI);
52 bool isAnyReg() const { return IsAnyReg; }
53 bool hasDef() const { return HasDef; }
55 unsigned getMetaIdx(unsigned Pos = 0) const {
56 assert(Pos < MetaEnd && "Meta operand index out of range.");
57 return (HasDef ? 1 : 0) + Pos;
60 const MachineOperand &getMetaOper(unsigned Pos) {
61 return MI->getOperand(getMetaIdx(Pos));
64 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
66 /// Get the operand index of the variable list of non-argument operands.
67 /// These hold the "live state".
68 unsigned getVarIdx() const {
69 return getMetaIdx() + MetaEnd
70 + MI->getOperand(getMetaIdx(NArgPos)).getImm();
73 /// Get the index at which stack map locations will be recorded.
74 /// Arguments are not recorded unless the anyregcc convention is used.
75 unsigned getStackMapStartIdx() const {
81 /// \brief Get the next scratch register operand index.
82 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
85 /// MI-level Statepoint operands
87 /// Statepoint operands take the form:
88 /// <num call arguments>, <call target>, [call arguments],
89 /// <StackMaps::ConstantOp>, <flags>,
90 /// <StackMaps::ConstantOp>, <num other args>, [other args],
92 class StatepointOpers {
100 explicit StatepointOpers(const MachineInstr *MI):
103 /// Get starting index of non call related arguments
104 /// (statepoint flags, vm state and gc state).
105 unsigned getVarIdx() const {
106 return MI->getOperand(NCallArgsPos).getImm() + 2;
109 /// Returns the index of the operand containing the number of non-gc non-call
111 unsigned getNumVMSArgsIdx() const {
112 return getVarIdx() + 3;
115 /// Returns the number of non-gc non-call arguments attached to the
116 /// statepoint. Note that this is the number of arguments, not the number of
117 /// operands required to represent those arguments.
118 unsigned getNumVMSArgs() const {
119 return MI->getOperand(getNumVMSArgsIdx()).getImm();
122 /// Returns the target of the underlying call.
123 const MachineOperand &getCallTarget() const {
124 return MI->getOperand(CallTargetPos);
128 const MachineInstr *MI;
134 enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
136 LocationType LocType;
140 Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
141 Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
142 : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
147 unsigned short RegNo;
150 LiveOutReg() : Reg(0), RegNo(0), Size(0) {}
151 LiveOutReg(unsigned short Reg, unsigned short RegNo, unsigned short Size)
152 : Reg(Reg), RegNo(RegNo), Size(Size) {}
154 void MarkInvalid() { Reg = 0; }
156 // Only sort by the dwarf register number.
157 bool operator< (const LiveOutReg &LO) const { return RegNo < LO.RegNo; }
158 static bool IsInvalid(const LiveOutReg &LO) { return LO.Reg == 0; }
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,
219 LocationVec &Locs, 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()); }