OSDN Git Service

Replace a #include with a forward-declaration.
[android-x86/external-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 <cassert>
19 #include <vector>
20
21 namespace llvm {
22
23 class AsmPrinter;
24 class Constant;
25 class FoldingSetNodeID;
26 class TargetData;
27 class TargetMachine;
28 class Type;
29 class MachineConstantPool;
30 class raw_ostream;
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   virtual void print(raw_ostream &O) const = 0;
52 };
53
54 inline raw_ostream &operator<<(raw_ostream &OS,
55                                const MachineConstantPoolValue &V) {
56   V.print(OS);
57   return OS;
58 }
59   
60
61 /// This class is a data container for one entry in a MachineConstantPool.
62 /// It contains a pointer to the value and an offset from the start of
63 /// the constant pool.
64 /// @brief An entry in a MachineConstantPool
65 class MachineConstantPoolEntry {
66 public:
67   /// The constant itself.
68   union {
69     Constant *ConstVal;
70     MachineConstantPoolValue *MachineCPVal;
71   } Val;
72
73   /// The offset of the constant from the start of the pool. The top bit is set
74   /// when Val is a MachineConstantPoolValue.
75   unsigned Offset;
76
77   MachineConstantPoolEntry(Constant *V, unsigned O)
78     : Offset(O) {
79     assert((int)Offset >= 0 && "Offset is too large");
80     Val.ConstVal = V;
81   }
82   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
83     : Offset(O){
84     assert((int)Offset >= 0 && "Offset is too large");
85     Val.MachineCPVal = V; 
86     Offset |= 1 << (sizeof(unsigned)*8-1);
87   }
88
89   bool isMachineConstantPoolEntry() const {
90     return (int)Offset < 0;
91   }
92
93   int getOffset() const { 
94     return Offset & ~(1 << (sizeof(unsigned)*8-1));
95   }
96
97   const Type *getType() const;
98 };
99   
100 /// The MachineConstantPool class keeps track of constants referenced by a
101 /// function which must be spilled to memory.  This is used for constants which
102 /// are unable to be used directly as operands to instructions, which typically
103 /// include floating point and large integer constants.
104 ///
105 /// Instructions reference the address of these constant pool constants through
106 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
107 /// code, these virtual address references are converted to refer to the
108 /// address of the function constant pool values.
109 /// @brief The machine constant pool.
110 class MachineConstantPool {
111   const TargetData *TD;   ///< The machine's TargetData.
112   unsigned PoolAlignment; ///< The alignment for the pool.
113   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
114 public:
115   /// @brief The only constructor.
116   explicit MachineConstantPool(const TargetData *td)
117     : TD(td), PoolAlignment(1) {}
118   ~MachineConstantPool();
119     
120   /// getConstantPoolAlignment - Return the log2 of the alignment required by
121   /// the whole constant pool, of which the first element must be aligned.
122   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
123   
124   /// getConstantPoolIndex - Create a new entry in the constant pool or return
125   /// an existing one.  User must specify the log2 of the minimum required
126   /// alignment 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(raw_ostream &OS) const;
141
142   /// dump - Call print(cerr) to be called from the debugger.
143   void dump() const;
144 };
145
146 } // End llvm namespace
147
148 #endif