MachineMemOperand.h 14 KB

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