1fa0734ecSArgyrios Kyrtzidis //=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=// 2fa0734ecSArgyrios Kyrtzidis // 3fa0734ecSArgyrios Kyrtzidis // The LLVM Compiler Infrastructure 4fa0734ecSArgyrios Kyrtzidis // 5fa0734ecSArgyrios Kyrtzidis // This file is distributed under the University of Illinois Open Source 6fa0734ecSArgyrios Kyrtzidis // License. See LICENSE.TXT for details. 7fa0734ecSArgyrios Kyrtzidis // 8fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 9fa0734ecSArgyrios Kyrtzidis // 10fa0734ecSArgyrios Kyrtzidis // This file defines the template classes ExplodedNode and ExplodedGraph, 11fa0734ecSArgyrios Kyrtzidis // which represent a path-sensitive, intra-procedural "exploded graph." 12fa0734ecSArgyrios Kyrtzidis // 13fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 14fa0734ecSArgyrios Kyrtzidis 15f8cbac4bSTed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 163a02247dSChandler Carruth #include "clang/AST/ParentMap.h" 173a02247dSChandler Carruth #include "clang/AST/Stmt.h" 184f7df9beSJordan Rose #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 19001fd5b4STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 20fa0734ecSArgyrios Kyrtzidis #include "llvm/ADT/DenseMap.h" 213a02247dSChandler Carruth #include "llvm/ADT/DenseSet.h" 22fa0734ecSArgyrios Kyrtzidis #include "llvm/ADT/SmallVector.h" 237aa3687bSAnna Zaks #include "llvm/ADT/Statistic.h" 24fa0734ecSArgyrios Kyrtzidis #include <vector> 25fa0734ecSArgyrios Kyrtzidis 26fa0734ecSArgyrios Kyrtzidis using namespace clang; 27fa0734ecSArgyrios Kyrtzidis using namespace ento; 28fa0734ecSArgyrios Kyrtzidis 29fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 30fa0734ecSArgyrios Kyrtzidis // Node auditing. 31fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 32fa0734ecSArgyrios Kyrtzidis 33fa0734ecSArgyrios Kyrtzidis // An out of line virtual method to provide a home for the class vtable. 34fa0734ecSArgyrios Kyrtzidis ExplodedNode::Auditor::~Auditor() {} 35fa0734ecSArgyrios Kyrtzidis 36fa0734ecSArgyrios Kyrtzidis #ifndef NDEBUG 37fa0734ecSArgyrios Kyrtzidis static ExplodedNode::Auditor* NodeAuditor = 0; 38fa0734ecSArgyrios Kyrtzidis #endif 39fa0734ecSArgyrios Kyrtzidis 40fa0734ecSArgyrios Kyrtzidis void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { 41fa0734ecSArgyrios Kyrtzidis #ifndef NDEBUG 42fa0734ecSArgyrios Kyrtzidis NodeAuditor = A; 43fa0734ecSArgyrios Kyrtzidis #endif 44fa0734ecSArgyrios Kyrtzidis } 45fa0734ecSArgyrios Kyrtzidis 46fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 47a40f8ebcSTed Kremenek // Cleanup. 48a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 49a40f8ebcSTed Kremenek 5044d2973bSTed Kremenek ExplodedGraph::ExplodedGraph() 51746c06d0SJordan Rose : NumNodes(0), ReclaimNodeInterval(0) {} 5244d2973bSTed Kremenek 53a2aa929eSTed Kremenek ExplodedGraph::~ExplodedGraph() {} 54a40f8ebcSTed Kremenek 55a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 56a40f8ebcSTed Kremenek // Node reclamation. 57a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 58a40f8ebcSTed Kremenek 5904fa9e3dSTed Kremenek bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) { 6004fa9e3dSTed Kremenek if (!Ex->isLValue()) 6104fa9e3dSTed Kremenek return false; 6204fa9e3dSTed Kremenek return isa<DeclRefExpr>(Ex) || 6304fa9e3dSTed Kremenek isa<MemberExpr>(Ex) || 6404fa9e3dSTed Kremenek isa<ObjCIvarRefExpr>(Ex); 6504fa9e3dSTed Kremenek } 6604fa9e3dSTed Kremenek 671dd7fd71STed Kremenek bool ExplodedGraph::shouldCollect(const ExplodedNode *node) { 688f564058STed Kremenek // First, we only consider nodes for reclamation of the following 698f564058STed Kremenek // conditions apply: 701dd7fd71STed Kremenek // 711dd7fd71STed Kremenek // (1) 1 predecessor (that has one successor) 721dd7fd71STed Kremenek // (2) 1 successor (that has one predecessor) 738f564058STed Kremenek // 748f564058STed Kremenek // If a node has no successor it is on the "frontier", while a node 758f564058STed Kremenek // with no predecessor is a root. 768f564058STed Kremenek // 778f564058STed Kremenek // After these prerequisites, we discard all "filler" nodes that 788f564058STed Kremenek // are used only for intermediate processing, and are not essential 798f564058STed Kremenek // for analyzer history: 808f564058STed Kremenek // 818f564058STed Kremenek // (a) PreStmtPurgeDeadSymbols 828f564058STed Kremenek // 838f564058STed Kremenek // We then discard all other nodes where *all* of the following conditions 848f564058STed Kremenek // apply: 858f564058STed Kremenek // 86199fdd82SJordan Rose // (3) The ProgramPoint is for a PostStmt, but not a PostStore. 871dd7fd71STed Kremenek // (4) There is no 'tag' for the ProgramPoint. 881dd7fd71STed Kremenek // (5) The 'store' is the same as the predecessor. 891dd7fd71STed Kremenek // (6) The 'GDM' is the same as the predecessor. 901dd7fd71STed Kremenek // (7) The LocationContext is the same as the predecessor. 9196250482STed Kremenek // (8) Expressions that are *not* lvalue expressions. 9296250482STed Kremenek // (9) The PostStmt isn't for a non-consumed Stmt or Expr. 93*68a172caSAnton Yartsev // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or 94*68a172caSAnton Yartsev // PreImplicitCall (so that we would be able to find it when retrying a 95*68a172caSAnton Yartsev // call with no inlining). 96681cce99SJordan Rose // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well. 971dd7fd71STed Kremenek 981dd7fd71STed Kremenek // Conditions 1 and 2. 991dd7fd71STed Kremenek if (node->pred_size() != 1 || node->succ_size() != 1) 1001dd7fd71STed Kremenek return false; 1011dd7fd71STed Kremenek 1021dd7fd71STed Kremenek const ExplodedNode *pred = *(node->pred_begin()); 1031dd7fd71STed Kremenek if (pred->succ_size() != 1) 1041dd7fd71STed Kremenek return false; 1051dd7fd71STed Kremenek 1061dd7fd71STed Kremenek const ExplodedNode *succ = *(node->succ_begin()); 1071dd7fd71STed Kremenek if (succ->pred_size() != 1) 1081dd7fd71STed Kremenek return false; 1091dd7fd71STed Kremenek 1108f564058STed Kremenek // Now reclaim any nodes that are (by definition) not essential to 1118f564058STed Kremenek // analysis history and are not consulted by any client code. 1121dd7fd71STed Kremenek ProgramPoint progPoint = node->getLocation(); 1138f564058STed Kremenek if (progPoint.getAs<PreStmtPurgeDeadSymbols>()) 114f352d8c7STed Kremenek return !progPoint.getTag(); 1158f564058STed Kremenek 1168f564058STed Kremenek // Condition 3. 11787396b9bSDavid Blaikie if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>()) 1181dd7fd71STed Kremenek return false; 1191dd7fd71STed Kremenek 1201dd7fd71STed Kremenek // Condition 4. 12154417f6dSAnna Zaks if (progPoint.getTag()) 1221dd7fd71STed Kremenek return false; 1231dd7fd71STed Kremenek 1241dd7fd71STed Kremenek // Conditions 5, 6, and 7. 1251dd7fd71STed Kremenek ProgramStateRef state = node->getState(); 1261dd7fd71STed Kremenek ProgramStateRef pred_state = pred->getState(); 1271dd7fd71STed Kremenek if (state->store != pred_state->store || state->GDM != pred_state->GDM || 1281dd7fd71STed Kremenek progPoint.getLocationContext() != pred->getLocationContext()) 1291dd7fd71STed Kremenek return false; 1301dd7fd71STed Kremenek 13154417f6dSAnna Zaks // All further checks require expressions. As per #3, we know that we have 13254417f6dSAnna Zaks // a PostStmt. 13354417f6dSAnna Zaks const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt()); 13404fa9e3dSTed Kremenek if (!Ex) 13504fa9e3dSTed Kremenek return false; 13604fa9e3dSTed Kremenek 13704fa9e3dSTed Kremenek // Condition 8. 13804fa9e3dSTed Kremenek // Do not collect nodes for "interesting" lvalue expressions since they are 13904fa9e3dSTed Kremenek // used extensively for generating path diagnostics. 14004fa9e3dSTed Kremenek if (isInterestingLValueExpr(Ex)) 14196250482STed Kremenek return false; 14296250482STed Kremenek 14396250482STed Kremenek // Condition 9. 14467e0062bSAnna Zaks // Do not collect nodes for non-consumed Stmt or Expr to ensure precise 14567e0062bSAnna Zaks // diagnostic generation; specifically, so that we could anchor arrows 14667e0062bSAnna Zaks // pointing to the beginning of statements (as written in code). 1471dd7fd71STed Kremenek ParentMap &PM = progPoint.getLocationContext()->getParentMap(); 1481dd7fd71STed Kremenek if (!PM.isConsumedExpr(Ex)) 1491dd7fd71STed Kremenek return false; 1501dd7fd71STed Kremenek 15196250482STed Kremenek // Condition 10. 152bec49efdSAnna Zaks const ProgramPoint SuccLoc = succ->getLocation(); 15387396b9bSDavid Blaikie if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>()) 154e537cc05SJordan Rose if (CallEvent::isCallStmt(SP->getStmt())) 155bec49efdSAnna Zaks return false; 156bec49efdSAnna Zaks 157*68a172caSAnton Yartsev // Condition 10, continuation. 158*68a172caSAnton Yartsev if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>()) 159*68a172caSAnton Yartsev return false; 160*68a172caSAnton Yartsev 1611dd7fd71STed Kremenek return true; 1621dd7fd71STed Kremenek } 1631dd7fd71STed Kremenek 1641dd7fd71STed Kremenek void ExplodedGraph::collectNode(ExplodedNode *node) { 1651dd7fd71STed Kremenek // Removing a node means: 1661dd7fd71STed Kremenek // (a) changing the predecessors successor to the successor of this node 1671dd7fd71STed Kremenek // (b) changing the successors predecessor to the predecessor of this node 1681dd7fd71STed Kremenek // (c) Putting 'node' onto freeNodes. 1691dd7fd71STed Kremenek assert(node->pred_size() == 1 || node->succ_size() == 1); 1701dd7fd71STed Kremenek ExplodedNode *pred = *(node->pred_begin()); 1711dd7fd71STed Kremenek ExplodedNode *succ = *(node->succ_begin()); 1721dd7fd71STed Kremenek pred->replaceSuccessor(succ); 1731dd7fd71STed Kremenek succ->replacePredecessor(pred); 174a2aa929eSTed Kremenek FreeNodes.push_back(node); 1751dd7fd71STed Kremenek Nodes.RemoveNode(node); 1761dd7fd71STed Kremenek --NumNodes; 1771dd7fd71STed Kremenek node->~ExplodedNode(); 1781dd7fd71STed Kremenek } 1791dd7fd71STed Kremenek 18035e55fe4STed Kremenek void ExplodedGraph::reclaimRecentlyAllocatedNodes() { 181a2aa929eSTed Kremenek if (ChangedNodes.empty()) 182a40f8ebcSTed Kremenek return; 18344d2973bSTed Kremenek 184746c06d0SJordan Rose // Only periodically reclaim nodes so that we can build up a set of 18535e55fe4STed Kremenek // nodes that meet the reclamation criteria. Freshly created nodes 18635e55fe4STed Kremenek // by definition have no successor, and thus cannot be reclaimed (see below). 187746c06d0SJordan Rose assert(ReclaimCounter > 0); 188746c06d0SJordan Rose if (--ReclaimCounter != 0) 18935e55fe4STed Kremenek return; 190746c06d0SJordan Rose ReclaimCounter = ReclaimNodeInterval; 19135e55fe4STed Kremenek 19235e55fe4STed Kremenek for (NodeVector::iterator it = ChangedNodes.begin(), et = ChangedNodes.end(); 193a2aa929eSTed Kremenek it != et; ++it) { 194a2aa929eSTed Kremenek ExplodedNode *node = *it; 1951dd7fd71STed Kremenek if (shouldCollect(node)) 1961dd7fd71STed Kremenek collectNode(node); 197a40f8ebcSTed Kremenek } 198a2aa929eSTed Kremenek ChangedNodes.clear(); 199a40f8ebcSTed Kremenek } 200a40f8ebcSTed Kremenek 201a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 202fa0734ecSArgyrios Kyrtzidis // ExplodedNode. 203fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 204fa0734ecSArgyrios Kyrtzidis 2052b10f3f8SJordan Rose // An NodeGroup's storage type is actually very much like a TinyPtrVector: 2062b10f3f8SJordan Rose // it can be either a pointer to a single ExplodedNode, or a pointer to a 2072b10f3f8SJordan Rose // BumpVector allocated with the ExplodedGraph's allocator. This allows the 2082b10f3f8SJordan Rose // common case of single-node NodeGroups to be implemented with no extra memory. 2092b10f3f8SJordan Rose // 2102b10f3f8SJordan Rose // Consequently, each of the NodeGroup methods have up to four cases to handle: 2112b10f3f8SJordan Rose // 1. The flag is set and this group does not actually contain any nodes. 2122b10f3f8SJordan Rose // 2. The group is empty, in which case the storage value is null. 2132b10f3f8SJordan Rose // 3. The group contains a single node. 2142b10f3f8SJordan Rose // 4. The group contains more than one node. 21580547386SJordan Rose typedef BumpVector<ExplodedNode *> ExplodedNodeVector; 21680547386SJordan Rose typedef llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *> GroupStorage; 217fa0734ecSArgyrios Kyrtzidis 218fa0734ecSArgyrios Kyrtzidis void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { 219fa0734ecSArgyrios Kyrtzidis assert (!V->isSink()); 220fa0734ecSArgyrios Kyrtzidis Preds.addNode(V, G); 221fa0734ecSArgyrios Kyrtzidis V->Succs.addNode(this, G); 222fa0734ecSArgyrios Kyrtzidis #ifndef NDEBUG 223fa0734ecSArgyrios Kyrtzidis if (NodeAuditor) NodeAuditor->AddEdge(V, this); 224fa0734ecSArgyrios Kyrtzidis #endif 225fa0734ecSArgyrios Kyrtzidis } 226fa0734ecSArgyrios Kyrtzidis 227a40f8ebcSTed Kremenek void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { 2282b10f3f8SJordan Rose assert(!getFlag()); 2292b10f3f8SJordan Rose 23080547386SJordan Rose GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); 23180547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 23280547386SJordan Rose Storage = node; 23380547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 234a40f8ebcSTed Kremenek } 235a40f8ebcSTed Kremenek 236fa0734ecSArgyrios Kyrtzidis void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { 237fa0734ecSArgyrios Kyrtzidis assert(!getFlag()); 238fa0734ecSArgyrios Kyrtzidis 23980547386SJordan Rose GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); 24080547386SJordan Rose if (Storage.isNull()) { 24180547386SJordan Rose Storage = N; 24280547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 24380547386SJordan Rose return; 24480547386SJordan Rose } 245fa0734ecSArgyrios Kyrtzidis 24680547386SJordan Rose ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>(); 24780547386SJordan Rose 24880547386SJordan Rose if (!V) { 24980547386SJordan Rose // Switch from single-node to multi-node representation. 25080547386SJordan Rose ExplodedNode *Old = Storage.get<ExplodedNode *>(); 25180547386SJordan Rose 25280547386SJordan Rose BumpVectorContext &Ctx = G.getNodeAllocator(); 25380547386SJordan Rose V = G.getAllocator().Allocate<ExplodedNodeVector>(); 25480547386SJordan Rose new (V) ExplodedNodeVector(Ctx, 4); 25580547386SJordan Rose V->push_back(Old, Ctx); 25680547386SJordan Rose 25780547386SJordan Rose Storage = V; 25880547386SJordan Rose assert(!getFlag()); 25980547386SJordan Rose assert(Storage.is<ExplodedNodeVector *>()); 260fa0734ecSArgyrios Kyrtzidis } 26180547386SJordan Rose 26280547386SJordan Rose V->push_back(N, G.getNodeAllocator()); 263fa0734ecSArgyrios Kyrtzidis } 264fa0734ecSArgyrios Kyrtzidis 265fa0734ecSArgyrios Kyrtzidis unsigned ExplodedNode::NodeGroup::size() const { 266fa0734ecSArgyrios Kyrtzidis if (getFlag()) 267fa0734ecSArgyrios Kyrtzidis return 0; 268fa0734ecSArgyrios Kyrtzidis 26980547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 27080547386SJordan Rose if (Storage.isNull()) 27180547386SJordan Rose return 0; 27280547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 27380547386SJordan Rose return V->size(); 27480547386SJordan Rose return 1; 275fa0734ecSArgyrios Kyrtzidis } 276fa0734ecSArgyrios Kyrtzidis 27780547386SJordan Rose ExplodedNode * const *ExplodedNode::NodeGroup::begin() const { 278fa0734ecSArgyrios Kyrtzidis if (getFlag()) 27980547386SJordan Rose return 0; 280fa0734ecSArgyrios Kyrtzidis 28180547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 28280547386SJordan Rose if (Storage.isNull()) 28380547386SJordan Rose return 0; 28480547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 28580547386SJordan Rose return V->begin(); 28680547386SJordan Rose return Storage.getAddrOfPtr1(); 287fa0734ecSArgyrios Kyrtzidis } 288fa0734ecSArgyrios Kyrtzidis 28980547386SJordan Rose ExplodedNode * const *ExplodedNode::NodeGroup::end() const { 290fa0734ecSArgyrios Kyrtzidis if (getFlag()) 29180547386SJordan Rose return 0; 292fa0734ecSArgyrios Kyrtzidis 29380547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 29480547386SJordan Rose if (Storage.isNull()) 29580547386SJordan Rose return 0; 29680547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 29780547386SJordan Rose return V->end(); 29880547386SJordan Rose return Storage.getAddrOfPtr1() + 1; 299fa0734ecSArgyrios Kyrtzidis } 300fa0734ecSArgyrios Kyrtzidis 301fa0734ecSArgyrios Kyrtzidis ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, 30249b1e38eSTed Kremenek ProgramStateRef State, 30349ea5bf5SAnna Zaks bool IsSink, 30449ea5bf5SAnna Zaks bool* IsNew) { 305fa0734ecSArgyrios Kyrtzidis // Profile 'State' to determine if we already have an existing node. 306fa0734ecSArgyrios Kyrtzidis llvm::FoldingSetNodeID profile; 307fa0734ecSArgyrios Kyrtzidis void *InsertPos = 0; 308fa0734ecSArgyrios Kyrtzidis 30949ea5bf5SAnna Zaks NodeTy::Profile(profile, L, State, IsSink); 310fa0734ecSArgyrios Kyrtzidis NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); 311fa0734ecSArgyrios Kyrtzidis 312fa0734ecSArgyrios Kyrtzidis if (!V) { 313a2aa929eSTed Kremenek if (!FreeNodes.empty()) { 314a2aa929eSTed Kremenek V = FreeNodes.back(); 315a2aa929eSTed Kremenek FreeNodes.pop_back(); 316a40f8ebcSTed Kremenek } 317a40f8ebcSTed Kremenek else { 318fa0734ecSArgyrios Kyrtzidis // Allocate a new node. 319fa0734ecSArgyrios Kyrtzidis V = (NodeTy*) getAllocator().Allocate<NodeTy>(); 320a40f8ebcSTed Kremenek } 321a40f8ebcSTed Kremenek 32249ea5bf5SAnna Zaks new (V) NodeTy(L, State, IsSink); 323fa0734ecSArgyrios Kyrtzidis 324746c06d0SJordan Rose if (ReclaimNodeInterval) 32535e55fe4STed Kremenek ChangedNodes.push_back(V); 32635e55fe4STed Kremenek 327fa0734ecSArgyrios Kyrtzidis // Insert the node into the node set and return it. 328fa0734ecSArgyrios Kyrtzidis Nodes.InsertNode(V, InsertPos); 329fa0734ecSArgyrios Kyrtzidis ++NumNodes; 330fa0734ecSArgyrios Kyrtzidis 331fa0734ecSArgyrios Kyrtzidis if (IsNew) *IsNew = true; 332fa0734ecSArgyrios Kyrtzidis } 333fa0734ecSArgyrios Kyrtzidis else 334fa0734ecSArgyrios Kyrtzidis if (IsNew) *IsNew = false; 335fa0734ecSArgyrios Kyrtzidis 336fa0734ecSArgyrios Kyrtzidis return V; 337fa0734ecSArgyrios Kyrtzidis } 338fa0734ecSArgyrios Kyrtzidis 339fa0734ecSArgyrios Kyrtzidis ExplodedGraph * 34025fac2f6SJordan Rose ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks, 3410833c84aSJordan Rose InterExplodedGraphMap *ForwardMap, 3420833c84aSJordan Rose InterExplodedGraphMap *InverseMap) const{ 3430833c84aSJordan Rose 3440833c84aSJordan Rose if (Nodes.empty()) 3450833c84aSJordan Rose return 0; 346fa0734ecSArgyrios Kyrtzidis 347fa0734ecSArgyrios Kyrtzidis typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; 348fa0734ecSArgyrios Kyrtzidis Pass1Ty Pass1; 349fa0734ecSArgyrios Kyrtzidis 3500833c84aSJordan Rose typedef InterExplodedGraphMap Pass2Ty; 3510833c84aSJordan Rose InterExplodedGraphMap Pass2Scratch; 3520833c84aSJordan Rose Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch; 353fa0734ecSArgyrios Kyrtzidis 3540e62c1ccSChris Lattner SmallVector<const ExplodedNode*, 10> WL1, WL2; 355fa0734ecSArgyrios Kyrtzidis 356fa0734ecSArgyrios Kyrtzidis // ===- Pass 1 (reverse DFS) -=== 3570833c84aSJordan Rose for (ArrayRef<const NodeTy *>::iterator I = Sinks.begin(), E = Sinks.end(); 3580833c84aSJordan Rose I != E; ++I) { 3595a751b99SJordan Rose if (*I) 360fa0734ecSArgyrios Kyrtzidis WL1.push_back(*I); 361fa0734ecSArgyrios Kyrtzidis } 362fa0734ecSArgyrios Kyrtzidis 3630833c84aSJordan Rose // Process the first worklist until it is empty. 364fa0734ecSArgyrios Kyrtzidis while (!WL1.empty()) { 36525284cc9SRobert Wilhelm const ExplodedNode *N = WL1.pop_back_val(); 366fa0734ecSArgyrios Kyrtzidis 367fa0734ecSArgyrios Kyrtzidis // Have we already visited this node? If so, continue to the next one. 368fa0734ecSArgyrios Kyrtzidis if (Pass1.count(N)) 369fa0734ecSArgyrios Kyrtzidis continue; 370fa0734ecSArgyrios Kyrtzidis 371fa0734ecSArgyrios Kyrtzidis // Otherwise, mark this node as visited. 372fa0734ecSArgyrios Kyrtzidis Pass1.insert(N); 373fa0734ecSArgyrios Kyrtzidis 374fa0734ecSArgyrios Kyrtzidis // If this is a root enqueue it to the second worklist. 375fa0734ecSArgyrios Kyrtzidis if (N->Preds.empty()) { 376fa0734ecSArgyrios Kyrtzidis WL2.push_back(N); 377fa0734ecSArgyrios Kyrtzidis continue; 378fa0734ecSArgyrios Kyrtzidis } 379fa0734ecSArgyrios Kyrtzidis 380fa0734ecSArgyrios Kyrtzidis // Visit our predecessors and enqueue them. 38180547386SJordan Rose for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end(); 38280547386SJordan Rose I != E; ++I) 383fa0734ecSArgyrios Kyrtzidis WL1.push_back(*I); 384fa0734ecSArgyrios Kyrtzidis } 385fa0734ecSArgyrios Kyrtzidis 386fa0734ecSArgyrios Kyrtzidis // We didn't hit a root? Return with a null pointer for the new graph. 387fa0734ecSArgyrios Kyrtzidis if (WL2.empty()) 388fa0734ecSArgyrios Kyrtzidis return 0; 389fa0734ecSArgyrios Kyrtzidis 390fa0734ecSArgyrios Kyrtzidis // Create an empty graph. 391fa0734ecSArgyrios Kyrtzidis ExplodedGraph* G = MakeEmptyGraph(); 392fa0734ecSArgyrios Kyrtzidis 393fa0734ecSArgyrios Kyrtzidis // ===- Pass 2 (forward DFS to construct the new graph) -=== 394fa0734ecSArgyrios Kyrtzidis while (!WL2.empty()) { 39525284cc9SRobert Wilhelm const ExplodedNode *N = WL2.pop_back_val(); 396fa0734ecSArgyrios Kyrtzidis 397fa0734ecSArgyrios Kyrtzidis // Skip this node if we have already processed it. 398fa0734ecSArgyrios Kyrtzidis if (Pass2.find(N) != Pass2.end()) 399fa0734ecSArgyrios Kyrtzidis continue; 400fa0734ecSArgyrios Kyrtzidis 401fa0734ecSArgyrios Kyrtzidis // Create the corresponding node in the new graph and record the mapping 402fa0734ecSArgyrios Kyrtzidis // from the old node to the new node. 40349ea5bf5SAnna Zaks ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(), 0); 404fa0734ecSArgyrios Kyrtzidis Pass2[N] = NewN; 405fa0734ecSArgyrios Kyrtzidis 406fa0734ecSArgyrios Kyrtzidis // Also record the reverse mapping from the new node to the old node. 407fa0734ecSArgyrios Kyrtzidis if (InverseMap) (*InverseMap)[NewN] = N; 408fa0734ecSArgyrios Kyrtzidis 409fa0734ecSArgyrios Kyrtzidis // If this node is a root, designate it as such in the graph. 410fa0734ecSArgyrios Kyrtzidis if (N->Preds.empty()) 411fa0734ecSArgyrios Kyrtzidis G->addRoot(NewN); 412fa0734ecSArgyrios Kyrtzidis 413fa0734ecSArgyrios Kyrtzidis // In the case that some of the intended predecessors of NewN have already 414fa0734ecSArgyrios Kyrtzidis // been created, we should hook them up as predecessors. 415fa0734ecSArgyrios Kyrtzidis 416fa0734ecSArgyrios Kyrtzidis // Walk through the predecessors of 'N' and hook up their corresponding 417fa0734ecSArgyrios Kyrtzidis // nodes in the new graph (if any) to the freshly created node. 41880547386SJordan Rose for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end(); 41980547386SJordan Rose I != E; ++I) { 420fa0734ecSArgyrios Kyrtzidis Pass2Ty::iterator PI = Pass2.find(*I); 421fa0734ecSArgyrios Kyrtzidis if (PI == Pass2.end()) 422fa0734ecSArgyrios Kyrtzidis continue; 423fa0734ecSArgyrios Kyrtzidis 4240833c84aSJordan Rose NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G); 425fa0734ecSArgyrios Kyrtzidis } 426fa0734ecSArgyrios Kyrtzidis 427fa0734ecSArgyrios Kyrtzidis // In the case that some of the intended successors of NewN have already 428fa0734ecSArgyrios Kyrtzidis // been created, we should hook them up as successors. Otherwise, enqueue 429fa0734ecSArgyrios Kyrtzidis // the new nodes from the original graph that should have nodes created 430fa0734ecSArgyrios Kyrtzidis // in the new graph. 43180547386SJordan Rose for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end(); 43280547386SJordan Rose I != E; ++I) { 433fa0734ecSArgyrios Kyrtzidis Pass2Ty::iterator PI = Pass2.find(*I); 434fa0734ecSArgyrios Kyrtzidis if (PI != Pass2.end()) { 4350833c84aSJordan Rose const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G); 436fa0734ecSArgyrios Kyrtzidis continue; 437fa0734ecSArgyrios Kyrtzidis } 438fa0734ecSArgyrios Kyrtzidis 439fa0734ecSArgyrios Kyrtzidis // Enqueue nodes to the worklist that were marked during pass 1. 440fa0734ecSArgyrios Kyrtzidis if (Pass1.count(*I)) 441fa0734ecSArgyrios Kyrtzidis WL2.push_back(*I); 442fa0734ecSArgyrios Kyrtzidis } 443fa0734ecSArgyrios Kyrtzidis } 444fa0734ecSArgyrios Kyrtzidis 445fa0734ecSArgyrios Kyrtzidis return G; 446fa0734ecSArgyrios Kyrtzidis } 447fa0734ecSArgyrios Kyrtzidis 448