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