LLVM  14.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/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 
22  OS << "LocationSize::";
23  if (*this == beforeOrAfterPointer())
24  OS << "beforeOrAfterPointer";
25  else if (*this == afterPointer())
26  OS << "afterPointer";
27  else if (*this == mapEmpty())
28  OS << "mapEmpty";
29  else if (*this == mapTombstone())
30  OS << "mapTombstone";
31  else if (isPrecise())
32  OS << "precise(" << getValue() << ')';
33  else
34  OS << "upperBound(" << getValue() << ')';
35 }
36 
38  const auto &DL = LI->getModule()->getDataLayout();
39 
40  return MemoryLocation(
41  LI->getPointerOperand(),
42  LocationSize::precise(DL.getTypeStoreSize(LI->getType())),
43  LI->getAAMetadata());
44 }
45 
47  const auto &DL = SI->getModule()->getDataLayout();
48 
49  return MemoryLocation(SI->getPointerOperand(),
50  LocationSize::precise(DL.getTypeStoreSize(
51  SI->getValueOperand()->getType())),
52  SI->getAAMetadata());
53 }
54 
56  return MemoryLocation(VI->getPointerOperand(),
57  LocationSize::afterPointer(), VI->getAAMetadata());
58 }
59 
61  const auto &DL = CXI->getModule()->getDataLayout();
62 
63  return MemoryLocation(CXI->getPointerOperand(),
64  LocationSize::precise(DL.getTypeStoreSize(
65  CXI->getCompareOperand()->getType())),
66  CXI->getAAMetadata());
67 }
68 
70  const auto &DL = RMWI->getModule()->getDataLayout();
71 
72  return MemoryLocation(RMWI->getPointerOperand(),
73  LocationSize::precise(DL.getTypeStoreSize(
74  RMWI->getValOperand()->getType())),
75  RMWI->getAAMetadata());
76 }
77 
79  switch (Inst->getOpcode()) {
80  case Instruction::Load:
81  return get(cast<LoadInst>(Inst));
82  case Instruction::Store:
83  return get(cast<StoreInst>(Inst));
84  case Instruction::VAArg:
85  return get(cast<VAArgInst>(Inst));
86  case Instruction::AtomicCmpXchg:
87  return get(cast<AtomicCmpXchgInst>(Inst));
88  case Instruction::AtomicRMW:
89  return get(cast<AtomicRMWInst>(Inst));
90  default:
91  return None;
92  }
93 }
94 
96  return getForSource(cast<AnyMemTransferInst>(MTI));
97 }
98 
100  return getForSource(cast<AnyMemTransferInst>(MTI));
101 }
102 
105  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
106  Size = LocationSize::precise(C->getValue().getZExtValue());
107 
108  // memcpy/memmove can have AA tags. For memcpy, they apply
109  // to both the source and the destination.
110  return MemoryLocation(MTI->getRawSource(), Size, MTI->getAAMetadata());
111 }
112 
114  return getForDest(cast<AnyMemIntrinsic>(MI));
115 }
116 
118  return getForDest(cast<AnyMemIntrinsic>(MI));
119 }
120 
123  if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
124  Size = LocationSize::precise(C->getValue().getZExtValue());
125 
126  // memcpy/memmove can have AA tags. For memcpy, they apply
127  // to both the source and the destination.
128  return MemoryLocation(MI->getRawDest(), Size, MI->getAAMetadata());
129 }
130 
132  unsigned ArgIdx,
133  const TargetLibraryInfo *TLI) {
134  AAMDNodes AATags = Call->getAAMetadata();
135  const Value *Arg = Call->getArgOperand(ArgIdx);
136 
137  // We may be able to produce an exact size for known intrinsics.
138  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
139  const DataLayout &DL = II->getModule()->getDataLayout();
140 
141  switch (II->getIntrinsicID()) {
142  default:
143  break;
144  case Intrinsic::memset:
145  case Intrinsic::memcpy:
146  case Intrinsic::memcpy_inline:
147  case Intrinsic::memmove:
148  assert((ArgIdx == 0 || ArgIdx == 1) &&
149  "Invalid argument index for memory intrinsic");
150  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
151  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
152  AATags);
154 
155  case Intrinsic::lifetime_start:
156  case Intrinsic::lifetime_end:
157  case Intrinsic::invariant_start:
158  assert(ArgIdx == 1 && "Invalid argument index");
159  return MemoryLocation(
160  Arg,
162  cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
163  AATags);
164 
165  case Intrinsic::masked_load:
166  assert(ArgIdx == 0 && "Invalid argument index");
167  return MemoryLocation(
168  Arg,
169  LocationSize::upperBound(DL.getTypeStoreSize(II->getType())),
170  AATags);
171 
172  case Intrinsic::masked_store:
173  assert(ArgIdx == 1 && "Invalid argument index");
174  return MemoryLocation(
175  Arg,
177  DL.getTypeStoreSize(II->getArgOperand(0)->getType())),
178  AATags);
179 
180  case Intrinsic::invariant_end:
181  // The first argument to an invariant.end is a "descriptor" type (e.g. a
182  // pointer to a empty struct) which is never actually dereferenced.
183  if (ArgIdx == 0)
185  assert(ArgIdx == 2 && "Invalid argument index");
186  return MemoryLocation(
187  Arg,
189  cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
190  AATags);
191 
192  case Intrinsic::arm_neon_vld1:
193  assert(ArgIdx == 0 && "Invalid argument index");
194  // LLVM's vld1 and vst1 intrinsics currently only support a single
195  // vector register.
196  return MemoryLocation(
197  Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
198  AATags);
199 
200  case Intrinsic::arm_neon_vst1:
201  assert(ArgIdx == 0 && "Invalid argument index");
202  return MemoryLocation(Arg,
203  LocationSize::precise(DL.getTypeStoreSize(
204  II->getArgOperand(1)->getType())),
205  AATags);
206  }
207  }
208 
209  // We can bound the aliasing properties of memset_pattern16 just as we can
210  // for memcpy/memset. This is particularly important because the
211  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
212  // whenever possible.
213  LibFunc F;
214  if (TLI && TLI->getLibFunc(*Call, F) && TLI->has(F)) {
215  switch (F) {
216  case LibFunc_memset_pattern16:
217  assert((ArgIdx == 0 || ArgIdx == 1) &&
218  "Invalid argument index for memset_pattern16");
219  if (ArgIdx == 1)
221  if (const ConstantInt *LenCI =
222  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
223  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
224  AATags);
226  case LibFunc_bcmp:
227  case LibFunc_memcmp:
228  assert((ArgIdx == 0 || ArgIdx == 1) &&
229  "Invalid argument index for memcmp/bcmp");
230  if (const ConstantInt *LenCI =
231  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
232  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
233  AATags);
235  case LibFunc_memchr:
236  assert((ArgIdx == 0) && "Invalid argument index for memchr");
237  if (const ConstantInt *LenCI =
238  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
239  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
240  AATags);
242  case LibFunc_memccpy:
243  assert((ArgIdx == 0 || ArgIdx == 1) &&
244  "Invalid argument index for memccpy");
245  // We only know an upper bound on the number of bytes read/written.
246  if (const ConstantInt *LenCI =
247  dyn_cast<ConstantInt>(Call->getArgOperand(3)))
248  return MemoryLocation(
249  Arg, LocationSize::upperBound(LenCI->getZExtValue()), AATags);
251  default:
252  break;
253  };
254  }
255  // FIXME: Handle memset_pattern4 and memset_pattern8 also.
256 
257  return MemoryLocation::getBeforeOrAfter(Call->getArgOperand(ArgIdx), AATags);
258 }
llvm::LocationSize::print
void print(raw_ostream &OS) const
Definition: MemoryLocation.cpp:21
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
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:37
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
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:66
llvm::VAArgInst
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Definition: Instructions.h:1833
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:123
IntrinsicInst.h
llvm::MemTransferInst
This class wraps the llvm.memcpy/memmove intrinsics.
Definition: IntrinsicInst.h:917
llvm::LocationSize::upperBound
static LocationSize upperBound(uint64_t Value)
Definition: MemoryLocation.h:107
llvm::AAMDNodes
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:651
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
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:226
Module.h
llvm::MemIntrinsic
This is the common base class for memset/memcpy/memmove.
Definition: IntrinsicInst.h:874
llvm::Optional
Definition: APInt.h:33
llvm::LoadInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:267
llvm::LocationSize::isPrecise
bool isPrecise() const
Definition: MemoryLocation.h:165
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::AtomicRMWInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:865
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
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:230
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:160
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::AtomicMemTransferInst
Definition: IntrinsicInst.h:833
llvm::MemoryLocation::getForSource
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
Definition: MemoryLocation.cpp:95
llvm::MemTransferBase::getRawSource
Value * getRawSource() const
Return the arguments to the instruction.
Definition: IntrinsicInst.h:706
llvm::LocationSize::getValue
uint64_t getValue() const
Definition: MemoryLocation.h:158
TargetLibraryInfo.h
llvm::LocationSize::mapTombstone
constexpr static LocationSize mapTombstone()
Definition: MemoryLocation.h:134
llvm::TargetLibraryInfo::getLibFunc
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Definition: TargetLibraryInfo.h:291
llvm::Instruction
Definition: Instruction.h:45
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::LocationSize::precise
static LocationSize precise(uint64_t Value)
Definition: MemoryLocation.h:100
llvm::Instruction::getAAMetadata
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1370
llvm::None
const NoneType None
Definition: None.h:23
Type.h
llvm::AnyMemTransferInst
Definition: IntrinsicInst.h:1029
BasicBlock.h
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:304
VI
@ VI
Definition: SIInstrInfo.cpp:7689
llvm::MemoryLocation::getOrNone
static Optional< MemoryLocation > getOrNone(const Instruction *Inst)
Definition: MemoryLocation.cpp:78
MemoryLocation.h
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
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:131
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:655
llvm::TargetLibraryInfo::has
bool has(LibFunc F) const
Tests whether a library function is available.
Definition: TargetLibraryInfo.h:325
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:137
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:175
llvm::AtomicRMWInst
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:726
llvm::MemoryLocation::MemoryLocation
MemoryLocation()
Definition: MemoryLocation.h:285
llvm::MemIntrinsicBase::getLength
Value * getLength() const
Definition: IntrinsicInst.h:643
llvm::LocationSize::beforeOrAfterPointer
constexpr static LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
Definition: MemoryLocation.h:129
llvm::AtomicCmpXchgInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:651
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:221
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
llvm::AtomicMemIntrinsic
Definition: IntrinsicInst.h:781
llvm::AtomicRMWInst::getValOperand
Value * getValOperand()
Definition: Instructions.h:869
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:267
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:275
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:401
llvm::MemoryLocation::getForDest
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Definition: MemoryLocation.cpp:113
LLVMContext.h
llvm::AnyMemIntrinsic
Definition: IntrinsicInst.h:979
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:521
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:209