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