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 //===----------------------------------------------------------------------===//
10 /// @file This file declares the MachineConstantPool class which is an abstract
11 /// constant pool to keep track of constants referenced by a function.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
16 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Support/Streams.h"
31 class MachineConstantPool;
34 /// Abstract base class for all machine specific constantpool value subclasses.
36 class MachineConstantPoolValue {
40 explicit MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
41 virtual ~MachineConstantPoolValue() {}
43 /// getType - get type of this MachineConstantPoolValue.
45 inline const Type *getType() const { return Ty; }
47 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
48 unsigned Alignment) = 0;
50 virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
52 /// print - Implement operator<<...
54 void print(std::ostream &O) const;
55 void print(std::ostream *O) const { if (O) print(*O); }
56 virtual void print(raw_ostream &O) const = 0;
59 inline std::ostream &operator<<(std::ostream &OS,
60 const MachineConstantPoolValue &V) {
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.
81 MachineConstantPoolValue *MachineCPVal;
84 /// The offset of the constant from the start of the pool. The top bit is set
85 /// when Val is a MachineConstantPoolValue.
88 MachineConstantPoolEntry(Constant *V, unsigned O)
90 assert((int)Offset >= 0 && "Offset is too large");
93 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
95 assert((int)Offset >= 0 && "Offset is too large");
97 Offset |= 1 << (sizeof(unsigned)*8-1);
100 bool isMachineConstantPoolEntry() const {
101 return (int)Offset < 0;
104 int getOffset() const {
105 return Offset & ~(1 << (sizeof(unsigned)*8-1));
108 const Type *getType() const;
111 /// The MachineConstantPool class keeps track of constants referenced by a
112 /// function which must be spilled to memory. This is used for constants which
113 /// are unable to be used directly as operands to instructions, which typically
114 /// include floating point and large integer constants.
116 /// Instructions reference the address of these constant pool constants through
117 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
118 /// code, these virtual address references are converted to refer to the
119 /// address of the function constant pool values.
120 /// @brief The machine constant pool.
121 class MachineConstantPool {
122 const TargetData *TD; ///< The machine's TargetData.
123 unsigned PoolAlignment; ///< The alignment for the pool.
124 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
126 /// @brief The only constructor.
127 explicit MachineConstantPool(const TargetData *td)
128 : TD(td), PoolAlignment(1) {}
129 ~MachineConstantPool();
131 /// getConstantPoolAlignment - Return the log2 of the alignment required by
132 /// the whole constant pool, of which the first element must be aligned.
133 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
135 /// getConstantPoolIndex - Create a new entry in the constant pool or return
136 /// an existing one. User must specify an alignment in bytes for the object.
137 unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
138 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
140 /// isEmpty - Return true if this constant pool contains no constants.
141 bool isEmpty() const { return Constants.empty(); }
143 const std::vector<MachineConstantPoolEntry> &getConstants() const {
147 /// print - Used by the MachineFunction printer to print information about
148 /// constant pool objects. Implemented in MachineFunction.cpp
150 void print(std::ostream &OS) const;
151 void print(std::ostream *OS) const { if (OS) print(*OS); }
153 /// dump - Call print(std::cerr) to be called from the debugger.
158 } // End llvm namespace