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_STACKMAPS
11 #define LLVM_STACKMAPS
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/CodeGen/MachineInstr.h"
23 /// \brief MI-level patchpoint operands.
25 /// MI patchpoint operations take the form:
26 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
28 /// IR patchpoint intrinsics do not have the <cc> operand because calling
29 /// convention is part of the subclass data.
31 /// SD patchpoint nodes do not have a def operand because it is part of the
34 /// Patchpoints following the anyregcc convention are handled specially. For
35 /// these, the stack map also records the location of the return value and
37 class PatchPointOpers {
39 /// Enumerate the meta operands.
40 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
42 const MachineInstr *MI;
46 explicit PatchPointOpers(const MachineInstr *MI);
48 bool isAnyReg() const { return IsAnyReg; }
49 bool hasDef() const { return HasDef; }
51 unsigned getMetaIdx(unsigned Pos = 0) const {
52 assert(Pos < MetaEnd && "Meta operand index out of range.");
53 return (HasDef ? 1 : 0) + Pos;
56 const MachineOperand &getMetaOper(unsigned Pos) {
57 return MI->getOperand(getMetaIdx(Pos));
60 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
62 /// Get the operand index of the variable list of non-argument operands.
63 /// These hold the "live state".
64 unsigned getVarIdx() const {
65 return getMetaIdx() + MetaEnd
66 + MI->getOperand(getMetaIdx(NArgPos)).getImm();
69 /// Get the index at which stack map locations will be recorded.
70 /// Arguments are not recorded unless the anyregcc convention is used.
71 unsigned getStackMapStartIdx() const {
77 /// \brief Get the next scratch register operand index.
78 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
84 enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
90 Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
91 Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
92 : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
95 // Typedef a function pointer for functions that parse sequences of operands
96 // and return a Location, plus a new "next" operand iterator.
97 typedef std::pair<Location, MachineInstr::const_mop_iterator>
98 (*OperandParser)(MachineInstr::const_mop_iterator,
99 MachineInstr::const_mop_iterator, const TargetMachine&);
101 // OpTypes are used to encode information about the following logical
102 // operand (which may consist of several MachineOperands) for the
104 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
106 StackMaps(AsmPrinter &AP, OperandParser OpParser)
107 : AP(AP), OpParser(OpParser) {}
109 /// \brief Generate a stackmap record for a stackmap instruction.
111 /// MI must be a raw STACKMAP, not a PATCHPOINT.
112 void recordStackMap(const MachineInstr &MI);
114 /// \brief Generate a stackmap record for a patchpoint instruction.
115 void recordPatchPoint(const MachineInstr &MI);
117 /// If there is any stack map data, create a stack map section and serialize
118 /// the map info into it. This clears the stack map data structures
120 void serializeToStackMapSection();
123 typedef SmallVector<Location, 8> LocationVec;
125 struct CallsiteInfo {
126 const MCExpr *CSOffsetExpr;
128 LocationVec Locations;
129 CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
130 CallsiteInfo(const MCExpr *CSOffsetExpr, unsigned ID,
131 LocationVec Locations)
132 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations) {}
135 typedef std::vector<CallsiteInfo> CallsiteInfoList;
137 struct ConstantPool {
139 typedef std::map<int64_t, size_t> ConstantsMap;
140 std::vector<int64_t> ConstantsList;
141 ConstantsMap ConstantIndexes;
144 size_t getNumConstants() const { return ConstantsList.size(); }
145 int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
146 size_t getConstantIndex(int64_t ConstVal) {
147 size_t NextIdx = ConstantsList.size();
148 ConstantsMap::const_iterator I =
149 ConstantIndexes.insert(ConstantIndexes.end(),
150 std::make_pair(ConstVal, NextIdx));
151 if (I->second == NextIdx)
152 ConstantsList.push_back(ConstVal);
158 OperandParser OpParser;
159 CallsiteInfoList CSInfos;
160 ConstantPool ConstPool;
162 /// This should be called by the MC lowering code _immediately_ before
163 /// lowering the MI to an MCInst. It records where the operands for the
164 /// instruction are stored, and outputs a label to record the offset of
165 /// the call from the start of the text section. In special cases (e.g. AnyReg
166 /// calling convention) the return register is also recorded if requested.
167 void recordStackMapOpers(const MachineInstr &MI, uint32_t ID,
168 MachineInstr::const_mop_iterator MOI,
169 MachineInstr::const_mop_iterator MOE,
170 bool recordResult = false);
175 #endif // LLVM_STACKMAPS