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