Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// @file |
| 11 | /// This file declares the MachineConstantPool class which is an abstract |
| 12 | /// constant pool to keep track of constants referenced by a function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H |
| 17 | #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H |
| 18 | |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | #include "llvm/MC/SectionKind.h" |
| 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 | |
| 44 | /// getType - get type of this MachineConstantPoolValue. |
| 45 | /// |
| 46 | Type *getType() const { return Ty; } |
| 47 | |
| 48 | virtual int getExistingMachineCPValue(MachineConstantPool *CP, |
| 49 | unsigned 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 | |
| 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 | |
| 75 | /// The required alignment for this entry. The top bit is set when Val is |
| 76 | /// a target specific MachineConstantPoolValue. |
| 77 | unsigned Alignment; |
| 78 | |
| 79 | MachineConstantPoolEntry(const Constant *V, unsigned A) |
| 80 | : Alignment(A) { |
| 81 | Val.ConstVal = V; |
| 82 | } |
| 83 | |
| 84 | MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A) |
| 85 | : Alignment(A) { |
| 86 | Val.MachineCPVal = V; |
| 87 | Alignment |= 1U << (sizeof(unsigned) * CHAR_BIT - 1); |
| 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 { |
| 94 | return (int)Alignment < 0; |
| 95 | } |
| 96 | |
| 97 | int getAlignment() const { |
| 98 | return Alignment & ~(1 << (sizeof(unsigned) * CHAR_BIT - 1)); |
| 99 | } |
| 100 | |
| 101 | Type *getType() const; |
| 102 | |
| 103 | /// This method classifies the entry according to whether or not it may |
| 104 | /// generate a relocation entry. This must be conservative, so if it might |
| 105 | /// codegen to a relocatable entry, it should say so. |
| 106 | bool needsRelocation() const; |
| 107 | |
| 108 | SectionKind getSectionKind(const DataLayout *DL) const; |
| 109 | }; |
| 110 | |
| 111 | /// The MachineConstantPool class keeps track of constants referenced by a |
| 112 | /// function which must be spilled to memory. This is used for constants which |
| 113 | /// are unable to be used directly as operands to instructions, which typically |
| 114 | /// include floating point and large integer constants. |
| 115 | /// |
| 116 | /// Instructions reference the address of these constant pool constants through |
| 117 | /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine |
| 118 | /// code, these virtual address references are converted to refer to the |
| 119 | /// address of the function constant pool values. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 120 | /// The machine constant pool. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 121 | class MachineConstantPool { |
| 122 | unsigned PoolAlignment; ///< The alignment for the pool. |
| 123 | std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants. |
| 124 | /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry. |
| 125 | DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries; |
| 126 | const DataLayout &DL; |
| 127 | |
| 128 | const DataLayout &getDataLayout() const { return DL; } |
| 129 | |
| 130 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 131 | /// The only constructor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 132 | explicit MachineConstantPool(const DataLayout &DL) |
| 133 | : PoolAlignment(1), DL(DL) {} |
| 134 | ~MachineConstantPool(); |
| 135 | |
| 136 | /// getConstantPoolAlignment - Return the alignment required by |
| 137 | /// the whole constant pool, of which the first element must be aligned. |
| 138 | unsigned getConstantPoolAlignment() const { return PoolAlignment; } |
| 139 | |
| 140 | /// getConstantPoolIndex - Create a new entry in the constant pool or return |
| 141 | /// an existing one. User must specify the minimum required alignment for |
| 142 | /// the object. |
| 143 | unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment); |
| 144 | unsigned getConstantPoolIndex(MachineConstantPoolValue *V, |
| 145 | unsigned Alignment); |
| 146 | |
| 147 | /// isEmpty - Return true if this constant pool contains no constants. |
| 148 | bool isEmpty() const { return Constants.empty(); } |
| 149 | |
| 150 | const std::vector<MachineConstantPoolEntry> &getConstants() const { |
| 151 | return Constants; |
| 152 | } |
| 153 | |
| 154 | /// print - Used by the MachineFunction printer to print information about |
| 155 | /// constant pool objects. Implemented in MachineFunction.cpp |
| 156 | void print(raw_ostream &OS) const; |
| 157 | |
| 158 | /// dump - Call print(cerr) to be called from the debugger. |
| 159 | void dump() const; |
| 160 | }; |
| 161 | |
| 162 | } // end namespace llvm |
| 163 | |
| 164 | #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H |