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