MachineConstantPool.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// @file
  15. /// This file declares the MachineConstantPool class which is an abstract
  16. /// constant pool to keep track of constants referenced by a function.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
  20. #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/MC/SectionKind.h"
  23. #include "llvm/Support/Alignment.h"
  24. #include <climits>
  25. #include <vector>
  26. namespace llvm {
  27. class Constant;
  28. class DataLayout;
  29. class FoldingSetNodeID;
  30. class MachineConstantPool;
  31. class raw_ostream;
  32. class Type;
  33. /// Abstract base class for all machine specific constantpool value subclasses.
  34. ///
  35. class MachineConstantPoolValue {
  36. virtual void anchor();
  37. Type *Ty;
  38. public:
  39. explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
  40. virtual ~MachineConstantPoolValue() = default;
  41. Type *getType() const { return Ty; }
  42. virtual unsigned getSizeInBytes(const DataLayout &DL) const;
  43. virtual int getExistingMachineCPValue(MachineConstantPool *CP,
  44. Align Alignment) = 0;
  45. virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
  46. /// print - Implement operator<<
  47. virtual void print(raw_ostream &O) const = 0;
  48. };
  49. inline raw_ostream &operator<<(raw_ostream &OS,
  50. const MachineConstantPoolValue &V) {
  51. V.print(OS);
  52. return OS;
  53. }
  54. /// This class is a data container for one entry in a MachineConstantPool.
  55. /// It contains a pointer to the value and an offset from the start of
  56. /// the constant pool.
  57. /// An entry in a MachineConstantPool
  58. class MachineConstantPoolEntry {
  59. public:
  60. /// The constant itself.
  61. union {
  62. const Constant *ConstVal;
  63. MachineConstantPoolValue *MachineCPVal;
  64. } Val;
  65. /// The required alignment for this entry.
  66. Align Alignment;
  67. bool IsMachineConstantPoolEntry;
  68. MachineConstantPoolEntry(const Constant *V, Align A)
  69. : Alignment(A), IsMachineConstantPoolEntry(false) {
  70. Val.ConstVal = V;
  71. }
  72. MachineConstantPoolEntry(MachineConstantPoolValue *V, Align A)
  73. : Alignment(A), IsMachineConstantPoolEntry(true) {
  74. Val.MachineCPVal = V;
  75. }
  76. /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
  77. /// is indeed a target specific constantpool entry, not a wrapper over a
  78. /// Constant.
  79. bool isMachineConstantPoolEntry() const { return IsMachineConstantPoolEntry; }
  80. Align getAlign() const { return Alignment; }
  81. unsigned getSizeInBytes(const DataLayout &DL) const;
  82. /// This method classifies the entry according to whether or not it may
  83. /// generate a relocation entry. This must be conservative, so if it might
  84. /// codegen to a relocatable entry, it should say so.
  85. bool needsRelocation() const;
  86. SectionKind getSectionKind(const DataLayout *DL) const;
  87. };
  88. /// The MachineConstantPool class keeps track of constants referenced by a
  89. /// function which must be spilled to memory. This is used for constants which
  90. /// are unable to be used directly as operands to instructions, which typically
  91. /// include floating point and large integer constants.
  92. ///
  93. /// Instructions reference the address of these constant pool constants through
  94. /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
  95. /// code, these virtual address references are converted to refer to the
  96. /// address of the function constant pool values.
  97. /// The machine constant pool.
  98. class MachineConstantPool {
  99. Align PoolAlignment; ///< The alignment for the pool.
  100. std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
  101. /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
  102. DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
  103. const DataLayout &DL;
  104. const DataLayout &getDataLayout() const { return DL; }
  105. public:
  106. /// The only constructor.
  107. explicit MachineConstantPool(const DataLayout &DL)
  108. : PoolAlignment(1), DL(DL) {}
  109. ~MachineConstantPool();
  110. /// Return the alignment required by the whole constant pool, of which the
  111. /// first element must be aligned.
  112. Align getConstantPoolAlign() const { return PoolAlignment; }
  113. /// getConstantPoolIndex - Create a new entry in the constant pool or return
  114. /// an existing one. User must specify the minimum required alignment for
  115. /// the object.
  116. unsigned getConstantPoolIndex(const Constant *C, Align Alignment);
  117. unsigned getConstantPoolIndex(MachineConstantPoolValue *V, Align Alignment);
  118. /// isEmpty - Return true if this constant pool contains no constants.
  119. bool isEmpty() const { return Constants.empty(); }
  120. const std::vector<MachineConstantPoolEntry> &getConstants() const {
  121. return Constants;
  122. }
  123. /// print - Used by the MachineFunction printer to print information about
  124. /// constant pool objects. Implemented in MachineFunction.cpp
  125. void print(raw_ostream &OS) const;
  126. /// dump - Call print(cerr) to be called from the debugger.
  127. void dump() const;
  128. };
  129. } // end namespace llvm
  130. #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H
  131. #ifdef __GNUC__
  132. #pragma GCC diagnostic pop
  133. #endif