LLVM  9.0.0svn
MemoryLocation.cpp
Go to the documentation of this file.
1 //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/Type.h"
18 using namespace llvm;
19 
21  OS << "LocationSize::";
22  if (*this == unknown())
23  OS << "unknown";
24  else if (*this == mapEmpty())
25  OS << "mapEmpty";
26  else if (*this == mapTombstone())
27  OS << "mapTombstone";
28  else if (isPrecise())
29  OS << "precise(" << getValue() << ')';
30  else
31  OS << "upperBound(" << getValue() << ')';
32 }
33 
35  AAMDNodes AATags;
36  LI->getAAMetadata(AATags);
37  const auto &DL = LI->getModule()->getDataLayout();
38 
39  return MemoryLocation(
40  LI->getPointerOperand(),
41  LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
42 }
43 
45  AAMDNodes AATags;
46  SI->getAAMetadata(AATags);
47  const auto &DL = SI->getModule()->getDataLayout();
48 
49  return MemoryLocation(SI->getPointerOperand(),
50  LocationSize::precise(DL.getTypeStoreSize(
51  SI->getValueOperand()->getType())),
52  AATags);
53 }
54 
56  AAMDNodes AATags;
57  VI->getAAMetadata(AATags);
58 
60  AATags);
61 }
62 
64  AAMDNodes AATags;
65  CXI->getAAMetadata(AATags);
66  const auto &DL = CXI->getModule()->getDataLayout();
67 
68  return MemoryLocation(CXI->getPointerOperand(),
69  LocationSize::precise(DL.getTypeStoreSize(
70  CXI->getCompareOperand()->getType())),
71  AATags);
72 }
73 
75  AAMDNodes AATags;
76  RMWI->getAAMetadata(AATags);
77  const auto &DL = RMWI->getModule()->getDataLayout();
78 
79  return MemoryLocation(RMWI->getPointerOperand(),
80  LocationSize::precise(DL.getTypeStoreSize(
81  RMWI->getValOperand()->getType())),
82  AATags);
83 }
84 
86  return getForSource(cast<AnyMemTransferInst>(MTI));
87 }
88 
90  return getForSource(cast<AnyMemTransferInst>(MTI));
91 }
92 
94  auto Size = LocationSize::unknown();
95  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
96  Size = LocationSize::precise(C->getValue().getZExtValue());
97 
98  // memcpy/memmove can have AA tags. For memcpy, they apply
99  // to both the source and the destination.
100  AAMDNodes AATags;
101  MTI->getAAMetadata(AATags);
102 
103  return MemoryLocation(MTI->getRawSource(), Size, AATags);
104 }
105 
107  return getForDest(cast<AnyMemIntrinsic>(MI));
108 }
109 
111  return getForDest(cast<AnyMemIntrinsic>(MI));
112 }
113 
115  auto Size = LocationSize::unknown();
116  if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
117  Size = LocationSize::precise(C->getValue().getZExtValue());
118 
119  // memcpy/memmove can have AA tags. For memcpy, they apply
120  // to both the source and the destination.
121  AAMDNodes AATags;
122  MI->getAAMetadata(AATags);
123 
124  return MemoryLocation(MI->getRawDest(), Size, AATags);
125 }
126 
128  unsigned ArgIdx,
129  const TargetLibraryInfo *TLI) {
130  AAMDNodes AATags;
131  Call->getAAMetadata(AATags);
132  const Value *Arg = Call->getArgOperand(ArgIdx);
133 
134  // We may be able to produce an exact size for known intrinsics.
135  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
136  const DataLayout &DL = II->getModule()->getDataLayout();
137 
138  switch (II->getIntrinsicID()) {
139  default:
140  break;
141  case Intrinsic::memset:
142  case Intrinsic::memcpy:
143  case Intrinsic::memmove:
144  assert((ArgIdx == 0 || ArgIdx == 1) &&
145  "Invalid argument index for memory intrinsic");
146  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
147  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
148  AATags);
149  break;
150 
151  case Intrinsic::lifetime_start:
152  case Intrinsic::lifetime_end:
153  case Intrinsic::invariant_start:
154  assert(ArgIdx == 1 && "Invalid argument index");
155  return MemoryLocation(
156  Arg,
158  cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
159  AATags);
160 
161  case Intrinsic::invariant_end:
162  // The first argument to an invariant.end is a "descriptor" type (e.g. a
163  // pointer to a empty struct) which is never actually dereferenced.
164  if (ArgIdx == 0)
165  return MemoryLocation(Arg, LocationSize::precise(0), AATags);
166  assert(ArgIdx == 2 && "Invalid argument index");
167  return MemoryLocation(
168  Arg,
170  cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
171  AATags);
172 
173  case Intrinsic::arm_neon_vld1:
174  assert(ArgIdx == 0 && "Invalid argument index");
175  // LLVM's vld1 and vst1 intrinsics currently only support a single
176  // vector register.
177  return MemoryLocation(
178  Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
179  AATags);
180 
181  case Intrinsic::arm_neon_vst1:
182  assert(ArgIdx == 0 && "Invalid argument index");
183  return MemoryLocation(Arg,
185  II->getArgOperand(1)->getType())),
186  AATags);
187  }
188  }
189 
190  // We can bound the aliasing properties of memset_pattern16 just as we can
191  // for memcpy/memset. This is particularly important because the
192  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
193  // whenever possible.
194  LibFunc F;
195  if (TLI && Call->getCalledFunction() &&
196  TLI->getLibFunc(*Call->getCalledFunction(), F) &&
197  F == LibFunc_memset_pattern16 && TLI->has(F)) {
198  assert((ArgIdx == 0 || ArgIdx == 1) &&
199  "Invalid argument index for memset_pattern16");
200  if (ArgIdx == 1)
201  return MemoryLocation(Arg, LocationSize::precise(16), AATags);
202  if (const ConstantInt *LenCI =
203  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
204  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
205  AATags);
206  }
207  // FIXME: Handle memset_pattern4 and memset_pattern8 also.
208 
209  return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),
210  AATags);
211 }
uint64_t CallInst * C
Value * getValueOperand()
Definition: Instructions.h:409
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:528
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:1100
F(f)
An instruction for reading from memory.
Definition: Instructions.h:167
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:691
Value * getLength() const
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1241
static constexpr LocationSize mapTombstone()
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
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:244
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:320
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()
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Value * getPointerOperand()
Definition: Instructions.h:284
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:815
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
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:643
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:55
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
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:811
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1287
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:444
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
IRTranslator LLVM IR MI
Value * getPointerOperand()
Definition: Instructions.h:412
Value * getRawDest() const
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:43
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.