1 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file declares the MachineConstantPool class which is an abstract
12 /// constant pool to keep track of constants referenced by a function.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
19 #include "llvm/ADT/DenseSet.h"
27 class FoldingSetNodeID;
31 class MachineConstantPool;
34 /// Abstract base class for all machine specific constantpool value subclasses.
36 class MachineConstantPoolValue {
40 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
41 virtual ~MachineConstantPoolValue() {}
43 /// getType - get type of this MachineConstantPoolValue.
45 Type *getType() const { return Ty; }
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;
54 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
55 unsigned Alignment) = 0;
57 virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
59 /// print - Implement operator<<
60 virtual void print(raw_ostream &O) const = 0;
63 inline raw_ostream &operator<<(raw_ostream &OS,
64 const MachineConstantPoolValue &V) {
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 {
76 /// The constant itself.
78 const Constant *ConstVal;
79 MachineConstantPoolValue *MachineCPVal;
82 /// The required alignment for this entry. The top bit is set when Val is
83 /// a target specific MachineConstantPoolValue.
86 MachineConstantPoolEntry(const Constant *V, unsigned A)
90 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
93 Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
96 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
97 /// is indeed a target specific constantpool entry, not a wrapper over a
99 bool isMachineConstantPoolEntry() const {
100 return (int)Alignment < 0;
103 int getAlignment() const {
104 return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
107 Type *getType() const;
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:
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
119 /// 2: This entry may have arbitrary relocations.
120 unsigned getRelocationInfo() const;
123 /// The MachineConstantPool class keeps track of constants referenced by a
124 /// function which must be spilled to memory. This is used for constants which
125 /// are unable to be used directly as operands to instructions, which typically
126 /// include floating point and large integer constants.
128 /// Instructions reference the address of these constant pool constants through
129 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
130 /// code, these virtual address references are converted to refer to the
131 /// address of the function constant pool values.
132 /// @brief The machine constant pool.
133 class MachineConstantPool {
134 const TargetData *TD; ///< The machine's TargetData.
135 unsigned PoolAlignment; ///< The alignment for the pool.
136 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
137 /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
138 DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
140 /// @brief The only constructor.
141 explicit MachineConstantPool(const TargetData *td)
142 : TD(td), PoolAlignment(1) {}
143 ~MachineConstantPool();
145 /// getConstantPoolAlignment - Return the alignment required by
146 /// the whole constant pool, of which the first element must be aligned.
147 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
149 /// getConstantPoolIndex - Create a new entry in the constant pool or return
150 /// an existing one. User must specify the minimum required alignment for
152 unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
153 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
155 /// isEmpty - Return true if this constant pool contains no constants.
156 bool isEmpty() const { return Constants.empty(); }
158 const std::vector<MachineConstantPoolEntry> &getConstants() const {
162 /// print - Used by the MachineFunction printer to print information about
163 /// constant pool objects. Implemented in MachineFunction.cpp
165 void print(raw_ostream &OS) const;
167 /// dump - Call print(cerr) to be called from the debugger.
171 } // End llvm namespace