1//===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
2//
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
6//
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 "llvm/Support/Alignment.h"
21#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
44  Type *getType() const { return Ty; }
45
46  virtual unsigned getSizeInBytes(const DataLayout &DL) const;
47
48  virtual int getExistingMachineCPValue(MachineConstantPool *CP,
49                                        Align Alignment) = 0;
50
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.
66/// An entry in a MachineConstantPool
67class MachineConstantPoolEntry {
68public:
69  /// The constant itself.
70  union {
71    const Constant *ConstVal;
72    MachineConstantPoolValue *MachineCPVal;
73  } Val;
74
75  /// The required alignment for this entry.
76  Align Alignment;
77
78  bool IsMachineConstantPoolEntry;
79
80  MachineConstantPoolEntry(const Constant *V, Align A)
81      : Alignment(A), IsMachineConstantPoolEntry(false) {
82    Val.ConstVal = V;
83  }
84
85  MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A)
86      : Alignment(A), IsMachineConstantPoolEntry(true) {
87    Val.MachineCPVal = V;
88  }
89
90  /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
91  /// is indeed a target specific constantpool entry, not a wrapper over a
92  /// Constant.
93  bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; }
94
95  Align getAlign() const { return Alignment; }
96
97  unsigned getSizeInBytes(const DataLayout &DL) const;
98
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.
116/// The machine constant pool.
117class 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;
123
124  const DataLayout &getDataLayout() const { return DL; }
125
126public:
127  /// The only constructor.
128  explicit MachineConstantPool(const DataLayout &DL)
129      : PoolAlignment(1), DL(DL) {}
130  ~MachineConstantPool();
131
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; }
135
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.
139  unsigned getConstantPoolIndex(const Constant *C, Align Alignment);
140  unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment);
141
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
160