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"
20 #include "llvm/MC/SectionKind.h"
28 class FoldingSetNodeID;
32 class MachineConstantPool;
35 /// Abstract base class for all machine specific constantpool value subclasses.
37 class MachineConstantPoolValue {
38 virtual void anchor();
42 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
43 virtual ~MachineConstantPoolValue() {}
45 /// getType - get type of this MachineConstantPoolValue.
47 Type *getType() const { return Ty; }
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;
56 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
57 unsigned Alignment) = 0;
59 virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
61 /// print - Implement operator<<
62 virtual void print(raw_ostream &O) const = 0;
65 inline raw_ostream &operator<<(raw_ostream &OS,
66 const MachineConstantPoolValue &V) {
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 {
78 /// The constant itself.
80 const Constant *ConstVal;
81 MachineConstantPoolValue *MachineCPVal;
84 /// The required alignment for this entry. The top bit is set when Val is
85 /// a target specific MachineConstantPoolValue.
88 MachineConstantPoolEntry(const Constant *V, unsigned A)
92 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
95 Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
98 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
99 /// is indeed a target specific constantpool entry, not a wrapper over a
101 bool isMachineConstantPoolEntry() const {
102 return (int)Alignment < 0;
105 int getAlignment() const {
106 return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
109 Type *getType() const;
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:
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
121 /// 2: This entry may have arbitrary relocations.
122 unsigned getRelocationInfo() const;
124 SectionKind getSectionKind(const DataLayout *DL) const;
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.
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;
144 const DataLayout *getDataLayout() const;
146 /// @brief The only constructor.
147 explicit MachineConstantPool(const TargetMachine &TM)
148 : TM(TM), PoolAlignment(1) {}
149 ~MachineConstantPool();
151 /// getConstantPoolAlignment - Return the alignment required by
152 /// the whole constant pool, of which the first element must be aligned.
153 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
155 /// getConstantPoolIndex - Create a new entry in the constant pool or return
156 /// an existing one. User must specify the minimum required alignment for
158 unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
159 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
161 /// isEmpty - Return true if this constant pool contains no constants.
162 bool isEmpty() const { return Constants.empty(); }
164 const std::vector<MachineConstantPoolEntry> &getConstants() const {
168 /// print - Used by the MachineFunction printer to print information about
169 /// constant pool objects. Implemented in MachineFunction.cpp
171 void print(raw_ostream &OS) const;
173 /// dump - Call print(cerr) to be called from the debugger.
177 } // End llvm namespace