Add MachineConstantPoolEntry getOffset() accessor.
[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. The top bit is set
71   /// 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   int getOffset() const { 
91     return Offset & ~(1 << (sizeof(unsigned)*8-1));
92   }
93
94   const Type *getType() const;
95 };
96   
97 /// The MachineConstantPool class keeps track of constants referenced by a
98 /// function which must be spilled to memory.  This is used for constants which
99 /// are unable to be used directly as operands to instructions, which typically
100 /// include floating point and large integer constants.
101 ///
102 /// Instructions reference the address of these constant pool constants through
103 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
104 /// code, these virtual address references are converted to refer to the
105 /// address of the function constant pool values.
106 /// @brief The machine constant pool.
107 class MachineConstantPool {
108   const TargetData *TD;   ///< The machine's TargetData.
109   unsigned PoolAlignment; ///< The alignment for the pool.
110   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
111 public:
112   /// @brief The only constructor.
113   MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
114   ~MachineConstantPool();
115     
116   /// getConstantPoolAlignment - Return the log2 of the alignment required by
117   /// the whole constant pool, of which the first element must be aligned.
118   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
119   
120   /// getConstantPoolIndex - Create a new entry in the constant pool or return
121   /// an existing one.  User must specify an alignment in bytes for the object.
122   unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
123   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
124   
125   /// isEmpty - Return true if this constant pool contains no constants.
126   bool isEmpty() const { return Constants.empty(); }
127
128   const std::vector<MachineConstantPoolEntry> &getConstants() const {
129     return Constants;
130   }
131
132   /// print - Used by the MachineFunction printer to print information about
133   /// constant pool objects.  Implemented in MachineFunction.cpp
134   ///
135   void print(std::ostream &OS) const;
136
137   /// dump - Call print(std::cerr) to be called from the debugger.
138   ///
139   void dump() const;
140 };
141
142 } // End llvm namespace
143
144 #endif