OSDN Git Service

Merge upstream r127116
[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
11 /// This file declares the MachineConstantPool class which is an abstract
12 /// constant pool to keep track of constants referenced by a function.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
18
19 #include "llvm/ADT/DenseSet.h"
20 #include <cassert>
21 #include <climits>
22 #include <vector>
23
24 namespace llvm {
25
26 class Constant;
27 class FoldingSetNodeID;
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   const Type *getType() const { return Ty; }
46
47   
48   /// getRelocationInfo - This method classifies the entry according to
49   /// whether or not it may generate a relocation entry.  This must be
50   /// conservative, so if it might codegen to a relocatable entry, it should say
51   /// so.  The return values are the same as Constant::getRelocationInfo().
52   virtual unsigned getRelocationInfo() const = 0;
53   
54   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
55                                         unsigned Alignment) = 0;
56
57   virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
58
59   /// print - Implement operator<<
60   virtual void print(raw_ostream &O) const = 0;
61 };
62
63 inline raw_ostream &operator<<(raw_ostream &OS,
64                                const MachineConstantPoolValue &V) {
65   V.print(OS);
66   return OS;
67 }
68   
69
70 /// This class is a data container for one entry in a MachineConstantPool.
71 /// It contains a pointer to the value and an offset from the start of
72 /// the constant pool.
73 /// @brief An entry in a MachineConstantPool
74 class MachineConstantPoolEntry {
75 public:
76   /// The constant itself.
77   union {
78     const Constant *ConstVal;
79     MachineConstantPoolValue *MachineCPVal;
80   } Val;
81
82   /// The required alignment for this entry. The top bit is set when Val is
83   /// a MachineConstantPoolValue.
84   unsigned Alignment;
85
86   MachineConstantPoolEntry(const Constant *V, unsigned A)
87     : Alignment(A) {
88     Val.ConstVal = V;
89   }
90   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
91     : Alignment(A) {
92     Val.MachineCPVal = V; 
93     Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
94   }
95
96   bool isMachineConstantPoolEntry() const {
97     return (int)Alignment < 0;
98   }
99
100   int getAlignment() const { 
101     return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
102   }
103
104   const Type *getType() const;
105   
106   /// getRelocationInfo - This method classifies the entry according to
107   /// whether or not it may generate a relocation entry.  This must be
108   /// conservative, so if it might codegen to a relocatable entry, it should say
109   /// so.  The return values are:
110   /// 
111   ///  0: This constant pool entry is guaranteed to never have a relocation
112   ///     applied to it (because it holds a simple constant like '4').
113   ///  1: This entry has relocations, but the entries are guaranteed to be
114   ///     resolvable by the static linker, so the dynamic linker will never see
115   ///     them.
116   ///  2: This entry may have arbitrary relocations. 
117   unsigned getRelocationInfo() const;
118 };
119   
120 /// The MachineConstantPool class keeps track of constants referenced by a
121 /// function which must be spilled to memory.  This is used for constants which
122 /// are unable to be used directly as operands to instructions, which typically
123 /// include floating point and large integer constants.
124 ///
125 /// Instructions reference the address of these constant pool constants through
126 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
127 /// code, these virtual address references are converted to refer to the
128 /// address of the function constant pool values.
129 /// @brief The machine constant pool.
130 class MachineConstantPool {
131   const TargetData *TD;   ///< The machine's TargetData.
132   unsigned PoolAlignment; ///< The alignment for the pool.
133   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
134   /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
135   DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
136 public:
137   /// @brief The only constructor.
138   explicit MachineConstantPool(const TargetData *td)
139     : TD(td), PoolAlignment(1) {}
140   ~MachineConstantPool();
141     
142   /// getConstantPoolAlignment - Return the alignment required by
143   /// the whole constant pool, of which the first element must be aligned.
144   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
145   
146   /// getConstantPoolIndex - Create a new entry in the constant pool or return
147   /// an existing one.  User must specify the minimum required alignment for
148   /// the object.
149   unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
150   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
151   
152   /// isEmpty - Return true if this constant pool contains no constants.
153   bool isEmpty() const { return Constants.empty(); }
154
155   const std::vector<MachineConstantPoolEntry> &getConstants() const {
156     return Constants;
157   }
158
159   /// print - Used by the MachineFunction printer to print information about
160   /// constant pool objects.  Implemented in MachineFunction.cpp
161   ///
162   void print(raw_ostream &OS) const;
163
164   /// dump - Call print(cerr) to be called from the debugger.
165   void dump() const;
166 };
167
168 } // End llvm namespace
169
170 #endif