Ted Kremenek | f7c3bec | 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 | 5e1e05c | 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 | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | |
| 24 | static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) { |
| 25 | return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P); |
| 26 | } |
| 27 | |
| 28 | void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) { |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 29 | |
| 30 | assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 31 | assert (!getFlag()); |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 33 | if (getKind() == Size1) { |
| 34 | if (ExplodedNodeImpl* NOld = getNode()) { |
| 35 | std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>(); |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 36 | assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 37 | V->push_back(NOld); |
| 38 | V->push_back(N); |
Ted Kremenek | 4ecd9bb | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 39 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 40 | assert (getPtr() == (void*) V); |
| 41 | assert (getKind() == SizeOther); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 42 | } |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 43 | else { |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 44 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 45 | assert (getKind() == Size1); |
| 46 | } |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 47 | } |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 48 | else { |
| 49 | assert (getKind() == SizeOther); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 50 | getVector(getPtr()).push_back(N); |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 51 | } |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 54 | |
| 55 | unsigned ExplodedNodeImpl::NodeGroup::size() const { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 56 | if (getFlag()) |
| 57 | return 0; |
| 58 | |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 59 | if (getKind() == Size1) |
| 60 | return getNode() ? 1 : 0; |
| 61 | else |
| 62 | return getVector(getPtr()).size(); |
| 63 | } |
| 64 | |
| 65 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 66 | if (getFlag()) |
| 67 | return NULL; |
| 68 | |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 69 | if (getKind() == Size1) |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 70 | return (ExplodedNodeImpl**) (getPtr() ? &P : NULL); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 71 | else |
| 72 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin())); |
| 73 | } |
| 74 | |
| 75 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 76 | if (getFlag()) |
| 77 | return NULL; |
| 78 | |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 79 | if (getKind() == Size1) |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 80 | return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 81 | else |
Ted Kremenek | 399e901 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 82 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).end())); |
Ted Kremenek | f7c3bec | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | ExplodedNodeImpl::NodeGroup::~NodeGroup() { |
| 86 | if (getKind() == SizeOther) delete &getVector(getPtr()); |
| 87 | } |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame^] | 88 | |
| 89 | ExplodedGraphImpl* 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 | } |