132static bool isNewFormatTypeNode(
const MDNode *
N) {
133 if (
N->getNumOperands() < 3)
144template<
typename MDNodeTy>
146 MDNodeTy *Node =
nullptr;
149 TBAANodeImpl() =
default;
150 explicit TBAANodeImpl(MDNodeTy *
N) : Node(
N) {}
153 MDNodeTy *
getNode()
const {
return Node; }
157 bool isNewFormat()
const {
return isNewFormatTypeNode(Node); }
160 TBAANodeImpl<MDNodeTy>
getParent()
const {
164 if (Node->getNumOperands() < 2)
165 return TBAANodeImpl<MDNodeTy>();
168 return TBAANodeImpl<MDNodeTy>();
170 return TBAANodeImpl<MDNodeTy>(
P);
177using TBAANode = TBAANodeImpl<const MDNode>;
178using MutableTBAANode = TBAANodeImpl<MDNode>;
184template<
typename MDNodeTy>
185class TBAAStructTagNodeImpl {
190 explicit TBAAStructTagNodeImpl(MDNodeTy *
N) : Node(
N) {}
193 MDNodeTy *
getNode()
const {
return Node; }
197 bool isNewFormat()
const {
198 if (Node->getNumOperands() < 4)
201 if (!TBAANodeImpl<MDNodeTy>(AccessType).isNewFormat())
227 bool isTypeImmutable()
const {
228 unsigned OpNo = isNewFormat() ? 4 : 3;
229 if (Node->getNumOperands() < OpNo + 1)
241using TBAAStructTagNode = TBAAStructTagNodeImpl<const MDNode>;
242using MutableTBAAStructTagNode = TBAAStructTagNodeImpl<MDNode>;
248class TBAAStructTypeNode {
250 const MDNode *Node =
nullptr;
253 TBAAStructTypeNode() =
default;
254 explicit TBAAStructTypeNode(
const MDNode *
N) : Node(
N) {}
257 const MDNode *
getNode()
const {
return Node; }
261 bool isNewFormat()
const {
return isNewFormatTypeNode(Node); }
269 return Node->getOperand(isNewFormat() ? 2 : 0);
272 unsigned getNumFields()
const {
273 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
274 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
275 return (
getNode()->getNumOperands() - FirstFieldOpNo) / NumOpsPerField;
278 TBAAStructTypeNode getFieldType(
unsigned FieldIndex)
const {
279 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
280 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
281 unsigned OpIndex = FirstFieldOpNo + FieldIndex * NumOpsPerField;
283 return TBAAStructTypeNode(TypeNode);
289 bool NewFormat = isNewFormat();
291 const unsigned NumOperands =
Operands.size();
296 return TBAAStructTypeNode();
300 return TBAAStructTypeNode();
304 if (NumOperands <= 3) {
312 return TBAAStructTypeNode();
313 return TBAAStructTypeNode(
P);
319 unsigned FirstFieldOpNo = NewFormat ? 3 : 1;
320 unsigned NumOpsPerField = NewFormat ? 3 : 2;
323 for (
unsigned Idx = FirstFieldOpNo; Idx < NumOperands;
324 Idx += NumOpsPerField) {
328 assert(Idx >= FirstFieldOpNo + NumOpsPerField &&
329 "TBAAStructTypeNode::getField should have an offset match!");
330 TheIdx = Idx - NumOpsPerField;
336 TheIdx = NumOperands - NumOpsPerField;
342 return TBAAStructTypeNode();
343 return TBAAStructTypeNode(
P);
352 if (!shouldUseTBAA())
364 if (!shouldUseTBAA())
367 const auto *
N =
Loc.AATags.TBAA;
373 const auto *ErrnoTBAAMD =
375 if (!ErrnoTBAAMD ||
any_of(ErrnoTBAAMD->operands(), [&](
const auto *
Node) {
376 return Aliases(N, Node);
385 if (!shouldUseTBAA())
394 if (TBAAStructTagNode(M).isTypeImmutable())
402 if (!shouldUseTBAA())
406 if (
const MDNode *M =
Call->getMetadata(LLVMContext::MD_tbaa))
407 if (TBAAStructTagNode(M).isTypeImmutable())
421 if (!shouldUseTBAA())
425 if (
const MDNode *M =
Call->getMetadata(LLVMContext::MD_tbaa))
435 if (!shouldUseTBAA())
440 if (!Aliases(
M1, M2))
448 TBAAStructTagNode
Tag(
this);
449 TBAAStructTypeNode AccessType(
Tag.getAccessType());
451 if (Id->getString() ==
"vtable pointer")
457 const MDNode **GenericTag =
nullptr);
462 return const_cast<MDNode*
>(GenericTag);
474 while (TA.getNode()) {
475 if (!PathA.
insert(TA.getNode()))
482 while (TB.getNode()) {
483 if (!PathB.
insert(TB.getNode()))
488 int IA = PathA.
size() - 1;
489 int IB = PathB.
size() - 1;
491 const MDNode *Ret =
nullptr;
492 while (IA >= 0 && IB >= 0) {
493 if (PathA[IA] == PathB[IB])
507 Result.TBAAStruct =
nullptr;
517 Result.TBAA = Result.TBAAStruct =
nullptr;
534 if (TBAAStructTypeNode(AccessType).isNewFormat()) {
541 const_cast<MDNode*
>(AccessType),
542 OffsetNode, SizeNode};
547 const_cast<MDNode*
>(AccessType),
553 TBAAStructTypeNode FieldType) {
554 for (
unsigned I = 0,
E =
BaseType.getNumFields();
I !=
E; ++
I) {
555 TBAAStructTypeNode
T =
BaseType.getFieldType(
I);
570 TBAAStructTagNode SubobjectTag,
572 const MDNode **GenericTag,
576 if (BaseTag.getAccessType() == BaseTag.getBaseType() &&
577 BaseTag.getAccessType() == CommonType) {
589 bool NewFormat = BaseTag.isNewFormat();
590 TBAAStructTypeNode
BaseType(BaseTag.getBaseType());
591 uint64_t OffsetInBase = BaseTag.getOffset();
597 assert(!NewFormat &&
"Did not see access type in access path!");
601 if (
BaseType.getNode() == SubobjectTag.getBaseType()) {
602 MayAlias = OffsetInBase == SubobjectTag.getOffset() ||
603 BaseType.getNode() == BaseTag.getAccessType() ||
604 SubobjectTag.getBaseType() == SubobjectTag.getAccessType();
608 }
else if (SubobjectTag.isTypeImmutable() &&
609 !BaseTag.isTypeImmutable()) {
612 const MDNode *Tag = SubobjectTag.getNode();
613 unsigned FlagOpNo = SubobjectTag.isNewFormat() ? 4 : 3;
617 *GenericTag = SubobjectTag.getNode();
624 if (NewFormat &&
BaseType.getNode() == BaseTag.getAccessType())
637 TBAAStructTypeNode FieldType(SubobjectTag.getBaseType());
653 const MDNode **GenericTag) {
663 *GenericTag =
nullptr;
667 TBAAStructTagNode TagA(
A), TagB(
B);
669 TagB.getAccessType());
675 *GenericTag =
nullptr;
683 CommonType, GenericTag, MayAlias) ||
685 CommonType, GenericTag, MayAlias))
696bool TypeBasedAAResult::Aliases(
const MDNode *
A,
const MDNode *
B)
const {
700bool TypeBasedAAResult::shouldUseTBAA()
const {
757 TBAAStructTagNode
T(Tag);
758 return T.getAccessType() ==
T.getBaseType();
783 ConstantInt::get(InnerOffset->
getType(), NewOffset)));
785 ConstantInt::get(InnerSize->
getType(), NewSize)));
796 TBAAStructTagNode
Tag(MD);
799 if (!
Tag.isNewFormat())
822 MDNode *M = New.TBAAStruct;
827 New.TBAAStruct =
nullptr;
830 MDNode *CommonTag =
nullptr;
832 for (
size_t I = 0, E = M->getNumOperands();
I + 2 < E &&
Offset < AccessSize;
834 std::optional<uint64_t> FieldOffset =
836 std::optional<uint64_t> FieldSize =
839 if (!FieldOffset || !FieldSize || !FieldTag ||
841 (CommonTag && FieldTag != CommonTag))
843 CommonTag = FieldTag;
847 New.TBAA = CommonTag;
854 if (!
DL.typeSizeEqualsStoreSize(AccessTy))
857 if (
Size.isScalable())
860 return New.adjustForAccess(
Size.getKnownMinValue());
865 return New.adjustForAccess(AccessSize);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static MemAccessTy getAccessType(const TargetTransformInfo &TTI, Instruction *Inst, Value *OperandVal)
Return the type of the memory being accessed.
This file provides utility analysis objects describing memory locations.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file implements a set that has insertion order iteration characteristics.
static bool matchAccessTags(const MDNode *A, const MDNode *B, const MDNode **GenericTag=nullptr)
matchTags - Return true if the given couple of accesses are allowed to overlap.
static cl::opt< bool > EnableTBAA("enable-tbaa", cl::init(true), cl::Hidden)
static bool mayBeAccessToSubobjectOf(TBAAStructTagNode BaseTag, TBAAStructTagNode SubobjectTag, const MDNode *CommonType, const MDNode **GenericTag, bool &MayAlias)
Return true if for two given accesses, one of the accessed objects may be a subobject of the other.
static bool isScalarAccessTag(const MDNode *Tag)
static std::optional< uint64_t > getTBAAStructFieldAsInt64(const MDOperand &Op)
static bool hasField(TBAAStructTypeNode BaseType, TBAAStructTypeNode FieldType)
static const MDNode * createAccessTag(const MDNode *AccessType)
static const MDNode * getLeastCommonType(const MDNode *A, const MDNode *B)
This is the interface for a metadata-based TBAA.
static unsigned getSize(unsigned Kind)
This class stores info we want to provide to or retain within an alias query.
The possible results of an alias query.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
bool equalsInt(uint64_t V) const
A helper method that can be used to determine if the constant contained within is equal to a constant...
const APInt & getValue() const
Return the constant as an APInt value reference.
A parsed version of the target data layout string in and methods for querying it.
ImmutablePass class - This class is used to provide information that does not need to be run.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static LLVM_ABI MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
LLVM_ABI bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
static LLVM_ABI MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
const MDOperand & getOperand(unsigned I) const
static LLVM_ABI MDNode * getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B)
ArrayRef< MDOperand > operands() const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
unsigned getNumOperands() const
Return number of MDNode operands.
LLVM_ABI MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2={})
static LLVM_ABI MDNode * intersect(MDNode *A, MDNode *B)
LLVMContext & getContext() const
Tracking metadata reference owned by Metadata.
static MemoryEffectsBase none()
static MemoryEffectsBase unknown()
Representation for a specific memory location.
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
A Module instance is used to store all the information related to an LLVM module.
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
size_type size() const
Determine the number of elements in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A simple AA result that uses TBAA metadata to answer queries.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Instruction *CtxI)
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Legacy wrapper pass to provide the TypeBasedAAResult object.
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI TypeBasedAAResult run(Function &F, FunctionAnalysisManager &AM)
The instances of the Type class are immutable: once they are created, they are never changed.
Type * getType() const
All values are typed, get the type of this value.
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
unsigned M1(unsigned Val)
auto dyn_cast_or_null(const Y &Val)
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
@ Sub
Subtraction of integers.
LLVM_ABI ImmutablePass * createTypeBasedAAWrapperPass()
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI AAMDNodes concat(const AAMDNodes &Other) const
Determine the best AAMDNodes after concatenating two different locations together.
static LLVM_ABI MDNode * shiftTBAAStruct(MDNode *M, size_t off)
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
MDNode * Scope
The tag for alias scope specification (used with noalias).
static LLVM_ABI MDNode * extendToTBAA(MDNode *TBAA, ssize_t len)
MDNode * TBAA
The tag for type-based alias analysis.
AAMDNodes shift(size_t Offset) const
Create a new AAMDNode that describes this AAMDNode after applying a constant offset to the start of t...
LLVM_ABI AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
MDNode * NoAlias
The tag specifying the noalias scope.
LLVM_ABI AAMDNodes adjustForAccess(unsigned AccessSize)
Create a new AAMDNode for accessing AccessSize bytes of this AAMDNode.
static LLVM_ABI MDNode * shiftTBAA(MDNode *M, size_t off)
A special type used by analysis passes to provide an address that identifies that particular analysis...