LLVM 17.0.0git
AliasAnalysisSummary.cpp
Go to the documentation of this file.
2#include "llvm/IR/Argument.h"
4#include "llvm/IR/Type.h"
6
7namespace llvm {
8namespace cflaa {
9
10namespace {
11const unsigned AttrEscapedIndex = 0;
12const unsigned AttrUnknownIndex = 1;
13const unsigned AttrGlobalIndex = 2;
14const unsigned AttrCallerIndex = 3;
15const unsigned AttrFirstArgIndex = 4;
16const unsigned AttrLastArgIndex = NumAliasAttrs;
17const unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
18
19// It would be *slightly* prettier if we changed these to AliasAttrs, but it
20// seems that both GCC and MSVC emit dynamic initializers for const bitsets.
21using AliasAttr = unsigned;
22const AliasAttr AttrNone = 0;
23const AliasAttr AttrEscaped = 1 << AttrEscapedIndex;
24const AliasAttr AttrUnknown = 1 << AttrUnknownIndex;
25const AliasAttr AttrGlobal = 1 << AttrGlobalIndex;
26const AliasAttr AttrCaller = 1 << AttrCallerIndex;
27const AliasAttr ExternalAttrMask = AttrEscaped | AttrUnknown | AttrGlobal;
28}
29
30AliasAttrs getAttrNone() { return AttrNone; }
31
32AliasAttrs getAttrUnknown() { return AttrUnknown; }
33bool hasUnknownAttr(AliasAttrs Attr) { return Attr.test(AttrUnknownIndex); }
34
35AliasAttrs getAttrCaller() { return AttrCaller; }
36bool hasCallerAttr(AliasAttrs Attr) { return Attr.test(AttrCaller); }
38 return Attr.test(AttrUnknownIndex) || Attr.test(AttrCallerIndex);
39}
40
41AliasAttrs getAttrEscaped() { return AttrEscaped; }
42bool hasEscapedAttr(AliasAttrs Attr) { return Attr.test(AttrEscapedIndex); }
43
44static AliasAttr argNumberToAttr(unsigned ArgNum) {
45 if (ArgNum >= AttrMaxNumArgs)
46 return AttrUnknown;
47 // N.B. MSVC complains if we use `1U` here, since AliasAttr' ctor takes
48 // an unsigned long long.
49 return AliasAttr(1ULL << (ArgNum + AttrFirstArgIndex));
50}
51
53 if (isa<GlobalValue>(Val))
54 return AttrGlobal;
55
56 if (auto *Arg = dyn_cast<Argument>(&Val))
57 // Only pointer arguments should have the argument attribute,
58 // because things can't escape through scalars without us seeing a
59 // cast, and thus, interaction with them doesn't matter.
60 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
61 return argNumberToAttr(Arg->getArgNo());
62 return AttrNone;
63}
64
66 return Attr.reset(AttrEscapedIndex)
67 .reset(AttrUnknownIndex)
68 .reset(AttrCallerIndex)
69 .any();
70}
71
73 return Attr & AliasAttrs(ExternalAttrMask);
74}
75
76std::optional<InstantiatedValue>
78 auto Index = IValue.Index;
79 auto *V = (Index == 0) ? &Call : Call.getArgOperand(Index - 1);
80 if (V->getType()->isPointerTy())
81 return InstantiatedValue{V, IValue.DerefLevel};
82 return std::nullopt;
83}
84
85std::optional<InstantiatedRelation>
87 auto From = instantiateInterfaceValue(ERelation.From, Call);
88 if (!From)
89 return std::nullopt;
90 auto To = instantiateInterfaceValue(ERelation.To, Call);
91 if (!To)
92 return std::nullopt;
93 return InstantiatedRelation{*From, *To, ERelation.Offset};
94}
95
96std::optional<InstantiatedAttr>
98 auto Value = instantiateInterfaceValue(EAttr.IValue, Call);
99 if (!Value)
100 return std::nullopt;
101 return InstantiatedAttr{*Value, EAttr.Attr};
102}
103}
104}
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This file defines various utility types and functions useful to summary-based alias analysis.
BlockVerifier::State From
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1186
LLVM Value Representation.
Definition: Value.h:74
std::optional< InstantiatedValue > instantiateInterfaceValue(InterfaceValue IValue, CallBase &Call)
AliasAttrs getExternallyVisibleAttrs(AliasAttrs Attr)
Given an AliasAttrs, return a new AliasAttrs that only contains attributes meaningful to the caller.
bool hasUnknownOrCallerAttr(AliasAttrs Attr)
AliasAttrs getAttrCaller()
AttrCaller represent whether the said pointer comes from a source not known to the current function b...
std::optional< InstantiatedRelation > instantiateExternalRelation(ExternalRelation ERelation, CallBase &Call)
bool hasEscapedAttr(AliasAttrs Attr)
std::bitset< NumAliasAttrs > AliasAttrs
These are attributes that an alias analysis can use to mark certain special properties of a given poi...
AliasAttrs getAttrNone()
Attr represent whether the said pointer comes from an unknown source (such as opaque memory or an int...
std::optional< InstantiatedAttr > instantiateExternalAttribute(ExternalAttribute EAttr, CallBase &Call)
bool hasCallerAttr(AliasAttrs Attr)
AliasAttrs getGlobalOrArgAttrFromValue(const Value &Val)
AttrGlobal represent whether the said pointer is a global value.
static const unsigned NumAliasAttrs
The number of attributes that AliasAttr should contain.
bool isGlobalOrArgAttr(AliasAttrs Attr)
AliasAttrs getAttrUnknown()
AttrUnknown represent whether the said pointer comes from a source not known to alias analyses (such ...
bool hasUnknownAttr(AliasAttrs Attr)
AliasAttrs getAttrEscaped()
AttrEscaped represent whether the said pointer comes from a known source but escapes to the unknown w...
static AliasAttr argNumberToAttr(unsigned ArgNum)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
We use ExternalAttribute to describe an externally visible AliasAttrs for parameters/return value.
We use ExternalRelation to describe an externally visible aliasing relations between parameters/retur...
This is the result of instantiating ExternalAttribute at a particular callsite.
This is the result of instantiating ExternalRelation at a particular callsite.
This is the result of instantiating InterfaceValue at a particular call.
We use InterfaceValue to describe parameters/return value, as well as potential memory locations that...