blob: 4d07b620a4b4c372347e1a3f79e3d83a0e6ae2f4 [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"
20#include <climits>
21#include <vector>
22
23namespace llvm {
24
25class Constant;
26class DataLayout;
27class FoldingSetNodeID;
28class MachineConstantPool;
29class raw_ostream;
30class Type;
31
32/// Abstract base class for all machine specific constantpool value subclasses.
33///
34class MachineConstantPoolValue {
35 virtual void anchor();
36
37 Type *Ty;
38
39public:
40 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
41 virtual ~MachineConstantPoolValue() = default;
42
43 /// getType - get type of this MachineConstantPoolValue.
44 ///
45 Type *getType() const { return Ty; }
46
47 virtual int getExistingMachineCPValue(MachineConstantPool *CP,
48 unsigned Alignment) = 0;
49
50 virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
51
52 /// print - Implement operator<<
53 virtual void print(raw_ostream &O) const = 0;
54};
55
56inline raw_ostream &operator<<(raw_ostream &OS,
57 const MachineConstantPoolValue &V) {
58 V.print(OS);
59 return OS;
60}
61
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065/// An entry in a MachineConstantPool
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066class MachineConstantPoolEntry {
67public:
68 /// The constant itself.
69 union {
70 const Constant *ConstVal;
71 MachineConstantPoolValue *MachineCPVal;
72 } Val;
73
74 /// The required alignment for this entry. The top bit is set when Val is
75 /// a target specific MachineConstantPoolValue.
76 unsigned Alignment;
77
78 MachineConstantPoolEntry(const Constant *V, unsigned A)
79 : Alignment(A) {
80 Val.ConstVal = V;
81 }
82
83 MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
84 : Alignment(A) {
85 Val.MachineCPVal = V;
86 Alignment |= 1U << (sizeof(unsigned) * CHAR_BIT - 1);
87 }
88
89 /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
90 /// is indeed a target specific constantpool entry, not a wrapper over a
91 /// Constant.
92 bool isMachineConstantPoolEntry() const {
93 return (int)Alignment < 0;
94 }
95
96 int getAlignment() const {
97 return Alignment & ~(1 << (sizeof(unsigned) * CHAR_BIT - 1));
98 }
99
100 Type *getType() const;
101
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;
106
107 SectionKind getSectionKind(const DataLayout *DL) const;
108};
109
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.
114///
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119/// The machine constant pool.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120class 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;
126
127 const DataLayout &getDataLayout() const { return DL; }
128
129public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100130 /// The only constructor.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 explicit MachineConstantPool(const DataLayout &DL)
132 : PoolAlignment(1), DL(DL) {}
133 ~MachineConstantPool();
134
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; }
138
139 /// getConstantPoolIndex - Create a new entry in the constant pool or return
140 /// an existing one. User must specify the minimum required alignment for
141 /// the object.
142 unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
143 unsigned getConstantPoolIndex(MachineConstantPoolValue *V,
144 unsigned Alignment);
145
146 /// isEmpty - Return true if this constant pool contains no constants.
147 bool isEmpty() const { return Constants.empty(); }
148
149 const std::vector<MachineConstantPoolEntry> &getConstants() const {
150 return Constants;
151 }
152
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;
156
157 /// dump - Call print(cerr) to be called from the debugger.
158 void dump() const;
159};
160
161} // end namespace llvm
162
163#endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H