Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:30

0001 //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// @file
0010 /// This file declares the MachineConstantPool class which is an abstract
0011 /// constant pool to keep track of constants referenced by a function.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
0016 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
0017 
0018 #include "llvm/ADT/DenseSet.h"
0019 #include "llvm/MC/SectionKind.h"
0020 #include "llvm/Support/Alignment.h"
0021 #include <climits>
0022 #include <vector>
0023 
0024 namespace llvm {
0025 
0026 class Constant;
0027 class DataLayout;
0028 class FoldingSetNodeID;
0029 class MachineConstantPool;
0030 class raw_ostream;
0031 class Type;
0032 
0033 /// Abstract base class for all machine specific constantpool value subclasses.
0034 ///
0035 class MachineConstantPoolValue {
0036   virtual void anchor();
0037 
0038   Type *Ty;
0039 
0040 public:
0041   explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
0042   virtual ~MachineConstantPoolValue() = default;
0043 
0044   Type *getType() const { return Ty; }
0045 
0046   virtual unsigned getSizeInBytes(const DataLayout &DL) const;
0047 
0048   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
0049                                         Align Alignment) = 0;
0050 
0051   virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
0052 
0053   /// print - Implement operator<<
0054   virtual void print(raw_ostream &O) const = 0;
0055 };
0056 
0057 inline raw_ostream &operator<<(raw_ostream &OS,
0058                                const MachineConstantPoolValue &V) {
0059   V.print(OS);
0060   return OS;
0061 }
0062 
0063 /// This class is a data container for one entry in a MachineConstantPool.
0064 /// It contains a pointer to the value and an offset from the start of
0065 /// the constant pool.
0066 /// An entry in a MachineConstantPool
0067 class MachineConstantPoolEntry {
0068 public:
0069   /// The constant itself.
0070   union {
0071     const Constant *ConstVal;
0072     MachineConstantPoolValue *MachineCPVal;
0073   } Val;
0074 
0075   /// The required alignment for this entry.
0076   Align Alignment;
0077 
0078   bool IsMachineConstantPoolEntry;
0079 
0080   MachineConstantPoolEntry(const Constant *V, Align A)
0081       : Alignment(A), IsMachineConstantPoolEntry(false) {
0082     Val.ConstVal = V;
0083   }
0084 
0085   MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A)
0086       : Alignment(A), IsMachineConstantPoolEntry(true) {
0087     Val.MachineCPVal = V;
0088   }
0089 
0090   /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
0091   /// is indeed a target specific constantpool entry, not a wrapper over a
0092   /// Constant.
0093   bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; }
0094 
0095   Align getAlign() const { return Alignment; }
0096 
0097   unsigned getSizeInBytes(const DataLayout &DL) const;
0098 
0099   /// This method classifies the entry according to whether or not it may
0100   /// generate a relocation entry.  This must be conservative, so if it might
0101   /// codegen to a relocatable entry, it should say so.
0102   bool needsRelocation() const;
0103 
0104   SectionKind getSectionKind(const DataLayout *DL) const;
0105 };
0106 
0107 /// The MachineConstantPool class keeps track of constants referenced by a
0108 /// function which must be spilled to memory.  This is used for constants which
0109 /// are unable to be used directly as operands to instructions, which typically
0110 /// include floating point and large integer constants.
0111 ///
0112 /// Instructions reference the address of these constant pool constants through
0113 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
0114 /// code, these virtual address references are converted to refer to the
0115 /// address of the function constant pool values.
0116 /// The machine constant pool.
0117 class MachineConstantPool {
0118   Align PoolAlignment; ///< The alignment for the pool.
0119   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
0120   /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
0121   DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
0122   const DataLayout &DL;
0123 
0124   const DataLayout &getDataLayout() const { return DL; }
0125 
0126 public:
0127   /// The only constructor.
0128   explicit MachineConstantPool(const DataLayout &DL)
0129       : PoolAlignment(1), DL(DL) {}
0130   ~MachineConstantPool();
0131 
0132   /// Return the alignment required by the whole constant pool, of which the
0133   /// first element must be aligned.
0134   Align getConstantPoolAlign() const { return PoolAlignment; }
0135 
0136   /// getConstantPoolIndex - Create a new entry in the constant pool or return
0137   /// an existing one.  User must specify the minimum required alignment for
0138   /// the object.
0139   unsigned getConstantPoolIndex(const Constant *C, Align Alignment);
0140   unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment);
0141 
0142   /// isEmpty - Return true if this constant pool contains no constants.
0143   bool isEmpty() const { return Constants.empty(); }
0144 
0145   const std::vector<MachineConstantPoolEntry> &getConstants() const {
0146     return Constants;
0147   }
0148 
0149   /// print - Used by the MachineFunction printer to print information about
0150   /// constant pool objects.  Implemented in MachineFunction.cpp
0151   void print(raw_ostream &OS) const;
0152 
0153   /// dump - Call print(cerr) to be called from the debugger.
0154   void dump() const;
0155 };
0156 
0157 } // end namespace llvm
0158 
0159 #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H