Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 1 | //=-- 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 Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 19 | #include <vector> |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 20 | #include <list> |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | |
| 25 | static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) { |
| 26 | return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P); |
| 27 | } |
| 28 | |
| 29 | void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) { |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 30 | |
| 31 | assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 32 | assert (!getFlag()); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 34 | if (getKind() == Size1) { |
| 35 | if (ExplodedNodeImpl* NOld = getNode()) { |
| 36 | std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>(); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 37 | assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 38 | V->push_back(NOld); |
| 39 | V->push_back(N); |
Ted Kremenek | 45c63bd | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 40 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 41 | assert (getPtr() == (void*) V); |
| 42 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 43 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 44 | else { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 45 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 46 | assert (getKind() == Size1); |
| 47 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 48 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 49 | else { |
| 50 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 51 | getVector(getPtr()).push_back(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 52 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 55 | |
| 56 | unsigned ExplodedNodeImpl::NodeGroup::size() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 57 | if (getFlag()) |
| 58 | return 0; |
| 59 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 60 | if (getKind() == Size1) |
| 61 | return getNode() ? 1 : 0; |
| 62 | else |
| 63 | return getVector(getPtr()).size(); |
| 64 | } |
| 65 | |
| 66 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 67 | if (getFlag()) |
| 68 | return NULL; |
| 69 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 70 | if (getKind() == Size1) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 71 | return (ExplodedNodeImpl**) (getPtr() ? &P : NULL); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 72 | else |
| 73 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin())); |
| 74 | } |
| 75 | |
| 76 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 77 | if (getFlag()) |
| 78 | return NULL; |
| 79 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 80 | if (getKind() == Size1) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 81 | return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 82 | else |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 83 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).end())); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ExplodedNodeImpl::NodeGroup::~NodeGroup() { |
| 87 | if (getKind() == SizeOther) delete &getVector(getPtr()); |
| 88 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 89 | |
| 90 | ExplodedGraphImpl* ExplodedGraphImpl::Trim(ExplodedNodeImpl** BeginSources, |
| 91 | ExplodedNodeImpl** EndSources) const{ |
| 92 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 93 | typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass1Ty; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 94 | 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 Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 105 | std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 106 | |
| 107 | for (ExplodedNodeImpl** I = BeginSources; I != EndSources; ++I) |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 108 | WL1.push_back(std::make_pair(*I, *I)); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 109 | |
| 110 | // Process the worklist. |
| 111 | |
| 112 | while (!WL1.empty()) { |
| 113 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 114 | ExplodedNodeImpl* N = WL1.back().first; |
| 115 | ExplodedNodeImpl* Src = WL1.back().second; |
| 116 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 117 | WL1.pop_back(); |
| 118 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 119 | if (Pass1.find(N) != Pass1.end()) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 120 | continue; |
| 121 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 122 | 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 Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 141 | 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 Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 157 | } |
| 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 Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame^] | 214 | Pass1Ty::iterator pi = Pass1.find(*I); |
| 215 | |
| 216 | if (pi == Pass1.end() || pi->second == NULL) |
| 217 | continue; |
| 218 | |
| 219 | WL2.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (N->isSink()) |
| 223 | NewN->markAsSink(); |
| 224 | } |
| 225 | |
| 226 | return G; |
| 227 | } |