blob: 0835944250fdfcd489ad536c27462a59e5c3190e [file] [log] [blame]
Ted Kremenek9eb49a42008-01-13 04:56:13 +00001//=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- 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 the template classes ExplodedNode and ExplodedGraph,
11// which represent a path-sensitive, intra-procedural "exploded graph."
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
Ted Kremenek33c63692008-04-16 15:51:26 +000016#include "clang/AST/Stmt.h"
Ted Kremenekffe0f432008-03-07 22:58:01 +000017#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
Ted Kremenek9eb49a42008-01-13 04:56:13 +000020#include <vector>
Ted Kremenek7ec07fd2008-03-12 17:18:20 +000021#include <list>
Ted Kremenek9eb49a42008-01-13 04:56:13 +000022
23using namespace clang;
24
25
26static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
27 return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
28}
29
Ted Kremenek45b87892008-08-27 01:27:52 +000030void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) {
31 assert (!V->isSink());
32 Preds.addNode(V);
33 V->Succs.addNode(this);
34}
35
Ted Kremenek9eb49a42008-01-13 04:56:13 +000036void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek596f0a12008-03-05 19:08:55 +000037
38 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenekffe0f432008-03-07 22:58:01 +000039 assert (!getFlag());
Ted Kremenek596f0a12008-03-05 19:08:55 +000040
Ted Kremenek9eb49a42008-01-13 04:56:13 +000041 if (getKind() == Size1) {
42 if (ExplodedNodeImpl* NOld = getNode()) {
43 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek596f0a12008-03-05 19:08:55 +000044 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000045 V->push_back(NOld);
46 V->push_back(N);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000047 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek596f0a12008-03-05 19:08:55 +000048 assert (getPtr() == (void*) V);
49 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000050 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000051 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000052 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000053 assert (getKind() == Size1);
54 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000055 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000056 else {
57 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000058 getVector(getPtr()).push_back(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000059 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000060}
61
Ted Kremenek9eb49a42008-01-13 04:56:13 +000062
63unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000064 if (getFlag())
65 return 0;
66
Ted Kremenek9eb49a42008-01-13 04:56:13 +000067 if (getKind() == Size1)
68 return getNode() ? 1 : 0;
69 else
70 return getVector(getPtr()).size();
71}
72
73ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000074 if (getFlag())
75 return NULL;
76
Ted Kremenek9eb49a42008-01-13 04:56:13 +000077 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +000078 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000079 else
80 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
81}
82
83ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000084 if (getFlag())
85 return NULL;
86
Ted Kremenek9eb49a42008-01-13 04:56:13 +000087 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +000088 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +000089 else {
90 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-04-22 07:37:18 +000091 // we can dereference the last elem and then add 1 to the result.
92 return const_cast<ExplodedNodeImpl**>(&getVector(getPtr()).back()) + 1;
Ted Kremenek7b989572008-04-20 23:54:24 +000093 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000094}
95
96ExplodedNodeImpl::NodeGroup::~NodeGroup() {
97 if (getKind() == SizeOther) delete &getVector(getPtr());
98}
Ted Kremenekffe0f432008-03-07 22:58:01 +000099
100ExplodedGraphImpl* ExplodedGraphImpl::Trim(ExplodedNodeImpl** BeginSources,
101 ExplodedNodeImpl** EndSources) const{
102
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000103 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000104 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
105
106 Pass1Ty Pass1;
107 Pass2Ty Pass2;
108
109 llvm::SmallVector<ExplodedNodeImpl*, 10> WL2;
110
111 { // ===- Pass 1 (reverse BFS) -===
112
113 // Enqueue the source nodes to the first worklist.
114
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000115 std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1;
Ted Kremenek33c63692008-04-16 15:51:26 +0000116 std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1_Loops;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000117
118 for (ExplodedNodeImpl** I = BeginSources; I != EndSources; ++I)
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000119 WL1.push_back(std::make_pair(*I, *I));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000120
121 // Process the worklist.
122
Ted Kremenek33c63692008-04-16 15:51:26 +0000123 while (! (WL1.empty() && WL1_Loops.empty())) {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000124
Ted Kremenek33c63692008-04-16 15:51:26 +0000125 ExplodedNodeImpl *N, *Src;
126
127 // Only dequeue from the "loops" worklist if WL1 has no items.
128 // Thus we prioritize for paths that don't span loop boundaries.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000129
Ted Kremenek33c63692008-04-16 15:51:26 +0000130 if (WL1.empty()) {
131 N = WL1_Loops.back().first;
132 Src = WL1_Loops.back().second;
133 WL1_Loops.pop_back();
134 }
135 else {
136 N = WL1.back().first;
137 Src = WL1.back().second;
138 WL1.pop_back();
139 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000140
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000141 if (Pass1.find(N) != Pass1.end())
Ted Kremenekffe0f432008-03-07 22:58:01 +0000142 continue;
143
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000144 bool PredHasSameSource = false;
145 bool VisitPreds = true;
146
147 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
148 I!=E; ++I) {
149
150 Pass1Ty::iterator pi = Pass1.find(*I);
151
152 if (pi == Pass1.end())
153 continue;
154
155 VisitPreds = false;
156
157 if (pi->second == Src) {
158 PredHasSameSource = true;
159 break;
160 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000161 }
162
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000163 if (VisitPreds || !PredHasSameSource) {
164
165 Pass1[N] = Src;
166
167 if (N->Preds.empty()) {
168 WL2.push_back(N);
169 continue;
170 }
171 }
172 else
173 Pass1[N] = NULL;
174
175 if (VisitPreds)
176 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
Ted Kremenek33c63692008-04-16 15:51:26 +0000177 I!=E; ++I) {
178
179 ProgramPoint P = Src->getLocation();
180
181 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
182 if (Stmt* T = BE->getSrc()->getTerminator())
183 switch (T->getStmtClass()) {
184 default: break;
185 case Stmt::ForStmtClass:
186 case Stmt::WhileStmtClass:
187 case Stmt::DoStmtClass:
188 WL1_Loops.push_front(std::make_pair(*I, Src));
189 continue;
190
191 }
192
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000193 WL1.push_front(std::make_pair(*I, Src));
Ted Kremenek33c63692008-04-16 15:51:26 +0000194 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000195 }
196 }
197
198 if (WL2.empty())
199 return NULL;
200
201 ExplodedGraphImpl* G = MakeEmptyGraph();
202
203 // ===- Pass 2 (forward DFS to construct the new graph) -===
204
205 while (!WL2.empty()) {
206
207 ExplodedNodeImpl* N = WL2.back();
208 WL2.pop_back();
209
210 // Skip this node if we have already processed it.
211
212 if (Pass2.find(N) != Pass2.end())
213 continue;
214
215 // Create the corresponding node in the new graph.
216
217 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
218 Pass2[N] = NewN;
219
220 if (N->Preds.empty())
221 G->addRoot(NewN);
222
223 // In the case that some of the intended predecessors of NewN have already
224 // been created, we should hook them up as predecessors.
225
226 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
227
228 Pass2Ty::iterator PI = Pass2.find(*I);
229
230 if (PI == Pass2.end())
231 continue;
232
233 NewN->addPredecessor(PI->second);
234 }
235
236 // In the case that some of the intended successors of NewN have already
237 // been created, we should hook them up as successors. Otherwise, enqueue
238 // the new nodes from the original graph that should have nodes created
239 // in the new graph.
240
241 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
242
243 Pass2Ty::iterator PI = Pass2.find(*I);
244
245 if (PI != Pass2.end()) {
246 PI->second->addPredecessor(NewN);
247 continue;
248 }
249
250 // Enqueue nodes to the worklist that were marked during pass 1.
251
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000252 Pass1Ty::iterator pi = Pass1.find(*I);
253
254 if (pi == Pass1.end() || pi->second == NULL)
255 continue;
256
257 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000258 }
259
260 if (N->isSink())
261 NewN->markAsSink();
262 }
263
264 return G;
265}