LLVM  8.0.1
MemoryLocation.cpp
Go to the documentation of this file.
1 //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
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 
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Type.h"
19 using namespace llvm;
20 
22  OS << "LocationSize::";
23  if (*this == unknown())
24  OS << "unknown";
25  else if (*this == mapEmpty())
26  OS << "mapEmpty";
27  else if (*this == mapTombstone())
28  OS << "mapTombstone";
29  else if (isPrecise())
30  OS << "precise(" << getValue() << ')';
31  else
32  OS << "upperBound(" << getValue() << ')';
33 }
34 
36  AAMDNodes AATags;
37  LI->getAAMetadata(AATags);
38  const auto &DL = LI->getModule()->getDataLayout();
39 
40  return MemoryLocation(
41  LI->getPointerOperand(),
42  LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
43 }
44 
46  AAMDNodes AATags;
47  SI->getAAMetadata(AATags);
48  const auto &DL = SI->getModule()->getDataLayout();
49 
50  return MemoryLocation(SI->getPointerOperand(),
51  LocationSize::precise(DL.getTypeStoreSize(
52  SI->getValueOperand()->getType())),
53  AATags);
54 }
55 
57  AAMDNodes AATags;
58  VI->getAAMetadata(AATags);
59 
61  AATags);
62 }
63 
65  AAMDNodes AATags;
66  CXI->getAAMetadata(AATags);
67  const auto &DL = CXI->getModule()->getDataLayout();
68 
69  return MemoryLocation(CXI->getPointerOperand(),
70  LocationSize::precise(DL.getTypeStoreSize(
71  CXI->getCompareOperand()->getType())),
72  AATags);
73 }
74 
76  AAMDNodes AATags;
77  RMWI->getAAMetadata(AATags);
78  const auto &DL = RMWI->getModule()->getDataLayout();
79 
80  return MemoryLocation(RMWI->getPointerOperand(),
81  LocationSize::precise(DL.getTypeStoreSize(
82  RMWI->getValOperand()->getType())),
83  AATags);
84 }
85 
87  return getForSource(cast<AnyMemTransferInst>(MTI));
88 }
89 
91  return getForSource(cast<AnyMemTransferInst>(MTI));
92 }
93 
95  auto Size = LocationSize::unknown();
96  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
97  Size = LocationSize::precise(C->getValue().getZExtValue());
98 
99  // memcpy/memmove can have AA tags. For memcpy, they apply
100  // to both the source and the destination.
101  AAMDNodes AATags;
102  MTI->getAAMetadata(AATags);
103 
104  return MemoryLocation(MTI->getRawSource(), Size, AATags);
105 }
106 
108  return getForDest(cast<AnyMemIntrinsic>(MI));
109 }
110 
112  return getForDest(cast<AnyMemIntrinsic>(MI));
113 }
114 
116  auto Size = LocationSize::unknown();
117  if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
118  Size = LocationSize::precise(C->getValue().getZExtValue());
119 
120  // memcpy/memmove can have AA tags. For memcpy, they apply
121  // to both the source and the destination.
122  AAMDNodes AATags;
123  MI->getAAMetadata(AATags);
124 
125  return MemoryLocation(MI->getRawDest(), Size, AATags);
126 }
127 
129  unsigned ArgIdx,
130  const TargetLibraryInfo *TLI) {
131  AAMDNodes AATags;
132  Call->getAAMetadata(AATags);
133  const Value *Arg = Call->getArgOperand(ArgIdx);
134 
135  // We may be able to produce an exact size for known intrinsics.
136  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
137  const DataLayout &DL = II->getModule()->getDataLayout();
138 
139  switch (II->getIntrinsicID()) {
140  default:
141  break;
142  case Intrinsic::memset:
143  case Intrinsic::memcpy:
144  case Intrinsic::memmove:
145  assert((ArgIdx == 0 || ArgIdx == 1) &&
146  "Invalid argument index for memory intrinsic");
147  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
148  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
149  AATags);
150  break;
151 
155  assert(ArgIdx == 1 && "Invalid argument index");
156  return MemoryLocation(
157  Arg,
159  cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
160  AATags);
161 
163  // The first argument to an invariant.end is a "descriptor" type (e.g. a
164  // pointer to a empty struct) which is never actually dereferenced.
165  if (ArgIdx == 0)
166  return MemoryLocation(Arg, LocationSize::precise(0), AATags);
167  assert(ArgIdx == 2 && "Invalid argument index");
168  return MemoryLocation(
169  Arg,
171  cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
172  AATags);
173 
175  assert(ArgIdx == 0 && "Invalid argument index");
176  // LLVM's vld1 and vst1 intrinsics currently only support a single
177  // vector register.
178  return MemoryLocation(
179  Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
180  AATags);
181 
183  assert(ArgIdx == 0 && "Invalid argument index");
184  return MemoryLocation(Arg,
186  II->getArgOperand(1)->getType())),
187  AATags);
188  }
189  }
190 
191  // We can bound the aliasing properties of memset_pattern16 just as we can
192  // for memcpy/memset. This is particularly important because the
193  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
194  // whenever possible.
195  LibFunc F;
196  if (TLI && Call->getCalledFunction() &&
197  TLI->getLibFunc(*Call->getCalledFunction(), F) &&
198  F == LibFunc_memset_pattern16 && TLI->has(F)) {
199  assert((ArgIdx == 0 || ArgIdx == 1) &&
200  "Invalid argument index for memset_pattern16");
201  if (ArgIdx == 1)
202  return MemoryLocation(Arg, LocationSize::precise(16), AATags);
203  if (const ConstantInt *LenCI =
204  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
205  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
206  AATags);
207  }
208  // FIXME: Handle memset_pattern4 and memset_pattern8 also.
209 
210  return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),
211  AATags);
212 }
uint64_t CallInst * C
Value * getValueOperand()
Definition: Instructions.h:410
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
This class represents lattice values for constants.
Definition: AllocatorList.h:24
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:529
static constexpr LocationSize unknown()
bool isPrecise() const
static LocationSize precise(uint64_t Value)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1014
F(f)
An instruction for reading from memory.
Definition: Instructions.h:168
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:692
Value * getLength() const
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1135
static constexpr LocationSize mapTombstone()
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
Value * getPointerOperand()
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
bool has(LibFunc F) const
Tests whether a library function is available.
An instruction for storing to memory.
Definition: Instructions.h:321
uint64_t getValue() const
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static constexpr LocationSize mapEmpty()
Value * getPointerOperand()
Definition: Instructions.h:285
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Representation for a specific memory location.
Value * getValOperand()
Definition: Instructions.h:800
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
Value * getRawSource() const
Return the arguments to the instruction.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:56
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
amdgpu Simplify well known AMD library false Value Value * Arg
void print(raw_ostream &OS) const
This class wraps the llvm.memcpy/memmove intrinsics.
This file provides utility analysis objects describing memory locations.
Value * getPointerOperand()
Definition: Instructions.h:796
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1181
uint32_t Size
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:419
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
Value * getPointerOperand()
Definition: Instructions.h:413
Value * getRawDest() const
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.