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