Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 20 | #include "llvm/Support/Alignment.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | #include <climits> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class Constant; |
| 27 | class DataLayout; |
| 28 | class FoldingSetNodeID; |
| 29 | class MachineConstantPool; |
| 30 | class raw_ostream; |
| 31 | class Type; |
| 32 | |
| 33 | /// Abstract base class for all machine specific constantpool value subclasses. |
| 34 | /// |
| 35 | class MachineConstantPoolValue { |
| 36 | virtual void anchor(); |
| 37 | |
| 38 | Type *Ty; |
| 39 | |
| 40 | public: |
| 41 | explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {} |
| 42 | virtual ~MachineConstantPoolValue() = default; |
| 43 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | Type *getType() const { return Ty; } |
| 45 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 46 | virtual unsigned getSizeInBytes(const DataLayout &DL) const; |
| 47 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | virtual int getExistingMachineCPValue(MachineConstantPool *CP, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 49 | Align Alignment) = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | |
| 51 | virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0; |
| 52 | |
| 53 | /// print - Implement operator<< |
| 54 | virtual void print(raw_ostream &O) const = 0; |
| 55 | }; |
| 56 | |
| 57 | inline 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | /// An entry in a MachineConstantPool |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | class MachineConstantPoolEntry { |
| 68 | public: |
| 69 | /// The constant itself. |
| 70 | union { |
| 71 | const Constant *ConstVal; |
| 72 | MachineConstantPoolValue *MachineCPVal; |
| 73 | } Val; |
| 74 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 75 | /// The required alignment for this entry. |
| 76 | Align Alignment; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 78 | bool IsMachineConstantPoolEntry; |
| 79 | |
| 80 | MachineConstantPoolEntry(const Constant *V, Align A) |
| 81 | : Alignment(A), IsMachineConstantPoolEntry(false) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 82 | Val.ConstVal = V; |
| 83 | } |
| 84 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 85 | MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A) |
| 86 | : Alignment(A), IsMachineConstantPoolEntry(true) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | Val.MachineCPVal = V; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry |
| 91 | /// is indeed a target specific constantpool entry, not a wrapper over a |
| 92 | /// Constant. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 93 | bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 94 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 95 | Align getAlign() const { return Alignment; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 97 | unsigned getSizeInBytes(const DataLayout &DL) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 116 | /// The machine constant pool. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | class MachineConstantPool { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 118 | Align PoolAlignment; ///< The alignment for the pool. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
| 126 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 127 | /// The only constructor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 128 | explicit MachineConstantPool(const DataLayout &DL) |
| 129 | : PoolAlignment(1), DL(DL) {} |
| 130 | ~MachineConstantPool(); |
| 131 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 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; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 139 | unsigned getConstantPoolIndex(const Constant *C, Align Alignment); |
| 140 | unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 |