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