[Stackmap] Add AnyReg calling convention support for patchpoint intrinsic.
[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_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
18 namespace llvm {
19
20 class AsmPrinter;
21 class MCExpr;
22
23 class StackMaps {
24 public:
25   struct Location {
26     enum LocationType { Unprocessed, Register, Direct, Indirect, Constant,
27                         ConstantIndex };
28     LocationType LocType;
29     unsigned Reg;
30     int64_t Offset;
31     Location() : LocType(Unprocessed), Reg(0), Offset(0) {}
32     Location(LocationType LocType, unsigned Reg, int64_t Offset)
33       : LocType(LocType), Reg(Reg), Offset(Offset) {}
34   };
35
36   // Typedef a function pointer for functions that parse sequences of operands
37   // and return a Location, plus a new "next" operand iterator.
38   typedef std::pair<Location, MachineInstr::const_mop_iterator>
39     (*OperandParser)(MachineInstr::const_mop_iterator,
40                      MachineInstr::const_mop_iterator);
41
42   // OpTypes are used to encode information about the following logical
43   // operand (which may consist of several MachineOperands) for the
44   // OpParser.
45   typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType;
46
47   StackMaps(AsmPrinter &AP, OperandParser OpParser)
48     : AP(AP), OpParser(OpParser) {}
49
50   /// This should be called by the MC lowering code _immediately_ before
51   /// lowering the MI to an MCInst. It records where the operands for the
52   /// instruction are stored, and outputs a label to record the offset of
53   /// the call from the start of the text section. In special cases (e.g. AnyReg
54   /// calling convention) the return register is also recorded if requested.
55   void recordStackMap(const MachineInstr &MI, uint32_t ID,
56                       MachineInstr::const_mop_iterator MOI,
57                       MachineInstr::const_mop_iterator MOE,
58                       bool recordResult = false);
59
60   /// If there is any stack map data, create a stack map section and serialize
61   /// the map info into it. This clears the stack map data structures
62   /// afterwards.
63   void serializeToStackMapSection();
64
65 private:
66
67   typedef SmallVector<Location, 8> LocationVec;
68
69   struct CallsiteInfo {
70     const MCExpr *CSOffsetExpr;
71     unsigned ID;
72     LocationVec Locations;
73     CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
74     CallsiteInfo(const MCExpr *CSOffsetExpr, unsigned ID,
75                  LocationVec Locations)
76       : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations) {}
77   };
78
79   typedef std::vector<CallsiteInfo> CallsiteInfoList;
80
81   struct ConstantPool {
82   private:
83     typedef std::map<int64_t, size_t> ConstantsMap;
84     std::vector<int64_t> ConstantsList;
85     ConstantsMap ConstantIndexes;
86
87   public:
88     size_t getNumConstants() const { return ConstantsList.size(); }
89     int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
90     size_t getConstantIndex(int64_t ConstVal) {
91       size_t NextIdx = ConstantsList.size();
92       ConstantsMap::const_iterator I =
93         ConstantIndexes.insert(ConstantIndexes.end(),
94                                std::make_pair(ConstVal, NextIdx));
95       if (I->second == NextIdx)
96         ConstantsList.push_back(ConstVal);
97       return I->second;
98     }
99   };
100
101   AsmPrinter &AP;
102   OperandParser OpParser;
103   CallsiteInfoList CSInfos;
104   ConstantPool ConstPool;
105 };
106
107 }
108
109 #endif // LLVM_STACKMAPS