OSDN Git Service

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