LLVM  5.0.0
MachineMemOperand.h
Go to the documentation of this file.
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/BitmaskEnum.h"
20 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Value.h" // PointerLikeTypeTraits<Value*>
26 #include "llvm/Support/DataTypes.h"
27 
28 namespace llvm {
29 
30 class FoldingSetNodeID;
31 class MDNode;
32 class raw_ostream;
33 class MachineFunction;
34 class ModuleSlotTracker;
35 
36 /// This class contains a discriminated union of information about pointers in
37 /// memory operands, relating them back to LLVM IR or to virtual locations (such
38 /// as frame indices) that are exposed during codegen.
40  /// This is the IR pointer value for the access, or it is null if unknown.
41  /// If this is null, then the access is to a pointer in the default address
42  /// space.
44 
45  /// Offset - This is an offset from the base Value*.
46  int64_t Offset;
47 
48  explicit MachinePointerInfo(const Value *v = nullptr, int64_t offset = 0)
49  : V(v), Offset(offset) {}
50 
52  int64_t offset = 0)
53  : V(v), Offset(offset) {}
54 
55  MachinePointerInfo getWithOffset(int64_t O) const {
56  if (V.isNull()) return MachinePointerInfo();
57  if (V.is<const Value*>())
58  return MachinePointerInfo(V.get<const Value*>(), Offset+O);
59  return MachinePointerInfo(V.get<const PseudoSourceValue*>(), Offset+O);
60  }
61 
62  /// Return true if memory region [V, V+Offset+Size) is known to be
63  /// dereferenceable.
64  bool isDereferenceable(unsigned Size, LLVMContext &C,
65  const DataLayout &DL) const;
66 
67  /// Return the LLVM IR address space number that this pointer points into.
68  unsigned getAddrSpace() const;
69 
70  /// Return a MachinePointerInfo record that refers to the constant pool.
72 
73  /// Return a MachinePointerInfo record that refers to the specified
74  /// FrameIndex.
76  int64_t Offset = 0);
77 
78  /// Return a MachinePointerInfo record that refers to a jump table entry.
80 
81  /// Return a MachinePointerInfo record that refers to a GOT entry.
83 
84  /// Stack pointer relative access.
86 };
87 
88 
89 //===----------------------------------------------------------------------===//
90 /// A description of a memory reference used in the backend.
91 /// Instead of holding a StoreInst or LoadInst, this class holds the address
92 /// Value of the reference along with a byte size and offset. This allows it
93 /// to describe lowered loads and stores. Also, the special PseudoSourceValue
94 /// objects can be used to represent loads and stores to memory locations
95 /// that aren't explicit in the regular LLVM IR.
96 ///
98 public:
99  /// Flags values. These may be or'd together.
100  enum Flags : uint16_t {
101  // No flags set.
102  MONone = 0,
103  /// The memory access reads data.
104  MOLoad = 1u << 0,
105  /// The memory access writes data.
106  MOStore = 1u << 1,
107  /// The memory access is volatile.
108  MOVolatile = 1u << 2,
109  /// The memory access is non-temporal.
110  MONonTemporal = 1u << 3,
111  /// The memory access is dereferenceable (i.e., doesn't trap).
112  MODereferenceable = 1u << 4,
113  /// The memory access always returns the same value (or traps).
114  MOInvariant = 1u << 5,
115 
116  // Reserved for use by target-specific passes.
117  // Targets may override getSerializableMachineMemOperandTargetFlags() to
118  // enable MIR serialization/parsing of these flags. If more of these flags
119  // are added, the MIR printing/parsing code will need to be updated as well.
120  MOTargetFlag1 = 1u << 6,
121  MOTargetFlag2 = 1u << 7,
122  MOTargetFlag3 = 1u << 8,
123 
124  LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ MOTargetFlag3)
125  };
126 
127 private:
128  /// Atomic information for this memory operation.
129  struct MachineAtomicInfo {
130  /// Synchronization scope ID for this memory operation.
131  unsigned SSID : 8; // SyncScope::ID
132  /// Atomic ordering requirements for this memory operation. For cmpxchg
133  /// atomic operations, atomic ordering requirements when store occurs.
134  unsigned Ordering : 4; // enum AtomicOrdering
135  /// For cmpxchg atomic operations, atomic ordering requirements when store
136  /// does not occur.
137  unsigned FailureOrdering : 4; // enum AtomicOrdering
138  };
139 
140  MachinePointerInfo PtrInfo;
141  uint64_t Size;
142  Flags FlagVals;
143  uint16_t BaseAlignLog2; // log_2(base_alignment) + 1
144  MachineAtomicInfo AtomicInfo;
145  AAMDNodes AAInfo;
146  const MDNode *Ranges;
147 
148 public:
149  /// Construct a MachineMemOperand object with the specified PtrInfo, flags,
150  /// size, and base alignment. For atomic operations the synchronization scope
151  /// and atomic ordering requirements must also be specified. For cmpxchg
152  /// atomic operations the atomic ordering requirements when store does not
153  /// occur must also be specified.
154  MachineMemOperand(MachinePointerInfo PtrInfo, Flags flags, uint64_t s,
155  unsigned base_alignment,
156  const AAMDNodes &AAInfo = AAMDNodes(),
157  const MDNode *Ranges = nullptr,
160  AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
161 
162  const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
163 
164  /// Return the base address of the memory access. This may either be a normal
165  /// LLVM IR Value, or one of the special values used in CodeGen.
166  /// Special values are those obtained via
167  /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
168  /// other PseudoSourceValue member functions which return objects which stand
169  /// for frame/stack pointer relative references and other special references
170  /// which are not representable in the high-level IR.
171  const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
172 
174  return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
175  }
176 
177  const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
178 
179  /// Return the raw flags of the source value, \see Flags.
180  Flags getFlags() const { return FlagVals; }
181 
182  /// Bitwise OR the current flags with the given flags.
183  void setFlags(Flags f) { FlagVals |= f; }
184 
185  /// For normal values, this is a byte offset added to the base address.
186  /// For PseudoSourceValue::FPRel values, this is the FrameIndex number.
187  int64_t getOffset() const { return PtrInfo.Offset; }
188 
189  unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
190 
191  /// Return the size in bytes of the memory reference.
192  uint64_t getSize() const { return Size; }
193 
194  /// Return the minimum known alignment in bytes of the actual memory
195  /// reference.
196  uint64_t getAlignment() const;
197 
198  /// Return the minimum known alignment in bytes of the base address, without
199  /// the offset.
200  uint64_t getBaseAlignment() const { return (1u << BaseAlignLog2) >> 1; }
201 
202  /// Return the AA tags for the memory reference.
203  AAMDNodes getAAInfo() const { return AAInfo; }
204 
205  /// Return the range tag for the memory reference.
206  const MDNode *getRanges() const { return Ranges; }
207 
208  /// Returns the synchronization scope ID for this memory operation.
210  return static_cast<SyncScope::ID>(AtomicInfo.SSID);
211  }
212 
213  /// Return the atomic ordering requirements for this memory operation. For
214  /// cmpxchg atomic operations, return the atomic ordering requirements when
215  /// store occurs.
217  return static_cast<AtomicOrdering>(AtomicInfo.Ordering);
218  }
219 
220  /// For cmpxchg atomic operations, return the atomic ordering requirements
221  /// when store does not occur.
223  return static_cast<AtomicOrdering>(AtomicInfo.FailureOrdering);
224  }
225 
226  bool isLoad() const { return FlagVals & MOLoad; }
227  bool isStore() const { return FlagVals & MOStore; }
228  bool isVolatile() const { return FlagVals & MOVolatile; }
229  bool isNonTemporal() const { return FlagVals & MONonTemporal; }
230  bool isDereferenceable() const { return FlagVals & MODereferenceable; }
231  bool isInvariant() const { return FlagVals & MOInvariant; }
232 
233  /// Returns true if this operation has an atomic ordering requirement of
234  /// unordered or higher, false otherwise.
235  bool isAtomic() const { return getOrdering() != AtomicOrdering::NotAtomic; }
236 
237  /// Returns true if this memory operation doesn't have any ordering
238  /// constraints other than normal aliasing. Volatile and atomic memory
239  /// operations can't be reordered.
240  ///
241  /// Currently, we don't model the difference between volatile and atomic
242  /// operations. They should retain their ordering relative to all memory
243  /// operations.
244  bool isUnordered() const { return !isVolatile(); }
245 
246  /// Update this MachineMemOperand to reflect the alignment of MMO, if it has a
247  /// greater alignment. This must only be used when the new alignment applies
248  /// to all users of this MachineMemOperand.
249  void refineAlignment(const MachineMemOperand *MMO);
250 
251  /// Change the SourceValue for this MachineMemOperand. This should only be
252  /// used when an object is being relocated and all references to it are being
253  /// updated.
254  void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
255  void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
256  void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
257 
258  /// Profile - Gather unique data for the object.
259  ///
260  void Profile(FoldingSetNodeID &ID) const;
261 
262  /// Support for operator<<.
263  /// @{
264  void print(raw_ostream &OS) const;
265  void print(raw_ostream &OS, ModuleSlotTracker &MST) const;
266  /// @}
267 
268  friend bool operator==(const MachineMemOperand &LHS,
269  const MachineMemOperand &RHS) {
270  return LHS.getValue() == RHS.getValue() &&
271  LHS.getPseudoValue() == RHS.getPseudoValue() &&
272  LHS.getSize() == RHS.getSize() &&
273  LHS.getOffset() == RHS.getOffset() &&
274  LHS.getFlags() == RHS.getFlags() &&
275  LHS.getAAInfo() == RHS.getAAInfo() &&
276  LHS.getRanges() == RHS.getRanges() &&
277  LHS.getAlignment() == RHS.getAlignment() &&
278  LHS.getAddrSpace() == RHS.getAddrSpace();
279  }
280 
281  friend bool operator!=(const MachineMemOperand &LHS,
282  const MachineMemOperand &RHS) {
283  return !(LHS == RHS);
284  }
285 };
286 
288  MRO.print(OS);
289  return OS;
290 }
291 
292 } // End llvm namespace
293 
294 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue)
LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can perform bitwise operatio...
Definition: BitmaskEnum.h:42
static MachinePointerInfo getJumpTable(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a jump table entry.
Flags getFlags() const
Return the raw flags of the source value,.
Atomic ordering constants.
DataLayout DL
This file contains the declarations for metadata subclasses.
Metadata node.
Definition: Metadata.h:862
Manage lifetime of a slot tracker for printing IR.
const MachinePointerInfo & getPointerInfo() const
AtomicOrdering getOrdering() const
Return the atomic ordering requirements for this memory operation.
MachineMemOperand(MachinePointerInfo PtrInfo, Flags flags, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
Construct a MachineMemOperand object with the specified PtrInfo, flags, size, and base alignment...
A description of a memory reference used in the backend.
MachinePointerInfo(const PseudoSourceValue *v, int64_t offset=0)
void setFlags(Flags f)
Bitwise OR the current flags with the given flags.
MachinePointerInfo getWithOffset(int64_t O) const
The memory access is dereferenceable (i.e., doesn't trap).
friend bool operator!=(const MachineMemOperand &LHS, const MachineMemOperand &RHS)
AtomicOrdering
Atomic ordering for LLVM's memory model.
uint64_t getAlignment() const
Return the minimum known alignment in bytes of the actual memory reference.
static MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset)
Stack pointer relative access.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:311
The memory access is volatile.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
int64_t Offset
Offset - This is an offset from the base Value*.
void Profile(FoldingSetNodeID &ID) const
Profile - Gather unique data for the object.
void setOffset(int64_t NewOffset)
The memory access is non-temporal.
const PseudoSourceValue * getPseudoValue() const
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID for this memory operation.
This class contains a discriminated union of information about pointers in memory operands...
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:115
void refineAlignment(const MachineMemOperand *MMO)
Update this MachineMemOperand to reflect the alignment of MMO, if it has a greater alignment...
The memory access writes data.
const void * getOpaqueValue() const
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:58
void setValue(const Value *NewSV)
Change the SourceValue for this MachineMemOperand.
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:135
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:642
unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
const Value * getValue() const
Return the base address of the memory access.
Special value supplied for machine level alias analysis.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Flags
Flags values. These may be or'd together.
void print(raw_ostream &OS) const
Support for operator<<.
The memory access reads data.
friend bool operator==(const MachineMemOperand &LHS, const MachineMemOperand &RHS)
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
bool isUnordered() const
Returns true if this memory operation doesn't have any ordering constraints other than normal aliasin...
bool isAtomic() const
Returns true if this operation has an atomic ordering requirement of unordered or higher...
#define C
Definition: LargeTest.cpp:25
The memory access always returns the same value (or traps).
unsigned getAddrSpace() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2018
static MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
void setValue(const PseudoSourceValue *NewSV)
MachinePointerInfo(const Value *v=nullptr, int64_t offset=0)
static MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
const MDNode * getRanges() const
Return the range tag for the memory reference.
LLVM Value Representation.
Definition: Value.h:73
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
uint64_t getSize() const
Return the size in bytes of the memory reference.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
AtomicOrdering getFailureOrdering() const
For cmpxchg atomic operations, return the atomic ordering requirements when store does not occur...
int is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:123
uint64_t getBaseAlignment() const
Return the minimum known alignment in bytes of the base address, without the offset.