1 //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
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 #include "llvm/Analysis/MemoryLocation.h"
11 #include "llvm/Analysis/TargetLibraryInfo.h"
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"
21 void LocationSize::print(raw_ostream &OS) const {
22 OS << "LocationSize::";
23 if (*this == unknown())
25 else if (*this == mapEmpty())
27 else if (*this == mapTombstone())
30 OS << "precise(" << getValue() << ')';
32 OS << "upperBound(" << getValue() << ')';
35 MemoryLocation MemoryLocation::get(const LoadInst *LI) {
37 LI->getAAMetadata(AATags);
38 const auto &DL = LI->getModule()->getDataLayout();
40 return MemoryLocation(
41 LI->getPointerOperand(),
42 LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
45 MemoryLocation MemoryLocation::get(const StoreInst *SI) {
47 SI->getAAMetadata(AATags);
48 const auto &DL = SI->getModule()->getDataLayout();
50 return MemoryLocation(SI->getPointerOperand(),
51 LocationSize::precise(DL.getTypeStoreSize(
52 SI->getValueOperand()->getType())),
56 MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
58 VI->getAAMetadata(AATags);
60 return MemoryLocation(VI->getPointerOperand(), LocationSize::unknown(),
64 MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
66 CXI->getAAMetadata(AATags);
67 const auto &DL = CXI->getModule()->getDataLayout();
69 return MemoryLocation(CXI->getPointerOperand(),
70 LocationSize::precise(DL.getTypeStoreSize(
71 CXI->getCompareOperand()->getType())),
75 MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
77 RMWI->getAAMetadata(AATags);
78 const auto &DL = RMWI->getModule()->getDataLayout();
80 return MemoryLocation(RMWI->getPointerOperand(),
81 LocationSize::precise(DL.getTypeStoreSize(
82 RMWI->getValOperand()->getType())),
86 MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
87 return getForSource(cast<AnyMemTransferInst>(MTI));
90 MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
91 return getForSource(cast<AnyMemTransferInst>(MTI));
94 MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
95 auto Size = LocationSize::unknown();
96 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
97 Size = LocationSize::precise(C->getValue().getZExtValue());
99 // memcpy/memmove can have AA tags. For memcpy, they apply
100 // to both the source and the destination.
102 MTI->getAAMetadata(AATags);
104 return MemoryLocation(MTI->getRawSource(), Size, AATags);
107 MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
108 return getForDest(cast<AnyMemIntrinsic>(MI));
111 MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
112 return getForDest(cast<AnyMemIntrinsic>(MI));
115 MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
116 auto Size = LocationSize::unknown();
117 if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
118 Size = LocationSize::precise(C->getValue().getZExtValue());
120 // memcpy/memmove can have AA tags. For memcpy, they apply
121 // to both the source and the destination.
123 MI->getAAMetadata(AATags);
125 return MemoryLocation(MI->getRawDest(), Size, AATags);
128 MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
130 const TargetLibraryInfo *TLI) {
132 Call->getAAMetadata(AATags);
133 const Value *Arg = Call->getArgOperand(ArgIdx);
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();
139 switch (II->getIntrinsicID()) {
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()),
152 case Intrinsic::lifetime_start:
153 case Intrinsic::lifetime_end:
154 case Intrinsic::invariant_start:
155 assert(ArgIdx == 1 && "Invalid argument index");
156 return MemoryLocation(
158 LocationSize::precise(
159 cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
162 case Intrinsic::invariant_end:
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.
166 return MemoryLocation(Arg, LocationSize::precise(0), AATags);
167 assert(ArgIdx == 2 && "Invalid argument index");
168 return MemoryLocation(
170 LocationSize::precise(
171 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
174 case Intrinsic::arm_neon_vld1:
175 assert(ArgIdx == 0 && "Invalid argument index");
176 // LLVM's vld1 and vst1 intrinsics currently only support a single
178 return MemoryLocation(
179 Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
182 case Intrinsic::arm_neon_vst1:
183 assert(ArgIdx == 0 && "Invalid argument index");
184 return MemoryLocation(Arg,
185 LocationSize::precise(DL.getTypeStoreSize(
186 II->getArgOperand(1)->getType())),
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.
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");
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()),
208 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
210 return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),