blob: 2ba46d77d63d57b146bb8ac28914d8eda095b28b [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 Kremenekffe0f432008-03-07 22:58:01 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallVector.h"
Ted Kremenek9eb49a42008-01-13 04:56:13 +000019#include <vector>
Ted Kremenek7ec07fd2008-03-12 17:18:20 +000020#include <list>
Ted Kremenek9eb49a42008-01-13 04:56:13 +000021
22using namespace clang;
23
24
25static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
26 return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
27}
28
29void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek596f0a12008-03-05 19:08:55 +000030
31 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenekffe0f432008-03-07 22:58:01 +000032 assert (!getFlag());
Ted Kremenek596f0a12008-03-05 19:08:55 +000033
Ted Kremenek9eb49a42008-01-13 04:56:13 +000034 if (getKind() == Size1) {
35 if (ExplodedNodeImpl* NOld = getNode()) {
36 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek596f0a12008-03-05 19:08:55 +000037 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000038 V->push_back(NOld);
39 V->push_back(N);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000040 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek596f0a12008-03-05 19:08:55 +000041 assert (getPtr() == (void*) V);
42 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000043 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000044 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000045 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000046 assert (getKind() == Size1);
47 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000048 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000049 else {
50 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000051 getVector(getPtr()).push_back(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000052 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000053}
54
Ted Kremenek9eb49a42008-01-13 04:56:13 +000055
56unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000057 if (getFlag())
58 return 0;
59
Ted Kremenek9eb49a42008-01-13 04:56:13 +000060 if (getKind() == Size1)
61 return getNode() ? 1 : 0;
62 else
63 return getVector(getPtr()).size();
64}
65
66ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000067 if (getFlag())
68 return NULL;
69
Ted Kremenek9eb49a42008-01-13 04:56:13 +000070 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +000071 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000072 else
73 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
74}
75
76ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000077 if (getFlag())
78 return NULL;
79
Ted Kremenek9eb49a42008-01-13 04:56:13 +000080 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +000081 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000082 else
Ted Kremenek596f0a12008-03-05 19:08:55 +000083 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).end()));
Ted Kremenek9eb49a42008-01-13 04:56:13 +000084}
85
86ExplodedNodeImpl::NodeGroup::~NodeGroup() {
87 if (getKind() == SizeOther) delete &getVector(getPtr());
88}
Ted Kremenekffe0f432008-03-07 22:58:01 +000089
90ExplodedGraphImpl* ExplodedGraphImpl::Trim(ExplodedNodeImpl** BeginSources,
91 ExplodedNodeImpl** EndSources) const{
92
Ted Kremenek7ec07fd2008-03-12 17:18:20 +000093 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +000094 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
95
96 Pass1Ty Pass1;
97 Pass2Ty Pass2;
98
99 llvm::SmallVector<ExplodedNodeImpl*, 10> WL2;
100
101 { // ===- Pass 1 (reverse BFS) -===
102
103 // Enqueue the source nodes to the first worklist.
104
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000105 std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000106
107 for (ExplodedNodeImpl** I = BeginSources; I != EndSources; ++I)
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000108 WL1.push_back(std::make_pair(*I, *I));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000109
110 // Process the worklist.
111
112 while (!WL1.empty()) {
113
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000114 ExplodedNodeImpl* N = WL1.back().first;
115 ExplodedNodeImpl* Src = WL1.back().second;
116
Ted Kremenekffe0f432008-03-07 22:58:01 +0000117 WL1.pop_back();
118
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000119 if (Pass1.find(N) != Pass1.end())
Ted Kremenekffe0f432008-03-07 22:58:01 +0000120 continue;
121
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000122 bool PredHasSameSource = false;
123 bool VisitPreds = true;
124
125 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
126 I!=E; ++I) {
127
128 Pass1Ty::iterator pi = Pass1.find(*I);
129
130 if (pi == Pass1.end())
131 continue;
132
133 VisitPreds = false;
134
135 if (pi->second == Src) {
136 PredHasSameSource = true;
137 break;
138 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000139 }
140
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000141 if (VisitPreds || !PredHasSameSource) {
142
143 Pass1[N] = Src;
144
145 if (N->Preds.empty()) {
146 WL2.push_back(N);
147 continue;
148 }
149 }
150 else
151 Pass1[N] = NULL;
152
153 if (VisitPreds)
154 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
155 I!=E; ++I)
156 WL1.push_front(std::make_pair(*I, Src));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000157 }
158 }
159
160 if (WL2.empty())
161 return NULL;
162
163 ExplodedGraphImpl* G = MakeEmptyGraph();
164
165 // ===- Pass 2 (forward DFS to construct the new graph) -===
166
167 while (!WL2.empty()) {
168
169 ExplodedNodeImpl* N = WL2.back();
170 WL2.pop_back();
171
172 // Skip this node if we have already processed it.
173
174 if (Pass2.find(N) != Pass2.end())
175 continue;
176
177 // Create the corresponding node in the new graph.
178
179 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
180 Pass2[N] = NewN;
181
182 if (N->Preds.empty())
183 G->addRoot(NewN);
184
185 // In the case that some of the intended predecessors of NewN have already
186 // been created, we should hook them up as predecessors.
187
188 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
189
190 Pass2Ty::iterator PI = Pass2.find(*I);
191
192 if (PI == Pass2.end())
193 continue;
194
195 NewN->addPredecessor(PI->second);
196 }
197
198 // In the case that some of the intended successors of NewN have already
199 // been created, we should hook them up as successors. Otherwise, enqueue
200 // the new nodes from the original graph that should have nodes created
201 // in the new graph.
202
203 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
204
205 Pass2Ty::iterator PI = Pass2.find(*I);
206
207 if (PI != Pass2.end()) {
208 PI->second->addPredecessor(NewN);
209 continue;
210 }
211
212 // Enqueue nodes to the worklist that were marked during pass 1.
213
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000214 Pass1Ty::iterator pi = Pass1.find(*I);
215
216 if (pi == Pass1.end() || pi->second == NULL)
217 continue;
218
219 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000220 }
221
222 if (N->isSink())
223 NewN->markAsSink();
224 }
225
226 return G;
227}