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