20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/Support/TimeProfiler.h"
23#include "llvm/Support/raw_ostream.h"
31 llvm::TimeTraceScope(
"ComputePersistentOrigins");
33 llvm::BitVector PersistentOrigins(NumOrigins);
39 auto CheckOrigin = [&](
OriginID OID) {
40 if (PersistentOrigins.test(OID.Value))
42 auto &FirstSeenBlock = OriginToFirstSeenBlock[OID.Value];
43 if (FirstSeenBlock ==
nullptr)
45 if (FirstSeenBlock != B) {
47 PersistentOrigins.set(OID.Value);
51 switch (F->getKind()) {
57 CheckOrigin(OF->getDestOriginID());
58 CheckOrigin(OF->getSrcOriginID());
64 CheckOrigin(Cur->getOuterOriginID());
75 return PersistentOrigins;
94 : PersistentOrigins(Persistent), BlockLocalOrigins(BlockLocal) {}
98 return PersistentOrigins ==
Other.PersistentOrigins &&
99 BlockLocalOrigins ==
Other.BlockLocalOrigins;
103 void dump(llvm::raw_ostream &OS)
const {
104 OS <<
"LoanPropagationLattice State:\n";
105 OS <<
" Persistent Origins:\n";
106 if (PersistentOrigins.isEmpty())
108 for (
const auto &Entry : PersistentOrigins) {
109 if (Entry.second.isEmpty())
110 OS <<
" Origin " << Entry.first <<
" contains no loans\n";
111 for (
const LoanID &LID : Entry.second)
112 OS <<
" Origin " << Entry.first <<
" contains Loan " << LID <<
"\n";
114 OS <<
" Block-Local Origins:\n";
115 if (BlockLocalOrigins.isEmpty())
117 for (
const auto &Entry : BlockLocalOrigins) {
118 if (Entry.second.isEmpty())
119 OS <<
" Origin " << Entry.first <<
" contains no loans\n";
120 for (
const LoanID &LID : Entry.second)
121 OS <<
" Origin " << Entry.first <<
" contains Loan " << LID <<
"\n";
129 AnalysisImpl(
const CFG &
C, AnalysisDeclContext &AC, FactManager &F,
130 OriginLoanMap::Factory &OriginLoanMapFactory,
131 LoanSet::Factory &LoanSetFactory)
132 : DataflowAnalysis(
C, AC, F), OriginLoanMapFactory(OriginLoanMapFactory),
133 LoanSetFactory(LoanSetFactory),
136 using Base::transfer;
138 StringRef getAnalysisName()
const {
return "LoanPropagation"; }
140 Lattice getInitialState() {
return Lattice{}; }
144 Lattice join(Lattice A, Lattice B) {
146 A.PersistentOrigins, B.PersistentOrigins, OriginLoanMapFactory,
148 assert((S1 || S2) &&
"unexpectedly merging 2 empty sets");
153 return utils::join(*S1, *S2, LoanSetFactory);
158 return Lattice(JoinedOrigins, OriginLoanMapFactory.getEmptyMap());
162 Lattice
transfer(Lattice In,
const IssueFact &F) {
164 LoanID LID = F.getLoanID();
165 LoanSet NewLoans = LoanSetFactory.add(LoanSetFactory.getEmptySet(), LID);
166 return setLoans(In, OID, NewLoans);
172 Lattice
transfer(Lattice In,
const OriginFlowFact &F) {
173 OriginID DestOID = F.getDestOriginID();
174 OriginID SrcOID = F.getSrcOriginID();
177 F.getKillDest() ? LoanSetFactory.getEmptySet() : getLoans(In, DestOID);
178 LoanSet SrcLoans = getLoans(In, SrcOID);
181 return setLoans(In, DestOID, MergedLoans);
185 return getLoans(getState(P), OID);
190 bool isPersistent(
OriginID OID)
const {
191 return PersistentOrigins.test(OID.Value);
195 if (isPersistent(OID))
196 return Lattice(OriginLoanMapFactory.add(L.PersistentOrigins, OID, Loans),
197 L.BlockLocalOrigins);
198 return Lattice(L.PersistentOrigins,
199 OriginLoanMapFactory.add(L.BlockLocalOrigins, OID, Loans));
204 isPersistent(OID) ? &L.PersistentOrigins : &L.BlockLocalOrigins;
205 if (
auto *Loans = Map->lookup(OID))
207 return LoanSetFactory.getEmptySet();
210 OriginLoanMap::Factory &OriginLoanMapFactory;
211 LoanSet::Factory &LoanSetFactory;
215 llvm::BitVector PersistentOrigins;
220 using AnalysisImpl::AnalysisImpl;
225 OriginLoanMap::Factory &OriginLoanMapFactory,
226 LoanSet::Factory &LoanSetFactory)
227 : PImpl(
std::make_unique<
Impl>(
C, AC, F, OriginLoanMapFactory,
235 return PImpl->getLoans(OID, P);
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
A generic, policy-based driver for dataflow analyses.
llvm::ArrayRef< const Fact * > getFacts(const CFGBlock *B) const
OriginManager & getOriginMgr()
An abstract base class for a single, atomic lifetime-relevant event.
@ InvalidateOrigin
An origin is invalidated (e.g. vector resized).
@ TestPoint
A marker for a specific point in the code, for testing.
@ Expire
A loan expires as its underlying storage is freed (e.g., variable goes out of scope).
@ Issue
A new loan is issued from a borrow expression (e.g., &x).
@ OriginFlow
An origin is propagated from a source to a destination (e.g., p = q).
@ MovedOrigin
An origin that is moved (e.g., passed to an rvalue reference parameter).
@ Use
An origin is used (eg. appears as l-value expression like DeclRefExpr).
@ OriginEscapes
An origin that escapes the function scope (e.g., via return).
OriginID getOriginID() const
LoanSet getLoans(OriginID OID, ProgramPoint P) const
~LoanPropagationAnalysis()
LoanPropagationAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, OriginLoanMap::Factory &OriginLoanMapFactory, LoanSet::Factory &LoanSetFactory)
A list of origins representing levels of indirection for pointer-like types.
OriginList * peelOuterOrigin() const
unsigned getNumOrigins() const
const OriginList * getUsedOrigins() const
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env, Environment::ValueModel &Model)
Evaluates S and updates Env accordingly.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
@ Asymmetric
An asymmetric join preserves keys unique to the first map as-is, while applying the JoinValues operat...
static llvm::ImmutableSet< T > join(llvm::ImmutableSet< T > A, llvm::ImmutableSet< T > B, typename llvm::ImmutableSet< T >::Factory &F)
Computes the union of two ImmutableSets.
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
llvm::ImmutableSet< LoanID > LoanSet
utils::ID< struct LoanTag > LoanID
utils::ID< struct OriginTag > OriginID
llvm::ImmutableMap< OriginID, LoanSet > OriginLoanMap
static llvm::BitVector computePersistentOrigins(const FactManager &FactMgr, const CFG &C)
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
bool operator!=(CanQual< T > x, CanQual< U > y)
@ Other
Other implicit parameter.