1e580d831SEugene Zelenko //===- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -------------===// 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" 16e580d831SEugene Zelenko #include "clang/AST/Expr.h" 17e580d831SEugene Zelenko #include "clang/AST/ExprObjC.h" 183a02247dSChandler Carruth #include "clang/AST/ParentMap.h" 193a02247dSChandler Carruth #include "clang/AST/Stmt.h" 20e580d831SEugene Zelenko #include "clang/Analysis/ProgramPoint.h" 21e580d831SEugene Zelenko #include "clang/Analysis/Support/BumpVector.h" 22e580d831SEugene Zelenko #include "clang/Basic/LLVM.h" 234f7df9beSJordan Rose #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 24001fd5b4STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 25e580d831SEugene Zelenko #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" 263a02247dSChandler Carruth #include "llvm/ADT/DenseSet.h" 27e580d831SEugene Zelenko #include "llvm/ADT/FoldingSet.h" 28e580d831SEugene Zelenko #include "llvm/ADT/Optional.h" 29e580d831SEugene Zelenko #include "llvm/ADT/PointerUnion.h" 30fa0734ecSArgyrios Kyrtzidis #include "llvm/ADT/SmallVector.h" 31e580d831SEugene Zelenko #include "llvm/Support/Casting.h" 32e580d831SEugene Zelenko #include <cassert> 33e580d831SEugene Zelenko #include <memory> 34fa0734ecSArgyrios Kyrtzidis 35fa0734ecSArgyrios Kyrtzidis using namespace clang; 36fa0734ecSArgyrios Kyrtzidis using namespace ento; 37fa0734ecSArgyrios Kyrtzidis 38fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 39a40f8ebcSTed Kremenek // Cleanup. 40a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 41a40f8ebcSTed Kremenek 42e580d831SEugene Zelenko ExplodedGraph::ExplodedGraph() = default; 4344d2973bSTed Kremenek 44e580d831SEugene Zelenko ExplodedGraph::~ExplodedGraph() = default; 45a40f8ebcSTed Kremenek 46a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 47a40f8ebcSTed Kremenek // Node reclamation. 48a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 49a40f8ebcSTed Kremenek 5004fa9e3dSTed Kremenek bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) { 5104fa9e3dSTed Kremenek if (!Ex->isLValue()) 5204fa9e3dSTed Kremenek return false; 5304fa9e3dSTed Kremenek return isa<DeclRefExpr>(Ex) || 5404fa9e3dSTed Kremenek isa<MemberExpr>(Ex) || 5504fa9e3dSTed Kremenek isa<ObjCIvarRefExpr>(Ex); 5604fa9e3dSTed Kremenek } 5704fa9e3dSTed Kremenek 581dd7fd71STed Kremenek bool ExplodedGraph::shouldCollect(const ExplodedNode *node) { 598f564058STed Kremenek // First, we only consider nodes for reclamation of the following 608f564058STed Kremenek // conditions apply: 611dd7fd71STed Kremenek // 621dd7fd71STed Kremenek // (1) 1 predecessor (that has one successor) 631dd7fd71STed Kremenek // (2) 1 successor (that has one predecessor) 648f564058STed Kremenek // 658f564058STed Kremenek // If a node has no successor it is on the "frontier", while a node 668f564058STed Kremenek // with no predecessor is a root. 678f564058STed Kremenek // 688f564058STed Kremenek // After these prerequisites, we discard all "filler" nodes that 698f564058STed Kremenek // are used only for intermediate processing, and are not essential 708f564058STed Kremenek // for analyzer history: 718f564058STed Kremenek // 728f564058STed Kremenek // (a) PreStmtPurgeDeadSymbols 738f564058STed Kremenek // 748f564058STed Kremenek // We then discard all other nodes where *all* of the following conditions 758f564058STed Kremenek // apply: 768f564058STed Kremenek // 77199fdd82SJordan Rose // (3) The ProgramPoint is for a PostStmt, but not a PostStore. 781dd7fd71STed Kremenek // (4) There is no 'tag' for the ProgramPoint. 791dd7fd71STed Kremenek // (5) The 'store' is the same as the predecessor. 801dd7fd71STed Kremenek // (6) The 'GDM' is the same as the predecessor. 811dd7fd71STed Kremenek // (7) The LocationContext is the same as the predecessor. 8296250482STed Kremenek // (8) Expressions that are *not* lvalue expressions. 8396250482STed Kremenek // (9) The PostStmt isn't for a non-consumed Stmt or Expr. 8468a172caSAnton Yartsev // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or 8568a172caSAnton Yartsev // PreImplicitCall (so that we would be able to find it when retrying a 8668a172caSAnton Yartsev // call with no inlining). 87681cce99SJordan Rose // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well. 881dd7fd71STed Kremenek 891dd7fd71STed Kremenek // Conditions 1 and 2. 901dd7fd71STed Kremenek if (node->pred_size() != 1 || node->succ_size() != 1) 911dd7fd71STed Kremenek return false; 921dd7fd71STed Kremenek 931dd7fd71STed Kremenek const ExplodedNode *pred = *(node->pred_begin()); 941dd7fd71STed Kremenek if (pred->succ_size() != 1) 951dd7fd71STed Kremenek return false; 961dd7fd71STed Kremenek 971dd7fd71STed Kremenek const ExplodedNode *succ = *(node->succ_begin()); 981dd7fd71STed Kremenek if (succ->pred_size() != 1) 991dd7fd71STed Kremenek return false; 1001dd7fd71STed Kremenek 1018f564058STed Kremenek // Now reclaim any nodes that are (by definition) not essential to 1028f564058STed Kremenek // analysis history and are not consulted by any client code. 1031dd7fd71STed Kremenek ProgramPoint progPoint = node->getLocation(); 1048f564058STed Kremenek if (progPoint.getAs<PreStmtPurgeDeadSymbols>()) 105f352d8c7STed Kremenek return !progPoint.getTag(); 1068f564058STed Kremenek 1078f564058STed Kremenek // Condition 3. 10887396b9bSDavid Blaikie if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>()) 1091dd7fd71STed Kremenek return false; 1101dd7fd71STed Kremenek 1111dd7fd71STed Kremenek // Condition 4. 11254417f6dSAnna Zaks if (progPoint.getTag()) 1131dd7fd71STed Kremenek return false; 1141dd7fd71STed Kremenek 1151dd7fd71STed Kremenek // Conditions 5, 6, and 7. 1161dd7fd71STed Kremenek ProgramStateRef state = node->getState(); 1171dd7fd71STed Kremenek ProgramStateRef pred_state = pred->getState(); 1181dd7fd71STed Kremenek if (state->store != pred_state->store || state->GDM != pred_state->GDM || 1191dd7fd71STed Kremenek progPoint.getLocationContext() != pred->getLocationContext()) 1201dd7fd71STed Kremenek return false; 1211dd7fd71STed Kremenek 12254417f6dSAnna Zaks // All further checks require expressions. As per #3, we know that we have 12354417f6dSAnna Zaks // a PostStmt. 12454417f6dSAnna Zaks const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt()); 12504fa9e3dSTed Kremenek if (!Ex) 12604fa9e3dSTed Kremenek return false; 12704fa9e3dSTed Kremenek 12804fa9e3dSTed Kremenek // Condition 8. 12904fa9e3dSTed Kremenek // Do not collect nodes for "interesting" lvalue expressions since they are 13004fa9e3dSTed Kremenek // used extensively for generating path diagnostics. 13104fa9e3dSTed Kremenek if (isInterestingLValueExpr(Ex)) 13296250482STed Kremenek return false; 13396250482STed Kremenek 13496250482STed Kremenek // Condition 9. 13567e0062bSAnna Zaks // Do not collect nodes for non-consumed Stmt or Expr to ensure precise 13667e0062bSAnna Zaks // diagnostic generation; specifically, so that we could anchor arrows 13767e0062bSAnna Zaks // pointing to the beginning of statements (as written in code). 1381dd7fd71STed Kremenek ParentMap &PM = progPoint.getLocationContext()->getParentMap(); 1391dd7fd71STed Kremenek if (!PM.isConsumedExpr(Ex)) 1401dd7fd71STed Kremenek return false; 1411dd7fd71STed Kremenek 14296250482STed Kremenek // Condition 10. 143bec49efdSAnna Zaks const ProgramPoint SuccLoc = succ->getLocation(); 14487396b9bSDavid Blaikie if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>()) 145e537cc05SJordan Rose if (CallEvent::isCallStmt(SP->getStmt())) 146bec49efdSAnna Zaks return false; 147bec49efdSAnna Zaks 14868a172caSAnton Yartsev // Condition 10, continuation. 14968a172caSAnton Yartsev if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>()) 15068a172caSAnton Yartsev return false; 15168a172caSAnton Yartsev 1521dd7fd71STed Kremenek return true; 1531dd7fd71STed Kremenek } 1541dd7fd71STed Kremenek 1551dd7fd71STed Kremenek void ExplodedGraph::collectNode(ExplodedNode *node) { 1561dd7fd71STed Kremenek // Removing a node means: 1571dd7fd71STed Kremenek // (a) changing the predecessors successor to the successor of this node 1581dd7fd71STed Kremenek // (b) changing the successors predecessor to the predecessor of this node 1591dd7fd71STed Kremenek // (c) Putting 'node' onto freeNodes. 1601dd7fd71STed Kremenek assert(node->pred_size() == 1 || node->succ_size() == 1); 1611dd7fd71STed Kremenek ExplodedNode *pred = *(node->pred_begin()); 1621dd7fd71STed Kremenek ExplodedNode *succ = *(node->succ_begin()); 1631dd7fd71STed Kremenek pred->replaceSuccessor(succ); 1641dd7fd71STed Kremenek succ->replacePredecessor(pred); 165a2aa929eSTed Kremenek FreeNodes.push_back(node); 1661dd7fd71STed Kremenek Nodes.RemoveNode(node); 1671dd7fd71STed Kremenek --NumNodes; 1681dd7fd71STed Kremenek node->~ExplodedNode(); 1691dd7fd71STed Kremenek } 1701dd7fd71STed Kremenek 17135e55fe4STed Kremenek void ExplodedGraph::reclaimRecentlyAllocatedNodes() { 172a2aa929eSTed Kremenek if (ChangedNodes.empty()) 173a40f8ebcSTed Kremenek return; 17444d2973bSTed Kremenek 175746c06d0SJordan Rose // Only periodically reclaim nodes so that we can build up a set of 17635e55fe4STed Kremenek // nodes that meet the reclamation criteria. Freshly created nodes 17735e55fe4STed Kremenek // by definition have no successor, and thus cannot be reclaimed (see below). 178746c06d0SJordan Rose assert(ReclaimCounter > 0); 179746c06d0SJordan Rose if (--ReclaimCounter != 0) 18035e55fe4STed Kremenek return; 181746c06d0SJordan Rose ReclaimCounter = ReclaimNodeInterval; 18235e55fe4STed Kremenek 183e580d831SEugene Zelenko for (const auto node : ChangedNodes) 1841dd7fd71STed Kremenek if (shouldCollect(node)) 1851dd7fd71STed Kremenek collectNode(node); 186a2aa929eSTed Kremenek ChangedNodes.clear(); 187a40f8ebcSTed Kremenek } 188a40f8ebcSTed Kremenek 189a40f8ebcSTed Kremenek //===----------------------------------------------------------------------===// 190fa0734ecSArgyrios Kyrtzidis // ExplodedNode. 191fa0734ecSArgyrios Kyrtzidis //===----------------------------------------------------------------------===// 192fa0734ecSArgyrios Kyrtzidis 1932b10f3f8SJordan Rose // An NodeGroup's storage type is actually very much like a TinyPtrVector: 1942b10f3f8SJordan Rose // it can be either a pointer to a single ExplodedNode, or a pointer to a 1952b10f3f8SJordan Rose // BumpVector allocated with the ExplodedGraph's allocator. This allows the 1962b10f3f8SJordan Rose // common case of single-node NodeGroups to be implemented with no extra memory. 1972b10f3f8SJordan Rose // 1982b10f3f8SJordan Rose // Consequently, each of the NodeGroup methods have up to four cases to handle: 1992b10f3f8SJordan Rose // 1. The flag is set and this group does not actually contain any nodes. 2002b10f3f8SJordan Rose // 2. The group is empty, in which case the storage value is null. 2012b10f3f8SJordan Rose // 3. The group contains a single node. 2022b10f3f8SJordan Rose // 4. The group contains more than one node. 203e580d831SEugene Zelenko using ExplodedNodeVector = BumpVector<ExplodedNode *>; 204e580d831SEugene Zelenko using GroupStorage = llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *>; 205fa0734ecSArgyrios Kyrtzidis 206fa0734ecSArgyrios Kyrtzidis void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { 207fa0734ecSArgyrios Kyrtzidis assert(!V->isSink()); 208fa0734ecSArgyrios Kyrtzidis Preds.addNode(V, G); 209fa0734ecSArgyrios Kyrtzidis V->Succs.addNode(this, G); 210fa0734ecSArgyrios Kyrtzidis } 211fa0734ecSArgyrios Kyrtzidis 212a40f8ebcSTed Kremenek void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { 2132b10f3f8SJordan Rose assert(!getFlag()); 2142b10f3f8SJordan Rose 21580547386SJordan Rose GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); 21680547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 21780547386SJordan Rose Storage = node; 21880547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 219a40f8ebcSTed Kremenek } 220a40f8ebcSTed Kremenek 221fa0734ecSArgyrios Kyrtzidis void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { 222fa0734ecSArgyrios Kyrtzidis assert(!getFlag()); 223fa0734ecSArgyrios Kyrtzidis 22480547386SJordan Rose GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); 22580547386SJordan Rose if (Storage.isNull()) { 22680547386SJordan Rose Storage = N; 22780547386SJordan Rose assert(Storage.is<ExplodedNode *>()); 22880547386SJordan Rose return; 22980547386SJordan Rose } 230fa0734ecSArgyrios Kyrtzidis 23180547386SJordan Rose ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>(); 23280547386SJordan Rose 23380547386SJordan Rose if (!V) { 23480547386SJordan Rose // Switch from single-node to multi-node representation. 23580547386SJordan Rose ExplodedNode *Old = Storage.get<ExplodedNode *>(); 23680547386SJordan Rose 23780547386SJordan Rose BumpVectorContext &Ctx = G.getNodeAllocator(); 23880547386SJordan Rose V = G.getAllocator().Allocate<ExplodedNodeVector>(); 23980547386SJordan Rose new (V) ExplodedNodeVector(Ctx, 4); 24080547386SJordan Rose V->push_back(Old, Ctx); 24180547386SJordan Rose 24280547386SJordan Rose Storage = V; 24380547386SJordan Rose assert(!getFlag()); 24480547386SJordan Rose assert(Storage.is<ExplodedNodeVector *>()); 245fa0734ecSArgyrios Kyrtzidis } 24680547386SJordan Rose 24780547386SJordan Rose V->push_back(N, G.getNodeAllocator()); 248fa0734ecSArgyrios Kyrtzidis } 249fa0734ecSArgyrios Kyrtzidis 250fa0734ecSArgyrios Kyrtzidis unsigned ExplodedNode::NodeGroup::size() const { 251fa0734ecSArgyrios Kyrtzidis if (getFlag()) 252fa0734ecSArgyrios Kyrtzidis return 0; 253fa0734ecSArgyrios Kyrtzidis 25480547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 25580547386SJordan Rose if (Storage.isNull()) 25680547386SJordan Rose return 0; 25780547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 25880547386SJordan Rose return V->size(); 25980547386SJordan Rose return 1; 260fa0734ecSArgyrios Kyrtzidis } 261fa0734ecSArgyrios Kyrtzidis 26280547386SJordan Rose ExplodedNode * const *ExplodedNode::NodeGroup::begin() const { 263fa0734ecSArgyrios Kyrtzidis if (getFlag()) 2640dbb783cSCraig Topper return nullptr; 265fa0734ecSArgyrios Kyrtzidis 26680547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 26780547386SJordan Rose if (Storage.isNull()) 2680dbb783cSCraig Topper return nullptr; 26980547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 27080547386SJordan Rose return V->begin(); 27180547386SJordan Rose return Storage.getAddrOfPtr1(); 272fa0734ecSArgyrios Kyrtzidis } 273fa0734ecSArgyrios Kyrtzidis 27480547386SJordan Rose ExplodedNode * const *ExplodedNode::NodeGroup::end() const { 275fa0734ecSArgyrios Kyrtzidis if (getFlag()) 2760dbb783cSCraig Topper return nullptr; 277fa0734ecSArgyrios Kyrtzidis 27880547386SJordan Rose const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); 27980547386SJordan Rose if (Storage.isNull()) 2800dbb783cSCraig Topper return nullptr; 28180547386SJordan Rose if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) 28280547386SJordan Rose return V->end(); 28380547386SJordan Rose return Storage.getAddrOfPtr1() + 1; 284fa0734ecSArgyrios Kyrtzidis } 285fa0734ecSArgyrios Kyrtzidis 28684a2b30bSGeorge Karpenkov int64_t ExplodedNode::getID(ExplodedGraph *G) const { 28784a2b30bSGeorge Karpenkov Optional<int64_t> Out = G->getAllocator().identifyObject(this); 28884a2b30bSGeorge Karpenkov assert(Out && "Wrong allocator used"); 28984a2b30bSGeorge Karpenkov assert(*Out % alignof(ExplodedNode) == 0 && "Wrong alignment information"); 29084a2b30bSGeorge Karpenkov return *Out / alignof(ExplodedNode); 29184a2b30bSGeorge Karpenkov } 29284a2b30bSGeorge Karpenkov 29398bee022SGeorge Karpenkov bool ExplodedNode::isTrivial() const { 29498bee022SGeorge Karpenkov return pred_size() == 1 && succ_size() == 1 && 295*ff6df778SGeorge Karpenkov getFirstPred()->getState()->getID() == getState()->getID() && 296*ff6df778SGeorge Karpenkov getFirstPred()->succ_size() == 1; 29798bee022SGeorge Karpenkov } 29898bee022SGeorge Karpenkov 299fa0734ecSArgyrios Kyrtzidis ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, 30049b1e38eSTed Kremenek ProgramStateRef State, 30149ea5bf5SAnna Zaks bool IsSink, 30249ea5bf5SAnna Zaks bool* IsNew) { 303fa0734ecSArgyrios Kyrtzidis // Profile 'State' to determine if we already have an existing node. 304fa0734ecSArgyrios Kyrtzidis llvm::FoldingSetNodeID profile; 3050dbb783cSCraig Topper void *InsertPos = nullptr; 306fa0734ecSArgyrios Kyrtzidis 30749ea5bf5SAnna Zaks NodeTy::Profile(profile, L, State, IsSink); 308fa0734ecSArgyrios Kyrtzidis NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); 309fa0734ecSArgyrios Kyrtzidis 310fa0734ecSArgyrios Kyrtzidis if (!V) { 311a2aa929eSTed Kremenek if (!FreeNodes.empty()) { 312a2aa929eSTed Kremenek V = FreeNodes.back(); 313a2aa929eSTed Kremenek FreeNodes.pop_back(); 314a40f8ebcSTed Kremenek } 315a40f8ebcSTed Kremenek else { 316fa0734ecSArgyrios Kyrtzidis // Allocate a new node. 317fa0734ecSArgyrios Kyrtzidis V = (NodeTy*) getAllocator().Allocate<NodeTy>(); 318a40f8ebcSTed Kremenek } 319a40f8ebcSTed Kremenek 32049ea5bf5SAnna Zaks new (V) NodeTy(L, State, IsSink); 321fa0734ecSArgyrios Kyrtzidis 322746c06d0SJordan Rose if (ReclaimNodeInterval) 32335e55fe4STed Kremenek ChangedNodes.push_back(V); 32435e55fe4STed Kremenek 325fa0734ecSArgyrios Kyrtzidis // Insert the node into the node set and return it. 326fa0734ecSArgyrios Kyrtzidis Nodes.InsertNode(V, InsertPos); 327fa0734ecSArgyrios Kyrtzidis ++NumNodes; 328fa0734ecSArgyrios Kyrtzidis 329fa0734ecSArgyrios Kyrtzidis if (IsNew) *IsNew = true; 330fa0734ecSArgyrios Kyrtzidis } 331fa0734ecSArgyrios Kyrtzidis else 332fa0734ecSArgyrios Kyrtzidis if (IsNew) *IsNew = false; 333fa0734ecSArgyrios Kyrtzidis 334fa0734ecSArgyrios Kyrtzidis return V; 335fa0734ecSArgyrios Kyrtzidis } 336fa0734ecSArgyrios Kyrtzidis 3374067e35fSBen Craig ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L, 3384067e35fSBen Craig ProgramStateRef State, 3394067e35fSBen Craig bool IsSink) { 3404067e35fSBen Craig NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>(); 3414067e35fSBen Craig new (V) NodeTy(L, State, IsSink); 3424067e35fSBen Craig return V; 3434067e35fSBen Craig } 3444067e35fSBen Craig 345b564d1fbSDavid Blaikie std::unique_ptr<ExplodedGraph> 34625fac2f6SJordan Rose ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks, 3470833c84aSJordan Rose InterExplodedGraphMap *ForwardMap, 3480833c84aSJordan Rose InterExplodedGraphMap *InverseMap) const { 3490833c84aSJordan Rose if (Nodes.empty()) 3500dbb783cSCraig Topper return nullptr; 351fa0734ecSArgyrios Kyrtzidis 352e580d831SEugene Zelenko using Pass1Ty = llvm::DenseSet<const ExplodedNode *>; 353fa0734ecSArgyrios Kyrtzidis Pass1Ty Pass1; 354fa0734ecSArgyrios Kyrtzidis 355e580d831SEugene Zelenko using Pass2Ty = InterExplodedGraphMap; 3560833c84aSJordan Rose InterExplodedGraphMap Pass2Scratch; 3570833c84aSJordan Rose Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch; 358fa0734ecSArgyrios Kyrtzidis 3590e62c1ccSChris Lattner SmallVector<const ExplodedNode*, 10> WL1, WL2; 360fa0734ecSArgyrios Kyrtzidis 361fa0734ecSArgyrios Kyrtzidis // ===- Pass 1 (reverse DFS) -=== 362e580d831SEugene Zelenko for (const auto Sink : Sinks) 363e580d831SEugene Zelenko if (Sink) 364e580d831SEugene Zelenko WL1.push_back(Sink); 365fa0734ecSArgyrios Kyrtzidis 3660833c84aSJordan Rose // Process the first worklist until it is empty. 367fa0734ecSArgyrios Kyrtzidis while (!WL1.empty()) { 36825284cc9SRobert Wilhelm const ExplodedNode *N = WL1.pop_back_val(); 369fa0734ecSArgyrios Kyrtzidis 370fa0734ecSArgyrios Kyrtzidis // Have we already visited this node? If so, continue to the next one. 371ad8e079cSBenjamin Kramer if (!Pass1.insert(N).second) 372fa0734ecSArgyrios Kyrtzidis continue; 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. 381ad8e079cSBenjamin Kramer WL1.append(N->Preds.begin(), N->Preds.end()); 382fa0734ecSArgyrios Kyrtzidis } 383fa0734ecSArgyrios Kyrtzidis 384fa0734ecSArgyrios Kyrtzidis // We didn't hit a root? Return with a null pointer for the new graph. 385fa0734ecSArgyrios Kyrtzidis if (WL2.empty()) 3860dbb783cSCraig Topper return nullptr; 387fa0734ecSArgyrios Kyrtzidis 388fa0734ecSArgyrios Kyrtzidis // Create an empty graph. 389b564d1fbSDavid Blaikie std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph(); 390fa0734ecSArgyrios Kyrtzidis 391fa0734ecSArgyrios Kyrtzidis // ===- Pass 2 (forward DFS to construct the new graph) -=== 392fa0734ecSArgyrios Kyrtzidis while (!WL2.empty()) { 39325284cc9SRobert Wilhelm const ExplodedNode *N = WL2.pop_back_val(); 394fa0734ecSArgyrios Kyrtzidis 395fa0734ecSArgyrios Kyrtzidis // Skip this node if we have already processed it. 396fa0734ecSArgyrios Kyrtzidis if (Pass2.find(N) != Pass2.end()) 397fa0734ecSArgyrios Kyrtzidis continue; 398fa0734ecSArgyrios Kyrtzidis 399fa0734ecSArgyrios Kyrtzidis // Create the corresponding node in the new graph and record the mapping 400fa0734ecSArgyrios Kyrtzidis // from the old node to the new node. 4014067e35fSBen Craig ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State, N->isSink()); 402fa0734ecSArgyrios Kyrtzidis Pass2[N] = NewN; 403fa0734ecSArgyrios Kyrtzidis 404fa0734ecSArgyrios Kyrtzidis // Also record the reverse mapping from the new node to the old node. 405fa0734ecSArgyrios Kyrtzidis if (InverseMap) (*InverseMap)[NewN] = N; 406fa0734ecSArgyrios Kyrtzidis 407fa0734ecSArgyrios Kyrtzidis // If this node is a root, designate it as such in the graph. 408fa0734ecSArgyrios Kyrtzidis if (N->Preds.empty()) 409fa0734ecSArgyrios Kyrtzidis G->addRoot(NewN); 410fa0734ecSArgyrios Kyrtzidis 411fa0734ecSArgyrios Kyrtzidis // In the case that some of the intended predecessors of NewN have already 412fa0734ecSArgyrios Kyrtzidis // been created, we should hook them up as predecessors. 413fa0734ecSArgyrios Kyrtzidis 414fa0734ecSArgyrios Kyrtzidis // Walk through the predecessors of 'N' and hook up their corresponding 415fa0734ecSArgyrios Kyrtzidis // nodes in the new graph (if any) to the freshly created node. 41680547386SJordan Rose for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end(); 41780547386SJordan Rose I != E; ++I) { 418fa0734ecSArgyrios Kyrtzidis Pass2Ty::iterator PI = Pass2.find(*I); 419fa0734ecSArgyrios Kyrtzidis if (PI == Pass2.end()) 420fa0734ecSArgyrios Kyrtzidis continue; 421fa0734ecSArgyrios Kyrtzidis 4220833c84aSJordan Rose NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G); 423fa0734ecSArgyrios Kyrtzidis } 424fa0734ecSArgyrios Kyrtzidis 425fa0734ecSArgyrios Kyrtzidis // In the case that some of the intended successors of NewN have already 426fa0734ecSArgyrios Kyrtzidis // been created, we should hook them up as successors. Otherwise, enqueue 427fa0734ecSArgyrios Kyrtzidis // the new nodes from the original graph that should have nodes created 428fa0734ecSArgyrios Kyrtzidis // in the new graph. 42980547386SJordan Rose for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end(); 43080547386SJordan Rose I != E; ++I) { 431fa0734ecSArgyrios Kyrtzidis Pass2Ty::iterator PI = Pass2.find(*I); 432fa0734ecSArgyrios Kyrtzidis if (PI != Pass2.end()) { 4330833c84aSJordan Rose const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G); 434fa0734ecSArgyrios Kyrtzidis continue; 435fa0734ecSArgyrios Kyrtzidis } 436fa0734ecSArgyrios Kyrtzidis 437fa0734ecSArgyrios Kyrtzidis // Enqueue nodes to the worklist that were marked during pass 1. 438fa0734ecSArgyrios Kyrtzidis if (Pass1.count(*I)) 439fa0734ecSArgyrios Kyrtzidis WL2.push_back(*I); 440fa0734ecSArgyrios Kyrtzidis } 441fa0734ecSArgyrios Kyrtzidis } 442fa0734ecSArgyrios Kyrtzidis 443fa0734ecSArgyrios Kyrtzidis return G; 444fa0734ecSArgyrios Kyrtzidis } 445