1 //==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
17 #define LLVM_CODEGEN_MACHINEMEMOPERAND_H
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"
27 class FoldingSetNodeID;
30 class MachineFunction;
31 class ModuleSlotTracker;
33 /// MachinePointerInfo - This class contains a discriminated union of
34 /// information about pointers in memory operands, relating them back to LLVM IR
35 /// or to virtual locations (such as frame indices) that are exposed during
37 struct MachinePointerInfo {
38 /// V - This is the IR pointer value for the access, or it is null if unknown.
39 /// If this is null, then the access is to a pointer in the default address
41 PointerUnion<const Value *, const PseudoSourceValue *> V;
43 /// Offset - This is an offset from the base Value*.
46 explicit MachinePointerInfo(const Value *v = nullptr, int64_t offset = 0)
47 : V(v), Offset(offset) {}
49 explicit MachinePointerInfo(const PseudoSourceValue *v,
51 : V(v), Offset(offset) {}
53 MachinePointerInfo getWithOffset(int64_t O) const {
54 if (V.isNull()) return MachinePointerInfo();
55 if (V.is<const Value*>())
56 return MachinePointerInfo(V.get<const Value*>(), Offset+O);
57 return MachinePointerInfo(V.get<const PseudoSourceValue*>(), Offset+O);
60 /// getAddrSpace - Return the LLVM IR address space number that this pointer
62 unsigned getAddrSpace() const;
64 /// getConstantPool - Return a MachinePointerInfo record that refers to the
66 static MachinePointerInfo getConstantPool(MachineFunction &MF);
68 /// getFixedStack - Return a MachinePointerInfo record that refers to the
69 /// the specified FrameIndex.
70 static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI,
73 /// getJumpTable - Return a MachinePointerInfo record that refers to a
75 static MachinePointerInfo getJumpTable(MachineFunction &MF);
77 /// getGOT - Return a MachinePointerInfo record that refers to a
79 static MachinePointerInfo getGOT(MachineFunction &MF);
81 /// getStack - stack pointer relative access.
82 static MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset);
86 //===----------------------------------------------------------------------===//
87 /// MachineMemOperand - A description of a memory reference used in the backend.
88 /// Instead of holding a StoreInst or LoadInst, this class holds the address
89 /// Value of the reference along with a byte size and offset. This allows it
90 /// to describe lowered loads and stores. Also, the special PseudoSourceValue
91 /// objects can be used to represent loads and stores to memory locations
92 /// that aren't explicit in the regular LLVM IR.
94 class MachineMemOperand {
95 MachinePointerInfo PtrInfo;
102 /// Flags values. These may be or'd together.
103 enum MemOperandFlags {
104 /// The memory access reads data.
106 /// The memory access writes data.
108 /// The memory access is volatile.
110 /// The memory access is non-temporal.
112 /// The memory access is invariant.
114 // Target hints allow target passes to annotate memory operations.
115 MOTargetStartBit = 5,
117 // This is the number of bits we need to represent flags.
121 /// MachineMemOperand - Construct an MachineMemOperand object with the
122 /// specified PtrInfo, flags, size, and base alignment.
123 MachineMemOperand(MachinePointerInfo PtrInfo, unsigned flags, uint64_t s,
124 unsigned base_alignment,
125 const AAMDNodes &AAInfo = AAMDNodes(),
126 const MDNode *Ranges = nullptr);
128 const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
130 /// getValue - Return the base address of the memory access. This may either
131 /// be a normal LLVM IR Value, or one of the special values used in CodeGen.
132 /// Special values are those obtained via
133 /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
134 /// other PseudoSourceValue member functions which return objects which stand
135 /// for frame/stack pointer relative references and other special references
136 /// which are not representable in the high-level IR.
137 const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
139 const PseudoSourceValue *getPseudoValue() const {
140 return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
143 const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
145 /// getFlags - Return the raw flags of the source value, \see MemOperandFlags.
146 unsigned int getFlags() const { return Flags & ((1 << MOMaxBits) - 1); }
148 /// Bitwise OR the current flags with the given flags.
149 void setFlags(unsigned f) { Flags |= (f & ((1 << MOMaxBits) - 1)); }
151 /// getOffset - For normal values, this is a byte offset added to the base
152 /// address. For PseudoSourceValue::FPRel values, this is the FrameIndex
154 int64_t getOffset() const { return PtrInfo.Offset; }
156 unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
158 /// getSize - Return the size in bytes of the memory reference.
159 uint64_t getSize() const { return Size; }
161 /// getAlignment - Return the minimum known alignment in bytes of the
162 /// actual memory reference.
163 uint64_t getAlignment() const;
165 /// getBaseAlignment - Return the minimum known alignment in bytes of the
166 /// base address, without the offset.
167 uint64_t getBaseAlignment() const { return (1u << (Flags >> MOMaxBits)) >> 1; }
169 /// getAAInfo - Return the AA tags for the memory reference.
170 AAMDNodes getAAInfo() const { return AAInfo; }
172 /// getRanges - Return the range tag for the memory reference.
173 const MDNode *getRanges() const { return Ranges; }
175 bool isLoad() const { return Flags & MOLoad; }
176 bool isStore() const { return Flags & MOStore; }
177 bool isVolatile() const { return Flags & MOVolatile; }
178 bool isNonTemporal() const { return Flags & MONonTemporal; }
179 bool isInvariant() const { return Flags & MOInvariant; }
181 /// isUnordered - Returns true if this memory operation doesn't have any
182 /// ordering constraints other than normal aliasing. Volatile and atomic
183 /// memory operations can't be reordered.
185 /// Currently, we don't model the difference between volatile and atomic
186 /// operations. They should retain their ordering relative to all memory
188 bool isUnordered() const { return !isVolatile(); }
190 /// refineAlignment - Update this MachineMemOperand to reflect the alignment
191 /// of MMO, if it has a greater alignment. This must only be used when the
192 /// new alignment applies to all users of this MachineMemOperand.
193 void refineAlignment(const MachineMemOperand *MMO);
195 /// setValue - Change the SourceValue for this MachineMemOperand. This
196 /// should only be used when an object is being relocated and all references
197 /// to it are being updated.
198 void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
199 void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
200 void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
202 /// Profile - Gather unique data for the object.
204 void Profile(FoldingSetNodeID &ID) const;
206 /// Support for operator<<.
208 void print(raw_ostream &OS) const;
209 void print(raw_ostream &OS, ModuleSlotTracker &MST) const;
212 friend bool operator==(const MachineMemOperand &LHS,
213 const MachineMemOperand &RHS) {
214 return LHS.getValue() == RHS.getValue() &&
215 LHS.getPseudoValue() == RHS.getPseudoValue() &&
216 LHS.getSize() == RHS.getSize() &&
217 LHS.getOffset() == RHS.getOffset() &&
218 LHS.getFlags() == RHS.getFlags() &&
219 LHS.getAAInfo() == RHS.getAAInfo() &&
220 LHS.getRanges() == RHS.getRanges() &&
221 LHS.getAlignment() == RHS.getAlignment() &&
222 LHS.getAddrSpace() == RHS.getAddrSpace();
225 friend bool operator!=(const MachineMemOperand &LHS,
226 const MachineMemOperand &RHS) {
227 return !(LHS == RHS);
231 inline raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MRO) {
236 } // End llvm namespace