MachineMemOperand.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- 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. // This file contains the declaration of the MachineMemOperand class, which is a
  11. // description of a memory reference. It is used to help track dependencies
  12. // in the backend.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
  16. #define LLVM_CODEGEN_MACHINEMEMOPERAND_H
  17. #include "llvm/ADT/PointerUnion.h"
  18. #include "llvm/CodeGen/PseudoSourceValue.h"
  19. #include "llvm/IR/Metadata.h"
  20. #include "llvm/IR/Value.h" // PointerLikeTypeTraits<Value*>
  21. #include "llvm/Support/DataTypes.h"
  22. namespace llvm {
  23. class FoldingSetNodeID;
  24. class MDNode;
  25. class raw_ostream;
  26. class ModuleSlotTracker;
  27. /// MachinePointerInfo - This class contains a discriminated union of
  28. /// information about pointers in memory operands, relating them back to LLVM IR
  29. /// or to virtual locations (such as frame indices) that are exposed during
  30. /// codegen.
  31. struct MachinePointerInfo {
  32. /// V - This is the IR pointer value for the access, or it is null if unknown.
  33. /// If this is null, then the access is to a pointer in the default address
  34. /// space.
  35. PointerUnion<const Value *, const PseudoSourceValue *> V;
  36. /// Offset - This is an offset from the base Value*.
  37. int64_t Offset;
  38. explicit MachinePointerInfo(const Value *v = nullptr, int64_t offset = 0)
  39. : V(v), Offset(offset) {}
  40. explicit MachinePointerInfo(const PseudoSourceValue *v,
  41. int64_t offset = 0)
  42. : V(v), Offset(offset) {}
  43. MachinePointerInfo getWithOffset(int64_t O) const {
  44. if (V.isNull()) return MachinePointerInfo();
  45. if (V.is<const Value*>())
  46. return MachinePointerInfo(V.get<const Value*>(), Offset+O);
  47. return MachinePointerInfo(V.get<const PseudoSourceValue*>(), Offset+O);
  48. }
  49. /// getAddrSpace - Return the LLVM IR address space number that this pointer
  50. /// points into.
  51. unsigned getAddrSpace() const;
  52. /// getConstantPool - Return a MachinePointerInfo record that refers to the
  53. /// constant pool.
  54. static MachinePointerInfo getConstantPool();
  55. /// getFixedStack - Return a MachinePointerInfo record that refers to the
  56. /// the specified FrameIndex.
  57. static MachinePointerInfo getFixedStack(int FI, int64_t offset = 0);
  58. /// getJumpTable - Return a MachinePointerInfo record that refers to a
  59. /// jump table entry.
  60. static MachinePointerInfo getJumpTable();
  61. /// getGOT - Return a MachinePointerInfo record that refers to a
  62. /// GOT entry.
  63. static MachinePointerInfo getGOT();
  64. /// getStack - stack pointer relative access.
  65. static MachinePointerInfo getStack(int64_t Offset);
  66. };
  67. // //
  68. ///////////////////////////////////////////////////////////////////////////////
  69. /// MachineMemOperand - A description of a memory reference used in the backend.
  70. /// Instead of holding a StoreInst or LoadInst, this class holds the address
  71. /// Value of the reference along with a byte size and offset. This allows it
  72. /// to describe lowered loads and stores. Also, the special PseudoSourceValue
  73. /// objects can be used to represent loads and stores to memory locations
  74. /// that aren't explicit in the regular LLVM IR.
  75. ///
  76. class MachineMemOperand {
  77. MachinePointerInfo PtrInfo;
  78. uint64_t Size;
  79. unsigned Flags;
  80. AAMDNodes AAInfo;
  81. const MDNode *Ranges;
  82. public:
  83. /// Flags values. These may be or'd together.
  84. enum MemOperandFlags {
  85. /// The memory access reads data.
  86. MOLoad = 1,
  87. /// The memory access writes data.
  88. MOStore = 2,
  89. /// The memory access is volatile.
  90. MOVolatile = 4,
  91. /// The memory access is non-temporal.
  92. MONonTemporal = 8,
  93. /// The memory access is invariant.
  94. MOInvariant = 16,
  95. // Target hints allow target passes to annotate memory operations.
  96. MOTargetStartBit = 5,
  97. MOTargetNumBits = 3,
  98. // This is the number of bits we need to represent flags.
  99. MOMaxBits = 8
  100. };
  101. /// MachineMemOperand - Construct an MachineMemOperand object with the
  102. /// specified PtrInfo, flags, size, and base alignment.
  103. MachineMemOperand(MachinePointerInfo PtrInfo, unsigned flags, uint64_t s,
  104. unsigned base_alignment,
  105. const AAMDNodes &AAInfo = AAMDNodes(),
  106. const MDNode *Ranges = nullptr);
  107. const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
  108. /// getValue - Return the base address of the memory access. This may either
  109. /// be a normal LLVM IR Value, or one of the special values used in CodeGen.
  110. /// Special values are those obtained via
  111. /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
  112. /// other PseudoSourceValue member functions which return objects which stand
  113. /// for frame/stack pointer relative references and other special references
  114. /// which are not representable in the high-level IR.
  115. const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
  116. const PseudoSourceValue *getPseudoValue() const {
  117. return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
  118. }
  119. const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
  120. /// getFlags - Return the raw flags of the source value, \see MemOperandFlags.
  121. unsigned int getFlags() const { return Flags & ((1 << MOMaxBits) - 1); }
  122. /// Bitwise OR the current flags with the given flags.
  123. void setFlags(unsigned f) { Flags |= (f & ((1 << MOMaxBits) - 1)); }
  124. /// getOffset - For normal values, this is a byte offset added to the base
  125. /// address. For PseudoSourceValue::FPRel values, this is the FrameIndex
  126. /// number.
  127. int64_t getOffset() const { return PtrInfo.Offset; }
  128. unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
  129. /// getSize - Return the size in bytes of the memory reference.
  130. uint64_t getSize() const { return Size; }
  131. /// getAlignment - Return the minimum known alignment in bytes of the
  132. /// actual memory reference.
  133. uint64_t getAlignment() const;
  134. /// getBaseAlignment - Return the minimum known alignment in bytes of the
  135. /// base address, without the offset.
  136. uint64_t getBaseAlignment() const { return (1u << (Flags >> MOMaxBits)) >> 1; }
  137. /// getAAInfo - Return the AA tags for the memory reference.
  138. AAMDNodes getAAInfo() const { return AAInfo; }
  139. /// getRanges - Return the range tag for the memory reference.
  140. const MDNode *getRanges() const { return Ranges; }
  141. bool isLoad() const { return Flags & MOLoad; }
  142. bool isStore() const { return Flags & MOStore; }
  143. bool isVolatile() const { return Flags & MOVolatile; }
  144. bool isNonTemporal() const { return Flags & MONonTemporal; }
  145. bool isInvariant() const { return Flags & MOInvariant; }
  146. /// isUnordered - Returns true if this memory operation doesn't have any
  147. /// ordering constraints other than normal aliasing. Volatile and atomic
  148. /// memory operations can't be reordered.
  149. ///
  150. /// Currently, we don't model the difference between volatile and atomic
  151. /// operations. They should retain their ordering relative to all memory
  152. /// operations.
  153. bool isUnordered() const { return !isVolatile(); }
  154. /// refineAlignment - Update this MachineMemOperand to reflect the alignment
  155. /// of MMO, if it has a greater alignment. This must only be used when the
  156. /// new alignment applies to all users of this MachineMemOperand.
  157. void refineAlignment(const MachineMemOperand *MMO);
  158. /// setValue - Change the SourceValue for this MachineMemOperand. This
  159. /// should only be used when an object is being relocated and all references
  160. /// to it are being updated.
  161. void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
  162. void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
  163. void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
  164. /// Profile - Gather unique data for the object.
  165. ///
  166. void Profile(FoldingSetNodeID &ID) const;
  167. /// Support for operator<<.
  168. /// @{
  169. void print(raw_ostream &OS) const;
  170. void print(raw_ostream &OS, ModuleSlotTracker &MST) const;
  171. /// @}
  172. friend bool operator==(const MachineMemOperand &LHS,
  173. const MachineMemOperand &RHS) {
  174. return LHS.getValue() == RHS.getValue() &&
  175. LHS.getPseudoValue() == RHS.getPseudoValue() &&
  176. LHS.getSize() == RHS.getSize() &&
  177. LHS.getOffset() == RHS.getOffset() &&
  178. LHS.getFlags() == RHS.getFlags() &&
  179. LHS.getAAInfo() == RHS.getAAInfo() &&
  180. LHS.getRanges() == RHS.getRanges() &&
  181. LHS.getAlignment() == RHS.getAlignment() &&
  182. LHS.getAddrSpace() == RHS.getAddrSpace();
  183. }
  184. friend bool operator!=(const MachineMemOperand &LHS,
  185. const MachineMemOperand &RHS) {
  186. return !(LHS == RHS);
  187. }
  188. };
  189. inline raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MRO) {
  190. MRO.print(OS);
  191. return OS;
  192. }
  193. } // End llvm namespace
  194. #endif