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