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