Mirrors ConstantPoolSDNode.
[oota-llvm.git] / include / llvm / CodeGen / MachineConstantPool.h
1 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file This file declares the MachineConstantPool class which is an abstract
11 /// constant pool to keep track of constants referenced by a function.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
16 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17
18 #include "llvm/CodeGen/SelectionDAGCSEMap.h"
19 #include <vector>
20 #include <iosfwd>
21
22 namespace llvm {
23
24 class AsmPrinter;
25 class Constant;
26 class TargetData;
27 class TargetMachine;
28 class MachineConstantPool;
29
30 /// Abstract base class for all machine specific constantpool value subclasses.
31 ///
32 class MachineConstantPoolValue {
33   const Type *Ty;
34
35 public:
36   MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
37   virtual ~MachineConstantPoolValue() {};
38
39   /// getType - get type of this MachineConstantPoolValue.
40   ///
41   inline const Type *getType() const { return Ty; }
42
43   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
44                                         unsigned Alignment) = 0;
45
46   virtual void AddSelectionDAGCSEId(SelectionDAGCSEMap::NodeID *Id) = 0;
47
48   /// print - Implement operator<<...
49   ///
50   virtual void print(std::ostream &O) const = 0;
51 };
52
53 inline std::ostream &operator<<(std::ostream &OS,
54                                 const MachineConstantPoolValue &V) {
55   V.print(OS);
56   return OS;
57 }
58
59 /// This class is a data container for one entry in a MachineConstantPool.
60 /// It contains a pointer to the value and an offset from the start of
61 /// the constant pool.
62 /// @brief An entry in a MachineConstantPool
63 struct MachineConstantPoolEntry {
64   /// The constant itself.
65   union {
66     Constant *ConstVal;
67     MachineConstantPoolValue *MachineCPVal;
68   } Val;
69
70   /// The offset of the constant from the start of the pool. It's really
71   /// 31-bit only. The top bit is set when Val is a MachineConstantPoolValue.
72   unsigned Offset;
73
74   MachineConstantPoolEntry(Constant *V, unsigned O)
75     : Offset(O) {
76     assert((int)Offset >= 0 && "Offset is too large");
77     Val.ConstVal = V;
78   }
79   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
80     : Offset(O){
81     assert((int)Offset >= 0 && "Offset is too large");
82     Val.MachineCPVal = V; 
83     Offset |= 1 << (sizeof(unsigned)*8-1);
84   }
85
86   bool isMachineConstantPoolEntry() const {
87     return (int)Offset < 0;
88   }
89
90   const Type *getType() const;
91 };
92   
93 /// The MachineConstantPool class keeps track of constants referenced by a
94 /// function which must be spilled to memory.  This is used for constants which
95 /// are unable to be used directly as operands to instructions, which typically
96 /// include floating point and large integer constants.
97 ///
98 /// Instructions reference the address of these constant pool constants through
99 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
100 /// code, these virtual address references are converted to refer to the
101 /// address of the function constant pool values.
102 /// @brief The machine constant pool.
103 class MachineConstantPool {
104   const TargetData *TD;   ///< The machine's TargetData.
105   unsigned PoolAlignment; ///< The alignment for the pool.
106   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
107 public:
108   /// @brief The only constructor.
109   MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
110   ~MachineConstantPool();
111     
112   /// getConstantPoolAlignment - Return the log2 of the alignment required by
113   /// the whole constant pool, of which the first element must be aligned.
114   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
115   
116   /// getConstantPoolIndex - Create a new entry in the constant pool or return
117   /// an existing one.  User must specify an alignment in bytes for the object.
118   unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
119   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
120   
121   /// isEmpty - Return true if this constant pool contains no constants.
122   bool isEmpty() const { return Constants.empty(); }
123
124   const std::vector<MachineConstantPoolEntry> &getConstants() const {
125     return Constants;
126   }
127
128   /// print - Used by the MachineFunction printer to print information about
129   /// constant pool objects.  Implemented in MachineFunction.cpp
130   ///
131   void print(std::ostream &OS) const;
132
133   /// dump - Call print(std::cerr) to be called from the debugger.
134   ///
135   void dump() const;
136 };
137
138 } // End llvm namespace
139
140 #endif