blob: 421b9e3846b6723cf15df48ebf5fe61563469d1b [file] [log] [blame]
Ted Kremenekf7c3bec2008-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 Kremenek5e1e05c2008-03-07 22:58:01 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallVector.h"
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000019#include <vector>
20
21using namespace clang;
22
23
24static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
25 return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
26}
27
28void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek399e9012008-03-05 19:08:55 +000029
30 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000031 assert (!getFlag());
Ted Kremenek399e9012008-03-05 19:08:55 +000032
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000033 if (getKind() == Size1) {
34 if (ExplodedNodeImpl* NOld = getNode()) {
35 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek399e9012008-03-05 19:08:55 +000036 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000037 V->push_back(NOld);
38 V->push_back(N);
Ted Kremenek4ecd9bb2008-01-29 23:31:09 +000039 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek399e9012008-03-05 19:08:55 +000040 assert (getPtr() == (void*) V);
41 assert (getKind() == SizeOther);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000042 }
Ted Kremenek399e9012008-03-05 19:08:55 +000043 else {
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000044 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek399e9012008-03-05 19:08:55 +000045 assert (getKind() == Size1);
46 }
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000047 }
Ted Kremenek399e9012008-03-05 19:08:55 +000048 else {
49 assert (getKind() == SizeOther);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000050 getVector(getPtr()).push_back(N);
Ted Kremenek399e9012008-03-05 19:08:55 +000051 }
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000052}
53
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000054
55unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000056 if (getFlag())
57 return 0;
58
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000059 if (getKind() == Size1)
60 return getNode() ? 1 : 0;
61 else
62 return getVector(getPtr()).size();
63}
64
65ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000066 if (getFlag())
67 return NULL;
68
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000069 if (getKind() == Size1)
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000070 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000071 else
72 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
73}
74
75ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000076 if (getFlag())
77 return NULL;
78
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000079 if (getKind() == Size1)
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000080 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000081 else
Ted Kremenek399e9012008-03-05 19:08:55 +000082 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).end()));
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000083}
84
85ExplodedNodeImpl::NodeGroup::~NodeGroup() {
86 if (getKind() == SizeOther) delete &getVector(getPtr());
87}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000088
89ExplodedGraphImpl* ExplodedGraphImpl::Trim(ExplodedNodeImpl** BeginSources,
90 ExplodedNodeImpl** EndSources) const{
91
92 typedef llvm::DenseSet<ExplodedNodeImpl*> Pass1Ty;
93 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
94
95 Pass1Ty Pass1;
96 Pass2Ty Pass2;
97
98 llvm::SmallVector<ExplodedNodeImpl*, 10> WL2;
99
100 { // ===- Pass 1 (reverse BFS) -===
101
102 // Enqueue the source nodes to the first worklist.
103
104 llvm::SmallVector<ExplodedNodeImpl*, 10> WL1;
105
106 for (ExplodedNodeImpl** I = BeginSources; I != EndSources; ++I)
107 WL1.push_back(*I);
108
109 // Process the worklist.
110
111 while (!WL1.empty()) {
112
113 ExplodedNodeImpl* N = WL1.back();
114 WL1.pop_back();
115
116 if (Pass1.count(N))
117 continue;
118
119 Pass1.insert(N);
120
121 if (N->Preds.empty()) {
122 WL2.push_back(N);
123 continue;
124 }
125
126 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
127 WL1.push_back(*I);
128 }
129 }
130
131 if (WL2.empty())
132 return NULL;
133
134 ExplodedGraphImpl* G = MakeEmptyGraph();
135
136 // ===- Pass 2 (forward DFS to construct the new graph) -===
137
138 while (!WL2.empty()) {
139
140 ExplodedNodeImpl* N = WL2.back();
141 WL2.pop_back();
142
143 // Skip this node if we have already processed it.
144
145 if (Pass2.find(N) != Pass2.end())
146 continue;
147
148 // Create the corresponding node in the new graph.
149
150 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
151 Pass2[N] = NewN;
152
153 if (N->Preds.empty())
154 G->addRoot(NewN);
155
156 // In the case that some of the intended predecessors of NewN have already
157 // been created, we should hook them up as predecessors.
158
159 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
160
161 Pass2Ty::iterator PI = Pass2.find(*I);
162
163 if (PI == Pass2.end())
164 continue;
165
166 NewN->addPredecessor(PI->second);
167 }
168
169 // In the case that some of the intended successors of NewN have already
170 // been created, we should hook them up as successors. Otherwise, enqueue
171 // the new nodes from the original graph that should have nodes created
172 // in the new graph.
173
174 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
175
176 Pass2Ty::iterator PI = Pass2.find(*I);
177
178 if (PI != Pass2.end()) {
179 PI->second->addPredecessor(NewN);
180 continue;
181 }
182
183 // Enqueue nodes to the worklist that were marked during pass 1.
184
185 if (Pass1.count(*I))
186 WL2.push_back(*I);
187 }
188
189 if (N->isSink())
190 NewN->markAsSink();
191 }
192
193 return G;
194}