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