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. It's really
71 /// 31-bit only. The top bit is set 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 const Type *getType() const;
93 /// The MachineConstantPool class keeps track of constants referenced by a
94 /// function which must be spilled to memory. This is used for constants which
95 /// are unable to be used directly as operands to instructions, which typically
96 /// include floating point and large integer constants.
98 /// Instructions reference the address of these constant pool constants through
99 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
100 /// code, these virtual address references are converted to refer to the
101 /// address of the function constant pool values.
102 /// @brief The machine constant pool.
103 class MachineConstantPool {
104 const TargetData *TD; ///< The machine's TargetData.
105 unsigned PoolAlignment; ///< The alignment for the pool.
106 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
108 /// @brief The only constructor.
109 MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
110 ~MachineConstantPool();
112 /// getConstantPoolAlignment - Return the log2 of the alignment required by
113 /// the whole constant pool, of which the first element must be aligned.
114 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
116 /// getConstantPoolIndex - Create a new entry in the constant pool or return
117 /// an existing one. User must specify an alignment in bytes for the object.
118 unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
119 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
121 /// isEmpty - Return true if this constant pool contains no constants.
122 bool isEmpty() const { return Constants.empty(); }
124 const std::vector<MachineConstantPoolEntry> &getConstants() const {
128 /// print - Used by the MachineFunction printer to print information about
129 /// constant pool objects. Implemented in MachineFunction.cpp
131 void print(std::ostream &OS) const;
133 /// dump - Call print(std::cerr) to be called from the debugger.
138 } // End llvm namespace