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 
39  LI->getAAMetadata(AATags);
40  const auto &DL = LI->getModule()->getDataLayout();
41 
42  return MemoryLocation(
43  LI->getPointerOperand(),
44  LocationSize::precise(DL.getTypeStoreSize(LI->getType())), AATags);
45 }
46 
49  SI->getAAMetadata(AATags);
50  const auto &DL = SI->getModule()->getDataLayout();
51 
52  return MemoryLocation(SI->getPointerOperand(),
53  LocationSize::precise(DL.getTypeStoreSize(
54  SI->getValueOperand()->getType())),
55  AATags);
56 }
57 
60  VI->getAAMetadata(AATags);
61 
62  return MemoryLocation(VI->getPointerOperand(),
64 }
65 
68  CXI->getAAMetadata(AATags);
69  const auto &DL = CXI->getModule()->getDataLayout();
70 
71  return MemoryLocation(CXI->getPointerOperand(),
72  LocationSize::precise(DL.getTypeStoreSize(
73  CXI->getCompareOperand()->getType())),
74  AATags);
75 }
76 
79  RMWI->getAAMetadata(AATags);
80  const auto &DL = RMWI->getModule()->getDataLayout();
81 
82  return MemoryLocation(RMWI->getPointerOperand(),
83  LocationSize::precise(DL.getTypeStoreSize(
84  RMWI->getValOperand()->getType())),
85  AATags);
86 }
87 
89  switch (Inst->getOpcode()) {
90  case Instruction::Load:
91  return get(cast<LoadInst>(Inst));
92  case Instruction::Store:
93  return get(cast<StoreInst>(Inst));
94  case Instruction::VAArg:
95  return get(cast<VAArgInst>(Inst));
96  case Instruction::AtomicCmpXchg:
97  return get(cast<AtomicCmpXchgInst>(Inst));
98  case Instruction::AtomicRMW:
99  return get(cast<AtomicRMWInst>(Inst));
100  default:
101  return None;
102  }
103 }
104 
106  return getForSource(cast<AnyMemTransferInst>(MTI));
107 }
108 
110  return getForSource(cast<AnyMemTransferInst>(MTI));
111 }
112 
115  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
116  Size = LocationSize::precise(C->getValue().getZExtValue());
117 
118  // memcpy/memmove can have AA tags. For memcpy, they apply
119  // to both the source and the destination.
121  MTI->getAAMetadata(AATags);
122 
123  return MemoryLocation(MTI->getRawSource(), Size, AATags);
124 }
125 
127  return getForDest(cast<AnyMemIntrinsic>(MI));
128 }
129 
131  return getForDest(cast<AnyMemIntrinsic>(MI));
132 }
133 
136  if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()))
137  Size = LocationSize::precise(C->getValue().getZExtValue());
138 
139  // memcpy/memmove can have AA tags. For memcpy, they apply
140  // to both the source and the destination.
142  MI->getAAMetadata(AATags);
143 
144  return MemoryLocation(MI->getRawDest(), Size, AATags);
145 }
146 
148  unsigned ArgIdx,
149  const TargetLibraryInfo *TLI) {
151  Call->getAAMetadata(AATags);
152  const Value *Arg = Call->getArgOperand(ArgIdx);
153 
154  // We may be able to produce an exact size for known intrinsics.
155  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
156  const DataLayout &DL = II->getModule()->getDataLayout();
157 
158  switch (II->getIntrinsicID()) {
159  default:
160  break;
161  case Intrinsic::memset:
162  case Intrinsic::memcpy:
163  case Intrinsic::memcpy_inline:
164  case Intrinsic::memmove:
165  assert((ArgIdx == 0 || ArgIdx == 1) &&
166  "Invalid argument index for memory intrinsic");
167  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
168  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
169  AATags);
171 
172  case Intrinsic::lifetime_start:
173  case Intrinsic::lifetime_end:
174  case Intrinsic::invariant_start:
175  assert(ArgIdx == 1 && "Invalid argument index");
176  return MemoryLocation(
177  Arg,
179  cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
180  AATags);
181 
182  case Intrinsic::masked_load:
183  assert(ArgIdx == 0 && "Invalid argument index");
184  return MemoryLocation(
185  Arg,
186  LocationSize::upperBound(DL.getTypeStoreSize(II->getType())),
187  AATags);
188 
189  case Intrinsic::masked_store:
190  assert(ArgIdx == 1 && "Invalid argument index");
191  return MemoryLocation(
192  Arg,
194  DL.getTypeStoreSize(II->getArgOperand(0)->getType())),
195  AATags);
196 
197  case Intrinsic::invariant_end:
198  // The first argument to an invariant.end is a "descriptor" type (e.g. a
199  // pointer to a empty struct) which is never actually dereferenced.
200  if (ArgIdx == 0)
202  assert(ArgIdx == 2 && "Invalid argument index");
203  return MemoryLocation(
204  Arg,
206  cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
207  AATags);
208 
209  case Intrinsic::arm_neon_vld1:
210  assert(ArgIdx == 0 && "Invalid argument index");
211  // LLVM's vld1 and vst1 intrinsics currently only support a single
212  // vector register.
213  return MemoryLocation(
214  Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
215  AATags);
216 
217  case Intrinsic::arm_neon_vst1:
218  assert(ArgIdx == 0 && "Invalid argument index");
219  return MemoryLocation(Arg,
220  LocationSize::precise(DL.getTypeStoreSize(
221  II->getArgOperand(1)->getType())),
222  AATags);
223  }
224  }
225 
226  // We can bound the aliasing properties of memset_pattern16 just as we can
227  // for memcpy/memset. This is particularly important because the
228  // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
229  // whenever possible.
230  LibFunc F;
231  if (TLI && TLI->getLibFunc(*Call, F) && TLI->has(F)) {
232  switch (F) {
233  case LibFunc_memset_pattern16:
234  assert((ArgIdx == 0 || ArgIdx == 1) &&
235  "Invalid argument index for memset_pattern16");
236  if (ArgIdx == 1)
238  if (const ConstantInt *LenCI =
239  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
240  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
241  AATags);
243  case LibFunc_bcmp:
244  case LibFunc_memcmp:
245  assert((ArgIdx == 0 || ArgIdx == 1) &&
246  "Invalid argument index for memcmp/bcmp");
247  if (const ConstantInt *LenCI =
248  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
249  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
250  AATags);
252  case LibFunc_memchr:
253  assert((ArgIdx == 0) && "Invalid argument index for memchr");
254  if (const ConstantInt *LenCI =
255  dyn_cast<ConstantInt>(Call->getArgOperand(2)))
256  return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
257  AATags);
259  case LibFunc_memccpy:
260  assert((ArgIdx == 0 || ArgIdx == 1) &&
261  "Invalid argument index for memccpy");
262  // We only know an upper bound on the number of bytes read/written.
263  if (const ConstantInt *LenCI =
264  dyn_cast<ConstantInt>(Call->getArgOperand(3)))
265  return MemoryLocation(
266  Arg, LocationSize::upperBound(LenCI->getZExtValue()), AATags);
268  default:
269  break;
270  };
271  }
272  // FIXME: Handle memset_pattern4 and memset_pattern8 also.
273 
274  return MemoryLocation::getBeforeOrAfter(Call->getArgOperand(ArgIdx), AATags);
275 }
llvm::LocationSize::print
void print(raw_ostream &OS) const
Definition: MemoryLocation.cpp:21
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
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
---------------------— PointerInfo ------------------------------------—
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:1835
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
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:895
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::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:852
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::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::Instruction::getAAMetadata
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
Definition: TypeBasedAliasAnalysis.cpp:524
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:811
llvm::MemoryLocation::getForSource
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
Definition: MemoryLocation.cpp:105
llvm::MemTransferBase::getRawSource
Value * getRawSource() const
Return the arguments to the instruction.
Definition: IntrinsicInst.h:684
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:289
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::None
const NoneType None
Definition: None.h:23
Type.h
llvm::AnyMemTransferInst
Definition: IntrinsicInst.h:1007
BasicBlock.h
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:304
VI
@ VI
Definition: SIInstrInfo.cpp:7542
llvm::MemoryLocation::getOrNone
static Optional< MemoryLocation > getOrNone(const Instruction *Inst)
Definition: MemoryLocation.cpp:88
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:147
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:323
DataLayout.h
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
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:621
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:219
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
llvm::AtomicMemIntrinsic
Definition: IntrinsicInst.h:759
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:126
LLVMContext.h
llvm::AnyMemIntrinsic
Definition: IntrinsicInst.h:957
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
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