MachineMemOperand.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- 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. // This file contains the declaration of the MachineMemOperand class, which is a
  10. // description of a memory reference. It is used to help track dependencies
  11. // in the backend.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
  15. #define LLVM_CODEGEN_MACHINEMEMOPERAND_H
  16. #include "llvm/ADT/BitmaskEnum.h"
  17. #include "llvm/ADT/PointerUnion.h"
  18. #include "llvm/CodeGen/PseudoSourceValue.h"
  19. #include "llvm/IR/DerivedTypes.h"
  20. #include "llvm/IR/Value.h" // PointerLikeTypeTraits<Value*>
  21. #include "llvm/Support/AtomicOrdering.h"
  22. #include "llvm/Support/DataTypes.h"
  23. namespace llvm {
  24. class FoldingSetNodeID;
  25. class MDNode;
  26. class raw_ostream;
  27. class MachineFunction;
  28. class ModuleSlotTracker;
  29. /// This class contains a discriminated union of information about pointers in
  30. /// memory operands, relating them back to LLVM IR or to virtual locations (such
  31. /// as frame indices) that are exposed during codegen.
  32. struct MachinePointerInfo {
  33. /// This is the IR pointer value for the access, or it is null if unknown.
  34. /// If this is null, then the access is to a pointer in the default address
  35. /// space.
  36. PointerUnion<const Value *, const PseudoSourceValue *> V;
  37. /// Offset - This is an offset from the base Value*.
  38. int64_t Offset;
  39. unsigned AddrSpace = 0;
  40. uint8_t StackID;
  41. explicit MachinePointerInfo(const Value *v, int64_t offset = 0,
  42. uint8_t ID = 0)
  43. : V(v), Offset(offset), StackID(ID) {
  44. AddrSpace = v ? v->getType()->getPointerAddressSpace() : 0;
  45. }
  46. explicit MachinePointerInfo(const PseudoSourceValue *v, int64_t offset = 0,
  47. uint8_t ID = 0)
  48. : V(v), Offset(offset), StackID(ID) {
  49. AddrSpace = v ? v->getAddressSpace() : 0;
  50. }
  51. explicit MachinePointerInfo(unsigned AddressSpace = 0, int64_t offset = 0)
  52. : V((const Value *)nullptr), Offset(offset), AddrSpace(AddressSpace),
  53. StackID(0) {}
  54. explicit MachinePointerInfo(
  55. PointerUnion<const Value *, const PseudoSourceValue *> v,
  56. int64_t offset = 0,
  57. uint8_t ID = 0)
  58. : V(v), Offset(offset), StackID(ID) {
  59. if (V) {
  60. if (const auto *ValPtr = V.dyn_cast<const Value*>())
  61. AddrSpace = ValPtr->getType()->getPointerAddressSpace();
  62. else
  63. AddrSpace = V.get<const PseudoSourceValue*>()->getAddressSpace();
  64. }
  65. }
  66. MachinePointerInfo getWithOffset(int64_t O) const {
  67. if (V.isNull())
  68. return MachinePointerInfo(AddrSpace, Offset + O);
  69. if (V.is<const Value*>())
  70. return MachinePointerInfo(V.get<const Value*>(), Offset + O, StackID);
  71. return MachinePointerInfo(V.get<const PseudoSourceValue*>(), Offset + O,
  72. StackID);
  73. }
  74. /// Return true if memory region [V, V+Offset+Size) is known to be
  75. /// dereferenceable.
  76. bool isDereferenceable(unsigned Size, LLVMContext &C,
  77. const DataLayout &DL) const;
  78. /// Return the LLVM IR address space number that this pointer points into.
  79. unsigned getAddrSpace() const;
  80. /// Return a MachinePointerInfo record that refers to the constant pool.
  81. static MachinePointerInfo getConstantPool(MachineFunction &MF);
  82. /// Return a MachinePointerInfo record that refers to the specified
  83. /// FrameIndex.
  84. static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI,
  85. int64_t Offset = 0);
  86. /// Return a MachinePointerInfo record that refers to a jump table entry.
  87. static MachinePointerInfo getJumpTable(MachineFunction &MF);
  88. /// Return a MachinePointerInfo record that refers to a GOT entry.
  89. static MachinePointerInfo getGOT(MachineFunction &MF);
  90. /// Stack pointer relative access.
  91. static MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset,
  92. uint8_t ID = 0);
  93. /// Stack memory without other information.
  94. static MachinePointerInfo getUnknownStack(MachineFunction &MF);
  95. };
  96. //===----------------------------------------------------------------------===//
  97. /// A description of a memory reference used in the backend.
  98. /// Instead of holding a StoreInst or LoadInst, this class holds the address
  99. /// Value of the reference along with a byte size and offset. This allows it
  100. /// to describe lowered loads and stores. Also, the special PseudoSourceValue
  101. /// objects can be used to represent loads and stores to memory locations
  102. /// that aren't explicit in the regular LLVM IR.
  103. ///
  104. class MachineMemOperand {
  105. public:
  106. /// Flags values. These may be or'd together.
  107. enum Flags : uint16_t {
  108. // No flags set.
  109. MONone = 0,
  110. /// The memory access reads data.
  111. MOLoad = 1u << 0,
  112. /// The memory access writes data.
  113. MOStore = 1u << 1,
  114. /// The memory access is volatile.
  115. MOVolatile = 1u << 2,
  116. /// The memory access is non-temporal.
  117. MONonTemporal = 1u << 3,
  118. /// The memory access is dereferenceable (i.e., doesn't trap).
  119. MODereferenceable = 1u << 4,
  120. /// The memory access always returns the same value (or traps).
  121. MOInvariant = 1u << 5,
  122. // Reserved for use by target-specific passes.
  123. // Targets may override getSerializableMachineMemOperandTargetFlags() to
  124. // enable MIR serialization/parsing of these flags. If more of these flags
  125. // are added, the MIR printing/parsing code will need to be updated as well.
  126. MOTargetFlag1 = 1u << 6,
  127. MOTargetFlag2 = 1u << 7,
  128. MOTargetFlag3 = 1u << 8,
  129. LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ MOTargetFlag3)
  130. };
  131. private:
  132. /// Atomic information for this memory operation.
  133. struct MachineAtomicInfo {
  134. /// Synchronization scope ID for this memory operation.
  135. unsigned SSID : 8; // SyncScope::ID
  136. /// Atomic ordering requirements for this memory operation. For cmpxchg
  137. /// atomic operations, atomic ordering requirements when store occurs.
  138. unsigned Ordering : 4; // enum AtomicOrdering
  139. /// For cmpxchg atomic operations, atomic ordering requirements when store
  140. /// does not occur.
  141. unsigned FailureOrdering : 4; // enum AtomicOrdering
  142. };
  143. MachinePointerInfo PtrInfo;
  144. uint64_t Size;
  145. Flags FlagVals;
  146. Align BaseAlign;
  147. MachineAtomicInfo AtomicInfo;
  148. AAMDNodes AAInfo;
  149. const MDNode *Ranges;
  150. public:
  151. /// Construct a MachineMemOperand object with the specified PtrInfo, flags,
  152. /// size, and base alignment. For atomic operations the synchronization scope
  153. /// and atomic ordering requirements must also be specified. For cmpxchg
  154. /// atomic operations the atomic ordering requirements when store does not
  155. /// occur must also be specified.
  156. MachineMemOperand(MachinePointerInfo PtrInfo, Flags flags, uint64_t s,
  157. Align a, const AAMDNodes &AAInfo = AAMDNodes(),
  158. const MDNode *Ranges = nullptr,
  159. SyncScope::ID SSID = SyncScope::System,
  160. AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
  161. AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
  162. const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
  163. /// Return the base address of the memory access. This may either be a normal
  164. /// LLVM IR Value, or one of the special values used in CodeGen.
  165. /// Special values are those obtained via
  166. /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
  167. /// other PseudoSourceValue member functions which return objects which stand
  168. /// for frame/stack pointer relative references and other special references
  169. /// which are not representable in the high-level IR.
  170. const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
  171. const PseudoSourceValue *getPseudoValue() const {
  172. return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
  173. }
  174. const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
  175. /// Return the raw flags of the source value, \see Flags.
  176. Flags getFlags() const { return FlagVals; }
  177. /// Bitwise OR the current flags with the given flags.
  178. void setFlags(Flags f) { FlagVals |= f; }
  179. /// For normal values, this is a byte offset added to the base address.
  180. /// For PseudoSourceValue::FPRel values, this is the FrameIndex number.
  181. int64_t getOffset() const { return PtrInfo.Offset; }
  182. unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
  183. /// Return the size in bytes of the memory reference.
  184. uint64_t getSize() const { return Size; }
  185. /// Return the size in bits of the memory reference.
  186. uint64_t getSizeInBits() const { return Size * 8; }
  187. /// Return the minimum known alignment in bytes of the actual memory
  188. /// reference.
  189. Align getAlign() const;
  190. /// Return the minimum known alignment in bytes of the base address, without
  191. /// the offset.
  192. Align getBaseAlign() const { return BaseAlign; }
  193. /// Return the AA tags for the memory reference.
  194. AAMDNodes getAAInfo() const { return AAInfo; }
  195. /// Return the range tag for the memory reference.
  196. const MDNode *getRanges() const { return Ranges; }
  197. /// Returns the synchronization scope ID for this memory operation.
  198. SyncScope::ID getSyncScopeID() const {
  199. return static_cast<SyncScope::ID>(AtomicInfo.SSID);
  200. }
  201. /// Return the atomic ordering requirements for this memory operation. For
  202. /// cmpxchg atomic operations, return the atomic ordering requirements when
  203. /// store occurs.
  204. AtomicOrdering getOrdering() const {
  205. return static_cast<AtomicOrdering>(AtomicInfo.Ordering);
  206. }
  207. /// For cmpxchg atomic operations, return the atomic ordering requirements
  208. /// when store does not occur.
  209. AtomicOrdering getFailureOrdering() const {
  210. return static_cast<AtomicOrdering>(AtomicInfo.FailureOrdering);
  211. }
  212. bool isLoad() const { return FlagVals & MOLoad; }
  213. bool isStore() const { return FlagVals & MOStore; }
  214. bool isVolatile() const { return FlagVals & MOVolatile; }
  215. bool isNonTemporal() const { return FlagVals & MONonTemporal; }
  216. bool isDereferenceable() const { return FlagVals & MODereferenceable; }
  217. bool isInvariant() const { return FlagVals & MOInvariant; }
  218. /// Returns true if this operation has an atomic ordering requirement of
  219. /// unordered or higher, false otherwise.
  220. bool isAtomic() const { return getOrdering() != AtomicOrdering::NotAtomic; }
  221. /// Returns true if this memory operation doesn't have any ordering
  222. /// constraints other than normal aliasing. Volatile and (ordered) atomic
  223. /// memory operations can't be reordered.
  224. bool isUnordered() const {
  225. return (getOrdering() == AtomicOrdering::NotAtomic ||
  226. getOrdering() == AtomicOrdering::Unordered) &&
  227. !isVolatile();
  228. }
  229. /// Update this MachineMemOperand to reflect the alignment of MMO, if it has a
  230. /// greater alignment. This must only be used when the new alignment applies
  231. /// to all users of this MachineMemOperand.
  232. void refineAlignment(const MachineMemOperand *MMO);
  233. /// Change the SourceValue for this MachineMemOperand. This should only be
  234. /// used when an object is being relocated and all references to it are being
  235. /// updated.
  236. void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
  237. void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
  238. void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
  239. /// Profile - Gather unique data for the object.
  240. ///
  241. void Profile(FoldingSetNodeID &ID) const;
  242. /// Support for operator<<.
  243. /// @{
  244. void print(raw_ostream &OS, ModuleSlotTracker &MST,
  245. SmallVectorImpl<StringRef> &SSNs, const LLVMContext &Context,
  246. const MachineFrameInfo *MFI, const TargetInstrInfo *TII) const;
  247. /// @}
  248. friend bool operator==(const MachineMemOperand &LHS,
  249. const MachineMemOperand &RHS) {
  250. return LHS.getValue() == RHS.getValue() &&
  251. LHS.getPseudoValue() == RHS.getPseudoValue() &&
  252. LHS.getSize() == RHS.getSize() &&
  253. LHS.getOffset() == RHS.getOffset() &&
  254. LHS.getFlags() == RHS.getFlags() &&
  255. LHS.getAAInfo() == RHS.getAAInfo() &&
  256. LHS.getRanges() == RHS.getRanges() &&
  257. LHS.getAlign() == RHS.getAlign() &&
  258. LHS.getAddrSpace() == RHS.getAddrSpace();
  259. }
  260. friend bool operator!=(const MachineMemOperand &LHS,
  261. const MachineMemOperand &RHS) {
  262. return !(LHS == RHS);
  263. }
  264. };
  265. } // End llvm namespace
  266. #endif