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 ModuleSlotTracker;
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
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
40 PointerUnion<const Value *, const PseudoSourceValue *> V;
42 /// Offset - This is an offset from the base Value*.
45 explicit MachinePointerInfo(const Value *v = nullptr, int64_t offset = 0)
46 : V(v), Offset(offset) {}
48 explicit MachinePointerInfo(const PseudoSourceValue *v,
50 : V(v), Offset(offset) {}
52 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);
59 /// getAddrSpace - Return the LLVM IR address space number that this pointer
61 unsigned getAddrSpace() const;
63 /// getConstantPool - Return a MachinePointerInfo record that refers to the
65 static MachinePointerInfo getConstantPool();
67 /// getFixedStack - Return a MachinePointerInfo record that refers to the
68 /// the specified FrameIndex.
69 static MachinePointerInfo getFixedStack(int FI, int64_t offset = 0);
71 /// getJumpTable - Return a MachinePointerInfo record that refers to a
73 static MachinePointerInfo getJumpTable();
75 /// getGOT - Return a MachinePointerInfo record that refers to a
77 static MachinePointerInfo getGOT();
79 /// getStack - stack pointer relative access.
80 static MachinePointerInfo getStack(int64_t Offset);
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.
92 class MachineMemOperand {
93 MachinePointerInfo PtrInfo;
100 /// Flags values. These may be or'd together.
101 enum MemOperandFlags {
102 /// The memory access reads data.
104 /// The memory access writes data.
106 /// The memory access is volatile.
108 /// The memory access is non-temporal.
110 /// The memory access is invariant.
112 // Target hints allow target passes to annotate memory operations.
113 MOTargetStartBit = 5,
115 // This is the number of bits we need to represent flags.
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);
126 const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
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.
135 const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
137 const PseudoSourceValue *getPseudoValue() const {
138 return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
141 const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
143 /// getFlags - Return the raw flags of the source value, \see MemOperandFlags.
144 unsigned int getFlags() const { return Flags & ((1 << MOMaxBits) - 1); }
146 /// Bitwise OR the current flags with the given flags.
147 void setFlags(unsigned f) { Flags |= (f & ((1 << MOMaxBits) - 1)); }
149 /// getOffset - For normal values, this is a byte offset added to the base
150 /// address. For PseudoSourceValue::FPRel values, this is the FrameIndex
152 int64_t getOffset() const { return PtrInfo.Offset; }
154 unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
156 /// getSize - Return the size in bytes of the memory reference.
157 uint64_t getSize() const { return Size; }
159 /// getAlignment - Return the minimum known alignment in bytes of the
160 /// actual memory reference.
161 uint64_t getAlignment() const;
163 /// getBaseAlignment - Return the minimum known alignment in bytes of the
164 /// base address, without the offset.
165 uint64_t getBaseAlignment() const { return (1u << (Flags >> MOMaxBits)) >> 1; }
167 /// getAAInfo - Return the AA tags for the memory reference.
168 AAMDNodes getAAInfo() const { return AAInfo; }
170 /// getRanges - Return the range tag for the memory reference.
171 const MDNode *getRanges() const { return Ranges; }
173 bool isLoad() const { return Flags & MOLoad; }
174 bool isStore() const { return Flags & MOStore; }
175 bool isVolatile() const { return Flags & MOVolatile; }
176 bool isNonTemporal() const { return Flags & MONonTemporal; }
177 bool isInvariant() const { return Flags & MOInvariant; }
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.
183 /// Currently, we don't model the difference between volatile and atomic
184 /// operations. They should retain their ordering relative to all memory
186 bool isUnordered() const { return !isVolatile(); }
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);
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.
196 void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
197 void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
198 void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
200 /// Profile - Gather unique data for the object.
202 void Profile(FoldingSetNodeID &ID) const;
204 /// Support for operator<<.
206 void print(raw_ostream &OS) const;
207 void print(raw_ostream &OS, ModuleSlotTracker &MST) const;
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();
223 friend bool operator!=(const MachineMemOperand &LHS,
224 const MachineMemOperand &RHS) {
225 return !(LHS == RHS);
229 inline raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MRO) {
234 } // End llvm namespace