blob: df863d6f8d05fe9809bbcdc4f6bb3b57d706037f [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";
29 else
30 OS << "precise(" << getValue() << ')';
31}
32
Chandler Carruth70c61c12015-06-04 02:03:15 +000033MemoryLocation MemoryLocation::get(const LoadInst *LI) {
34 AAMDNodes AATags;
35 LI->getAAMetadata(AATags);
36 const auto &DL = LI->getModule()->getDataLayout();
37
38 return MemoryLocation(LI->getPointerOperand(),
39 DL.getTypeStoreSize(LI->getType()), AATags);
40}
41
42MemoryLocation MemoryLocation::get(const StoreInst *SI) {
43 AAMDNodes AATags;
44 SI->getAAMetadata(AATags);
45 const auto &DL = SI->getModule()->getDataLayout();
46
47 return MemoryLocation(SI->getPointerOperand(),
48 DL.getTypeStoreSize(SI->getValueOperand()->getType()),
49 AATags);
50}
51
52MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
53 AAMDNodes AATags;
54 VI->getAAMetadata(AATags);
55
56 return MemoryLocation(VI->getPointerOperand(), UnknownSize, AATags);
57}
58
59MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
60 AAMDNodes AATags;
61 CXI->getAAMetadata(AATags);
62 const auto &DL = CXI->getModule()->getDataLayout();
63
64 return MemoryLocation(
65 CXI->getPointerOperand(),
66 DL.getTypeStoreSize(CXI->getCompareOperand()->getType()), AATags);
67}
68
69MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
70 AAMDNodes AATags;
71 RMWI->getAAMetadata(AATags);
72 const auto &DL = RMWI->getModule()->getDataLayout();
73
74 return MemoryLocation(RMWI->getPointerOperand(),
75 DL.getTypeStoreSize(RMWI->getValOperand()->getType()),
76 AATags);
77}
78
79MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
Daniel Neilsoncc45e922018-04-23 19:06:49 +000080 return getForSource(cast<AnyMemTransferInst>(MTI));
81}
82
83MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
84 return getForSource(cast<AnyMemTransferInst>(MTI));
85}
86
87MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
Chandler Carruth70c61c12015-06-04 02:03:15 +000088 uint64_t Size = UnknownSize;
89 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
90 Size = C->getValue().getZExtValue();
91
92 // memcpy/memmove can have AA tags. For memcpy, they apply
93 // to both the source and the destination.
94 AAMDNodes AATags;
95 MTI->getAAMetadata(AATags);
96
97 return MemoryLocation(MTI->getRawSource(), Size, AATags);
98}
99
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000100MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
101 return getForDest(cast<AnyMemIntrinsic>(MI));
102}
103
104MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
105 return getForDest(cast<AnyMemIntrinsic>(MI));
106}
107
108MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000109 uint64_t Size = UnknownSize;
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000110 if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
Chandler Carruth70c61c12015-06-04 02:03:15 +0000111 Size = C->getValue().getZExtValue();
112
113 // memcpy/memmove can have AA tags. For memcpy, they apply
114 // to both the source and the destination.
115 AAMDNodes AATags;
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000116 MI->getAAMetadata(AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000117
Daniel Neilsoncc45e922018-04-23 19:06:49 +0000118 return MemoryLocation(MI->getRawDest(), Size, AATags);
Chandler Carruth70c61c12015-06-04 02:03:15 +0000119}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000120
Chandler Carruthc41404a2015-06-17 07:12:40 +0000121MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS,
122 unsigned ArgIdx,
Philip Reamescb8b3272018-09-07 21:36:11 +0000123 const TargetLibraryInfo *TLI) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000124 AAMDNodes AATags;
125 CS->getAAMetadata(AATags);
126 const Value *Arg = CS.getArgument(ArgIdx);
127
128 // We may be able to produce an exact size for known intrinsics.
129 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
130 const DataLayout &DL = II->getModule()->getDataLayout();
131
132 switch (II->getIntrinsicID()) {
133 default:
134 break;
135 case Intrinsic::memset:
136 case Intrinsic::memcpy:
137 case Intrinsic::memmove:
138 assert((ArgIdx == 0 || ArgIdx == 1) &&
139 "Invalid argument index for memory intrinsic");
140 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
141 return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
142 break;
143
144 case Intrinsic::lifetime_start:
145 case Intrinsic::lifetime_end:
146 case Intrinsic::invariant_start:
147 assert(ArgIdx == 1 && "Invalid argument index");
148 return MemoryLocation(
149 Arg, cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AATags);
150
151 case Intrinsic::invariant_end:
Philip Reames684fa572018-08-16 20:48:55 +0000152 // The first argument to an invariant.end is a "descriptor" type (e.g. a
153 // pointer to a empty struct) which is never actually dereferenced.
154 if (ArgIdx == 0)
155 return MemoryLocation(Arg, 0, AATags);
Chandler Carruthc41404a2015-06-17 07:12:40 +0000156 assert(ArgIdx == 2 && "Invalid argument index");
157 return MemoryLocation(
158 Arg, cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AATags);
159
160 case Intrinsic::arm_neon_vld1:
161 assert(ArgIdx == 0 && "Invalid argument index");
162 // LLVM's vld1 and vst1 intrinsics currently only support a single
163 // vector register.
164 return MemoryLocation(Arg, DL.getTypeStoreSize(II->getType()), AATags);
165
166 case Intrinsic::arm_neon_vst1:
167 assert(ArgIdx == 0 && "Invalid argument index");
168 return MemoryLocation(
169 Arg, DL.getTypeStoreSize(II->getArgOperand(1)->getType()), AATags);
170 }
171 }
172
173 // We can bound the aliasing properties of memset_pattern16 just as we can
174 // for memcpy/memset. This is particularly important because the
175 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
176 // whenever possible.
David L. Jonesd21529f2017-01-23 23:16:46 +0000177 LibFunc F;
Philip Reamescb8b3272018-09-07 21:36:11 +0000178 if (TLI && CS.getCalledFunction() &&
179 TLI->getLibFunc(*CS.getCalledFunction(), F) &&
180 F == LibFunc_memset_pattern16 && TLI->has(F)) {
Chandler Carruthc41404a2015-06-17 07:12:40 +0000181 assert((ArgIdx == 0 || ArgIdx == 1) &&
182 "Invalid argument index for memset_pattern16");
183 if (ArgIdx == 1)
184 return MemoryLocation(Arg, 16, AATags);
185 if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
186 return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
187 }
188 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
189
190 return MemoryLocation(CS.getArgument(ArgIdx), UnknownSize, AATags);
191}