]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/StackMaps.h
Merge bmake-20170510
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / StackMaps.h
1 //===------------------- StackMaps.h - StackMaps ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CODEGEN_STACKMAPS_H
11 #define LLVM_CODEGEN_STACKMAPS_H
12
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 <vector>
18
19 namespace llvm {
20
21 class AsmPrinter;
22 class MCExpr;
23 class MCStreamer;
24
25 /// \brief MI-level stackmap operands.
26 ///
27 /// MI stackmap operations take the form:
28 /// <id>, <numBytes>, live args...
29 class StackMapOpers {
30 public:
31   /// Enumerate the meta operands.
32   enum { IDPos, NBytesPos };
33
34 private:
35   const MachineInstr* MI;
36
37 public:
38   explicit StackMapOpers(const MachineInstr *MI);
39
40   /// Return the ID for the given stackmap
41   uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
42
43   /// Return the number of patchable bytes the given stackmap should emit.
44   uint32_t getNumPatchBytes() const {
45     return MI->getOperand(NBytesPos).getImm();
46   }
47
48   /// Get the operand index of the variable list of non-argument operands.
49   /// These hold the "live state".
50   unsigned getVarIdx() const {
51     // Skip ID, nShadowBytes.
52     return 2;
53   }
54 };
55
56 /// \brief MI-level patchpoint operands.
57 ///
58 /// MI patchpoint operations take the form:
59 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
60 ///
61 /// IR patchpoint intrinsics do not have the <cc> operand because calling
62 /// convention is part of the subclass data.
63 ///
64 /// SD patchpoint nodes do not have a def operand because it is part of the
65 /// SDValue.
66 ///
67 /// Patchpoints following the anyregcc convention are handled specially. For
68 /// these, the stack map also records the location of the return value and
69 /// arguments.
70 class PatchPointOpers {
71 public:
72   /// Enumerate the meta operands.
73   enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
74
75 private:
76   const MachineInstr *MI;
77   bool HasDef;
78
79   unsigned getMetaIdx(unsigned Pos = 0) const {
80     assert(Pos < MetaEnd && "Meta operand index out of range.");
81     return (HasDef ? 1 : 0) + Pos;
82   }
83
84   const MachineOperand &getMetaOper(unsigned Pos) const {
85     return MI->getOperand(getMetaIdx(Pos));
86   }
87
88 public:
89   explicit PatchPointOpers(const MachineInstr *MI);
90
91   bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); }
92   bool hasDef() const { return HasDef; }
93
94   /// Return the ID for the given patchpoint.
95   uint64_t getID() const { return getMetaOper(IDPos).getImm(); }
96
97   /// Return the number of patchable bytes the given patchpoint should emit.
98   uint32_t getNumPatchBytes() const {
99     return getMetaOper(NBytesPos).getImm();
100   }
101
102   /// Returns the target of the underlying call.
103   const MachineOperand &getCallTarget() const {
104     return getMetaOper(TargetPos);
105   }
106
107   /// Returns the calling convention
108   CallingConv::ID getCallingConv() const {
109     return getMetaOper(CCPos).getImm();
110   }
111
112   unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
113
114   /// Return the number of call arguments
115   uint32_t getNumCallArgs() const {
116     return MI->getOperand(getMetaIdx(NArgPos)).getImm();
117   }
118
119   /// Get the operand index of the variable list of non-argument operands.
120   /// These hold the "live state".
121   unsigned getVarIdx() const {
122     return getMetaIdx() + MetaEnd + getNumCallArgs();
123   }
124
125   /// Get the index at which stack map locations will be recorded.
126   /// Arguments are not recorded unless the anyregcc convention is used.
127   unsigned getStackMapStartIdx() const {
128     if (isAnyReg())
129       return getArgIdx();
130     return getVarIdx();
131   }
132
133   /// \brief Get the next scratch register operand index.
134   unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
135 };
136
137 /// MI-level Statepoint operands
138 ///
139 /// Statepoint operands take the form:
140 ///   <id>, <num patch bytes >, <num call arguments>, <call target>,
141 ///   [call arguments], <StackMaps::ConstantOp>, <calling convention>,
142 ///   <StackMaps::ConstantOp>, <statepoint flags>,
143 ///   <StackMaps::ConstantOp>, <num other args>, [other args],
144 ///   [gc values]
145 class StatepointOpers {
146 private:
147   // These values are aboolute offsets into the operands of the statepoint
148   // instruction.
149   enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
150
151   // These values are relative offests from the start of the statepoint meta
152   // arguments (i.e. the end of the call arguments).
153   enum { CCOffset = 1, FlagsOffset = 3, NumVMSArgsOffset = 5 };
154
155 public:
156   explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
157
158   /// Get starting index of non call related arguments
159   /// (calling convention, statepoint flags, vm state and gc state).
160   unsigned getVarIdx() const {
161     return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
162   }
163
164   /// Return the ID for the given statepoint.
165   uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
166
167   /// Return the number of patchable bytes the given statepoint should emit.
168   uint32_t getNumPatchBytes() const {
169     return MI->getOperand(NBytesPos).getImm();
170   }
171
172   /// Returns the target of the underlying call.
173   const MachineOperand &getCallTarget() const {
174     return MI->getOperand(CallTargetPos);
175   }
176
177 private:
178   const MachineInstr *MI;
179 };
180
181 class StackMaps {
182 public:
183   struct Location {
184     enum LocationType {
185       Unprocessed,
186       Register,
187       Direct,
188       Indirect,
189       Constant,
190       ConstantIndex
191     };
192     LocationType Type;
193     unsigned Size;
194     unsigned Reg;
195     int64_t Offset;
196     Location() : Type(Unprocessed), Size(0), Reg(0), Offset(0) {}
197     Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
198         : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
199   };
200
201   struct LiveOutReg {
202     unsigned short Reg;
203     unsigned short DwarfRegNum;
204     unsigned short Size;
205
206     LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
207     LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
208                unsigned short Size)
209         : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
210   };
211
212   // OpTypes are used to encode information about the following logical
213   // operand (which may consist of several MachineOperands) for the
214   // OpParser.
215   typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
216
217   StackMaps(AsmPrinter &AP);
218
219   void reset() {
220     CSInfos.clear();
221     ConstPool.clear();
222     FnInfos.clear();
223   }
224
225   /// \brief Generate a stackmap record for a stackmap instruction.
226   ///
227   /// MI must be a raw STACKMAP, not a PATCHPOINT.
228   void recordStackMap(const MachineInstr &MI);
229
230   /// \brief Generate a stackmap record for a patchpoint instruction.
231   void recordPatchPoint(const MachineInstr &MI);
232
233   /// \brief Generate a stackmap record for a statepoint instruction.
234   void recordStatepoint(const MachineInstr &MI);
235
236   /// If there is any stack map data, create a stack map section and serialize
237   /// the map info into it. This clears the stack map data structures
238   /// afterwards.
239   void serializeToStackMapSection();
240
241 private:
242   static const char *WSMP;
243   typedef SmallVector<Location, 8> LocationVec;
244   typedef SmallVector<LiveOutReg, 8> LiveOutVec;
245   typedef MapVector<uint64_t, uint64_t> ConstantPool;
246
247   struct FunctionInfo {
248     uint64_t StackSize;
249     uint64_t RecordCount;
250     FunctionInfo() : StackSize(0), RecordCount(1) {}
251     explicit FunctionInfo(uint64_t StackSize) : StackSize(StackSize), RecordCount(1) {}
252   };
253
254   struct CallsiteInfo {
255     const MCExpr *CSOffsetExpr;
256     uint64_t ID;
257     LocationVec Locations;
258     LiveOutVec LiveOuts;
259     CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {}
260     CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
261                  LocationVec &&Locations, LiveOutVec &&LiveOuts)
262         : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
263           LiveOuts(std::move(LiveOuts)) {}
264   };
265
266   typedef MapVector<const MCSymbol *, FunctionInfo> FnInfoMap;
267   typedef std::vector<CallsiteInfo> CallsiteInfoList;
268
269   AsmPrinter &AP;
270   CallsiteInfoList CSInfos;
271   ConstantPool ConstPool;
272   FnInfoMap FnInfos;
273
274   MachineInstr::const_mop_iterator
275   parseOperand(MachineInstr::const_mop_iterator MOI,
276                MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
277                LiveOutVec &LiveOuts) const;
278
279   /// \brief Create a live-out register record for the given register @p Reg.
280   LiveOutReg createLiveOutReg(unsigned Reg,
281                               const TargetRegisterInfo *TRI) const;
282
283   /// \brief Parse the register live-out mask and return a vector of live-out
284   /// registers that need to be recorded in the stackmap.
285   LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
286
287   /// This should be called by the MC lowering code _immediately_ before
288   /// lowering the MI to an MCInst. It records where the operands for the
289   /// instruction are stored, and outputs a label to record the offset of
290   /// the call from the start of the text section. In special cases (e.g. AnyReg
291   /// calling convention) the return register is also recorded if requested.
292   void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
293                            MachineInstr::const_mop_iterator MOI,
294                            MachineInstr::const_mop_iterator MOE,
295                            bool recordResult = false);
296
297   /// \brief Emit the stackmap header.
298   void emitStackmapHeader(MCStreamer &OS);
299
300   /// \brief Emit the function frame record for each function.
301   void emitFunctionFrameRecords(MCStreamer &OS);
302
303   /// \brief Emit the constant pool.
304   void emitConstantPoolEntries(MCStreamer &OS);
305
306   /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
307   void emitCallsiteEntries(MCStreamer &OS);
308
309   void print(raw_ostream &OS);
310   void debug() { print(dbgs()); }
311 };
312 }
313
314 #endif