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 <climits> 21 #include <vector> 22 23 namespace llvm { 24 25 class Constant; 26 class DataLayout; 27 class FoldingSetNodeID; 28 class MachineConstantPool; 29 class raw_ostream; 30 class Type; 31 32 /// Abstract base class for all machine specific constantpool value subclasses. 33 /// 34 class MachineConstantPoolValue { 35 virtual void anchor(); 36 37 Type *Ty; 38 39 public: MachineConstantPoolValue(Type * ty)40 explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {} 41 virtual ~MachineConstantPoolValue() = default; 42 43 /// getType - get type of this MachineConstantPoolValue. 44 /// getType()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 56 inline 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. 65 /// An entry in a MachineConstantPool 66 class MachineConstantPoolEntry { 67 public: 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 MachineConstantPoolEntry(const Constant * V,unsigned A)78 MachineConstantPoolEntry(const Constant *V, unsigned A) 79 : Alignment(A) { 80 Val.ConstVal = V; 81 } 82 MachineConstantPoolEntry(MachineConstantPoolValue * V,unsigned A)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. isMachineConstantPoolEntry()92 bool isMachineConstantPoolEntry() const { 93 return (int)Alignment < 0; 94 } 95 getAlignment()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. 119 /// The machine constant pool. 120 class 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 getDataLayout()127 const DataLayout &getDataLayout() const { return DL; } 128 129 public: 130 /// The only constructor. MachineConstantPool(const DataLayout & DL)131 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. getConstantPoolAlignment()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. isEmpty()147 bool isEmpty() const { return Constants.empty(); } 148 getConstants()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 164