1 //===- MemoryLocation.h - Memory location descriptions ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_MEMORYLOCATION_H
17 #define LLVM_ANALYSIS_MEMORYLOCATION_H
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Metadata.h"
27 class MemTransferInst;
30 /// Representation for a specific memory location.
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.
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 {
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) };
46 /// The address of the start of the location.
49 /// The maximum size of the location, in address-units, or
50 /// UnknownSize if the size is not known.
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
58 /// The metadata nodes which describes the aliasing of the location (each
59 /// member is null if that kind of information is unavailable).
62 /// Return a location with information about the memory reference by the given
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))
72 else if (auto *I = dyn_cast<StoreInst>(Inst))
74 else if (auto *I = dyn_cast<VAArgInst>(Inst))
76 else if (auto *I = dyn_cast<AtomicCmpXchgInst>(Inst))
78 else if (auto *I = dyn_cast<AtomicRMWInst>(Inst))
80 llvm_unreachable("unsupported memory instruction");
83 /// Return a location representing the source of a memory transfer.
84 static MemoryLocation getForSource(const MemTransferInst *MTI);
86 /// Return a location representing the destination of a memory set or
88 static MemoryLocation getForDest(const MemIntrinsic *MI);
90 explicit MemoryLocation(const Value *Ptr = nullptr,
91 uint64_t Size = UnknownSize,
92 const AAMDNodes &AATags = AAMDNodes())
93 : Ptr(Ptr), Size(Size), AATags(AATags) {}
95 MemoryLocation getWithNewPtr(const Value *NewPtr) const {
96 MemoryLocation Copy(*this);
101 MemoryLocation getWithNewSize(uint64_t NewSize) const {
102 MemoryLocation Copy(*this);
107 MemoryLocation getWithoutAATags() const {
108 MemoryLocation Copy(*this);
109 Copy.AATags = AAMDNodes();
113 bool operator==(const MemoryLocation &Other) const {
114 return Ptr == Other.Ptr && Size == Other.Size && AATags == Other.AATags;
118 // Specialize DenseMapInfo for MemoryLocation.
119 template <> struct DenseMapInfo<MemoryLocation> {
120 static inline MemoryLocation getEmptyKey() {
121 return MemoryLocation(DenseMapInfo<const Value *>::getEmptyKey(), 0);
123 static inline MemoryLocation getTombstoneKey() {
124 return MemoryLocation(DenseMapInfo<const Value *>::getTombstoneKey(), 0);
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);
131 static bool isEqual(const MemoryLocation &LHS, const MemoryLocation &RHS) {