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 MemoryLocation
MemoryLocation::get(const LoadInst
*LI
) {
23 LI
->getAAMetadata(AATags
);
24 const auto &DL
= LI
->getModule()->getDataLayout();
26 return MemoryLocation(LI
->getPointerOperand(),
27 DL
.getTypeStoreSize(LI
->getType()), AATags
);
30 MemoryLocation
MemoryLocation::get(const StoreInst
*SI
) {
32 SI
->getAAMetadata(AATags
);
33 const auto &DL
= SI
->getModule()->getDataLayout();
35 return MemoryLocation(SI
->getPointerOperand(),
36 DL
.getTypeStoreSize(SI
->getValueOperand()->getType()),
40 MemoryLocation
MemoryLocation::get(const VAArgInst
*VI
) {
42 VI
->getAAMetadata(AATags
);
44 return MemoryLocation(VI
->getPointerOperand(), UnknownSize
, AATags
);
47 MemoryLocation
MemoryLocation::get(const AtomicCmpXchgInst
*CXI
) {
49 CXI
->getAAMetadata(AATags
);
50 const auto &DL
= CXI
->getModule()->getDataLayout();
52 return MemoryLocation(
53 CXI
->getPointerOperand(),
54 DL
.getTypeStoreSize(CXI
->getCompareOperand()->getType()), AATags
);
57 MemoryLocation
MemoryLocation::get(const AtomicRMWInst
*RMWI
) {
59 RMWI
->getAAMetadata(AATags
);
60 const auto &DL
= RMWI
->getModule()->getDataLayout();
62 return MemoryLocation(RMWI
->getPointerOperand(),
63 DL
.getTypeStoreSize(RMWI
->getValOperand()->getType()),
67 MemoryLocation
MemoryLocation::getForSource(const MemTransferInst
*MTI
) {
68 uint64_t Size
= UnknownSize
;
69 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MTI
->getLength()))
70 Size
= C
->getValue().getZExtValue();
72 // memcpy/memmove can have AA tags. For memcpy, they apply
73 // to both the source and the destination.
75 MTI
->getAAMetadata(AATags
);
77 return MemoryLocation(MTI
->getRawSource(), Size
, AATags
);
80 MemoryLocation
MemoryLocation::getForDest(const MemIntrinsic
*MTI
) {
81 uint64_t Size
= UnknownSize
;
82 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MTI
->getLength()))
83 Size
= C
->getValue().getZExtValue();
85 // memcpy/memmove can have AA tags. For memcpy, they apply
86 // to both the source and the destination.
88 MTI
->getAAMetadata(AATags
);
90 return MemoryLocation(MTI
->getRawDest(), Size
, AATags
);
93 MemoryLocation
MemoryLocation::getForArgument(ImmutableCallSite CS
,
95 const TargetLibraryInfo
&TLI
) {
97 CS
->getAAMetadata(AATags
);
98 const Value
*Arg
= CS
.getArgument(ArgIdx
);
100 // We may be able to produce an exact size for known intrinsics.
101 if (const IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(CS
.getInstruction())) {
102 const DataLayout
&DL
= II
->getModule()->getDataLayout();
104 switch (II
->getIntrinsicID()) {
107 case Intrinsic::memset
:
108 case Intrinsic::memcpy
:
109 case Intrinsic::memmove
:
110 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
111 "Invalid argument index for memory intrinsic");
112 if (ConstantInt
*LenCI
= dyn_cast
<ConstantInt
>(II
->getArgOperand(2)))
113 return MemoryLocation(Arg
, LenCI
->getZExtValue(), AATags
);
116 case Intrinsic::lifetime_start
:
117 case Intrinsic::lifetime_end
:
118 case Intrinsic::invariant_start
:
119 assert(ArgIdx
== 1 && "Invalid argument index");
120 return MemoryLocation(
121 Arg
, cast
<ConstantInt
>(II
->getArgOperand(0))->getZExtValue(), AATags
);
123 case Intrinsic::invariant_end
:
124 assert(ArgIdx
== 2 && "Invalid argument index");
125 return MemoryLocation(
126 Arg
, cast
<ConstantInt
>(II
->getArgOperand(1))->getZExtValue(), AATags
);
128 case Intrinsic::arm_neon_vld1
:
129 assert(ArgIdx
== 0 && "Invalid argument index");
130 // LLVM's vld1 and vst1 intrinsics currently only support a single
132 return MemoryLocation(Arg
, DL
.getTypeStoreSize(II
->getType()), AATags
);
134 case Intrinsic::arm_neon_vst1
:
135 assert(ArgIdx
== 0 && "Invalid argument index");
136 return MemoryLocation(
137 Arg
, DL
.getTypeStoreSize(II
->getArgOperand(1)->getType()), AATags
);
141 // We can bound the aliasing properties of memset_pattern16 just as we can
142 // for memcpy/memset. This is particularly important because the
143 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
144 // whenever possible.
146 if (CS
.getCalledFunction() && TLI
.getLibFunc(*CS
.getCalledFunction(), F
) &&
147 F
== LibFunc_memset_pattern16
&& TLI
.has(F
)) {
148 assert((ArgIdx
== 0 || ArgIdx
== 1) &&
149 "Invalid argument index for memset_pattern16");
151 return MemoryLocation(Arg
, 16, AATags
);
152 if (const ConstantInt
*LenCI
= dyn_cast
<ConstantInt
>(CS
.getArgument(2)))
153 return MemoryLocation(Arg
, LenCI
->getZExtValue(), AATags
);
155 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
157 return MemoryLocation(CS
.getArgument(ArgIdx
), UnknownSize
, AATags
);