clang 22.0.0git
Dataflow.h
Go to the documentation of this file.
1//===- Dataflow.h - Generic Dataflow Analysis Framework --------*- 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 a generic, policy-based driver for dataflow analyses.
10// It provides a flexible framework that combines the dataflow runner and
11// transfer functions, allowing derived classes to implement specific analyses
12// by defining their lattice, join, and transfer functions.
13//
14//===----------------------------------------------------------------------===//
15#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
16#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
17
20#include "clang/Analysis/CFG.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TimeProfiler.h"
25#include <optional>
26
28
29enum class Direction { Forward, Backward };
30
31/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
32/// `Fact`. identified by a lifetime-related event (`Fact`).
33///
34/// A `ProgramPoint` has "after" semantics: it represents the location
35/// immediately after its corresponding `Fact`.
36using ProgramPoint = const Fact *;
37
38/// A generic, policy-based driver for dataflow analyses. It combines
39/// the dataflow runner and the transferer logic into a single class hierarchy.
40///
41/// The derived class is expected to provide:
42/// - A `Lattice` type.
43/// - `StringRef getAnalysisName() const`
44/// - `Lattice getInitialState();` The initial state of the analysis.
45/// - `Lattice join(Lattice, Lattice);` Merges states from multiple CFG paths.
46/// - `Lattice transfer(Lattice, const FactType&);` Defines how a single
47/// lifetime-relevant `Fact` transforms the lattice state. Only overloads
48/// for facts relevant to the analysis need to be implemented.
49///
50/// \tparam Derived The CRTP derived class that implements the specific
51/// analysis.
52/// \tparam LatticeType The dataflow lattice used by the analysis.
53/// \tparam Dir The direction of the analysis (Forward or Backward).
54/// TODO: Maybe use the dataflow framework! The framework might need changes
55/// to support the current comparison done at block-entry.
56template <typename Derived, typename LatticeType, Direction Dir>
58public:
59 using Lattice = LatticeType;
61
62private:
63 const CFG &Cfg;
65
66 /// The dataflow state before a basic block is processed.
67 llvm::DenseMap<const CFGBlock *, Lattice> InStates;
68 /// The dataflow state after a basic block is processed.
69 llvm::DenseMap<const CFGBlock *, Lattice> OutStates;
70 /// The dataflow state at a Program Point.
71 /// In a forward analysis, this is the state after the Fact at that point has
72 /// been applied, while in a backward analysis, it is the state before.
73 llvm::DenseMap<ProgramPoint, Lattice> PerPointStates;
74
75 static constexpr bool isForward() { return Dir == Direction::Forward; }
76
77protected:
79
80 explicit DataflowAnalysis(const CFG &Cfg, AnalysisDeclContext &AC,
82 : Cfg(Cfg), AC(AC), FactMgr(FactMgr) {}
83
84public:
85 void run() {
86 Derived &D = static_cast<Derived &>(*this);
87 llvm::TimeTraceScope Time(D.getAnalysisName());
88
89 using Worklist =
90 std::conditional_t<Dir == Direction::Forward, ForwardDataflowWorklist,
92 Worklist W(Cfg, AC);
93
94 const CFGBlock *Start = isForward() ? &Cfg.getEntry() : &Cfg.getExit();
95 InStates[Start] = D.getInitialState();
96 W.enqueueBlock(Start);
97
98 while (const CFGBlock *B = W.dequeue()) {
99 Lattice StateIn = *getInState(B);
100 Lattice StateOut = transferBlock(B, StateIn);
101 OutStates[B] = StateOut;
102 for (const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
103 if (!AdjacentB)
104 continue;
105 std::optional<Lattice> OldInState = getInState(AdjacentB);
106 Lattice NewInState =
107 !OldInState ? StateOut : D.join(*OldInState, StateOut);
108 // Enqueue the adjacent block if its in-state has changed or if we have
109 // never seen it.
110 if (!OldInState || NewInState != *OldInState) {
111 InStates[AdjacentB] = NewInState;
112 W.enqueueBlock(AdjacentB);
113 }
114 }
115 }
116 }
117
118protected:
119 Lattice getState(ProgramPoint P) const { return PerPointStates.lookup(P); }
120
121 std::optional<Lattice> getInState(const CFGBlock *B) const {
122 auto It = InStates.find(B);
123 if (It == InStates.end())
124 return std::nullopt;
125 return It->second;
126 }
127
128 Lattice getOutState(const CFGBlock *B) const { return OutStates.lookup(B); }
129
130 void dump() const {
131 const Derived *D = static_cast<const Derived *>(this);
132 llvm::dbgs() << "==========================================\n";
133 llvm::dbgs() << D->getAnalysisName() << " results:\n";
134 llvm::dbgs() << "==========================================\n";
135 const CFGBlock &B = isForward() ? Cfg.getExit() : Cfg.getEntry();
136 getOutState(&B).dump(llvm::dbgs());
137 }
138
139private:
140 /// Computes the state at one end of a block by applying all its facts
141 /// sequentially to a given state from the other end.
142 Lattice transferBlock(const CFGBlock *Block, Lattice State) {
143 auto Facts = FactMgr.getFacts(Block);
144 if constexpr (isForward()) {
145 for (const Fact *F : Facts) {
146 State = transferFact(State, F);
147 PerPointStates[F] = State;
148 }
149 } else {
150 for (const Fact *F : llvm::reverse(Facts)) {
151 // In backward analysis, capture the state before applying the fact.
152 PerPointStates[F] = State;
153 State = transferFact(State, F);
154 }
155 }
156 return State;
157 }
158
159 Lattice transferFact(Lattice In, const Fact *F) {
160 assert(F);
161 Derived *D = static_cast<Derived *>(this);
162 switch (F->getKind()) {
164 return D->transfer(In, *F->getAs<IssueFact>());
166 return D->transfer(In, *F->getAs<ExpireFact>());
168 return D->transfer(In, *F->getAs<OriginFlowFact>());
170 return D->transfer(In, *F->getAs<ReturnOfOriginFact>());
171 case Fact::Kind::Use:
172 return D->transfer(In, *F->getAs<UseFact>());
174 return D->transfer(In, *F->getAs<TestPointFact>());
175 }
176 llvm_unreachable("Unknown fact kind");
177 }
178
179public:
180 Lattice transfer(Lattice In, const IssueFact &) { return In; }
181 Lattice transfer(Lattice In, const ExpireFact &) { return In; }
182 Lattice transfer(Lattice In, const OriginFlowFact &) { return In; }
183 Lattice transfer(Lattice In, const ReturnOfOriginFact &) { return In; }
184 Lattice transfer(Lattice In, const UseFact &) { return In; }
185 Lattice transfer(Lattice In, const TestPointFact &) { return In; }
186};
187} // namespace clang::lifetimes::internal
188#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
Definition CFG.h:605
succ_range succs()
Definition CFG.h:1000
void dump() const
Definition CFG.cpp:6258
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
DataflowAnalysis(const CFG &Cfg, AnalysisDeclContext &AC, FactManager &FactMgr)
Definition Dataflow.h:80
std::optional< Lattice > getInState(const CFGBlock *B) const
Definition Dataflow.h:121
Lattice transfer(Lattice In, const OriginFlowFact &)
Definition Dataflow.h:182
Lattice transfer(Lattice In, const TestPointFact &)
Definition Dataflow.h:185
Lattice getOutState(const CFGBlock *B) const
Definition Dataflow.h:128
Lattice transfer(Lattice In, const ReturnOfOriginFact &)
Definition Dataflow.h:183
Lattice transfer(Lattice In, const UseFact &)
Definition Dataflow.h:184
Lattice transfer(Lattice In, const IssueFact &)
Definition Dataflow.h:180
Lattice transfer(Lattice In, const ExpireFact &)
Definition Dataflow.h:181
DataflowAnalysis< Derived, Lattice, Dir > Base
Definition Dataflow.h:60
Lattice getState(ProgramPoint P) const
Definition Dataflow.h:119
llvm::ArrayRef< const Fact * > getFacts(const CFGBlock *B) const
Definition Facts.h:187
An abstract base class for a single, atomic lifetime-relevant event.
Definition Facts.h:27
@ TestPoint
A marker for a specific point in the code, for testing.
Definition Facts.h:46
@ Expire
A loan expires as its underlying storage is freed (e.g., variable goes out of scope).
Definition Facts.h:35
@ ReturnOfOrigin
An origin escapes the function by flowing into the return value.
Definition Facts.h:42
@ Issue
A new loan is issued from a borrow expression (e.g., &x).
Definition Facts.h:32
@ OriginFlow
An origin is propagated from a source to a destination (e.g., p = q).
Definition Facts.h:40
@ Use
An origin is used (eg. appears as l-value expression like DeclRefExpr).
Definition Facts.h:44
A dummy-fact used to mark a specific point in the code for testing.
Definition Facts.h:170
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:74
A worklist implementation for backward dataflow analysis.
A worklist implementation for forward dataflow analysis.