[ptr-traits] Add a bunch of includes to provide complete types that are
[oota-llvm.git] / 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 "llvm/Support/Debug.h"
18 #include <map>
19 #include <vector>
20
21 namespace llvm {
22
23 class AsmPrinter;
24 class MCExpr;
25 class MCStreamer;
26
27 /// \brief MI-level patchpoint operands.
28 ///
29 /// MI patchpoint operations take the form:
30 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
31 ///
32 /// IR patchpoint intrinsics do not have the <cc> operand because calling
33 /// convention is part of the subclass data.
34 ///
35 /// SD patchpoint nodes do not have a def operand because it is part of the
36 /// SDValue.
37 ///
38 /// Patchpoints following the anyregcc convention are handled specially. For
39 /// these, the stack map also records the location of the return value and
40 /// arguments.
41 class PatchPointOpers {
42 public:
43   /// Enumerate the meta operands.
44   enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
45
46 private:
47   const MachineInstr *MI;
48   bool HasDef;
49   bool IsAnyReg;
50
51 public:
52   explicit PatchPointOpers(const MachineInstr *MI);
53
54   bool isAnyReg() const { return IsAnyReg; }
55   bool hasDef() const { return HasDef; }
56
57   unsigned getMetaIdx(unsigned Pos = 0) const {
58     assert(Pos < MetaEnd && "Meta operand index out of range.");
59     return (HasDef ? 1 : 0) + Pos;
60   }
61
62   const MachineOperand &getMetaOper(unsigned Pos) {
63     return MI->getOperand(getMetaIdx(Pos));
64   }
65
66   unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
67
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();
73   }
74
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 {
78     if (IsAnyReg)
79       return getArgIdx();
80     return getVarIdx();
81   }
82
83   /// \brief Get the next scratch register operand index.
84   unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
85 };
86
87 /// MI-level Statepoint operands
88 ///
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],
94 ///   [gc values]
95 class StatepointOpers {
96 private:
97   // These values are aboolute offsets into the operands of the statepoint
98   // instruction.
99   enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
100
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 };
104
105 public:
106   explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
107
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;
112   }
113
114   /// Return the ID for the given statepoint.
115   uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
116
117   /// Return the number of patchable bytes the given statepoint should emit.
118   uint32_t getNumPatchBytes() const {
119     return MI->getOperand(NBytesPos).getImm();
120   }
121
122   /// Returns the target of the underlying call.
123   const MachineOperand &getCallTarget() const {
124     return MI->getOperand(CallTargetPos);
125   }
126
127 private:
128   const MachineInstr *MI;
129 };
130
131 class StackMaps {
132 public:
133   struct Location {
134     enum LocationType {
135       Unprocessed,
136       Register,
137       Direct,
138       Indirect,
139       Constant,
140       ConstantIndex
141     };
142     LocationType Type;
143     unsigned Size;
144     unsigned Reg;
145     int64_t Offset;
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) {}
149   };
150
151   struct LiveOutReg {
152     unsigned short Reg;
153     unsigned short DwarfRegNum;
154     unsigned short Size;
155
156     LiveOutReg() : Reg(0), DwarfRegNum(0), Size(0) {}
157     LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
158                unsigned short Size)
159         : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
160   };
161
162   // OpTypes are used to encode information about the following logical
163   // operand (which may consist of several MachineOperands) for the
164   // OpParser.
165   typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
166
167   StackMaps(AsmPrinter &AP);
168
169   void reset() {
170     CSInfos.clear();
171     ConstPool.clear();
172     FnStackSize.clear();
173   }
174
175   /// \brief Generate a stackmap record for a stackmap instruction.
176   ///
177   /// MI must be a raw STACKMAP, not a PATCHPOINT.
178   void recordStackMap(const MachineInstr &MI);
179
180   /// \brief Generate a stackmap record for a patchpoint instruction.
181   void recordPatchPoint(const MachineInstr &MI);
182
183   /// \brief Generate a stackmap record for a statepoint instruction.
184   void recordStatepoint(const MachineInstr &MI);
185
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
188   /// afterwards.
189   void serializeToStackMapSection();
190
191 private:
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;
197
198   struct CallsiteInfo {
199     const MCExpr *CSOffsetExpr;
200     uint64_t ID;
201     LocationVec Locations;
202     LiveOutVec LiveOuts;
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)) {}
208   };
209
210   typedef std::vector<CallsiteInfo> CallsiteInfoList;
211
212   AsmPrinter &AP;
213   CallsiteInfoList CSInfos;
214   ConstantPool ConstPool;
215   FnStackSizeMap FnStackSize;
216
217   MachineInstr::const_mop_iterator
218   parseOperand(MachineInstr::const_mop_iterator MOI,
219                MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
220                LiveOutVec &LiveOuts) const;
221
222   /// \brief Create a live-out register record for the given register @p Reg.
223   LiveOutReg createLiveOutReg(unsigned Reg,
224                               const TargetRegisterInfo *TRI) const;
225
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;
229
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);
239
240   /// \brief Emit the stackmap header.
241   void emitStackmapHeader(MCStreamer &OS);
242
243   /// \brief Emit the function frame record for each function.
244   void emitFunctionFrameRecords(MCStreamer &OS);
245
246   /// \brief Emit the constant pool.
247   void emitConstantPoolEntries(MCStreamer &OS);
248
249   /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call.
250   void emitCallsiteEntries(MCStreamer &OS);
251
252   void print(raw_ostream &OS);
253   void debug() { print(dbgs()); }
254 };
255 }
256
257 #endif