1 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/CodeGen/SelectionDAGNodes.h"
20 #include "llvm/Support/Streams.h"
30 class MachineConstantPool;
32 /// Abstract base class for all machine specific constantpool value subclasses.
34 class MachineConstantPoolValue {
38 MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
39 virtual ~MachineConstantPoolValue() {};
41 /// getType - get type of this MachineConstantPoolValue.
43 inline const Type *getType() const { return Ty; }
45 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
46 unsigned Alignment) = 0;
48 virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
50 /// print - Implement operator<<...
52 void print(llvm_ostream &O) const {
53 if (O.stream()) print(*O.stream());
55 virtual void print(std::ostream &O) const = 0;
58 inline llvm_ostream &operator<<(llvm_ostream &OS,
59 const MachineConstantPoolValue &V) {
63 inline std::ostream &operator<<(std::ostream &OS,
64 const MachineConstantPoolValue &V) {
69 /// This class is a data container for one entry in a MachineConstantPool.
70 /// It contains a pointer to the value and an offset from the start of
71 /// the constant pool.
72 /// @brief An entry in a MachineConstantPool
73 class MachineConstantPoolEntry {
75 /// The constant itself.
78 MachineConstantPoolValue *MachineCPVal;
81 /// The offset of the constant from the start of the pool. The top bit is set
82 /// when Val is a MachineConstantPoolValue.
85 MachineConstantPoolEntry(Constant *V, unsigned O)
87 assert((int)Offset >= 0 && "Offset is too large");
90 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
92 assert((int)Offset >= 0 && "Offset is too large");
94 Offset |= 1 << (sizeof(unsigned)*8-1);
97 bool isMachineConstantPoolEntry() const {
98 return (int)Offset < 0;
101 int getOffset() const {
102 return Offset & ~(1 << (sizeof(unsigned)*8-1));
105 const Type *getType() const;
108 /// The MachineConstantPool class keeps track of constants referenced by a
109 /// function which must be spilled to memory. This is used for constants which
110 /// are unable to be used directly as operands to instructions, which typically
111 /// include floating point and large integer constants.
113 /// Instructions reference the address of these constant pool constants through
114 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
115 /// code, these virtual address references are converted to refer to the
116 /// address of the function constant pool values.
117 /// @brief The machine constant pool.
118 class MachineConstantPool {
119 const TargetData *TD; ///< The machine's TargetData.
120 unsigned PoolAlignment; ///< The alignment for the pool.
121 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
123 /// @brief The only constructor.
124 MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
125 ~MachineConstantPool();
127 /// getConstantPoolAlignment - Return the log2 of the alignment required by
128 /// the whole constant pool, of which the first element must be aligned.
129 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
131 /// getConstantPoolIndex - Create a new entry in the constant pool or return
132 /// an existing one. User must specify an alignment in bytes for the object.
133 unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
134 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
136 /// isEmpty - Return true if this constant pool contains no constants.
137 bool isEmpty() const { return Constants.empty(); }
139 const std::vector<MachineConstantPoolEntry> &getConstants() const {
143 /// print - Used by the MachineFunction printer to print information about
144 /// constant pool objects. Implemented in MachineFunction.cpp
146 void print(llvm_ostream &OS) const {
147 if (OS.stream()) print(*OS.stream());
149 void print(std::ostream &OS) const;
151 /// dump - Call print(std::cerr) to be called from the debugger.
156 } // End llvm namespace