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 patchpoint operands.
27 /// MI patchpoint operations take the form:
28 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
30 /// IR patchpoint intrinsics do not have the <cc> operand because calling
31 /// convention is part of the subclass data.
33 /// SD patchpoint nodes do not have a def operand because it is part of the
36 /// Patchpoints following the anyregcc convention are handled specially. For
37 /// these, the stack map also records the location of the return value and
39 class PatchPointOpers {
41 /// Enumerate the meta operands.
42 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
45 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 /// <id>, <num patch bytes >, <num call arguments>, <call target>,
89 /// [call arguments], <StackMaps::ConstantOp>, <calling convention>,
90 /// <StackMaps::ConstantOp>, <statepoint flags>,
91 /// <StackMaps::ConstantOp>, <num other args>, [other args],
93 class StatepointOpers {
95 // These values are aboolute offsets into the operands of the statepoint
97 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
99 // These values are relative offests from the start of the statepoint meta
100 // arguments (i.e. the end of the call arguments).
101 enum { CCOffset = 1, FlagsOffset = 3, NumVMSArgsOffset = 5 };
104 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
106 /// Get starting index of non call related arguments
107 /// (calling convention, statepoint flags, vm state and gc state).
108 unsigned getVarIdx() const {
109 return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
112 /// Return the ID for the given statepoint.
113 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
115 /// Return the number of patchable bytes the given statepoint should emit.
116 uint32_t getNumPatchBytes() const {
117 return MI->getOperand(NBytesPos).getImm();
120 /// Returns the target of the underlying call.
121 const MachineOperand &getCallTarget() const {
122 return MI->getOperand(CallTargetPos);
126 const MachineInstr *MI;
144 Location() : Type(Unprocessed), Size(0), Reg(0), Offset(0) {}
145 Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
146 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
151 unsigned short DwarfRegNum;
154 LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
155 LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
157 : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
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, LocationVec &Locs,
218 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);
250 void print(raw_ostream &OS);
251 void debug() { print(dbgs()); }