1 //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// 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/DenseSet.h"
19 #include "llvm/MC/SectionKind.h"
20 #include "llvm/Support/Alignment.h"
28 class FoldingSetNodeID;
29 class MachineConstantPool;
33 /// Abstract base class for all machine specific constantpool value subclasses.
35 class MachineConstantPoolValue {
36 virtual void anchor();
41 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
42 virtual ~MachineConstantPoolValue() = default;
44 /// getType - get type of this MachineConstantPoolValue.
46 Type *getType() const { return Ty; }
48 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
51 virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
53 /// print - Implement operator<<
54 virtual void print(raw_ostream &O) const = 0;
57 inline raw_ostream &operator<<(raw_ostream &OS,
58 const MachineConstantPoolValue &V) {
63 /// This class is a data container for one entry in a MachineConstantPool.
64 /// It contains a pointer to the value and an offset from the start of
65 /// the constant pool.
66 /// An entry in a MachineConstantPool
67 class MachineConstantPoolEntry {
69 /// The constant itself.
71 const Constant *ConstVal;
72 MachineConstantPoolValue *MachineCPVal;
75 /// The required alignment for this entry.
78 bool IsMachineConstantPoolEntry;
80 MachineConstantPoolEntry(const Constant *V, Align A)
81 : Alignment(A), IsMachineConstantPoolEntry(false) {
85 MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A)
86 : Alignment(A), IsMachineConstantPoolEntry(true) {
90 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
91 /// is indeed a target specific constantpool entry, not a wrapper over a
93 bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; }
95 Align getAlign() const { return Alignment; }
97 Type *getType() const;
99 /// This method classifies the entry according to whether or not it may
100 /// generate a relocation entry. This must be conservative, so if it might
101 /// codegen to a relocatable entry, it should say so.
102 bool needsRelocation() const;
104 SectionKind getSectionKind(const DataLayout *DL) const;
107 /// The MachineConstantPool class keeps track of constants referenced by a
108 /// function which must be spilled to memory. This is used for constants which
109 /// are unable to be used directly as operands to instructions, which typically
110 /// include floating point and large integer constants.
112 /// Instructions reference the address of these constant pool constants through
113 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
114 /// code, these virtual address references are converted to refer to the
115 /// address of the function constant pool values.
116 /// The machine constant pool.
117 class MachineConstantPool {
118 Align PoolAlignment; ///< The alignment for the pool.
119 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
120 /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
121 DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
122 const DataLayout &DL;
124 const DataLayout &getDataLayout() const { return DL; }
127 /// The only constructor.
128 explicit MachineConstantPool(const DataLayout &DL)
129 : PoolAlignment(1), DL(DL) {}
130 ~MachineConstantPool();
132 /// Return the alignment required by the whole constant pool, of which the
133 /// first element must be aligned.
134 Align getConstantPoolAlign() const { return PoolAlignment; }
136 /// getConstantPoolIndex - Create a new entry in the constant pool or return
137 /// an existing one. User must specify the minimum required alignment for
139 unsigned getConstantPoolIndex(const Constant *C, Align Alignment);
140 unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment);
142 /// isEmpty - Return true if this constant pool contains no constants.
143 bool isEmpty() const { return Constants.empty(); }
145 const std::vector<MachineConstantPoolEntry> &getConstants() const {
149 /// print - Used by the MachineFunction printer to print information about
150 /// constant pool objects. Implemented in MachineFunction.cpp
151 void print(raw_ostream &OS) const;
153 /// dump - Call print(cerr) to be called from the debugger.
157 } // end namespace llvm
159 #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H