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_STACKMAPS
12 #define LLVM_STACKMAPS
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/MachineInstr.h"
24 /// \brief MI-level patchpoint operands.
26 /// MI patchpoint operations take the form:
27 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
29 /// IR patchpoint intrinsics do not have the <cc> operand because calling
30 /// convention is part of the subclass data.
32 /// SD patchpoint nodes do not have a def operand because it is part of the
35 /// Patchpoints following the anyregcc convention are handled specially. For
36 /// these, the stack map also records the location of the return value and
38 class PatchPointOpers {
40 /// Enumerate the meta operands.
41 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
43 const MachineInstr *MI;
47 explicit PatchPointOpers(const MachineInstr *MI);
49 bool isAnyReg() const { return IsAnyReg; }
50 bool hasDef() const { return HasDef; }
52 unsigned getMetaIdx(unsigned Pos = 0) const {
53 assert(Pos < MetaEnd && "Meta operand index out of range.");
54 return (HasDef ? 1 : 0) + Pos;
57 const MachineOperand &getMetaOper(unsigned Pos) {
58 return MI->getOperand(getMetaIdx(Pos));
61 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
63 /// Get the operand index of the variable list of non-argument operands.
64 /// These hold the "live state".
65 unsigned getVarIdx() const {
66 return getMetaIdx() + MetaEnd
67 + MI->getOperand(getMetaIdx(NArgPos)).getImm();
70 /// Get the index at which stack map locations will be recorded.
71 /// Arguments are not recorded unless the anyregcc convention is used.
72 unsigned getStackMapStartIdx() const {
78 /// \brief Get the next scratch register operand index.
79 unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
85 enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
91 Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
92 Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
93 : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
101 LiveOutReg() : Reg(0), RegNo(0), Size(0) {}
102 LiveOutReg(unsigned short Reg, unsigned short RegNo, unsigned short Size)
103 : Reg(Reg), RegNo(RegNo), Size(Size) {}
105 void MarkInvalid() { Reg = 0; }
107 // Only sort by the dwarf register number.
108 bool operator< (const LiveOutReg &LO) const { return RegNo < LO.RegNo; }
109 static bool IsInvalid(const LiveOutReg &LO) { return LO.Reg == 0; }
112 // OpTypes are used to encode information about the following logical
113 // operand (which may consist of several MachineOperands) for the
115 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
117 StackMaps(AsmPrinter &AP) : AP(AP) {}
119 /// \brief Generate a stackmap record for a stackmap instruction.
121 /// MI must be a raw STACKMAP, not a PATCHPOINT.
122 void recordStackMap(const MachineInstr &MI);
124 /// \brief Generate a stackmap record for a patchpoint instruction.
125 void recordPatchPoint(const MachineInstr &MI);
127 /// If there is any stack map data, create a stack map section and serialize
128 /// the map info into it. This clears the stack map data structures
130 void serializeToStackMapSection();
133 typedef SmallVector<Location, 8> LocationVec;
134 typedef SmallVector<LiveOutReg, 8> LiveOutVec;
136 struct CallsiteInfo {
137 const MCExpr *CSOffsetExpr;
139 LocationVec Locations;
141 CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
142 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
143 LocationVec &Locations, LiveOutVec &LiveOuts)
144 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations),
145 LiveOuts(LiveOuts) {}
148 typedef std::vector<CallsiteInfo> CallsiteInfoList;
150 struct ConstantPool {
152 typedef std::map<int64_t, size_t> ConstantsMap;
153 std::vector<int64_t> ConstantsList;
154 ConstantsMap ConstantIndexes;
157 size_t getNumConstants() const { return ConstantsList.size(); }
158 int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
159 size_t getConstantIndex(int64_t ConstVal) {
160 size_t NextIdx = ConstantsList.size();
161 ConstantsMap::const_iterator I =
162 ConstantIndexes.insert(ConstantIndexes.end(),
163 std::make_pair(ConstVal, NextIdx));
164 if (I->second == NextIdx)
165 ConstantsList.push_back(ConstVal);
171 CallsiteInfoList CSInfos;
172 ConstantPool ConstPool;
174 MachineInstr::const_mop_iterator
175 parseOperand(MachineInstr::const_mop_iterator MOI,
176 MachineInstr::const_mop_iterator MOE,
177 LocationVec &Locs, LiveOutVec &LiveOuts) const;
179 /// \brief Create a live-out register record for the given register @p Reg.
180 LiveOutReg createLiveOutReg(unsigned Reg, const MCRegisterInfo &MCRI,
181 const TargetRegisterInfo *TRI) const;
183 /// \brief Parse the register live-out mask and return a vector of live-out
184 /// registers that need to be recorded in the stackmap.
185 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
187 /// This should be called by the MC lowering code _immediately_ before
188 /// lowering the MI to an MCInst. It records where the operands for the
189 /// instruction are stored, and outputs a label to record the offset of
190 /// the call from the start of the text section. In special cases (e.g. AnyReg
191 /// calling convention) the return register is also recorded if requested.
192 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
193 MachineInstr::const_mop_iterator MOI,
194 MachineInstr::const_mop_iterator MOE,
195 bool recordResult = false);
200 #endif // LLVM_STACKMAPS