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