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