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"
14#include "llvm/CodeGen/MachineInstr.h"
15#include <map>
16#include <vector>
17
18namespace llvm {
19
20class AsmPrinter;
21class 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.
37class PatchPointOpers {
38public:
39  /// Enumerate the meta operands.
40  enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
41private:
42  const MachineInstr *MI;
43  bool HasDef;
44  bool IsAnyReg;
45public:
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
66      + MI->getOperand(getMetaIdx(NArgPos)).getImm();
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
81class StackMaps {
82public:
83  struct Location {
84    enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
85                        ConstantIndex };
86    LocationType LocType;
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>
98    (*OperandParser)(MachineInstr::const_mop_iterator,
99                     MachineInstr::const_mop_iterator, const TargetMachine&);
100
101  // OpTypes are used to encode information about the following logical
102  // operand (which may consist of several MachineOperands) for the
103  // OpParser.
104  typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
105
106  StackMaps(AsmPrinter &AP, OperandParser OpParser)
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.
120  void serializeToStackMapSection();
121
122private:
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,
168                           MachineInstr::const_mop_iterator MOI,
169                           MachineInstr::const_mop_iterator MOE,
170                           bool recordResult = false);
171};
172
173}
174
175#endif // LLVM_STACKMAPS
176