Add support for stack map generation in the X86 backend.
[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.
54   void recordStackMap(const MachineInstr &MI, uint32_t ID,
55                       MachineInstr::const_mop_iterator MOI,
56                       MachineInstr::const_mop_iterator MOE);
57
58   /// If there is any stack map data, create a stack map section and serialize
59   /// the map info into it. This clears the stack map data structures
60   /// afterwards.
61   void serializeToStackMapSection();
62
63 private:
64
65   typedef SmallVector<Location, 8> LocationVec;
66
67   struct CallsiteInfo {
68     const MCExpr *CSOffsetExpr;
69     unsigned ID;
70     LocationVec Locations;
71     CallsiteInfo() : CSOffsetExpr(0), ID(0) {}
72     CallsiteInfo(const MCExpr *CSOffsetExpr, unsigned ID,
73                  LocationVec Locations)
74       : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations) {}
75   };
76
77   typedef std::vector<CallsiteInfo> CallsiteInfoList;
78
79   struct ConstantPool {
80   private:
81     typedef std::map<int64_t, size_t> ConstantsMap;
82     std::vector<int64_t> ConstantsList;
83     ConstantsMap ConstantIndexes;
84
85   public:
86     size_t getNumConstants() const { return ConstantsList.size(); }
87     int64_t getConstant(size_t Idx) const { return ConstantsList[Idx]; }
88     size_t getConstantIndex(int64_t ConstVal) {
89       size_t NextIdx = ConstantsList.size();
90       ConstantsMap::const_iterator I =
91         ConstantIndexes.insert(ConstantIndexes.end(),
92                                std::make_pair(ConstVal, NextIdx));
93       if (I->second == NextIdx)
94         ConstantsList.push_back(ConstVal);
95       return I->second;
96     }
97   };
98
99   AsmPrinter &AP;
100   OperandParser OpParser;
101   CallsiteInfoList CSInfos;
102   ConstantPool ConstPool;
103 };
104
105 }
106
107 #endif // LLVM_STACKMAPS