[PM/AA] Start refactoring AliasAnalysis to remove the analysis group and
[oota-llvm.git] / include / llvm / Analysis / MemoryLocation.h
1 //===- MemoryLocation.h - Memory location descriptions ----------*- 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 /// \file
10 /// This file provides utility analysis objects describing memory locations.
11 /// These are used both by the Alias Analysis infrastructure and more
12 /// specialized memory analysis layers.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ANALYSIS_MEMORYLOCATION_H
17 #define LLVM_ANALYSIS_MEMORYLOCATION_H
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Metadata.h"
22
23 namespace llvm {
24
25 class LoadInst;
26 class StoreInst;
27 class MemTransferInst;
28 class MemIntrinsic;
29
30 /// Representation for a specific memory location.
31 ///
32 /// This abstraction can be used to represent a specific location in memory.
33 /// The goal of the location is to represent enough information to describe
34 /// abstract aliasing, modification, and reference behaviors of whatever
35 /// value(s) are stored in memory at the particular location.
36 ///
37 /// The primary user of this interface is LLVM's Alias Analysis, but other
38 /// memory analyses such as MemoryDependence can use it as well.
39 class MemoryLocation {
40 public:
41   /// UnknownSize - This is a special value which can be used with the
42   /// size arguments in alias queries to indicate that the caller does not
43   /// know the sizes of the potential memory references.
44   enum : uint64_t { UnknownSize = ~UINT64_C(0) };
45
46   /// The address of the start of the location.
47   const Value *Ptr;
48
49   /// The maximum size of the location, in address-units, or
50   /// UnknownSize if the size is not known.
51   ///
52   /// Note that an unknown size does not mean the pointer aliases the entire
53   /// virtual address space, because there are restrictions on stepping out of
54   /// one object and into another. See
55   /// http://llvm.org/docs/LangRef.html#pointeraliasing
56   uint64_t Size;
57
58   /// The metadata nodes which describes the aliasing of the location (each
59   /// member is null if that kind of information is unavailable).
60   AAMDNodes AATags;
61
62   /// Return a location with information about the memory reference by the given
63   /// instruction.
64   static MemoryLocation get(const LoadInst *LI);
65   static MemoryLocation get(const StoreInst *SI);
66   static MemoryLocation get(const VAArgInst *VI);
67   static MemoryLocation get(const AtomicCmpXchgInst *CXI);
68   static MemoryLocation get(const AtomicRMWInst *RMWI);
69   static MemoryLocation get(const Instruction *Inst) {
70     if (auto *I = dyn_cast<LoadInst>(Inst))
71       return get(I);
72     else if (auto *I = dyn_cast<StoreInst>(Inst))
73       return get(I);
74     else if (auto *I = dyn_cast<VAArgInst>(Inst))
75       return get(I);
76     else if (auto *I = dyn_cast<AtomicCmpXchgInst>(Inst))
77       return get(I);
78     else if (auto *I = dyn_cast<AtomicRMWInst>(Inst))
79       return get(I);
80     llvm_unreachable("unsupported memory instruction");
81   }
82
83   /// Return a location representing the source of a memory transfer.
84   static MemoryLocation getForSource(const MemTransferInst *MTI);
85
86   /// Return a location representing the destination of a memory set or
87   /// transfer.
88   static MemoryLocation getForDest(const MemIntrinsic *MI);
89
90   explicit MemoryLocation(const Value *Ptr = nullptr,
91                           uint64_t Size = UnknownSize,
92                           const AAMDNodes &AATags = AAMDNodes())
93       : Ptr(Ptr), Size(Size), AATags(AATags) {}
94
95   MemoryLocation getWithNewPtr(const Value *NewPtr) const {
96     MemoryLocation Copy(*this);
97     Copy.Ptr = NewPtr;
98     return Copy;
99   }
100
101   MemoryLocation getWithNewSize(uint64_t NewSize) const {
102     MemoryLocation Copy(*this);
103     Copy.Size = NewSize;
104     return Copy;
105   }
106
107   MemoryLocation getWithoutAATags() const {
108     MemoryLocation Copy(*this);
109     Copy.AATags = AAMDNodes();
110     return Copy;
111   }
112
113   bool operator==(const MemoryLocation &Other) const {
114     return Ptr == Other.Ptr && Size == Other.Size && AATags == Other.AATags;
115   }
116 };
117
118 // Specialize DenseMapInfo for MemoryLocation.
119 template <> struct DenseMapInfo<MemoryLocation> {
120   static inline MemoryLocation getEmptyKey() {
121     return MemoryLocation(DenseMapInfo<const Value *>::getEmptyKey(), 0);
122   }
123   static inline MemoryLocation getTombstoneKey() {
124     return MemoryLocation(DenseMapInfo<const Value *>::getTombstoneKey(), 0);
125   }
126   static unsigned getHashValue(const MemoryLocation &Val) {
127     return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
128            DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
129            DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);
130   }
131   static bool isEqual(const MemoryLocation &LHS, const MemoryLocation &RHS) {
132     return LHS == RHS;
133   }
134 };
135 }
136
137 #endif