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/CodeGen/SelectionDAGCSEMap.h"
28 class MachineConstantPool;
30 /// Abstract base class for all machine specific constantpool value subclasses.
32 class MachineConstantPoolValue {
36 MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
37 virtual ~MachineConstantPoolValue() {};
39 /// getType - get type of this MachineConstantPoolValue.
41 inline const Type *getType() const { return Ty; }
43 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
44 unsigned Alignment) = 0;
46 virtual void AddSelectionDAGCSEId(SelectionDAGCSEMap::NodeID *Id) = 0;
48 /// print - Implement operator<<...
50 virtual void print(std::ostream &O) const = 0;
53 inline std::ostream &operator<<(std::ostream &OS,
54 const MachineConstantPoolValue &V) {
59 /// This class is a data container for one entry in a MachineConstantPool.
60 /// It contains a pointer to the value and an offset from the start of
61 /// the constant pool.
62 /// @brief An entry in a MachineConstantPool
63 struct MachineConstantPoolEntry {
64 /// The constant itself.
67 MachineConstantPoolValue *MachineCPVal;
70 /// The offset of the constant from the start of the pool. The top bit is set
71 /// when Val is a MachineConstantPoolValue.
74 MachineConstantPoolEntry(Constant *V, unsigned O)
76 assert((int)Offset >= 0 && "Offset is too large");
79 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
81 assert((int)Offset >= 0 && "Offset is too large");
83 Offset |= 1 << (sizeof(unsigned)*8-1);
86 bool isMachineConstantPoolEntry() const {
87 return (int)Offset < 0;
90 int getOffset() const {
91 return Offset & ~(1 << (sizeof(unsigned)*8-1));
94 const Type *getType() const;
97 /// The MachineConstantPool class keeps track of constants referenced by a
98 /// function which must be spilled to memory. This is used for constants which
99 /// are unable to be used directly as operands to instructions, which typically
100 /// include floating point and large integer constants.
102 /// Instructions reference the address of these constant pool constants through
103 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
104 /// code, these virtual address references are converted to refer to the
105 /// address of the function constant pool values.
106 /// @brief The machine constant pool.
107 class MachineConstantPool {
108 const TargetData *TD; ///< The machine's TargetData.
109 unsigned PoolAlignment; ///< The alignment for the pool.
110 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
112 /// @brief The only constructor.
113 MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
114 ~MachineConstantPool();
116 /// getConstantPoolAlignment - Return the log2 of the alignment required by
117 /// the whole constant pool, of which the first element must be aligned.
118 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
120 /// getConstantPoolIndex - Create a new entry in the constant pool or return
121 /// an existing one. User must specify an alignment in bytes for the object.
122 unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
123 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
125 /// isEmpty - Return true if this constant pool contains no constants.
126 bool isEmpty() const { return Constants.empty(); }
128 const std::vector<MachineConstantPoolEntry> &getConstants() const {
132 /// print - Used by the MachineFunction printer to print information about
133 /// constant pool objects. Implemented in MachineFunction.cpp
135 void print(std::ostream &OS) const;
137 /// dump - Call print(std::cerr) to be called from the debugger.
142 } // End llvm namespace