clang 24.0.0git
MovedLoans.cpp
Go to the documentation of this file.
1//===- MovedLoans.cpp - Moved Loans Analysis --------------------*- C++ -*-===//
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//
9// This file defines the MovedLoansAnalysis, a forward dataflow analysis that
10// tracks which loans have been moved out of their original storage location
11// at each program point.
12//
13//===----------------------------------------------------------------------===//
14
16#include "Dataflow.h"
22
24namespace {
25struct Lattice {
26 MovedLoansMap MovedLoans = MovedLoansMap(nullptr);
27
28 explicit Lattice(MovedLoansMap MovedLoans) : MovedLoans(MovedLoans) {}
29
30 Lattice() = default;
31
32 bool operator==(const Lattice &Other) const {
33 return MovedLoans == Other.MovedLoans;
34 }
35 bool operator!=(const Lattice &Other) const { return !(*this == Other); }
36};
37
38class AnalysisImpl
39 : public DataflowAnalysis<AnalysisImpl, Lattice, Direction::Forward> {
40public:
41 AnalysisImpl(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
42 const LoanPropagationAnalysis &LoanPropagation,
43 const LiveOriginsAnalysis &LiveOrigins,
44 const LoanManager &LoanMgr,
45 MovedLoansMap::Factory &MovedLoansMapFactory)
46 : DataflowAnalysis(C, AC, F), LoanPropagation(LoanPropagation),
47 LiveOrigins(LiveOrigins), LoanMgr(LoanMgr),
48 MovedLoansMapFactory(MovedLoansMapFactory) {}
49
50 using Base::transfer;
51
52 StringRef getAnalysisName() const { return "MovedLoans"; }
53
54 Lattice getInitialState() { return Lattice{}; }
55
56 /// Merges moved loan state from different control flow paths. When a loan
57 /// is moved on multiple paths, picks the lexically earliest move expression.
58 Lattice join(Lattice A, Lattice B) {
59 MovedLoansMap MovedLoans = utils::join(
60 A.MovedLoans, B.MovedLoans, MovedLoansMapFactory,
61 [](const Expr *const *MoveA, const Expr *const *MoveB) -> const Expr * {
62 assert(MoveA || MoveB);
63 if (!MoveA)
64 return *MoveB;
65 if (!MoveB)
66 return *MoveA;
67 return (*MoveA)->getExprLoc() < (*MoveB)->getExprLoc() ? *MoveA
68 : *MoveB;
69 },
71 return Lattice(MovedLoans);
72 }
73
74 /// Marks all live loans sharing the same access path as the moved origin as
75 /// potentially moved.
76 Lattice transfer(Lattice In, const MovedOriginFact &F) {
77 MovedLoansMap MovedLoans = In.MovedLoans;
78 OriginID MovedOrigin = F.getMovedOrigin();
79 LoanSet ImmediatelyMovedLoans = LoanPropagation.getLoans(MovedOrigin, &F);
80 auto IsInvalidated = [&](const AccessPath &Path) {
81 for (LoanID LID : ImmediatelyMovedLoans) {
82 const Loan *MovedLoan = LoanMgr.getLoan(LID);
83 if (MovedLoan->getAccessPath().isPrefixOf(Path))
84 return true;
85 }
86 return false;
87 };
88 LiveOriginSet Origins = LiveOrigins.getLiveOriginsAt(&F);
89 for (const LivenessMap &Live : {Origins.Persistent, Origins.BlockLocal})
90 for (auto [O, _] : Live)
91 for (LoanID LiveLoan : LoanPropagation.getLoans(O, &F)) {
92 const Loan *LiveLoanPtr = LoanMgr.getLoan(LiveLoan);
93 if (IsInvalidated(LiveLoanPtr->getAccessPath()))
94 MovedLoans =
95 MovedLoansMapFactory.add(MovedLoans, LiveLoan, F.getMoveExpr());
96 }
97 return Lattice(MovedLoans);
98 }
99
100 MovedLoansMap getMovedLoans(ProgramPoint P) { return getState(P).MovedLoans; }
101
102private:
103 const LoanPropagationAnalysis &LoanPropagation;
104 const LiveOriginsAnalysis &LiveOrigins;
105 const LoanManager &LoanMgr;
106 MovedLoansMap::Factory &MovedLoansMapFactory;
107};
108} // namespace
109
110class MovedLoansAnalysis::Impl final : public AnalysisImpl {
111 using AnalysisImpl::AnalysisImpl;
112};
113
115 const CFG &C, AnalysisDeclContext &AC, FactManager &F,
116 const LoanPropagationAnalysis &LoanPropagation,
117 const LiveOriginsAnalysis &LiveOrigins, const LoanManager &LoanMgr,
118 MovedLoansMap::Factory &MovedLoansMapFactory)
119 : PImpl(std::make_unique<Impl>(C, AC, F, LoanPropagation, LiveOrigins,
120 LoanMgr, MovedLoansMapFactory)) {
121 PImpl->run();
122}
123
125
127 return PImpl->getMovedLoans(P);
128}
129} // namespace clang::lifetimes::internal
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1271
A generic, policy-based driver for dataflow analyses.
Definition Dataflow.h:60
Manages the creation, storage and retrieval of loans.
Definition Loans.h:210
MovedLoansAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, const LoanPropagationAnalysis &LoanPropagation, const LiveOriginsAnalysis &LiveOrigins, const LoanManager &LoanMgr, MovedLoansMap::Factory &MovedLoansMapFactory)
MovedLoansMap getMovedLoans(ProgramPoint P) const
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env, Environment::ValueModel &Model)
Evaluates S and updates Env accordingly.
Definition Transfer.cpp:986
@ Asymmetric
An asymmetric join preserves keys unique to the first map as-is, while applying the JoinValues operat...
Definition Utils.h:66
SetTy< T > join(SetTy< T > A, SetTy< T > B, typename SetTy< T >::Factory &F)
Computes the union of two ImmutableSets.
Definition Utils.h:49
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:98
utils::ID< struct LoanTag > LoanID
Definition Loans.h:27
utils::ID< struct OriginTag > OriginID
Definition Origins.h:28
utils::MapTy< LoanID, const Expr * > MovedLoansMap
Definition MovedLoans.h:27
utils::SetTy< LoanID > LoanSet
utils::MapTy< OriginID, LivenessInfo > LivenessMap
Definition LiveOrigins.h:76
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:218
bool operator!=(CanQual< T > x, CanQual< U > y)
@ Other
Other implicit parameter.
Definition Decl.h:1774