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"
27 class FoldingSetNodeID
;
28 class MachineConstantPool
;
32 /// Abstract base class for all machine specific constantpool value subclasses.
34 class MachineConstantPoolValue
{
35 virtual void anchor();
40 explicit MachineConstantPoolValue(Type
*ty
) : Ty(ty
) {}
41 virtual ~MachineConstantPoolValue() = default;
43 /// getType - get type of this MachineConstantPoolValue.
45 Type
*getType() const { return Ty
; }
47 virtual int getExistingMachineCPValue(MachineConstantPool
*CP
,
48 unsigned Alignment
) = 0;
50 virtual void addSelectionDAGCSEId(FoldingSetNodeID
&ID
) = 0;
52 /// print - Implement operator<<
53 virtual void print(raw_ostream
&O
) const = 0;
56 inline raw_ostream
&operator<<(raw_ostream
&OS
,
57 const MachineConstantPoolValue
&V
) {
62 /// This class is a data container for one entry in a MachineConstantPool.
63 /// It contains a pointer to the value and an offset from the start of
64 /// the constant pool.
65 /// An entry in a MachineConstantPool
66 class MachineConstantPoolEntry
{
68 /// The constant itself.
70 const Constant
*ConstVal
;
71 MachineConstantPoolValue
*MachineCPVal
;
74 /// The required alignment for this entry. The top bit is set when Val is
75 /// a target specific MachineConstantPoolValue.
78 MachineConstantPoolEntry(const Constant
*V
, unsigned A
)
83 MachineConstantPoolEntry(MachineConstantPoolValue
*V
, unsigned A
)
86 Alignment
|= 1U << (sizeof(unsigned) * CHAR_BIT
- 1);
89 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
90 /// is indeed a target specific constantpool entry, not a wrapper over a
92 bool isMachineConstantPoolEntry() const {
93 return (int)Alignment
< 0;
96 int getAlignment() const {
97 return Alignment
& ~(1 << (sizeof(unsigned) * CHAR_BIT
- 1));
100 Type
*getType() const;
102 /// This method classifies the entry according to whether or not it may
103 /// generate a relocation entry. This must be conservative, so if it might
104 /// codegen to a relocatable entry, it should say so.
105 bool needsRelocation() const;
107 SectionKind
getSectionKind(const DataLayout
*DL
) const;
110 /// The MachineConstantPool class keeps track of constants referenced by a
111 /// function which must be spilled to memory. This is used for constants which
112 /// are unable to be used directly as operands to instructions, which typically
113 /// include floating point and large integer constants.
115 /// Instructions reference the address of these constant pool constants through
116 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
117 /// code, these virtual address references are converted to refer to the
118 /// address of the function constant pool values.
119 /// The machine constant pool.
120 class MachineConstantPool
{
121 unsigned PoolAlignment
; ///< The alignment for the pool.
122 std::vector
<MachineConstantPoolEntry
> Constants
; ///< The pool of constants.
123 /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
124 DenseSet
<MachineConstantPoolValue
*> MachineCPVsSharingEntries
;
125 const DataLayout
&DL
;
127 const DataLayout
&getDataLayout() const { return DL
; }
130 /// The only constructor.
131 explicit MachineConstantPool(const DataLayout
&DL
)
132 : PoolAlignment(1), DL(DL
) {}
133 ~MachineConstantPool();
135 /// getConstantPoolAlignment - Return the alignment required by
136 /// the whole constant pool, of which the first element must be aligned.
137 unsigned getConstantPoolAlignment() const { return PoolAlignment
; }
139 /// getConstantPoolIndex - Create a new entry in the constant pool or return
140 /// an existing one. User must specify the minimum required alignment for
142 unsigned getConstantPoolIndex(const Constant
*C
, unsigned Alignment
);
143 unsigned getConstantPoolIndex(MachineConstantPoolValue
*V
,
146 /// isEmpty - Return true if this constant pool contains no constants.
147 bool isEmpty() const { return Constants
.empty(); }
149 const std::vector
<MachineConstantPoolEntry
> &getConstants() const {
153 /// print - Used by the MachineFunction printer to print information about
154 /// constant pool objects. Implemented in MachineFunction.cpp
155 void print(raw_ostream
&OS
) const;
157 /// dump - Call print(cerr) to be called from the debugger.
161 } // end namespace llvm
163 #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H