LLVM  8.0.0svn
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(LI->getPointerOperand(),
41  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  DL.getTypeStoreSize(SI->getValueOperand()->getType()),
51  AATags);
52 }
53 
55  AAMDNodes AATags;
56  VI->getAAMetadata(AATags);
57 
59  AATags);
60 }
61 
63  AAMDNodes AATags;
64  CXI->getAAMetadata(AATags);
65  const auto &DL = CXI->getModule()->getDataLayout();
66 
67  return MemoryLocation(
68  CXI->getPointerOperand(),
69  DL.getTypeStoreSize(CXI->getCompareOperand()->getType()), AATags);
70 }
71 
73  AAMDNodes AATags;
74  RMWI->getAAMetadata(AATags);
75  const auto &DL = RMWI->getModule()->getDataLayout();
76 
77  return MemoryLocation(RMWI->getPointerOperand(),
78  DL.getTypeStoreSize(RMWI->getValOperand()->getType()),
79  AATags);
80 }
81 
83  return getForSource(cast<AnyMemTransferInst>(MTI));
84 }
85 
87  return getForSource(cast<AnyMemTransferInst>(MTI));
88 }
89 
92  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
93  Size = C->getValue().getZExtValue();
94 
95  // memcpy/memmove can have AA tags. For memcpy, they apply
96  // to both the source and the destination.
97  AAMDNodes AATags;
98  MTI->getAAMetadata(AATags);
99 
100  return MemoryLocation(MTI->getRawSource(), Size, AATags);
101 }
102 
104  return getForDest(cast<AnyMemIntrinsic>(MI));
105 }
106 
108  return getForDest(cast<AnyMemIntrinsic>(MI));
109 }
110 
113  if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
114  Size = C->getValue().getZExtValue();
115 
116  // memcpy/memmove can have AA tags. For memcpy, they apply
117  // to both the source and the destination.
118  AAMDNodes AATags;
119  MI->getAAMetadata(AATags);
120 
121  return MemoryLocation(MI->getRawDest(), Size, AATags);
122 }
123 
125  unsigned ArgIdx,
126  const TargetLibraryInfo *TLI) {
127  AAMDNodes AATags;
128  CS->getAAMetadata(AATags);
129  const Value *Arg = CS.getArgument(ArgIdx);
130 
131  // We may be able to produce an exact size for known intrinsics.
132  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
133  const DataLayout &DL = II->getModule()->getDataLayout();
134 
135  switch (II->getIntrinsicID()) {
136  default:
137  break;
138  case Intrinsic::memset:
139  case Intrinsic::memcpy:
140  case Intrinsic::memmove:
141  assert((ArgIdx == 0 || ArgIdx == 1) &&
142  "Invalid argument index for memory intrinsic");
143  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
144  return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
145  break;
146 
147  case Intrinsic::lifetime_start:
148  case Intrinsic::lifetime_end:
149  case Intrinsic::invariant_start:
150  assert(ArgIdx == 1 && "Invalid argument index");
151  return MemoryLocation(
152  Arg, cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AATags);
153 
154  case Intrinsic::invariant_end:
155  // The first argument to an invariant.end is a "descriptor" type (e.g. a
156  // pointer to a empty struct) which is never actually dereferenced.
157  if (ArgIdx == 0)
158  return MemoryLocation(Arg, 0, AATags);
159  assert(ArgIdx == 2 && "Invalid argument index");
160  return MemoryLocation(
161  Arg, cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AATags);
162 
163  case Intrinsic::arm_neon_vld1:
164  assert(ArgIdx == 0 && "Invalid argument index");
165  // LLVM's vld1 and vst1 intrinsics currently only support a single
166  // vector register.
167  return MemoryLocation(Arg, DL.getTypeStoreSize(II->getType()), AATags);
168 
169  case Intrinsic::arm_neon_vst1:
170  assert(ArgIdx == 0 && "Invalid argument index");
171  return MemoryLocation(
172  Arg, DL.getTypeStoreSize(II->getArgOperand(1)->getType()), AATags);
173  }
174  }
175 
176  // We can bound the aliasing properties of memset_pattern16 just as we can
177  // for memcpy/memset. This is particularly important because the
178  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
179  // whenever possible.
180  LibFunc F;
181  if (TLI && CS.getCalledFunction() &&
182  TLI->getLibFunc(*CS.getCalledFunction(), F) &&
183  F == LibFunc_memset_pattern16 && TLI->has(F)) {
184  assert((ArgIdx == 0 || ArgIdx == 1) &&
185  "Invalid argument index for memset_pattern16");
186  if (ArgIdx == 1)
187  return MemoryLocation(Arg, 16, AATags);
188  if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
189  return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
190  }
191  // FIXME: Handle memset_pattern4 and memset_pattern8 also.
192 
193  return MemoryLocation(CS.getArgument(ArgIdx), LocationSize::unknown(),
194  AATags);
195 }
uint64_t CallInst * C
Value * getValueOperand()
Definition: Instructions.h:399
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Compute iterated dominance frontiers using a linear time algorithm.
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:518
static constexpr LocationSize unknown()
bool isPrecise() const
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:681
Value * getLength() const
static constexpr LocationSize mapTombstone()
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:364
InstrTy * getInstruction() const
Definition: CallSite.h:92
Value * getPointerOperand()
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
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:310
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:274
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:789
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:186
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:785
Establish a view to a call site for examination.
Definition: CallSite.h:714
uint32_t Size
Definition: Profile.cpp:47
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:107
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:411
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:402
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.