1 //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/MemoryLocation.h"
10 #include "llvm/Analysis/TargetLibraryInfo.h"
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/IntrinsicsARM.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 == beforeOrAfterPointer())
24 OS
<< "beforeOrAfterPointer";
25 else if (*this == afterPointer())
27 else if (*this == mapEmpty())
29 else if (*this == mapTombstone())
32 OS
<< "precise(" << getValue() << ')';
34 OS
<< "upperBound(" << getValue() << ')';
37 MemoryLocation
MemoryLocation::get(const LoadInst
*LI
) {
39 LI
->getAAMetadata(AATags
);
40 const auto &DL
= LI
->getModule()->getDataLayout();
42 return MemoryLocation(
43 LI
->getPointerOperand(),
44 LocationSize::precise(DL
.getTypeStoreSize(LI
->getType())), AATags
);
47 MemoryLocation
MemoryLocation::get(const StoreInst
*SI
) {
49 SI
->getAAMetadata(AATags
);
50 const auto &DL
= SI
->getModule()->getDataLayout();
52 return MemoryLocation(SI
->getPointerOperand(),
53 LocationSize::precise(DL
.getTypeStoreSize(
54 SI
->getValueOperand()->getType())),
58 MemoryLocation
MemoryLocation::get(const VAArgInst
*VI
) {
60 VI
->getAAMetadata(AATags
);
62 return MemoryLocation(VI
->getPointerOperand(),
63 LocationSize::afterPointer(), AATags
);
66 MemoryLocation
MemoryLocation::get(const AtomicCmpXchgInst
*CXI
) {
68 CXI
->getAAMetadata(AATags
);
69 const auto &DL
= CXI
->getModule()->getDataLayout();
71 return MemoryLocation(CXI
->getPointerOperand(),
72 LocationSize::precise(DL
.getTypeStoreSize(
73 CXI
->getCompareOperand()->getType())),
77 MemoryLocation
MemoryLocation::get(const AtomicRMWInst
*RMWI
) {
79 RMWI
->getAAMetadata(AATags
);
80 const auto &DL
= RMWI
->getModule()->getDataLayout();
82 return MemoryLocation(RMWI
->getPointerOperand(),
83 LocationSize::precise(DL
.getTypeStoreSize(
84 RMWI
->getValOperand()->getType())),
88 Optional
<MemoryLocation
> MemoryLocation::getOrNone(const Instruction
*Inst
) {
89 switch (Inst
->getOpcode()) {
90 case Instruction::Load
:
91 return get(cast
<LoadInst
>(Inst
));
92 case Instruction::Store
:
93 return get(cast
<StoreInst
>(Inst
));
94 case Instruction::VAArg
:
95 return get(cast
<VAArgInst
>(Inst
));
96 case Instruction::AtomicCmpXchg
:
97 return get(cast
<AtomicCmpXchgInst
>(Inst
));
98 case Instruction::AtomicRMW
:
99 return get(cast
<AtomicRMWInst
>(Inst
));
105 MemoryLocation
MemoryLocation::getForSource(const MemTransferInst
*MTI
) {
106 return getForSource(cast
<AnyMemTransferInst
>(MTI
));
109 MemoryLocation
MemoryLocation::getForSource(const AtomicMemTransferInst
*MTI
) {
110 return getForSource(cast
<AnyMemTransferInst
>(MTI
));
113 MemoryLocation
MemoryLocation::getForSource(const AnyMemTransferInst
*MTI
) {
114 auto Size
= LocationSize::afterPointer();
115 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MTI
->getLength()))
116 Size
= LocationSize::precise(C
->getValue().getZExtValue());
118 // memcpy/memmove can have AA tags. For memcpy, they apply
119 // to both the source and the destination.
121 MTI
->getAAMetadata(AATags
);
123 return MemoryLocation(MTI
->getRawSource(), Size
, AATags
);
126 MemoryLocation
MemoryLocation::getForDest(const MemIntrinsic
*MI
) {
127 return getForDest(cast
<AnyMemIntrinsic
>(MI
));
130 MemoryLocation
MemoryLocation::getForDest(const AtomicMemIntrinsic
*MI
) {
131 return getForDest(cast
<AnyMemIntrinsic
>(MI
));
134 MemoryLocation
MemoryLocation::getForDest(const AnyMemIntrinsic
*MI
) {
135 auto Size
= LocationSize::afterPointer();
136 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MI
->getLength()))
137 Size
= LocationSize::precise(C
->getValue().getZExtValue());
139 // memcpy/memmove can have AA tags. For memcpy, they apply
140 // to both the source and the destination.
142 MI
->getAAMetadata(AATags
);
144 return MemoryLocation(MI
->getRawDest(), Size
, AATags
);
147 MemoryLocation
MemoryLocation::getForArgument(const CallBase
*Call
,
149 const TargetLibraryInfo
*TLI
) {
151 Call
->getAAMetadata(AATags
);
152 const Value
*Arg
= Call
->getArgOperand(ArgIdx
);
154 // We may be able to produce an exact size for known intrinsics.
155 if (const IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(Call
)) {
156 const DataLayout
&DL
= II
->getModule()->getDataLayout();
158 switch (II
->getIntrinsicID()) {
161 case Intrinsic::memset
:
162 case Intrinsic::memcpy
:
163 case Intrinsic::memcpy_inline
:
164 case Intrinsic::memmove
:
165 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
166 "Invalid argument index for memory intrinsic");
167 if (ConstantInt
*LenCI
= dyn_cast
<ConstantInt
>(II
->getArgOperand(2)))
168 return MemoryLocation(Arg
, LocationSize::precise(LenCI
->getZExtValue()),
170 return MemoryLocation::getAfter(Arg
, AATags
);
172 case Intrinsic::lifetime_start
:
173 case Intrinsic::lifetime_end
:
174 case Intrinsic::invariant_start
:
175 assert(ArgIdx
== 1 && "Invalid argument index");
176 return MemoryLocation(
178 LocationSize::precise(
179 cast
<ConstantInt
>(II
->getArgOperand(0))->getZExtValue()),
182 case Intrinsic::masked_load
:
183 assert(ArgIdx
== 0 && "Invalid argument index");
184 return MemoryLocation(
186 LocationSize::upperBound(DL
.getTypeStoreSize(II
->getType())),
189 case Intrinsic::masked_store
:
190 assert(ArgIdx
== 1 && "Invalid argument index");
191 return MemoryLocation(
193 LocationSize::upperBound(
194 DL
.getTypeStoreSize(II
->getArgOperand(0)->getType())),
197 case Intrinsic::invariant_end
:
198 // The first argument to an invariant.end is a "descriptor" type (e.g. a
199 // pointer to a empty struct) which is never actually dereferenced.
201 return MemoryLocation(Arg
, LocationSize::precise(0), AATags
);
202 assert(ArgIdx
== 2 && "Invalid argument index");
203 return MemoryLocation(
205 LocationSize::precise(
206 cast
<ConstantInt
>(II
->getArgOperand(1))->getZExtValue()),
209 case Intrinsic::arm_neon_vld1
:
210 assert(ArgIdx
== 0 && "Invalid argument index");
211 // LLVM's vld1 and vst1 intrinsics currently only support a single
213 return MemoryLocation(
214 Arg
, LocationSize::precise(DL
.getTypeStoreSize(II
->getType())),
217 case Intrinsic::arm_neon_vst1
:
218 assert(ArgIdx
== 0 && "Invalid argument index");
219 return MemoryLocation(Arg
,
220 LocationSize::precise(DL
.getTypeStoreSize(
221 II
->getArgOperand(1)->getType())),
226 // We can bound the aliasing properties of memset_pattern16 just as we can
227 // for memcpy/memset. This is particularly important because the
228 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
229 // whenever possible.
231 if (TLI
&& TLI
->getLibFunc(*Call
, F
) && TLI
->has(F
)) {
233 case LibFunc_memset_pattern16
:
234 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
235 "Invalid argument index for memset_pattern16");
237 return MemoryLocation(Arg
, LocationSize::precise(16), AATags
);
238 if (const ConstantInt
*LenCI
=
239 dyn_cast
<ConstantInt
>(Call
->getArgOperand(2)))
240 return MemoryLocation(Arg
, LocationSize::precise(LenCI
->getZExtValue()),
242 return MemoryLocation::getAfter(Arg
, AATags
);
245 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
246 "Invalid argument index for memcmp/bcmp");
247 if (const ConstantInt
*LenCI
=
248 dyn_cast
<ConstantInt
>(Call
->getArgOperand(2)))
249 return MemoryLocation(Arg
, LocationSize::precise(LenCI
->getZExtValue()),
251 return MemoryLocation::getAfter(Arg
, AATags
);
253 assert((ArgIdx
== 0) && "Invalid argument index for memchr");
254 if (const ConstantInt
*LenCI
=
255 dyn_cast
<ConstantInt
>(Call
->getArgOperand(2)))
256 return MemoryLocation(Arg
, LocationSize::precise(LenCI
->getZExtValue()),
258 return MemoryLocation::getAfter(Arg
, AATags
);
259 case LibFunc_memccpy
:
260 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
261 "Invalid argument index for memccpy");
262 // We only know an upper bound on the number of bytes read/written.
263 if (const ConstantInt
*LenCI
=
264 dyn_cast
<ConstantInt
>(Call
->getArgOperand(3)))
265 return MemoryLocation(
266 Arg
, LocationSize::upperBound(LenCI
->getZExtValue()), AATags
);
267 return MemoryLocation::getAfter(Arg
, AATags
);
272 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
274 return MemoryLocation::getBeforeOrAfter(Call
->getArgOperand(ArgIdx
), AATags
);