blob: 163830eee797e49cbcd37e13246df83a8f4ef824 [file] [log] [blame]
Chandler Carruth70c61c12015-06-04 02:03:15 +00001//===- MemoryLocation.cpp - Memory location descriptions -------------------==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chandler Carruth70c61c12015-06-04 02:03:15 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Analysis/MemoryLocation.h"
Chandler Carruthc41404a2015-06-17 07:12:40 +000010#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth70c61c12015-06-04 02:03:15 +000011#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/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
18using namespace llvm;
19
George Burgess IVd98d5052018-10-10 01:35:22 +000020void LocationSize::print(raw_ostream &OS) const {
21 OS << "LocationSize::";
22 if (*this == unknown())
23 OS << "unknown";
24 else if (*this == mapEmpty())
25 OS << "mapEmpty";
26 else if (*this == mapTombstone())
27 OS << "mapTombstone";
George Burgess IV40dc63e2018-10-10 06:39:40 +000028 else if (isPrecise())
George Burgess IVd98d5052018-10-10 01:35:22 +000029 OS << "precise(" << getValue() << ')';
George Burgess IV40dc63e2018-10-10 06:39:40 +000030 else
31 OS << "upperBound(" << getValue() << ')';
George Burgess IVd98d5052018-10-10 01:35:22 +000032}
33
Chandler Carruth70c61c12015-06-04 02:03:15 +000034MemoryLocation MemoryLocation::get(const LoadInst *LI) {
35 AAMDNodes AATags;
36 LI->getAAMetadata(AATags);
37 const auto &DL = LI->getModule()->getDataLayout();
38
George Burgess IV69952972018-12-23 03:36:44 +000039 return MemoryLocation(
40 LI->getPointerOperand(),
41 LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +000042}
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(),
George Burgess IV69952972018-12-23 03:36:44 +000050 LocationSize::precise(DL.getTypeStoreSize(
51 SI->getValueOperand()->getType())),
Chandler Carruth70c61c12015-06-04 02:03:15 +000052 AATags);
53}
54
55MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
56 AAMDNodes AATags;
57 VI->getAAMetadata(AATags);
58
George Burgess IV6ef80022018-10-10 21:28:44 +000059 return MemoryLocation(VI->getPointerOperand(), LocationSize::unknown(),
60 AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +000061}
62
63MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
64 AAMDNodes AATags;
65 CXI->getAAMetadata(AATags);
66 const auto &DL = CXI->getModule()->getDataLayout();
67
George Burgess IV69952972018-12-23 03:36:44 +000068 return MemoryLocation(CXI->getPointerOperand(),
69 LocationSize::precise(DL.getTypeStoreSize(
70 CXI->getCompareOperand()->getType())),
71 AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +000072}
73
74MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
75 AAMDNodes AATags;
76 RMWI->getAAMetadata(AATags);
77 const auto &DL = RMWI->getModule()->getDataLayout();
78
79 return MemoryLocation(RMWI->getPointerOperand(),
George Burgess IV69952972018-12-23 03:36:44 +000080 LocationSize::precise(DL.getTypeStoreSize(
81 RMWI->getValOperand()->getType())),
Chandler Carruth70c61c12015-06-04 02:03:15 +000082 AATags);
83}
84
85MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
Daniel Neilsoncc45e922018-04-23 19:06:49 +000086 return getForSource(cast<AnyMemTransferInst>(MTI));
87}
88
89MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
90 return getForSource(cast<AnyMemTransferInst>(MTI));
91}
92
93MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
George Burgess IV69952972018-12-23 03:36:44 +000094 auto Size = LocationSize::unknown();
Chandler Carruth70c61c12015-06-04 02:03:15 +000095 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
George Burgess IV69952972018-12-23 03:36:44 +000096 Size = LocationSize::precise(C->getValue().getZExtValue());
Chandler Carruth70c61c12015-06-04 02:03:15 +000097
98 // memcpy/memmove can have AA tags. For memcpy, they apply
99 // to both the source and the destination.
100 AAMDNodes AATags;
101 MTI->getAAMetadata(AATags);
102
103 return MemoryLocation(MTI->getRawSource(), Size, AATags);
104}
105
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000106MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
107 return getForDest(cast<AnyMemIntrinsic>(MI));
108}
109
110MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
111 return getForDest(cast<AnyMemIntrinsic>(MI));
112}
113
114MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
George Burgess IV69952972018-12-23 03:36:44 +0000115 auto Size = LocationSize::unknown();
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000116 if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
George Burgess IV69952972018-12-23 03:36:44 +0000117 Size = LocationSize::precise(C->getValue().getZExtValue());
Chandler Carruth70c61c12015-06-04 02:03:15 +0000118
119 // memcpy/memmove can have AA tags. For memcpy, they apply
120 // to both the source and the destination.
121 AAMDNodes AATags;
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000122 MI->getAAMetadata(AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000123
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000124 return MemoryLocation(MI->getRawDest(), Size, AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000125}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000126
Chandler Carruth363ac682019-01-07 05:42:51 +0000127MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
Chandler Carruthc41404a2015-06-17 07:12:40 +0000128 unsigned ArgIdx,
Philip Reamescb8b3272018-09-07 21:36:11 +0000129 const TargetLibraryInfo *TLI) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000130 AAMDNodes AATags;
Chandler Carruth363ac682019-01-07 05:42:51 +0000131 Call->getAAMetadata(AATags);
132 const Value *Arg = Call->getArgOperand(ArgIdx);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000133
134 // We may be able to produce an exact size for known intrinsics.
Chandler Carruth363ac682019-01-07 05:42:51 +0000135 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000136 const DataLayout &DL = II->getModule()->getDataLayout();
137
138 switch (II->getIntrinsicID()) {
139 default:
140 break;
141 case Intrinsic::memset:
142 case Intrinsic::memcpy:
143 case Intrinsic::memmove:
144 assert((ArgIdx == 0 || ArgIdx == 1) &&
145 "Invalid argument index for memory intrinsic");
146 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
George Burgess IV69952972018-12-23 03:36:44 +0000147 return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
148 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000149 break;
150
151 case Intrinsic::lifetime_start:
152 case Intrinsic::lifetime_end:
153 case Intrinsic::invariant_start:
154 assert(ArgIdx == 1 && "Invalid argument index");
155 return MemoryLocation(
George Burgess IV69952972018-12-23 03:36:44 +0000156 Arg,
157 LocationSize::precise(
158 cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
159 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000160
161 case Intrinsic::invariant_end:
Philip Reames684fa572018-08-16 20:48:55 +0000162 // The first argument to an invariant.end is a "descriptor" type (e.g. a
163 // pointer to a empty struct) which is never actually dereferenced.
164 if (ArgIdx == 0)
George Burgess IV69952972018-12-23 03:36:44 +0000165 return MemoryLocation(Arg, LocationSize::precise(0), AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000166 assert(ArgIdx == 2 && "Invalid argument index");
167 return MemoryLocation(
George Burgess IV69952972018-12-23 03:36:44 +0000168 Arg,
169 LocationSize::precise(
170 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
171 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000172
173 case Intrinsic::arm_neon_vld1:
174 assert(ArgIdx == 0 && "Invalid argument index");
175 // LLVM's vld1 and vst1 intrinsics currently only support a single
176 // vector register.
George Burgess IV69952972018-12-23 03:36:44 +0000177 return MemoryLocation(
178 Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
179 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000180
181 case Intrinsic::arm_neon_vst1:
182 assert(ArgIdx == 0 && "Invalid argument index");
George Burgess IV69952972018-12-23 03:36:44 +0000183 return MemoryLocation(Arg,
184 LocationSize::precise(DL.getTypeStoreSize(
185 II->getArgOperand(1)->getType())),
186 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000187 }
188 }
189
190 // We can bound the aliasing properties of memset_pattern16 just as we can
191 // for memcpy/memset. This is particularly important because the
192 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
193 // whenever possible.
David L. Jonesd21529f2017-01-23 23:16:46 +0000194 LibFunc F;
Chandler Carruth363ac682019-01-07 05:42:51 +0000195 if (TLI && Call->getCalledFunction() &&
196 TLI->getLibFunc(*Call->getCalledFunction(), F) &&
Philip Reamescb8b3272018-09-07 21:36:11 +0000197 F == LibFunc_memset_pattern16 && TLI->has(F)) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000198 assert((ArgIdx == 0 || ArgIdx == 1) &&
199 "Invalid argument index for memset_pattern16");
200 if (ArgIdx == 1)
George Burgess IV69952972018-12-23 03:36:44 +0000201 return MemoryLocation(Arg, LocationSize::precise(16), AATags);
Chandler Carruth363ac682019-01-07 05:42:51 +0000202 if (const ConstantInt *LenCI =
203 dyn_cast<ConstantInt>(Call->getArgOperand(2)))
George Burgess IV69952972018-12-23 03:36:44 +0000204 return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
205 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000206 }
207 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
208
Chandler Carruth363ac682019-01-07 05:42:51 +0000209 return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),
George Burgess IV6ef80022018-10-10 21:28:44 +0000210 AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000211}