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"
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 };
45 const MachineInstr *MI;
49 explicit PatchPointOpers(const MachineInstr *MI);
51 bool isAnyReg() const { return IsAnyReg; }
52 bool hasDef() const { return HasDef; }
54 unsigned getMetaIdx(unsigned Pos = 0) const {
55 assert(Pos < MetaEnd && "Meta operand index out of range.");
56 return (HasDef ? 1 : 0) + Pos;
59 const MachineOperand &getMetaOper(unsigned Pos) {
60 return MI->getOperand(getMetaIdx(Pos));
63 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
65 /// Get the operand index of the variable list of non-argument operands.
66 /// These hold the "live state".
67 unsigned getVarIdx() const {
68 return getMetaIdx() + MetaEnd
69 + MI->getOperand(getMetaIdx(NArgPos)).getImm();
72 /// Get the index at which stack map locations will be recorded.
73 /// Arguments are not recorded unless the anyregcc convention is used.
74 unsigned getStackMapStartIdx() const {
80 /// \brief Get the next scratch register operand index.
81 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
84 /// MI-level Statepoint operands
86 /// Statepoint operands take the form:
87 /// <num call arguments>, <call target>, [call arguments],
88 /// <StackMaps::ConstantOp>, <flags>,
89 /// <StackMaps::ConstantOp>, <num other args>, [other args],
91 class StatepointOpers {
99 explicit StatepointOpers(const MachineInstr *MI):
102 /// Get starting index of non call related arguments
103 /// (statepoint flags, vm state and gc state).
104 unsigned getVarIdx() const {
105 return MI->getOperand(NCallArgsPos).getImm() + 2;
108 /// Returns the index of the operand containing the number of non-gc non-call
110 unsigned getNumVMSArgsIdx() const {
111 return getVarIdx() + 3;
114 /// Returns the number of non-gc non-call arguments attached to the
115 /// statepoint. Note that this is the number of arguments, not the number of
116 /// operands required to represent those arguments.
117 unsigned getNumVMSArgs() const {
118 return MI->getOperand(getNumVMSArgsIdx()).getImm();
121 /// Returns the target of the underlying call.
122 const MachineOperand &getCallTarget() const {
123 return MI->getOperand(CallTargetPos);
127 const MachineInstr *MI;
133 enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
135 LocationType LocType;
139 Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
140 Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
141 : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
146 unsigned short RegNo;
149 LiveOutReg() : Reg(0), RegNo(0), Size(0) {}
150 LiveOutReg(unsigned short Reg, unsigned short RegNo, unsigned short Size)
151 : Reg(Reg), RegNo(RegNo), Size(Size) {}
153 void MarkInvalid() { Reg = 0; }
155 // Only sort by the dwarf register number.
156 bool operator< (const LiveOutReg &LO) const { return RegNo < LO.RegNo; }
157 static bool IsInvalid(const LiveOutReg &LO) { return LO.Reg == 0; }
160 // OpTypes are used to encode information about the following logical
161 // operand (which may consist of several MachineOperands) for the
163 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
165 StackMaps(AsmPrinter &AP);
173 /// \brief Generate a stackmap record for a stackmap instruction.
175 /// MI must be a raw STACKMAP, not a PATCHPOINT.
176 void recordStackMap(const MachineInstr &MI);
178 /// \brief Generate a stackmap record for a patchpoint instruction.
179 void recordPatchPoint(const MachineInstr &MI);
181 /// \brief Generate a stackmap record for a statepoint instruction.
182 void recordStatepoint(const MachineInstr &MI);
184 /// If there is any stack map data, create a stack map section and serialize
185 /// the map info into it. This clears the stack map data structures
187 void serializeToStackMapSection();
190 static const char *WSMP;
191 typedef SmallVector<Location, 8> LocationVec;
192 typedef SmallVector<LiveOutReg, 8> LiveOutVec;
193 typedef MapVector<uint64_t, uint64_t> ConstantPool;
194 typedef MapVector<const MCSymbol *, uint64_t> FnStackSizeMap;
196 struct CallsiteInfo {
197 const MCExpr *CSOffsetExpr;
199 LocationVec Locations;
201 CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {}
202 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
203 LocationVec &&Locations, LiveOutVec &&LiveOuts)
204 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
205 LiveOuts(std::move(LiveOuts)) {}
208 typedef std::vector<CallsiteInfo> CallsiteInfoList;
211 CallsiteInfoList CSInfos;
212 ConstantPool ConstPool;
213 FnStackSizeMap FnStackSize;
215 MachineInstr::const_mop_iterator
216 parseOperand(MachineInstr::const_mop_iterator MOI,
217 MachineInstr::const_mop_iterator MOE,
218 LocationVec &Locs, LiveOutVec &LiveOuts) const;
220 /// \brief Create a live-out register record for the given register @p Reg.
221 LiveOutReg createLiveOutReg(unsigned Reg,
222 const TargetRegisterInfo *TRI) const;
224 /// \brief Parse the register live-out mask and return a vector of live-out
225 /// registers that need to be recorded in the stackmap.
226 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
228 /// This should be called by the MC lowering code _immediately_ before
229 /// lowering the MI to an MCInst. It records where the operands for the
230 /// instruction are stored, and outputs a label to record the offset of
231 /// the call from the start of the text section. In special cases (e.g. AnyReg
232 /// calling convention) the return register is also recorded if requested.
233 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
234 MachineInstr::const_mop_iterator MOI,
235 MachineInstr::const_mop_iterator MOE,
236 bool recordResult = false);
238 /// \brief Emit the stackmap header.
239 void emitStackmapHeader(MCStreamer &OS);
241 /// \brief Emit the function frame record for each function.
242 void emitFunctionFrameRecords(MCStreamer &OS);
244 /// \brief Emit the constant pool.
245 void emitConstantPoolEntries(MCStreamer &OS);
247 /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
248 void emitCallsiteEntries(MCStreamer &OS, const TargetRegisterInfo *TRI);