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
24 class FoldingSetNodeID;
28 class MachineConstantPool;
31 /// Abstract base class for all machine specific constantpool value subclasses.
33 class MachineConstantPoolValue {
37 explicit MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
38 virtual ~MachineConstantPoolValue() {}
40 /// getType - get type of this MachineConstantPoolValue.
42 inline const Type *getType() const { return Ty; }
44 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
45 unsigned Alignment) = 0;
47 virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
49 /// print - Implement operator<<
50 virtual void print(raw_ostream &O) const = 0;
53 inline raw_ostream &operator<<(raw_ostream &OS,
54 const MachineConstantPoolValue &V) {
60 /// This class is a data container for one entry in a MachineConstantPool.
61 /// It contains a pointer to the value and an offset from the start of
62 /// the constant pool.
63 /// @brief An entry in a MachineConstantPool
64 class MachineConstantPoolEntry {
66 /// The constant itself.
69 MachineConstantPoolValue *MachineCPVal;
72 /// The offset of the constant from the start of the pool. The top bit is set
73 /// when Val is a MachineConstantPoolValue.
76 MachineConstantPoolEntry(Constant *V, unsigned O)
78 assert((int)Offset >= 0 && "Offset is too large");
81 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned O)
83 assert((int)Offset >= 0 && "Offset is too large");
85 Offset |= 1 << (sizeof(unsigned)*8-1);
88 bool isMachineConstantPoolEntry() const {
89 return (int)Offset < 0;
92 int getOffset() const {
93 return Offset & ~(1 << (sizeof(unsigned)*8-1));
96 const Type *getType() const;
99 /// The MachineConstantPool class keeps track of constants referenced by a
100 /// function which must be spilled to memory. This is used for constants which
101 /// are unable to be used directly as operands to instructions, which typically
102 /// include floating point and large integer constants.
104 /// Instructions reference the address of these constant pool constants through
105 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
106 /// code, these virtual address references are converted to refer to the
107 /// address of the function constant pool values.
108 /// @brief The machine constant pool.
109 class MachineConstantPool {
110 const TargetData *TD; ///< The machine's TargetData.
111 unsigned PoolAlignment; ///< The alignment for the pool.
112 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
114 /// @brief The only constructor.
115 explicit MachineConstantPool(const TargetData *td)
116 : TD(td), PoolAlignment(1) {}
117 ~MachineConstantPool();
119 /// getConstantPoolAlignment - Return the log2 of the alignment required by
120 /// the whole constant pool, of which the first element must be aligned.
121 unsigned getConstantPoolAlignment() const { return PoolAlignment; }
123 /// getConstantPoolIndex - Create a new entry in the constant pool or return
124 /// an existing one. User must specify the log2 of the minimum required
125 /// alignment for the object.
126 unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
127 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
129 /// isEmpty - Return true if this constant pool contains no constants.
130 bool isEmpty() const { return Constants.empty(); }
132 const std::vector<MachineConstantPoolEntry> &getConstants() const {
136 /// print - Used by the MachineFunction printer to print information about
137 /// constant pool objects. Implemented in MachineFunction.cpp
139 void print(raw_ostream &OS) const;
141 /// dump - Call print(cerr) to be called from the debugger.
145 } // End llvm namespace