blob: a9bc0ce300b22d4130dc7829501b05aaaca8bdcb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9/// @file
10/// This file declares the MachineConstantPool class which is an abstract
11/// constant pool to keep track of constants referenced by a function.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
16#define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/MC/SectionKind.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020020#include "llvm/Support/Alignment.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021#include <climits>
22#include <vector>
23
24namespace llvm {
25
26class Constant;
27class DataLayout;
28class FoldingSetNodeID;
29class MachineConstantPool;
30class raw_ostream;
31class Type;
32
33/// Abstract base class for all machine specific constantpool value subclasses.
34///
35class MachineConstantPoolValue {
36 virtual void anchor();
37
38 Type *Ty;
39
40public:
41 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
42 virtual ~MachineConstantPoolValue() = default;
43
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 Type *getType() const { return Ty; }
45
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020046 virtual unsigned getSizeInBytes(const DataLayout &DL) const;
47
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020049 Align Alignment) = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050
51 virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
52
53 /// print - Implement operator<<
54 virtual void print(raw_ostream &O) const = 0;
55};
56
57inline raw_ostream &operator<<(raw_ostream &OS,
58 const MachineConstantPoolValue &V) {
59 V.print(OS);
60 return OS;
61}
62
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010066/// An entry in a MachineConstantPool
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067class MachineConstantPoolEntry {
68public:
69 /// The constant itself.
70 union {
71 const Constant *ConstVal;
72 MachineConstantPoolValue *MachineCPVal;
73 } Val;
74
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020075 /// The required alignment for this entry.
76 Align Alignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020078 bool IsMachineConstantPoolEntry;
79
80 MachineConstantPoolEntry(const Constant *V, Align A)
81 : Alignment(A), IsMachineConstantPoolEntry(false) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010082 Val.ConstVal = V;
83 }
84
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020085 MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A)
86 : Alignment(A), IsMachineConstantPoolEntry(true) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 Val.MachineCPVal = V;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088 }
89
90 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
91 /// is indeed a target specific constantpool entry, not a wrapper over a
92 /// Constant.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020093 bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 Align getAlign() const { return Alignment; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020097 unsigned getSizeInBytes(const DataLayout &DL) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098
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;
103
104 SectionKind getSectionKind(const DataLayout *DL) const;
105};
106
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.
111///
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100116/// The machine constant pool.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117class MachineConstantPool {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200118 Align PoolAlignment; ///< The alignment for the pool.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119 std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
120 /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
121 DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
122 const DataLayout &DL;
123
124 const DataLayout &getDataLayout() const { return DL; }
125
126public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100127 /// The only constructor.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 explicit MachineConstantPool(const DataLayout &DL)
129 : PoolAlignment(1), DL(DL) {}
130 ~MachineConstantPool();
131
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200132 /// Return the alignment required by the whole constant pool, of which the
133 /// first element must be aligned.
134 Align getConstantPoolAlign() const { return PoolAlignment; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135
136 /// getConstantPoolIndex - Create a new entry in the constant pool or return
137 /// an existing one. User must specify the minimum required alignment for
138 /// the object.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200139 unsigned getConstantPoolIndex(const Constant *C, Align Alignment);
140 unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100141
142 /// isEmpty - Return true if this constant pool contains no constants.
143 bool isEmpty() const { return Constants.empty(); }
144
145 const std::vector<MachineConstantPoolEntry> &getConstants() const {
146 return Constants;
147 }
148
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;
152
153 /// dump - Call print(cerr) to be called from the debugger.
154 void dump() const;
155};
156
157} // end namespace llvm
158
159#endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H