Replace tablegen uses of EVT with MVT. Add isOverloaded() to MVT to facilitate. Remov...
[oota-llvm.git] / include / llvm / CodeGen / StackMaps.h
1 //===------------------- StackMaps.h - StackMaps ----------------*- C++ -*-===//
2
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_STACKMAPS
12 #define LLVM_STACKMAPS
13
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include <map>
17 #include <vector>
18
19 namespace llvm {
20
21 class AsmPrinter;
22 class MCExpr;
23
24 /// \brief MI-level patchpoint operands.
25 ///
26 /// MI patchpoint operations take the form:
27 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ...
28 ///
29 /// IR patchpoint intrinsics do not have the <cc> operand because calling
30 /// convention is part of the subclass data.
31 ///
32 /// SD patchpoint nodes do not have a def operand because it is part of the
33 /// SDValue.
34 ///
35 /// Patchpoints following the anyregcc convention are handled specially. For
36 /// these, the stack map also records the location of the return value and
37 /// arguments.
38 class PatchPointOpers {
39 public:
40   /// Enumerate the meta operands.
41   enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
42 private:
43   const MachineInstr *MI;
44   bool HasDef;
45   bool IsAnyReg;
46 public:
47   explicit PatchPointOpers(const MachineInstr *MI);
48
49   bool isAnyReg() const { return IsAnyReg; }
50   bool hasDef() const { return HasDef; }
51
52   unsigned getMetaIdx(unsigned Pos = 0) const {
53     assert(Pos < MetaEnd && "Meta operand index out of range.");
54     return (HasDef ? 1 : 0) + Pos;
55   }
56
57   const MachineOperand &getMetaOper(unsigned Pos) {
58     return MI->getOperand(getMetaIdx(Pos));
59   }
60
61   unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
62
63   /// Get the operand index of the variable list of non-argument operands.
64   /// These hold the "live state".
65   unsigned getVarIdx() const {
66     return getMetaIdx() + MetaEnd
67       + MI->getOperand(getMetaIdx(NArgPos)).getImm();
68   }
69
70   /// Get the index at which stack map locations will be recorded.
71   /// Arguments are not recorded unless the anyregcc convention is used.
72   unsigned getStackMapStartIdx() const {
73     if (IsAnyReg)
74       return getArgIdx();
75     return getVarIdx();
76   }
77
78   /// \brief Get the next scratch register operand index.
79   unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
80 };
81
82 class StackMaps {
83 public:
84   struct Location {
85     enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
86                         ConstantIndex };
87     LocationType LocType;
88     unsigned Size;
89     unsigned Reg;
90     int64_t Offset;
91     Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {}
92     Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset)
93       : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {}
94   };
95
96   struct LiveOutReg {
97     unsigned short Reg;
98     unsigned short RegNo;
99     unsigned short Size;
100
101     LiveOutReg() : Reg(0), RegNo(0), Size(0) {}
102     LiveOutReg(unsigned short Reg, unsigned short RegNo, unsigned short Size)
103       : Reg(Reg), RegNo(RegNo), Size(Size) {}
104
105     void MarkInvalid() { Reg = 0; }
106
107     // Only sort by the dwarf register number.
108     bool operator< (const LiveOutReg &LO) const { return RegNo < LO.RegNo; }
109     static bool IsInvalid(const LiveOutReg &LO) { return LO.Reg == 0; }
110   };
111
112   // OpTypes are used to encode information about the following logical
113   // operand (which may consist of several MachineOperands) for the
114   // OpParser.
115   typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
116
117   StackMaps(AsmPrinter &AP) : AP(AP) {}
118
119   /// \brief Generate a stackmap record for a stackmap instruction.
120   ///
121   /// MI must be a raw STACKMAP, not a PATCHPOINT.
122   void recordStackMap(const MachineInstr &MI);
123
124   /// \brief Generate a stackmap record for a patchpoint instruction.
125   void recordPatchPoint(const MachineInstr &MI);
126
127   /// If there is any stack map data, create a stack map section and serialize
128   /// the map info into it. This clears the stack map data structures
129   /// afterwards.
130   void serializeToStackMapSection();
131
132 private:
133   typedef SmallVector<Location, 8> LocationVec;
134   typedef SmallVector<LiveOutReg, 8> LiveOutVec;
135
136   struct CallsiteInfo {
137     const MCExpr *CSOffsetExpr;
138     uint64_t ID;
139     LocationVec Locations;
140     LiveOutVec LiveOuts;
141     CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
142     CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
143                  LocationVec &Locations, LiveOutVec &LiveOuts)
144       : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations),
145         LiveOuts(LiveOuts) {}
146   };
147
148   typedef std::vector<CallsiteInfo> CallsiteInfoList;
149
150   struct ConstantPool {
151   private:
152     typedef std::map<int64_t, size_t> ConstantsMap;
153     std::vector<int64_t> ConstantsList;
154     ConstantsMap ConstantIndexes;
155
156   public:
157     size_t getNumConstants() const { return ConstantsList.size(); }
158     int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
159     size_t getConstantIndex(int64_t ConstVal) {
160       size_t NextIdx = ConstantsList.size();
161       ConstantsMap::const_iterator I =
162         ConstantIndexes.insert(ConstantIndexes.end(),
163                                std::make_pair(ConstVal, NextIdx));
164       if (I->second == NextIdx)
165         ConstantsList.push_back(ConstVal);
166       return I->second;
167     }
168   };
169
170   AsmPrinter &AP;
171   CallsiteInfoList CSInfos;
172   ConstantPool ConstPool;
173
174   MachineInstr::const_mop_iterator
175   parseOperand(MachineInstr::const_mop_iterator MOI,
176                MachineInstr::const_mop_iterator MOE,
177                LocationVec &Locs, LiveOutVec &LiveOuts) const;
178
179   /// \brief Create a live-out register record for the given register @p Reg.
180   LiveOutReg createLiveOutReg(unsigned Reg, const MCRegisterInfo &MCRI,
181                               const TargetRegisterInfo *TRI) const;
182
183   /// \brief Parse the register live-out mask and return a vector of live-out
184   /// registers that need to be recorded in the stackmap.
185   LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
186
187   /// This should be called by the MC lowering code _immediately_ before
188   /// lowering the MI to an MCInst. It records where the operands for the
189   /// instruction are stored, and outputs a label to record the offset of
190   /// the call from the start of the text section. In special cases (e.g. AnyReg
191   /// calling convention) the return register is also recorded if requested.
192   void recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
193                            MachineInstr::const_mop_iterator MOI,
194                            MachineInstr::const_mop_iterator MOE,
195                            bool recordResult = false);
196 };
197
198 }
199
200 #endif // LLVM_STACKMAPS