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"
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 };
47 const MachineInstr *MI;
52 explicit PatchPointOpers(const MachineInstr *MI);
54 bool isAnyReg() const { return IsAnyReg; }
55 bool hasDef() const { return HasDef; }
57 unsigned getMetaIdx(unsigned Pos = 0) const {
58 assert(Pos < MetaEnd && "Meta operand index out of range.");
59 return (HasDef ? 1 : 0) + Pos;
62 const MachineOperand &getMetaOper(unsigned Pos) {
63 return MI->getOperand(getMetaIdx(Pos));
66 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
68 /// Get the operand index of the variable list of non-argument operands.
69 /// These hold the "live state".
70 unsigned getVarIdx() const {
71 return getMetaIdx() + MetaEnd +
72 MI->getOperand(getMetaIdx(NArgPos)).getImm();
75 /// Get the index at which stack map locations will be recorded.
76 /// Arguments are not recorded unless the anyregcc convention is used.
77 unsigned getStackMapStartIdx() const {
83 /// \brief Get the next scratch register operand index.
84 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
87 /// MI-level Statepoint operands
89 /// Statepoint operands take the form:
90 /// <id>, <num patch bytes >, <num call arguments>, <call target>,
91 /// [call arguments], <StackMaps::ConstantOp>, <calling convention>,
92 /// <StackMaps::ConstantOp>, <statepoint flags>,
93 /// <StackMaps::ConstantOp>, <num other args>, [other args],
95 class StatepointOpers {
97 // These values are aboolute offsets into the operands of the statepoint
99 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
101 // These values are relative offests from the start of the statepoint meta
102 // arguments (i.e. the end of the call arguments).
103 enum { CCOffset = 1, FlagsOffset = 3, NumVMSArgsOffset = 5 };
106 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
108 /// Get starting index of non call related arguments
109 /// (calling convention, statepoint flags, vm state and gc state).
110 unsigned getVarIdx() const {
111 return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
114 /// Return the ID for the given statepoint.
115 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
117 /// Return the number of patchable bytes the given statepoint should emit.
118 uint32_t getNumPatchBytes() const {
119 return MI->getOperand(NBytesPos).getImm();
122 /// Returns the target of the underlying call.
123 const MachineOperand &getCallTarget() const {
124 return MI->getOperand(CallTargetPos);
128 const MachineInstr *MI;
146 Location() : Type(Unprocessed), Size(0), Reg(0), Offset(0) {}
147 Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
148 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
153 unsigned short DwarfRegNum;
156 LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
157 LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
159 : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
162 // OpTypes are used to encode information about the following logical
163 // operand (which may consist of several MachineOperands) for the
165 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
167 StackMaps(AsmPrinter &AP);
175 /// \brief Generate a stackmap record for a stackmap instruction.
177 /// MI must be a raw STACKMAP, not a PATCHPOINT.
178 void recordStackMap(const MachineInstr &MI);
180 /// \brief Generate a stackmap record for a patchpoint instruction.
181 void recordPatchPoint(const MachineInstr &MI);
183 /// \brief Generate a stackmap record for a statepoint instruction.
184 void recordStatepoint(const MachineInstr &MI);
186 /// If there is any stack map data, create a stack map section and serialize
187 /// the map info into it. This clears the stack map data structures
189 void serializeToStackMapSection();
192 static const char *WSMP;
193 typedef SmallVector<Location, 8> LocationVec;
194 typedef SmallVector<LiveOutReg, 8> LiveOutVec;
195 typedef MapVector<uint64_t, uint64_t> ConstantPool;
196 typedef MapVector<const MCSymbol *, uint64_t> FnStackSizeMap;
198 struct CallsiteInfo {
199 const MCExpr *CSOffsetExpr;
201 LocationVec Locations;
203 CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {}
204 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
205 LocationVec &&Locations, LiveOutVec &&LiveOuts)
206 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
207 LiveOuts(std::move(LiveOuts)) {}
210 typedef std::vector<CallsiteInfo> CallsiteInfoList;
213 CallsiteInfoList CSInfos;
214 ConstantPool ConstPool;
215 FnStackSizeMap FnStackSize;
217 MachineInstr::const_mop_iterator
218 parseOperand(MachineInstr::const_mop_iterator MOI,
219 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
220 LiveOutVec &LiveOuts) const;
222 /// \brief Create a live-out register record for the given register @p Reg.
223 LiveOutReg createLiveOutReg(unsigned Reg,
224 const TargetRegisterInfo *TRI) const;
226 /// \brief Parse the register live-out mask and return a vector of live-out
227 /// registers that need to be recorded in the stackmap.
228 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
230 /// This should be called by the MC lowering code _immediately_ before
231 /// lowering the MI to an MCInst. It records where the operands for the
232 /// instruction are stored, and outputs a label to record the offset of
233 /// the call from the start of the text section. In special cases (e.g. AnyReg
234 /// calling convention) the return register is also recorded if requested.
235 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
236 MachineInstr::const_mop_iterator MOI,
237 MachineInstr::const_mop_iterator MOE,
238 bool recordResult = false);
240 /// \brief Emit the stackmap header.
241 void emitStackmapHeader(MCStreamer &OS);
243 /// \brief Emit the function frame record for each function.
244 void emitFunctionFrameRecords(MCStreamer &OS);
246 /// \brief Emit the constant pool.
247 void emitConstantPoolEntries(MCStreamer &OS);
249 /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
250 void emitCallsiteEntries(MCStreamer &OS);
252 void print(raw_ostream &OS);
253 void debug() { print(dbgs()); }