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