MachineMemOperand.h 14 KB

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