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