LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_STACKMAPS
11 #define LLVM_STACKMAPS
12 
13 #include "llvm/ADT/SmallVector.h"
15 #include <map>
16 #include <vector>
17 
18 namespace llvm {
19 
20 class AsmPrinter;
21 class MCExpr;
22 
23 /// \brief MI-level patchpoint operands.
24 ///
25 /// MI patchpoint operations take the form:
26 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
27 ///
28 /// IR patchpoint intrinsics do not have the <cc> operand because calling
29 /// convention is part of the subclass data.
30 ///
31 /// SD patchpoint nodes do not have a def operand because it is part of the
32 /// SDValue.
33 ///
34 /// Patchpoints following the anyregcc convention are handled specially. For
35 /// these, the stack map also records the location of the return value and
36 /// arguments.
38 public:
39  /// Enumerate the meta operands.
41 private:
42  const MachineInstr *MI;
43  bool HasDef;
44  bool IsAnyReg;
45 public:
46  explicit PatchPointOpers(const MachineInstr *MI);
47 
48  bool isAnyReg() const { return IsAnyReg; }
49  bool hasDef() const { return HasDef; }
50 
51  unsigned getMetaIdx(unsigned Pos = 0) const {
52  assert(Pos < MetaEnd && "Meta operand index out of range.");
53  return (HasDef ? 1 : 0) + Pos;
54  }
55 
56  const MachineOperand &getMetaOper(unsigned Pos) {
57  return MI->getOperand(getMetaIdx(Pos));
58  }
59 
60  unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
61 
62  /// Get the operand index of the variable list of non-argument operands.
63  /// These hold the "live state".
64  unsigned getVarIdx() const {
65  return getMetaIdx() + MetaEnd
67  }
68 
69  /// Get the index at which stack map locations will be recorded.
70  /// Arguments are not recorded unless the anyregcc convention is used.
71  unsigned getStackMapStartIdx() const {
72  if (IsAnyReg)
73  return getArgIdx();
74  return getVarIdx();
75  }
76 
77  /// \brief Get the next scratch register operand index.
78  unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
79 };
80 
81 class StackMaps {
82 public:
83  struct Location {
87  unsigned Size;
88  unsigned Reg;
89  int64_t Offset;
90  Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
91  Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
92  : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
93  };
94 
95  // Typedef a function pointer for functions that parse sequences of operands
96  // and return a Location, plus a new "next" operand iterator.
97  typedef std::pair<Location, MachineInstr::const_mop_iterator>
100 
101  // OpTypes are used to encode information about the following logical
102  // operand (which may consist of several MachineOperands) for the
103  // OpParser.
105 
107  : AP(AP), OpParser(OpParser) {}
108 
109  /// \brief Generate a stackmap record for a stackmap instruction.
110  ///
111  /// MI must be a raw STACKMAP, not a PATCHPOINT.
112  void recordStackMap(const MachineInstr &MI);
113 
114  /// \brief Generate a stackmap record for a patchpoint instruction.
115  void recordPatchPoint(const MachineInstr &MI);
116 
117  /// If there is any stack map data, create a stack map section and serialize
118  /// the map info into it. This clears the stack map data structures
119  /// afterwards.
121 
122 private:
123  typedef SmallVector<Location, 8> LocationVec;
124 
125  struct CallsiteInfo {
126  const MCExpr *CSOffsetExpr;
127  unsigned ID;
128  LocationVec Locations;
129  CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
130  CallsiteInfo(const MCExpr *CSOffsetExpr, unsigned ID,
131  LocationVec Locations)
132  : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations) {}
133  };
134 
135  typedef std::vector<CallsiteInfo> CallsiteInfoList;
136 
137  struct ConstantPool {
138  private:
139  typedef std::map<int64_t, size_t> ConstantsMap;
140  std::vector<int64_t> ConstantsList;
141  ConstantsMap ConstantIndexes;
142 
143  public:
144  size_t getNumConstants() const { return ConstantsList.size(); }
145  int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
146  size_t getConstantIndex(int64_t ConstVal) {
147  size_t NextIdx = ConstantsList.size();
148  ConstantsMap::const_iterator I =
149  ConstantIndexes.insert(ConstantIndexes.end(),
150  std::make_pair(ConstVal, NextIdx));
151  if (I->second == NextIdx)
152  ConstantsList.push_back(ConstVal);
153  return I->second;
154  }
155  };
156 
157  AsmPrinter &AP;
158  OperandParser OpParser;
159  CallsiteInfoList CSInfos;
160  ConstantPool ConstPool;
161 
162  /// This should be called by the MC lowering code _immediately_ before
163  /// lowering the MI to an MCInst. It records where the operands for the
164  /// instruction are stored, and outputs a label to record the offset of
165  /// the call from the start of the text section. In special cases (e.g. AnyReg
166  /// calling convention) the return register is also recorded if requested.
167  void recordStackMapOpers(const MachineInstr &MI, uint32_t ID,
170  bool recordResult = false);
171 };
172 
173 }
174 
175 #endif // LLVM_STACKMAPS
std::pair< Location, MachineInstr::const_mop_iterator >(* OperandParser)(MachineInstr::const_mop_iterator, MachineInstr::const_mop_iterator, const TargetMachine &)
Definition: StackMaps.h:98
unsigned getVarIdx() const
Definition: StackMaps.h:64
bool hasDef() const
Definition: StackMaps.h:49
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
PatchPointOpers(const MachineInstr *MI)
Definition: StackMaps.cpp:31
int64_t getImm() const
unsigned getArgIdx() const
Definition: StackMaps.h:60
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
MI-level patchpoint operands.
Definition: StackMaps.h:37
bool isAnyReg() const
Definition: StackMaps.h:48
unsigned getStackMapStartIdx() const
Definition: StackMaps.h:71
void recordPatchPoint(const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
Definition: StackMaps.cpp:133
void serializeToStackMapSection()
Definition: StackMaps.cpp:182
Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
Definition: StackMaps.h:91
LocationType LocType
Definition: StackMaps.h:86
unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
Definition: StackMaps.cpp:51
void recordStackMap(const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Definition: StackMaps.cpp:123
const MachineOperand & getMetaOper(unsigned Pos)
Definition: StackMaps.h:56
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getMetaIdx(unsigned Pos=0) const
Definition: StackMaps.h:51
const MachineOperand * const_mop_iterator
Definition: MachineInstr.h:282
StackMaps(AsmPrinter &AP, OperandParser OpParser)
Definition: StackMaps.h:106