LLVM  4.0.0
StackMaps.h
Go to the documentation of this file.
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"
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...
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.
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.
71 public:
72  /// Enumerate the meta operands.
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.
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
109  return getMetaOper(CCPos).getImm();
110  }
111 
112  unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
113 
114  /// Return the number of call arguments
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]
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.
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 {
191  };
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.
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.
240 
241 private:
242  static const char *WSMP;
243  typedef SmallVector<Location, 8> LocationVec;
244  typedef SmallVector<LiveOutReg, 8> LiveOutVec;
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 
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,
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
unsigned short DwarfRegNum
Definition: StackMaps.h:203
void clear()
Definition: MapVector.h:72
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given patchpoint should emit.
Definition: StackMaps.h:98
StatepointOpers(const MachineInstr *MI)
Definition: StackMaps.h:156
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition: StackMaps.h:121
Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
Definition: StackMaps.h:197
Reg
All possible values of the reg field in the ModR/M byte.
bool hasDef() const
Definition: StackMaps.h:92
PatchPointOpers(const MachineInstr *MI)
Definition: StackMaps.cpp:44
uint64_t getID() const
Return the ID for the given stackmap.
Definition: StackMaps.h:41
int64_t getImm() const
unsigned getArgIdx() const
Definition: StackMaps.h:112
void recordStatepoint(const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
Definition: StackMaps.cpp:381
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
CallingConv::ID getCallingConv() const
Returns the calling convention.
Definition: StackMaps.h:108
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum, unsigned short Size)
Definition: StackMaps.h:207
const MachineOperand & getCallTarget() const
Returns the target of the underlying call.
Definition: StackMaps.h:103
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:67
const MachineOperand & getCallTarget() const
Returns the target of the underlying call.
Definition: StackMaps.h:173
MI-level patchpoint operands.
Definition: StackMaps.h:70
bool isAnyReg() const
Definition: StackMaps.h:91
unsigned getStackMapStartIdx() const
Get the index at which stack map locations will be recorded.
Definition: StackMaps.h:127
void recordPatchPoint(const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
Definition: StackMaps.cpp:361
void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it...
Definition: StackMaps.cpp:531
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given statepoint should emit.
Definition: StackMaps.h:168
StackMapOpers(const MachineInstr *MI)
Definition: StackMaps.cpp:38
MachineOperand class - Representation of each machine instruction operand.
unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
Definition: StackMaps.cpp:59
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void recordStackMap(const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Definition: StackMaps.cpp:352
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given stackmap should emit.
Definition: StackMaps.h:44
Representation of each machine instruction.
Definition: MachineInstr.h:52
MI-level stackmap operands.
Definition: StackMaps.h:29
StackMaps(AsmPrinter &AP)
Definition: StackMaps.cpp:76
uint32_t getNumCallArgs() const
Return the number of call arguments.
Definition: StackMaps.h:115
MI-level Statepoint operands.
Definition: StackMaps.h:145
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition: StackMaps.h:50
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
uint64_t getID() const
Return the ID for the given patchpoint.
Definition: StackMaps.h:95
IRTranslator LLVM IR MI
unsigned getVarIdx() const
Get starting index of non call related arguments (calling convention, statepoint flags, vm state and gc state).
Definition: StackMaps.h:160
const MachineOperand * const_mop_iterator
Definition: MachineInstr.h:293
uint64_t getID() const
Return the ID for the given statepoint.
Definition: StackMaps.h:165