Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- C++ -*-==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains the declaration of the MachineMemOperand class, which is a
0010 // description of a memory reference. It is used to help track dependencies
0011 // in the backend.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
0016 #define LLVM_CODEGEN_MACHINEMEMOPERAND_H
0017 
0018 #include "llvm/ADT/BitmaskEnum.h"
0019 #include "llvm/ADT/PointerUnion.h"
0020 #include "llvm/Analysis/MemoryLocation.h"
0021 #include "llvm/CodeGen/PseudoSourceValue.h"
0022 #include "llvm/CodeGenTypes/LowLevelType.h"
0023 #include "llvm/IR/DerivedTypes.h"
0024 #include "llvm/IR/LLVMContext.h"
0025 #include "llvm/IR/Metadata.h"
0026 #include "llvm/IR/Value.h" // PointerLikeTypeTraits<Value*>
0027 #include "llvm/Support/AtomicOrdering.h"
0028 #include "llvm/Support/DataTypes.h"
0029 
0030 namespace llvm {
0031 
0032 class MDNode;
0033 class raw_ostream;
0034 class MachineFunction;
0035 class ModuleSlotTracker;
0036 class TargetInstrInfo;
0037 
0038 /// This class contains a discriminated union of information about pointers in
0039 /// memory operands, relating them back to LLVM IR or to virtual locations (such
0040 /// as frame indices) that are exposed during codegen.
0041 struct MachinePointerInfo {
0042   /// This is the IR pointer value for the access, or it is null if unknown.
0043   PointerUnion<const Value *, const PseudoSourceValue *> V;
0044 
0045   /// Offset - This is an offset from the base Value*.
0046   int64_t Offset;
0047 
0048   unsigned AddrSpace = 0;
0049 
0050   uint8_t StackID;
0051 
0052   explicit MachinePointerInfo(const Value *v, int64_t offset = 0,
0053                               uint8_t ID = 0)
0054       : V(v), Offset(offset), StackID(ID) {
0055     AddrSpace = v ? v->getType()->getPointerAddressSpace() : 0;
0056   }
0057 
0058   explicit MachinePointerInfo(const PseudoSourceValue *v, int64_t offset = 0,
0059                               uint8_t ID = 0)
0060       : V(v), Offset(offset), StackID(ID) {
0061     AddrSpace = v ? v->getAddressSpace() : 0;
0062   }
0063 
0064   explicit MachinePointerInfo(unsigned AddressSpace = 0, int64_t offset = 0)
0065       : V((const Value *)nullptr), Offset(offset), AddrSpace(AddressSpace),
0066         StackID(0) {}
0067 
0068   explicit MachinePointerInfo(
0069     PointerUnion<const Value *, const PseudoSourceValue *> v,
0070     int64_t offset = 0,
0071     uint8_t ID = 0)
0072     : V(v), Offset(offset), StackID(ID) {
0073     if (V) {
0074       if (const auto *ValPtr = dyn_cast_if_present<const Value *>(V))
0075         AddrSpace = ValPtr->getType()->getPointerAddressSpace();
0076       else
0077         AddrSpace = cast<const PseudoSourceValue *>(V)->getAddressSpace();
0078     }
0079   }
0080 
0081   MachinePointerInfo getWithOffset(int64_t O) const {
0082     if (V.isNull())
0083       return MachinePointerInfo(AddrSpace, Offset + O);
0084     if (isa<const Value *>(V))
0085       return MachinePointerInfo(cast<const Value *>(V), Offset + O, StackID);
0086     return MachinePointerInfo(cast<const PseudoSourceValue *>(V), Offset + O,
0087                               StackID);
0088   }
0089 
0090   /// Return true if memory region [V, V+Offset+Size) is known to be
0091   /// dereferenceable.
0092   bool isDereferenceable(unsigned Size, LLVMContext &C,
0093                          const DataLayout &DL) const;
0094 
0095   /// Return the LLVM IR address space number that this pointer points into.
0096   unsigned getAddrSpace() const;
0097 
0098   /// Return a MachinePointerInfo record that refers to the constant pool.
0099   static MachinePointerInfo getConstantPool(MachineFunction &MF);
0100 
0101   /// Return a MachinePointerInfo record that refers to the specified
0102   /// FrameIndex.
0103   static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI,
0104                                           int64_t Offset = 0);
0105 
0106   /// Return a MachinePointerInfo record that refers to a jump table entry.
0107   static MachinePointerInfo getJumpTable(MachineFunction &MF);
0108 
0109   /// Return a MachinePointerInfo record that refers to a GOT entry.
0110   static MachinePointerInfo getGOT(MachineFunction &MF);
0111 
0112   /// Stack pointer relative access.
0113   static MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset,
0114                                      uint8_t ID = 0);
0115 
0116   /// Stack memory without other information.
0117   static MachinePointerInfo getUnknownStack(MachineFunction &MF);
0118 };
0119 
0120 
0121 //===----------------------------------------------------------------------===//
0122 /// A description of a memory reference used in the backend.
0123 /// Instead of holding a StoreInst or LoadInst, this class holds the address
0124 /// Value of the reference along with a byte size and offset. This allows it
0125 /// to describe lowered loads and stores. Also, the special PseudoSourceValue
0126 /// objects can be used to represent loads and stores to memory locations
0127 /// that aren't explicit in the regular LLVM IR.
0128 ///
0129 class MachineMemOperand {
0130 public:
0131   /// Flags values. These may be or'd together.
0132   enum Flags : uint16_t {
0133     // No flags set.
0134     MONone = 0,
0135     /// The memory access reads data.
0136     MOLoad = 1u << 0,
0137     /// The memory access writes data.
0138     MOStore = 1u << 1,
0139     /// The memory access is volatile.
0140     MOVolatile = 1u << 2,
0141     /// The memory access is non-temporal.
0142     MONonTemporal = 1u << 3,
0143     /// The memory access is dereferenceable (i.e., doesn't trap).
0144     MODereferenceable = 1u << 4,
0145     /// The memory access always returns the same value (or traps).
0146     MOInvariant = 1u << 5,
0147 
0148     // Reserved for use by target-specific passes.
0149     // Targets may override getSerializableMachineMemOperandTargetFlags() to
0150     // enable MIR serialization/parsing of these flags.  If more of these flags
0151     // are added, the MIR printing/parsing code will need to be updated as well.
0152     MOTargetFlag1 = 1u << 6,
0153     MOTargetFlag2 = 1u << 7,
0154     MOTargetFlag3 = 1u << 8,
0155     MOTargetFlag4 = 1u << 9,
0156 
0157     LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ MOTargetFlag4)
0158   };
0159 
0160 private:
0161   /// Atomic information for this memory operation.
0162   struct MachineAtomicInfo {
0163     /// Synchronization scope ID for this memory operation.
0164     unsigned SSID : 8;            // SyncScope::ID
0165     /// Atomic ordering requirements for this memory operation. For cmpxchg
0166     /// atomic operations, atomic ordering requirements when store occurs.
0167     unsigned Ordering : 4;        // enum AtomicOrdering
0168     /// For cmpxchg atomic operations, atomic ordering requirements when store
0169     /// does not occur.
0170     unsigned FailureOrdering : 4; // enum AtomicOrdering
0171   };
0172 
0173   MachinePointerInfo PtrInfo;
0174 
0175   /// Track the memory type of the access. An access size which is unknown or
0176   /// too large to be represented by LLT should use the invalid LLT.
0177   LLT MemoryType;
0178 
0179   Flags FlagVals;
0180   Align BaseAlign;
0181   MachineAtomicInfo AtomicInfo;
0182   AAMDNodes AAInfo;
0183   const MDNode *Ranges;
0184 
0185 public:
0186   /// Construct a MachineMemOperand object with the specified PtrInfo, flags,
0187   /// size, and base alignment. For atomic operations the synchronization scope
0188   /// and atomic ordering requirements must also be specified. For cmpxchg
0189   /// atomic operations the atomic ordering requirements when store does not
0190   /// occur must also be specified.
0191   MachineMemOperand(MachinePointerInfo PtrInfo, Flags flags, LocationSize TS,
0192                     Align a, const AAMDNodes &AAInfo = AAMDNodes(),
0193                     const MDNode *Ranges = nullptr,
0194                     SyncScope::ID SSID = SyncScope::System,
0195                     AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
0196                     AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
0197   MachineMemOperand(MachinePointerInfo PtrInfo, Flags flags, LLT type, Align a,
0198                     const AAMDNodes &AAInfo = AAMDNodes(),
0199                     const MDNode *Ranges = nullptr,
0200                     SyncScope::ID SSID = SyncScope::System,
0201                     AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
0202                     AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
0203 
0204   const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
0205 
0206   /// Return the base address of the memory access. This may either be a normal
0207   /// LLVM IR Value, or one of the special values used in CodeGen.
0208   /// Special values are those obtained via
0209   /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
0210   /// other PseudoSourceValue member functions which return objects which stand
0211   /// for frame/stack pointer relative references and other special references
0212   /// which are not representable in the high-level IR.
0213   const Value *getValue() const {
0214     return dyn_cast_if_present<const Value *>(PtrInfo.V);
0215   }
0216 
0217   const PseudoSourceValue *getPseudoValue() const {
0218     return dyn_cast_if_present<const PseudoSourceValue *>(PtrInfo.V);
0219   }
0220 
0221   const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
0222 
0223   /// Return the raw flags of the source value, \see Flags.
0224   Flags getFlags() const { return FlagVals; }
0225 
0226   /// Bitwise OR the current flags with the given flags.
0227   void setFlags(Flags f) { FlagVals |= f; }
0228 
0229   /// For normal values, this is a byte offset added to the base address.
0230   /// For PseudoSourceValue::FPRel values, this is the FrameIndex number.
0231   int64_t getOffset() const { return PtrInfo.Offset; }
0232 
0233   unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
0234 
0235   /// Return the memory type of the memory reference. This should only be relied
0236   /// on for GlobalISel G_* operation legalization.
0237   LLT getMemoryType() const { return MemoryType; }
0238 
0239   /// Return the size in bytes of the memory reference.
0240   LocationSize getSize() const {
0241     return MemoryType.isValid()
0242                ? LocationSize::precise(MemoryType.getSizeInBytes())
0243                : LocationSize::beforeOrAfterPointer();
0244   }
0245 
0246   /// Return the size in bits of the memory reference.
0247   LocationSize getSizeInBits() const {
0248     return MemoryType.isValid()
0249                ? LocationSize::precise(MemoryType.getSizeInBits())
0250                : LocationSize::beforeOrAfterPointer();
0251   }
0252 
0253   LLT getType() const {
0254     return MemoryType;
0255   }
0256 
0257   /// Return the minimum known alignment in bytes of the actual memory
0258   /// reference.
0259   Align getAlign() const;
0260 
0261   /// Return the minimum known alignment in bytes of the base address, without
0262   /// the offset.
0263   Align getBaseAlign() const { return BaseAlign; }
0264 
0265   /// Return the AA tags for the memory reference.
0266   AAMDNodes getAAInfo() const { return AAInfo; }
0267 
0268   /// Return the range tag for the memory reference.
0269   const MDNode *getRanges() const { return Ranges; }
0270 
0271   /// Returns the synchronization scope ID for this memory operation.
0272   SyncScope::ID getSyncScopeID() const {
0273     return static_cast<SyncScope::ID>(AtomicInfo.SSID);
0274   }
0275 
0276   /// Return the atomic ordering requirements for this memory operation. For
0277   /// cmpxchg atomic operations, return the atomic ordering requirements when
0278   /// store occurs.
0279   AtomicOrdering getSuccessOrdering() const {
0280     return static_cast<AtomicOrdering>(AtomicInfo.Ordering);
0281   }
0282 
0283   /// For cmpxchg atomic operations, return the atomic ordering requirements
0284   /// when store does not occur.
0285   AtomicOrdering getFailureOrdering() const {
0286     return static_cast<AtomicOrdering>(AtomicInfo.FailureOrdering);
0287   }
0288 
0289   /// Return a single atomic ordering that is at least as strong as both the
0290   /// success and failure orderings for an atomic operation.  (For operations
0291   /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
0292   AtomicOrdering getMergedOrdering() const {
0293     return getMergedAtomicOrdering(getSuccessOrdering(), getFailureOrdering());
0294   }
0295 
0296   bool isLoad() const { return FlagVals & MOLoad; }
0297   bool isStore() const { return FlagVals & MOStore; }
0298   bool isVolatile() const { return FlagVals & MOVolatile; }
0299   bool isNonTemporal() const { return FlagVals & MONonTemporal; }
0300   bool isDereferenceable() const { return FlagVals & MODereferenceable; }
0301   bool isInvariant() const { return FlagVals & MOInvariant; }
0302 
0303   /// Returns true if this operation has an atomic ordering requirement of
0304   /// unordered or higher, false otherwise.
0305   bool isAtomic() const {
0306     return getSuccessOrdering() != AtomicOrdering::NotAtomic;
0307   }
0308 
0309   /// Returns true if this memory operation doesn't have any ordering
0310   /// constraints other than normal aliasing. Volatile and (ordered) atomic
0311   /// memory operations can't be reordered.
0312   bool isUnordered() const {
0313     return (getSuccessOrdering() == AtomicOrdering::NotAtomic ||
0314             getSuccessOrdering() == AtomicOrdering::Unordered) &&
0315            !isVolatile();
0316   }
0317 
0318   /// Update this MachineMemOperand to reflect the alignment of MMO, if it has a
0319   /// greater alignment. This must only be used when the new alignment applies
0320   /// to all users of this MachineMemOperand.
0321   void refineAlignment(const MachineMemOperand *MMO);
0322 
0323   /// Change the SourceValue for this MachineMemOperand. This should only be
0324   /// used when an object is being relocated and all references to it are being
0325   /// updated.
0326   void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
0327   void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
0328   void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
0329 
0330   /// Reset the tracked memory type.
0331   void setType(LLT NewTy) {
0332     MemoryType = NewTy;
0333   }
0334 
0335   /// Unset the tracked range metadata.
0336   void clearRanges() { Ranges = nullptr; }
0337 
0338   /// Support for operator<<.
0339   /// @{
0340   void print(raw_ostream &OS, ModuleSlotTracker &MST,
0341              SmallVectorImpl<StringRef> &SSNs, const LLVMContext &Context,
0342              const MachineFrameInfo *MFI, const TargetInstrInfo *TII) const;
0343   /// @}
0344 
0345   friend bool operator==(const MachineMemOperand &LHS,
0346                          const MachineMemOperand &RHS) {
0347     return LHS.getValue() == RHS.getValue() &&
0348            LHS.getPseudoValue() == RHS.getPseudoValue() &&
0349            LHS.getSize() == RHS.getSize() &&
0350            LHS.getOffset() == RHS.getOffset() &&
0351            LHS.getFlags() == RHS.getFlags() &&
0352            LHS.getAAInfo() == RHS.getAAInfo() &&
0353            LHS.getRanges() == RHS.getRanges() &&
0354            LHS.getAlign() == RHS.getAlign() &&
0355            LHS.getAddrSpace() == RHS.getAddrSpace();
0356   }
0357 
0358   friend bool operator!=(const MachineMemOperand &LHS,
0359                          const MachineMemOperand &RHS) {
0360     return !(LHS == RHS);
0361   }
0362 };
0363 
0364 } // End llvm namespace
0365 
0366 #endif