LLVM  15.0.0git
MemoryLocation.cpp
Go to the documentation of this file.
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 
11 #include "llvm/IR/DataLayout.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/IntrinsicInst.h"
14 #include "llvm/IR/IntrinsicsARM.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/Type.h"
17 using namespace llvm;
18 
20  OS << "LocationSize::";
21  if (*this == beforeOrAfterPointer())
22  OS << "beforeOrAfterPointer";
23  else if (*this == afterPointer())
24  OS << "afterPointer";
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 
36  const auto &DL = LI->getModule()->getDataLayout();
37 
38  return MemoryLocation(
39  LI->getPointerOperand(),
40  LocationSize::precise(DL.getTypeStoreSize(LI->getType())),
41  LI->getAAMetadata());
42 }
43 
45  const auto &DL = SI->getModule()->getDataLayout();
46 
47  return MemoryLocation(SI->getPointerOperand(),
48  LocationSize::precise(DL.getTypeStoreSize(
49  SI->getValueOperand()->getType())),
50  SI->getAAMetadata());
51 }
52 
54  return MemoryLocation(VI->getPointerOperand(),
55  LocationSize::afterPointer(), VI->getAAMetadata());
56 }
57 
59  const auto &DL = CXI->getModule()->getDataLayout();
60 
61  return MemoryLocation(CXI->getPointerOperand(),
62  LocationSize::precise(DL.getTypeStoreSize(
63  CXI->getCompareOperand()->getType())),
64  CXI->getAAMetadata());
65 }
66 
68  const auto &DL = RMWI->getModule()->getDataLayout();
69 
70  return MemoryLocation(RMWI->getPointerOperand(),
71  LocationSize::precise(DL.getTypeStoreSize(
72  RMWI->getValOperand()->getType())),
73  RMWI->getAAMetadata());
74 }
75 
77  switch (Inst->getOpcode()) {
78  case Instruction::Load:
79  return get(cast<LoadInst>(Inst));
80  case Instruction::Store:
81  return get(cast<StoreInst>(Inst));
82  case Instruction::VAArg:
83  return get(cast<VAArgInst>(Inst));
84  case Instruction::AtomicCmpXchg:
85  return get(cast<AtomicCmpXchgInst>(Inst));
86  case Instruction::AtomicRMW:
87  return get(cast<AtomicRMWInst>(Inst));
88  default:
89  return None;
90  }
91 }
92 
94  return getForSource(cast<AnyMemTransferInst>(MTI));
95 }
96 
98  return getForSource(cast<AnyMemTransferInst>(MTI));
99 }
100 
102  assert(MTI->getRawSource() == MTI->getArgOperand(1));
103  return getForArgument(MTI, 1, nullptr);
104 }
105 
107  return getForDest(cast<AnyMemIntrinsic>(MI));
108 }
109 
111  return getForDest(cast<AnyMemIntrinsic>(MI));
112 }
113 
115  assert(MI->getRawDest() == MI->getArgOperand(0));
116  return getForArgument(MI, 0, nullptr);
117 }
118 
121  if (!CB->onlyAccessesArgMemory())
122  return None;
123 
124  if (CB->hasOperandBundles())
125  // TODO: remove implementation restriction
126  return None;
127 
128  Value *UsedV = nullptr;
129  Optional<unsigned> UsedIdx;
130  for (unsigned i = 0; i < CB->arg_size(); i++) {
131  if (!CB->getArgOperand(i)->getType()->isPointerTy())
132  continue;
133  if (CB->onlyReadsMemory(i))
134  continue;
135  if (!UsedV) {
136  // First potentially writing parameter
137  UsedV = CB->getArgOperand(i);
138  UsedIdx = i;
139  continue;
140  }
141  UsedIdx = None;
142  if (UsedV != CB->getArgOperand(i))
143  // Can't describe writing to two distinct locations.
144  // TODO: This results in an inprecision when two values derived from the
145  // same object are passed as arguments to the same function.
146  return None;
147  }
148  if (!UsedV)
149  // We don't currently have a way to represent a "does not write" result
150  // and thus have to be conservative and return unknown.
151  return None;
152 
153  if (UsedIdx)
154  return getForArgument(CB, *UsedIdx, &TLI);
155  return MemoryLocation::getBeforeOrAfter(UsedV, CB->getAAMetadata());
156 }
157 
159  unsigned ArgIdx,
160  const TargetLibraryInfo *TLI) {
161  AAMDNodes AATags = Call->getAAMetadata();
162  const Value *Arg = Call->getArgOperand(ArgIdx);
163 
164  // We may be able to produce an exact size for known intrinsics.
165  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
166  const DataLayout &DL = II->getModule()->getDataLayout();
167 
168  switch (II->getIntrinsicID()) {
169  default:
170  break;
171  case Intrinsic::memset:
172  case Intrinsic::memcpy:
173  case Intrinsic::memcpy_inline:
174  case Intrinsic::memmove:
175  case Intrinsic::memcpy_element_unordered_atomic:
176  case Intrinsic::memmove_element_unordered_atomic:
177  case Intrinsic::memset_element_unordered_atomic:
178  assert((ArgIdx == 0 || ArgIdx == 1) &&
179  "Invalid argument index for memory intrinsic");
180  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
181  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
182  AATags);
184 
185  case Intrinsic::lifetime_start:
186  case Intrinsic::lifetime_end:
187  case Intrinsic::invariant_start:
188  assert(ArgIdx == 1 && "Invalid argument index");
189  return MemoryLocation(
190  Arg,
192  cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
193  AATags);
194 
195  case Intrinsic::masked_load:
196  assert(ArgIdx == 0 && "Invalid argument index");
197  return MemoryLocation(
198  Arg,
199  LocationSize::upperBound(DL.getTypeStoreSize(II->getType())),
200  AATags);
201 
202  case Intrinsic::masked_store:
203  assert(ArgIdx == 1 && "Invalid argument index");
204  return MemoryLocation(
205  Arg,
207  DL.getTypeStoreSize(II->getArgOperand(0)->getType())),
208  AATags);
209 
210  case Intrinsic::invariant_end:
211  // The first argument to an invariant.end is a "descriptor" type (e.g. a
212  // pointer to a empty struct) which is never actually dereferenced.
213  if (ArgIdx == 0)
215  assert(ArgIdx == 2 && "Invalid argument index");
216  return MemoryLocation(
217  Arg,
219  cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
220  AATags);
221 
222  case Intrinsic::arm_neon_vld1:
223  assert(ArgIdx == 0 && "Invalid argument index");
224  // LLVM's vld1 and vst1 intrinsics currently only support a single
225  // vector register.
226  return MemoryLocation(
227  Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
228  AATags);
229 
230  case Intrinsic::arm_neon_vst1:
231  assert(ArgIdx == 0 && "Invalid argument index");
232  return MemoryLocation(Arg,
233  LocationSize::precise(DL.getTypeStoreSize(
234  II->getArgOperand(1)->getType())),
235  AATags);
236  }
237 
238  assert(
239  !isa<AnyMemTransferInst>(II) &&
240  "all memory transfer intrinsics should be handled by the switch above");
241  }
242 
243  // We can bound the aliasing properties of memset_pattern16 just as we can
244  // for memcpy/memset. This is particularly important because the
245  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
246  // whenever possible.
247  LibFunc F;
248  if (TLI && TLI->getLibFunc(*Call, F) && TLI->has(F)) {
249  switch (F) {
250  case LibFunc_strcpy:
251  case LibFunc_strcat:
252  case LibFunc_strncat:
253  assert((ArgIdx == 0 || ArgIdx == 1) && "Invalid argument index for str function");
255 
256  case LibFunc_memset_chk: {
257  assert(ArgIdx == 0 && "Invalid argument index for memset_chk");
259  if (const auto *Len = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
260  // memset_chk writes at most Len bytes. It may write less, if Len
261  // exceeds the specified max size and aborts.
262  Size = LocationSize::upperBound(Len->getZExtValue());
263  }
264  return MemoryLocation(Arg, Size, AATags);
265  }
266  case LibFunc_strncpy: {
267  assert((ArgIdx == 0 || ArgIdx == 1) &&
268  "Invalid argument index for strncpy");
270  if (const auto *Len = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
271  // strncpy is guaranteed to write Len bytes, but only reads up to Len
272  // bytes.
273  Size = ArgIdx == 0 ? LocationSize::precise(Len->getZExtValue())
274  : LocationSize::upperBound(Len->getZExtValue());
275  }
276  return MemoryLocation(Arg, Size, AATags);
277  }
278  case LibFunc_memset_pattern16:
279  case LibFunc_memset_pattern4:
280  case LibFunc_memset_pattern8:
281  assert((ArgIdx == 0 || ArgIdx == 1) &&
282  "Invalid argument index for memset_pattern16");
283  if (ArgIdx == 1) {
284  unsigned Size = 16;
285  if (F == LibFunc_memset_pattern4)
286  Size = 4;
287  else if (F == LibFunc_memset_pattern8)
288  Size = 8;
290  }
291  if (const ConstantInt *LenCI =
292  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
293  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
294  AATags);
296  case LibFunc_bcmp:
297  case LibFunc_memcmp:
298  assert((ArgIdx == 0 || ArgIdx == 1) &&
299  "Invalid argument index for memcmp/bcmp");
300  if (const ConstantInt *LenCI =
301  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
302  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
303  AATags);
305  case LibFunc_memchr:
306  assert((ArgIdx == 0) && "Invalid argument index for memchr");
307  if (const ConstantInt *LenCI =
308  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
309  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
310  AATags);
312  case LibFunc_memccpy:
313  assert((ArgIdx == 0 || ArgIdx == 1) &&
314  "Invalid argument index for memccpy");
315  // We only know an upper bound on the number of bytes read/written.
316  if (const ConstantInt *LenCI =
317  dyn_cast<ConstantInt>(Call->getArgOperand(3)))
318  return MemoryLocation(
319  Arg, LocationSize::upperBound(LenCI->getZExtValue()), AATags);
321  default:
322  break;
323  };
324  }
325 
326  return MemoryLocation::getBeforeOrAfter(Call->getArgOperand(ArgIdx), AATags);
327 }
i
i
Definition: README.txt:29
llvm::LocationSize::print
void print(raw_ostream &OS) const
Definition: MemoryLocation.cpp:19
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm::MemoryLocation::get
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Definition: MemoryLocation.cpp:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::Instruction::getModule
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:65
llvm::VAArgInst
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Definition: Instructions.h:1832
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::LocationSize::afterPointer
constexpr static LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
Definition: MemoryLocation.h:124
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:218
llvm::MemTransferInst
This class wraps the llvm.memcpy/memmove intrinsics.
Definition: IntrinsicInst.h:1005
llvm::LocationSize::upperBound
static LocationSize upperBound(uint64_t Value)
Definition: MemoryLocation.h:108
llvm::AAMDNodes
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:652
llvm::MemoryLocation::Size
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
Definition: MemoryLocation.h:227
Module.h
llvm::MemIntrinsic
This is the common base class for memset/memcpy/memmove.
Definition: IntrinsicInst.h:962
llvm::Optional
Definition: APInt.h:33
llvm::LoadInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:268
llvm::LocationSize::isPrecise
bool isPrecise() const
Definition: MemoryLocation.h:166
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::AtomicRMWInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:866
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::MemoryLocation::AATags
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
Definition: MemoryLocation.h:231
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::Instruction::getOpcode
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:157
llvm::LocationSize
Definition: MemoryLocation.h:66
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:35
llvm::AtomicMemTransferInst
Definition: IntrinsicInst.h:921
llvm::MemoryLocation::getForSource
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
Definition: MemoryLocation.cpp:93
llvm::MemTransferBase::getRawSource
Value * getRawSource() const
Return the arguments to the instruction.
Definition: IntrinsicInst.h:794
llvm::LocationSize::getValue
uint64_t getValue() const
Definition: MemoryLocation.h:159
TargetLibraryInfo.h
llvm::LocationSize::mapTombstone
constexpr static LocationSize mapTombstone()
Definition: MemoryLocation.h:135
llvm::TargetLibraryInfo::getLibFunc
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Definition: TargetLibraryInfo.h:294
llvm::Instruction
Definition: Instruction.h:42
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::LocationSize::precise
static LocationSize precise(uint64_t Value)
Definition: MemoryLocation.h:101
llvm::Instruction::getAAMetadata
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1402
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
llvm::None
const NoneType None
Definition: None.h:24
llvm::CallBase::onlyReadsMemory
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1720
Type.h
llvm::AnyMemTransferInst
Definition: IntrinsicInst.h:1117
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:305
VI
@ VI
Definition: SIInstrInfo.cpp:7842
llvm::MemoryLocation::getOrNone
static Optional< MemoryLocation > getOrNone(const Instruction *Inst)
Definition: MemoryLocation.cpp:76
MemoryLocation.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MemoryLocation::getForArgument
static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
Definition: MemoryLocation.cpp:158
llvm::CallBase::hasOperandBundles
bool hasOperandBundles() const
Return true if this User has any operand bundles.
Definition: InstrTypes.h:1945
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::AtomicCmpXchgInst::getCompareOperand
Value * getCompareOperand()
Definition: Instructions.h:656
llvm::TargetLibraryInfo::has
bool has(LibFunc F) const
Tests whether a library function is available.
Definition: TargetLibraryInfo.h:328
llvm::CallBase::onlyAccessesArgMemory
bool onlyAccessesArgMemory() const
Determine if the call can access memmory only using pointers based on its arguments.
Definition: InstrTypes.h:1869
DataLayout.h
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::LocationSize::mapEmpty
constexpr static LocationSize mapEmpty()
Definition: MemoryLocation.h:138
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:176
llvm::AtomicRMWInst
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:727
llvm::MemoryLocation::MemoryLocation
MemoryLocation()
Definition: MemoryLocation.h:288
llvm::LocationSize::beforeOrAfterPointer
constexpr static LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
Definition: MemoryLocation.h:130
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1339
llvm::AtomicCmpXchgInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:652
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:222
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:46
llvm::AtomicMemIntrinsic
Definition: IntrinsicInst.h:869
llvm::AtomicRMWInst::getValOperand
Value * getValOperand()
Definition: Instructions.h:870
Instructions.h
llvm::MemoryLocation::getAfter
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Definition: MemoryLocation.h:270
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1341
llvm::MemoryLocation::getBeforeOrAfter
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
Definition: MemoryLocation.h:278
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
llvm::MemoryLocation::getForDest
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Definition: MemoryLocation.cpp:106
llvm::AnyMemIntrinsic
Definition: IntrinsicInst.h:1067
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::AtomicCmpXchgInst
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:522
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:210