blob: 3cd4b4475ef6fb2861e358c6413f39d234e99f02 [file] [log] [blame]
Chandler Carruth70c61c12015-06-04 02:03:15 +00001//===- 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
10#include "llvm/Analysis/MemoryLocation.h"
Chandler Carruthc41404a2015-06-17 07:12:40 +000011#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth70c61c12015-06-04 02:03:15 +000012#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"
19using namespace llvm;
20
George Burgess IVd98d5052018-10-10 01:35:22 +000021void LocationSize::print(raw_ostream &OS) const {
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";
George Burgess IV40dc63e2018-10-10 06:39:40 +000029 else if (isPrecise())
George Burgess IVd98d5052018-10-10 01:35:22 +000030 OS << "precise(" << getValue() << ')';
George Burgess IV40dc63e2018-10-10 06:39:40 +000031 else
32 OS << "upperBound(" << getValue() << ')';
George Burgess IVd98d5052018-10-10 01:35:22 +000033}
34
Chandler Carruth70c61c12015-06-04 02:03:15 +000035MemoryLocation MemoryLocation::get(const LoadInst *LI) {
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
44MemoryLocation MemoryLocation::get(const StoreInst *SI) {
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
54MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
55 AAMDNodes AATags;
56 VI->getAAMetadata(AATags);
57
58 return MemoryLocation(VI->getPointerOperand(), UnknownSize, AATags);
59}
60
61MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
62 AAMDNodes AATags;
63 CXI->getAAMetadata(AATags);
64 const auto &DL = CXI->getModule()->getDataLayout();
65
66 return MemoryLocation(
67 CXI->getPointerOperand(),
68 DL.getTypeStoreSize(CXI->getCompareOperand()->getType()), AATags);
69}
70
71MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
72 AAMDNodes AATags;
73 RMWI->getAAMetadata(AATags);
74 const auto &DL = RMWI->getModule()->getDataLayout();
75
76 return MemoryLocation(RMWI->getPointerOperand(),
77 DL.getTypeStoreSize(RMWI->getValOperand()->getType()),
78 AATags);
79}
80
81MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
Daniel Neilsoncc45e922018-04-23 19:06:49 +000082 return getForSource(cast<AnyMemTransferInst>(MTI));
83}
84
85MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
86 return getForSource(cast<AnyMemTransferInst>(MTI));
87}
88
89MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
Chandler Carruth70c61c12015-06-04 02:03:15 +000090 uint64_t Size = UnknownSize;
91 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
92 Size = C->getValue().getZExtValue();
93
94 // memcpy/memmove can have AA tags. For memcpy, they apply
95 // to both the source and the destination.
96 AAMDNodes AATags;
97 MTI->getAAMetadata(AATags);
98
99 return MemoryLocation(MTI->getRawSource(), Size, AATags);
100}
101
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000102MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
103 return getForDest(cast<AnyMemIntrinsic>(MI));
104}
105
106MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
107 return getForDest(cast<AnyMemIntrinsic>(MI));
108}
109
110MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000111 uint64_t Size = UnknownSize;
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000112 if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
Chandler Carruth70c61c12015-06-04 02:03:15 +0000113 Size = C->getValue().getZExtValue();
114
115 // memcpy/memmove can have AA tags. For memcpy, they apply
116 // to both the source and the destination.
117 AAMDNodes AATags;
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000118 MI->getAAMetadata(AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000119
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000120 return MemoryLocation(MI->getRawDest(), Size, AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000121}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000122
Chandler Carruthc41404a2015-06-17 07:12:40 +0000123MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS,
124 unsigned ArgIdx,
Philip Reamescb8b3272018-09-07 21:36:11 +0000125 const TargetLibraryInfo *TLI) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000126 AAMDNodes AATags;
127 CS->getAAMetadata(AATags);
128 const Value *Arg = CS.getArgument(ArgIdx);
129
130 // We may be able to produce an exact size for known intrinsics.
131 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
132 const DataLayout &DL = II->getModule()->getDataLayout();
133
134 switch (II->getIntrinsicID()) {
135 default:
136 break;
137 case Intrinsic::memset:
138 case Intrinsic::memcpy:
139 case Intrinsic::memmove:
140 assert((ArgIdx == 0 || ArgIdx == 1) &&
141 "Invalid argument index for memory intrinsic");
142 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
143 return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
144 break;
145
146 case Intrinsic::lifetime_start:
147 case Intrinsic::lifetime_end:
148 case Intrinsic::invariant_start:
149 assert(ArgIdx == 1 && "Invalid argument index");
150 return MemoryLocation(
151 Arg, cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AATags);
152
153 case Intrinsic::invariant_end:
Philip Reames684fa572018-08-16 20:48:55 +0000154 // The first argument to an invariant.end is a "descriptor" type (e.g. a
155 // pointer to a empty struct) which is never actually dereferenced.
156 if (ArgIdx == 0)
157 return MemoryLocation(Arg, 0, AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000158 assert(ArgIdx == 2 && "Invalid argument index");
159 return MemoryLocation(
160 Arg, cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AATags);
161
162 case Intrinsic::arm_neon_vld1:
163 assert(ArgIdx == 0 && "Invalid argument index");
164 // LLVM's vld1 and vst1 intrinsics currently only support a single
165 // vector register.
166 return MemoryLocation(Arg, DL.getTypeStoreSize(II->getType()), AATags);
167
168 case Intrinsic::arm_neon_vst1:
169 assert(ArgIdx == 0 && "Invalid argument index");
170 return MemoryLocation(
171 Arg, DL.getTypeStoreSize(II->getArgOperand(1)->getType()), AATags);
172 }
173 }
174
175 // We can bound the aliasing properties of memset_pattern16 just as we can
176 // for memcpy/memset. This is particularly important because the
177 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
178 // whenever possible.
David L. Jonesd21529f2017-01-23 23:16:46 +0000179 LibFunc F;
Philip Reamescb8b3272018-09-07 21:36:11 +0000180 if (TLI && CS.getCalledFunction() &&
181 TLI->getLibFunc(*CS.getCalledFunction(), F) &&
182 F == LibFunc_memset_pattern16 && TLI->has(F)) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000183 assert((ArgIdx == 0 || ArgIdx == 1) &&
184 "Invalid argument index for memset_pattern16");
185 if (ArgIdx == 1)
186 return MemoryLocation(Arg, 16, AATags);
187 if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
188 return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
189 }
190 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
191
192 return MemoryLocation(CS.getArgument(ArgIdx), UnknownSize, AATags);
193}