xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/MemoryLocation.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
19 using namespace llvm;
20 
21 void 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 if (isPrecise())
30     OS << "precise(" << getValue() << ')';
31   else
32     OS << "upperBound(" << getValue() << ')';
33 }
34 
35 MemoryLocation MemoryLocation::get(const LoadInst *LI) {
36   AAMDNodes AATags;
37   LI->getAAMetadata(AATags);
38   const auto &DL = LI->getModule()->getDataLayout();
39 
40   return MemoryLocation(
41       LI->getPointerOperand(),
42       LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
43 }
44 
45 MemoryLocation MemoryLocation::get(const StoreInst *SI) {
46   AAMDNodes AATags;
47   SI->getAAMetadata(AATags);
48   const auto &DL = SI->getModule()->getDataLayout();
49 
50   return MemoryLocation(SI->getPointerOperand(),
51                         LocationSize::precise(DL.getTypeStoreSize(
52                             SI->getValueOperand()->getType())),
53                         AATags);
54 }
55 
56 MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
57   AAMDNodes AATags;
58   VI->getAAMetadata(AATags);
59 
60   return MemoryLocation(VI->getPointerOperand(), LocationSize::unknown(),
61                         AATags);
62 }
63 
64 MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
65   AAMDNodes AATags;
66   CXI->getAAMetadata(AATags);
67   const auto &DL = CXI->getModule()->getDataLayout();
68 
69   return MemoryLocation(CXI->getPointerOperand(),
70                         LocationSize::precise(DL.getTypeStoreSize(
71                             CXI->getCompareOperand()->getType())),
72                         AATags);
73 }
74 
75 MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
76   AAMDNodes AATags;
77   RMWI->getAAMetadata(AATags);
78   const auto &DL = RMWI->getModule()->getDataLayout();
79 
80   return MemoryLocation(RMWI->getPointerOperand(),
81                         LocationSize::precise(DL.getTypeStoreSize(
82                             RMWI->getValOperand()->getType())),
83                         AATags);
84 }
85 
86 Optional<MemoryLocation> MemoryLocation::getOrNone(const Instruction *Inst) {
87   switch (Inst->getOpcode()) {
88   case Instruction::Load:
89     return get(cast<LoadInst>(Inst));
90   case Instruction::Store:
91     return get(cast<StoreInst>(Inst));
92   case Instruction::VAArg:
93     return get(cast<VAArgInst>(Inst));
94   case Instruction::AtomicCmpXchg:
95     return get(cast<AtomicCmpXchgInst>(Inst));
96   case Instruction::AtomicRMW:
97     return get(cast<AtomicRMWInst>(Inst));
98   default:
99     return None;
100   }
101 }
102 
103 MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
104   return getForSource(cast<AnyMemTransferInst>(MTI));
105 }
106 
107 MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
108   return getForSource(cast<AnyMemTransferInst>(MTI));
109 }
110 
111 MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
112   auto Size = LocationSize::unknown();
113   if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
114     Size = LocationSize::precise(C->getValue().getZExtValue());
115 
116   // memcpy/memmove can have AA tags. For memcpy, they apply
117   // to both the source and the destination.
118   AAMDNodes AATags;
119   MTI->getAAMetadata(AATags);
120 
121   return MemoryLocation(MTI->getRawSource(), Size, AATags);
122 }
123 
124 MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
125   return getForDest(cast<AnyMemIntrinsic>(MI));
126 }
127 
128 MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
129   return getForDest(cast<AnyMemIntrinsic>(MI));
130 }
131 
132 MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
133   auto Size = LocationSize::unknown();
134   if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
135     Size = LocationSize::precise(C->getValue().getZExtValue());
136 
137   // memcpy/memmove can have AA tags. For memcpy, they apply
138   // to both the source and the destination.
139   AAMDNodes AATags;
140   MI->getAAMetadata(AATags);
141 
142   return MemoryLocation(MI->getRawDest(), Size, AATags);
143 }
144 
145 MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
146                                               unsigned ArgIdx,
147                                               const TargetLibraryInfo *TLI) {
148   AAMDNodes AATags;
149   Call->getAAMetadata(AATags);
150   const Value *Arg = Call->getArgOperand(ArgIdx);
151 
152   // We may be able to produce an exact size for known intrinsics.
153   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
154     const DataLayout &DL = II->getModule()->getDataLayout();
155 
156     switch (II->getIntrinsicID()) {
157     default:
158       break;
159     case Intrinsic::memset:
160     case Intrinsic::memcpy:
161     case Intrinsic::memmove:
162       assert((ArgIdx == 0 || ArgIdx == 1) &&
163              "Invalid argument index for memory intrinsic");
164       if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
165         return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
166                               AATags);
167       break;
168 
169     case Intrinsic::lifetime_start:
170     case Intrinsic::lifetime_end:
171     case Intrinsic::invariant_start:
172       assert(ArgIdx == 1 && "Invalid argument index");
173       return MemoryLocation(
174           Arg,
175           LocationSize::precise(
176               cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
177           AATags);
178 
179     case Intrinsic::invariant_end:
180       // The first argument to an invariant.end is a "descriptor" type (e.g. a
181       // pointer to a empty struct) which is never actually dereferenced.
182       if (ArgIdx == 0)
183         return MemoryLocation(Arg, LocationSize::precise(0), AATags);
184       assert(ArgIdx == 2 && "Invalid argument index");
185       return MemoryLocation(
186           Arg,
187           LocationSize::precise(
188               cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
189           AATags);
190 
191     case Intrinsic::arm_neon_vld1:
192       assert(ArgIdx == 0 && "Invalid argument index");
193       // LLVM's vld1 and vst1 intrinsics currently only support a single
194       // vector register.
195       return MemoryLocation(
196           Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
197           AATags);
198 
199     case Intrinsic::arm_neon_vst1:
200       assert(ArgIdx == 0 && "Invalid argument index");
201       return MemoryLocation(Arg,
202                             LocationSize::precise(DL.getTypeStoreSize(
203                                 II->getArgOperand(1)->getType())),
204                             AATags);
205     }
206   }
207 
208   // We can bound the aliasing properties of memset_pattern16 just as we can
209   // for memcpy/memset.  This is particularly important because the
210   // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
211   // whenever possible.
212   LibFunc F;
213   if (TLI && Call->getCalledFunction() &&
214       TLI->getLibFunc(*Call->getCalledFunction(), F) &&
215       F == LibFunc_memset_pattern16 && TLI->has(F)) {
216     assert((ArgIdx == 0 || ArgIdx == 1) &&
217            "Invalid argument index for memset_pattern16");
218     if (ArgIdx == 1)
219       return MemoryLocation(Arg, LocationSize::precise(16), AATags);
220     if (const ConstantInt *LenCI =
221             dyn_cast<ConstantInt>(Call->getArgOperand(2)))
222       return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
223                             AATags);
224   }
225   // FIXME: Handle memset_pattern4 and memset_pattern8 also.
226 
227   return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),
228                         AATags);
229 }
230