LLVM API Documentation

MachineMemOperand.h
Go to the documentation of this file.
00001 //==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the MachineMemOperand class, which is a
00011 // description of a memory reference. It is used to help track dependencies
00012 // in the backend.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
00017 #define LLVM_CODEGEN_MACHINEMEMOPERAND_H
00018 
00019 #include "llvm/ADT/PointerUnion.h"
00020 #include "llvm/CodeGen/PseudoSourceValue.h"
00021 #include "llvm/IR/Metadata.h"
00022 #include "llvm/IR/Value.h"  // PointerLikeTypeTraits<Value*>
00023 #include "llvm/Support/DataTypes.h"
00024 
00025 namespace llvm {
00026 
00027 class FoldingSetNodeID;
00028 class MDNode;
00029 class raw_ostream;
00030 
00031 /// MachinePointerInfo - This class contains a discriminated union of
00032 /// information about pointers in memory operands, relating them back to LLVM IR
00033 /// or to virtual locations (such as frame indices) that are exposed during
00034 /// codegen.
00035 struct MachinePointerInfo {
00036   /// V - This is the IR pointer value for the access, or it is null if unknown.
00037   /// If this is null, then the access is to a pointer in the default address
00038   /// space.
00039   PointerUnion<const Value *, const PseudoSourceValue *> V;
00040 
00041   /// Offset - This is an offset from the base Value*.
00042   int64_t Offset;
00043 
00044   explicit MachinePointerInfo(const Value *v = nullptr, int64_t offset = 0)
00045     : V(v), Offset(offset) {}
00046 
00047   explicit MachinePointerInfo(const PseudoSourceValue *v,
00048                               int64_t offset = 0)
00049     : V(v), Offset(offset) {}
00050 
00051   MachinePointerInfo getWithOffset(int64_t O) const {
00052     if (V.isNull()) return MachinePointerInfo();
00053     if (V.is<const Value*>())
00054       return MachinePointerInfo(V.get<const Value*>(), Offset+O);
00055     return MachinePointerInfo(V.get<const PseudoSourceValue*>(), Offset+O);
00056   }
00057 
00058   /// getAddrSpace - Return the LLVM IR address space number that this pointer
00059   /// points into.
00060   unsigned getAddrSpace() const;
00061 
00062   /// getConstantPool - Return a MachinePointerInfo record that refers to the
00063   /// constant pool.
00064   static MachinePointerInfo getConstantPool();
00065 
00066   /// getFixedStack - Return a MachinePointerInfo record that refers to the
00067   /// the specified FrameIndex.
00068   static MachinePointerInfo getFixedStack(int FI, int64_t offset = 0);
00069 
00070   /// getJumpTable - Return a MachinePointerInfo record that refers to a
00071   /// jump table entry.
00072   static MachinePointerInfo getJumpTable();
00073 
00074   /// getGOT - Return a MachinePointerInfo record that refers to a
00075   /// GOT entry.
00076   static MachinePointerInfo getGOT();
00077 
00078   /// getStack - stack pointer relative access.
00079   static MachinePointerInfo getStack(int64_t Offset);
00080 };
00081 
00082 
00083 //===----------------------------------------------------------------------===//
00084 /// MachineMemOperand - A description of a memory reference used in the backend.
00085 /// Instead of holding a StoreInst or LoadInst, this class holds the address
00086 /// Value of the reference along with a byte size and offset. This allows it
00087 /// to describe lowered loads and stores. Also, the special PseudoSourceValue
00088 /// objects can be used to represent loads and stores to memory locations
00089 /// that aren't explicit in the regular LLVM IR.
00090 ///
00091 class MachineMemOperand {
00092   MachinePointerInfo PtrInfo;
00093   uint64_t Size;
00094   unsigned Flags;
00095   AAMDNodes AAInfo;
00096   const MDNode *Ranges;
00097 
00098 public:
00099   /// Flags values. These may be or'd together.
00100   enum MemOperandFlags {
00101     /// The memory access reads data.
00102     MOLoad = 1,
00103     /// The memory access writes data.
00104     MOStore = 2,
00105     /// The memory access is volatile.
00106     MOVolatile = 4,
00107     /// The memory access is non-temporal.
00108     MONonTemporal = 8,
00109     /// The memory access is invariant.
00110     MOInvariant = 16,
00111     // Target hints allow target passes to annotate memory operations.
00112     MOTargetStartBit = 5,
00113     MOTargetNumBits = 3,
00114     // This is the number of bits we need to represent flags.
00115     MOMaxBits = 8
00116   };
00117 
00118   /// MachineMemOperand - Construct an MachineMemOperand object with the
00119   /// specified PtrInfo, flags, size, and base alignment.
00120   MachineMemOperand(MachinePointerInfo PtrInfo, unsigned flags, uint64_t s,
00121                     unsigned base_alignment,
00122                     const AAMDNodes &AAInfo = AAMDNodes(),
00123                     const MDNode *Ranges = nullptr);
00124 
00125   const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
00126 
00127   /// getValue - Return the base address of the memory access. This may either
00128   /// be a normal LLVM IR Value, or one of the special values used in CodeGen.
00129   /// Special values are those obtained via
00130   /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
00131   /// other PseudoSourceValue member functions which return objects which stand
00132   /// for frame/stack pointer relative references and other special references
00133   /// which are not representable in the high-level IR.
00134   const Value *getValue() const { return PtrInfo.V.dyn_cast<const Value*>(); }
00135 
00136   const PseudoSourceValue *getPseudoValue() const {
00137     return PtrInfo.V.dyn_cast<const PseudoSourceValue*>();
00138   }
00139 
00140   const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
00141 
00142   /// getFlags - Return the raw flags of the source value, \see MemOperandFlags.
00143   unsigned int getFlags() const { return Flags & ((1 << MOMaxBits) - 1); }
00144 
00145   /// Bitwise OR the current flags with the given flags.
00146   void setFlags(unsigned f) { Flags |= (f & ((1 << MOMaxBits) - 1)); }
00147 
00148   /// getOffset - For normal values, this is a byte offset added to the base
00149   /// address. For PseudoSourceValue::FPRel values, this is the FrameIndex
00150   /// number.
00151   int64_t getOffset() const { return PtrInfo.Offset; }
00152 
00153   unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
00154 
00155   /// getSize - Return the size in bytes of the memory reference.
00156   uint64_t getSize() const { return Size; }
00157 
00158   /// getAlignment - Return the minimum known alignment in bytes of the
00159   /// actual memory reference.
00160   uint64_t getAlignment() const;
00161 
00162   /// getBaseAlignment - Return the minimum known alignment in bytes of the
00163   /// base address, without the offset.
00164   uint64_t getBaseAlignment() const { return (1u << (Flags >> MOMaxBits)) >> 1; }
00165 
00166   /// getAAInfo - Return the AA tags for the memory reference.
00167   AAMDNodes getAAInfo() const { return AAInfo; }
00168 
00169   /// getRanges - Return the range tag for the memory reference.
00170   const MDNode *getRanges() const { return Ranges; }
00171 
00172   bool isLoad() const { return Flags & MOLoad; }
00173   bool isStore() const { return Flags & MOStore; }
00174   bool isVolatile() const { return Flags & MOVolatile; }
00175   bool isNonTemporal() const { return Flags & MONonTemporal; }
00176   bool isInvariant() const { return Flags & MOInvariant; }
00177 
00178   /// isUnordered - Returns true if this memory operation doesn't have any
00179   /// ordering constraints other than normal aliasing. Volatile and atomic
00180   /// memory operations can't be reordered.
00181   ///
00182   /// Currently, we don't model the difference between volatile and atomic
00183   /// operations. They should retain their ordering relative to all memory
00184   /// operations.
00185   bool isUnordered() const { return !isVolatile(); }
00186 
00187   /// refineAlignment - Update this MachineMemOperand to reflect the alignment
00188   /// of MMO, if it has a greater alignment. This must only be used when the
00189   /// new alignment applies to all users of this MachineMemOperand.
00190   void refineAlignment(const MachineMemOperand *MMO);
00191 
00192   /// setValue - Change the SourceValue for this MachineMemOperand. This
00193   /// should only be used when an object is being relocated and all references
00194   /// to it are being updated.
00195   void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
00196   void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
00197   void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
00198 
00199   /// Profile - Gather unique data for the object.
00200   ///
00201   void Profile(FoldingSetNodeID &ID) const;
00202 };
00203 
00204 raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MRO);
00205 
00206 } // End llvm namespace
00207 
00208 #endif