MachineConstantPool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
  16. #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/MC/SectionKind.h"
  19. #include <cassert>
  20. #include <climits>
  21. #include <vector>
  22. namespace llvm {
  23. class Constant;
  24. class FoldingSetNodeID;
  25. class DataLayout;
  26. class TargetMachine;
  27. class Type;
  28. class MachineConstantPool;
  29. class raw_ostream;
  30. /// Abstract base class for all machine specific constantpool value subclasses.
  31. ///
  32. class MachineConstantPoolValue {
  33. virtual void anchor();
  34. Type *Ty;
  35. public:
  36. explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
  37. virtual ~MachineConstantPoolValue() {}
  38. /// getType - get type of this MachineConstantPoolValue.
  39. ///
  40. Type *getType() const { return Ty; }
  41. /// getRelocationInfo - This method classifies the entry according to
  42. /// whether or not it may generate a relocation entry. This must be
  43. /// conservative, so if it might codegen to a relocatable entry, it should say
  44. /// so. The return values are the same as Constant::getRelocationInfo().
  45. virtual unsigned getRelocationInfo() const = 0;
  46. virtual int getExistingMachineCPValue(MachineConstantPool *CP,
  47. unsigned Alignment) = 0;
  48. virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
  49. /// print - Implement operator<<
  50. virtual void print(raw_ostream &O) const = 0;
  51. };
  52. inline raw_ostream &operator<<(raw_ostream &OS,
  53. const MachineConstantPoolValue &V) {
  54. V.print(OS);
  55. return OS;
  56. }
  57. /// This class is a data container for one entry in a MachineConstantPool.
  58. /// It contains a pointer to the value and an offset from the start of
  59. /// the constant pool.
  60. /// @brief An entry in a MachineConstantPool
  61. class MachineConstantPoolEntry {
  62. public:
  63. /// The constant itself.
  64. union {
  65. const Constant *ConstVal;
  66. MachineConstantPoolValue *MachineCPVal;
  67. } Val;
  68. /// The required alignment for this entry. The top bit is set when Val is
  69. /// a target specific MachineConstantPoolValue.
  70. unsigned Alignment;
  71. MachineConstantPoolEntry(const Constant *V, unsigned A)
  72. : Alignment(A) {
  73. Val.ConstVal = V;
  74. }
  75. MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
  76. : Alignment(A) {
  77. Val.MachineCPVal = V;
  78. Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
  79. }
  80. /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
  81. /// is indeed a target specific constantpool entry, not a wrapper over a
  82. /// Constant.
  83. bool isMachineConstantPoolEntry() const {
  84. return (int)Alignment < 0;
  85. }
  86. int getAlignment() const {
  87. return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
  88. }
  89. Type *getType() const;
  90. /// getRelocationInfo - This method classifies the entry according to
  91. /// whether or not it may generate a relocation entry. This must be
  92. /// conservative, so if it might codegen to a relocatable entry, it should say
  93. /// so. The return values are:
  94. ///
  95. /// 0: This constant pool entry is guaranteed to never have a relocation
  96. /// applied to it (because it holds a simple constant like '4').
  97. /// 1: This entry has relocations, but the entries are guaranteed to be
  98. /// resolvable by the static linker, so the dynamic linker will never see
  99. /// them.
  100. /// 2: This entry may have arbitrary relocations.
  101. unsigned getRelocationInfo() const;
  102. SectionKind getSectionKind(const DataLayout *DL) const;
  103. };
  104. /// The MachineConstantPool class keeps track of constants referenced by a
  105. /// function which must be spilled to memory. This is used for constants which
  106. /// are unable to be used directly as operands to instructions, which typically
  107. /// include floating point and large integer constants.
  108. ///
  109. /// Instructions reference the address of these constant pool constants through
  110. /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
  111. /// code, these virtual address references are converted to refer to the
  112. /// address of the function constant pool values.
  113. /// @brief The machine constant pool.
  114. class MachineConstantPool {
  115. unsigned PoolAlignment; ///< The alignment for the pool.
  116. std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
  117. /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
  118. DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
  119. const DataLayout &DL;
  120. const DataLayout &getDataLayout() const { return DL; }
  121. public:
  122. /// @brief The only constructor.
  123. explicit MachineConstantPool(const DataLayout &DL)
  124. : PoolAlignment(1), DL(DL) {}
  125. ~MachineConstantPool();
  126. /// getConstantPoolAlignment - Return the alignment required by
  127. /// the whole constant pool, of which the first element must be aligned.
  128. unsigned getConstantPoolAlignment() const { return PoolAlignment; }
  129. /// getConstantPoolIndex - Create a new entry in the constant pool or return
  130. /// an existing one. User must specify the minimum required alignment for
  131. /// the object.
  132. unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
  133. unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
  134. /// isEmpty - Return true if this constant pool contains no constants.
  135. bool isEmpty() const { return Constants.empty(); }
  136. const std::vector<MachineConstantPoolEntry> &getConstants() const {
  137. return Constants;
  138. }
  139. /// print - Used by the MachineFunction printer to print information about
  140. /// constant pool objects. Implemented in MachineFunction.cpp
  141. ///
  142. void print(raw_ostream &OS) const;
  143. /// dump - Call print(cerr) to be called from the debugger.
  144. void dump() const;
  145. };
  146. } // End llvm namespace
  147. #endif