blob: 92c062092d9f9fb47a4269c3c885a9c6aeaf97cf [file] [log] [blame]
Ted Kremenekef27b4b2008-01-14 23:24:37 +00001//==- GREngine.cpp - Path-Sensitive Dataflow Engine ----------------*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a generic engine for intraprocedural, path-sensitive,
11// dataflow analysis via graph reachability engine.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/GREngine.h"
16#include "clang/AST/Expr.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Casting.h"
19#include "llvm/ADT/DenseMap.h"
20#include <vector>
21
22using llvm::cast;
23using llvm::isa;
24using namespace clang;
25
26namespace {
27 class VISIBILITY_HIDDEN DFS : public GRWorkList {
28 llvm::SmallVector<GRWorkListUnit,20> Stack;
29public:
30 virtual bool hasWork() const {
31 return !Stack.empty();
32 }
33
34 virtual void Enqueue(const GRWorkListUnit& U) {
35 Stack.push_back(U);
36 }
37
38 virtual GRWorkListUnit Dequeue() {
39 assert (!Stack.empty());
40 const GRWorkListUnit& U = Stack.back();
41 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
42 return U;
43 }
44};
45} // end anonymous namespace
46
47GRWorkList* GRWorkList::MakeDFS() { return new DFS(); }
48
49/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
50bool GREngineImpl::ExecuteWorkList(unsigned Steps) {
51
52 if (G->num_roots() == 0) { // Initialize the analysis by constructing
53 // the root if none exists.
54
55 CFGBlock* Entry = &cfg.getEntry();
56
57 assert (Entry->empty() &&
58 "Entry block must be empty.");
59
60 assert (Entry->succ_size() == 1 &&
61 "Entry block must have 1 successor.");
62
63 // Get the solitary successor.
64 CFGBlock* Succ = *(Entry->succ_begin());
65
66 // Construct an edge representing the
67 // starting location in the function.
68 BlockEdge StartLoc(cfg, Entry, Succ);
69
70 // Generate the root.
71 GenerateNode(StartLoc, getInitialState());
72 }
73
74 while (Steps && WList->hasWork()) {
75 --Steps;
76 const GRWorkListUnit& WU = WList->Dequeue();
77 ExplodedNodeImpl* Node = WU.getNode();
78
79 // Dispatch on the location type.
80 switch (Node->getLocation().getKind()) {
81 default:
82 assert (isa<BlockEdge>(Node->getLocation()));
83 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
84 break;
85
86 case ProgramPoint::BlockEntranceKind:
87 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
88 break;
89
90 case ProgramPoint::BlockExitKind:
Ted Kremenek3226a652008-01-15 00:24:08 +000091 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenekef27b4b2008-01-14 23:24:37 +000092 break;
93
94 case ProgramPoint::PostStmtKind:
95 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
96 WU.getIndex(), Node);
97 break;
98 }
99 }
100
101 return WList->hasWork();
102}
103
104void GREngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) {
105
106 CFGBlock* Blk = L.getDst();
107
108 // Check if we are entering the EXIT block.
109 if (Blk == &cfg.getExit()) {
110
111 assert (cfg.getExit().size() == 0 && "EXIT block cannot contain Stmts.");
112
113 // Process the final state transition.
114 void* State = ProcessEOP(Blk, Pred->State);
115
116 bool IsNew;
117 ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew);
118 Node->addPredecessor(Pred);
119
120 // If the node was freshly created, mark it as an "End-Of-Path" node.
121 if (IsNew) G->addEndOfPath(Node);
122
123 // This path is done. Don't enqueue any more nodes.
124 return;
125 }
126
127 // FIXME: we will dispatch to a function that
128 // manipulates the state at the entrance to a block.
129
Ted Kremenek3226a652008-01-15 00:24:08 +0000130 GenerateNode(BlockEntrance(Blk), Pred->State, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000131}
132
133void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L,
134 ExplodedNodeImpl* Pred) {
135
136 if (Stmt* S = L.getFirstStmt()) {
137 GRNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this);
138 ProcessStmt(S, Builder);
139 }
Ted Kremenek3226a652008-01-15 00:24:08 +0000140 else
141 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000142}
143
144
Ted Kremenek3226a652008-01-15 00:24:08 +0000145void GREngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) {
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000146
147 if (Stmt* Terminator = B->getTerminator())
148 ProcessTerminator(Terminator, B, Pred);
149 else {
150 assert (B->succ_size() == 1 &&
151 "Blocks with no terminator should have at most 1 successor.");
152
153 GenerateNode(BlockEdge(cfg,B,*(B->succ_begin())), Pred->State, Pred);
154 }
155}
156
157void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B,
158 unsigned StmtIdx, ExplodedNodeImpl* Pred) {
159
160 assert (!B->empty());
161
Ted Kremenek3226a652008-01-15 00:24:08 +0000162 if (StmtIdx == B->size())
163 HandleBlockExit(B, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000164 else {
165 GRNodeBuilderImpl Builder(B, StmtIdx, Pred, this);
166 ProcessStmt(L.getStmt(), Builder);
167 }
168}
169
170typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy;
171/// PopulateParentMap - Recurse the AST starting at 'Parent' and add the
172/// mappings between child and parent to ParentMap.
173static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) {
174 for (Stmt::child_iterator I=Parent->child_begin(),
175 E=Parent->child_end(); I!=E; ++I) {
176
177 assert (M.find(*I) == M.end());
178 M[*I] = Parent;
179 PopulateParentMap(*I, M);
180 }
181}
182
183/// GenerateNode - Utility method to generate nodes, hook up successors,
184/// and add nodes to the worklist.
185void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State,
186 ExplodedNodeImpl* Pred) {
187
188 bool IsNew;
189 ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew);
190
191 if (Pred)
192 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
193 else {
194 assert (IsNew);
195 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
196 }
197
198 // Only add 'Node' to the worklist if it was freshly generated.
199 if (IsNew) WList->Enqueue(GRWorkListUnit(Node));
200}
201
202GRNodeBuilderImpl::GRNodeBuilderImpl(CFGBlock* b, unsigned idx,
203 ExplodedNodeImpl* N, GREngineImpl* e)
204 : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) {
205 Deferred.insert(N);
206}
207
208GRNodeBuilderImpl::~GRNodeBuilderImpl() {
209 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
210 if (!(*I)->isInfeasible())
211 GenerateAutoTransition(*I);
212}
213
214void GRNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) {
215 assert (!N->isInfeasible());
216
217 PostStmt Loc(getStmt());
218
219 if (Loc == N->getLocation()) {
220 // Note: 'N' should be a fresh node because otherwise it shouldn't be
221 // a member of Deferred.
222 Eng.WList->Enqueue(N, B, Idx+1);
223 return;
224 }
225
226 bool IsNew;
227 ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew);
228 Succ->addPredecessor(N);
229
230 if (IsNew)
231 Eng.WList->Enqueue(Succ, B, Idx+1);
232}
233
234ExplodedNodeImpl* GRNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State,
235 ExplodedNodeImpl* Pred) {
236
237 bool IsNew;
238 ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew);
239 N->addPredecessor(Pred);
240 Deferred.erase(Pred);
241
242 HasGeneratedNode = true;
243
244 if (IsNew) {
245 Deferred.insert(N);
246 LastNode = N;
247 return N;
248 }
249
250 LastNode = NULL;
251 return NULL;
252}