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.
34637d1e66SAngel Garcia Gomez ExplodedNode::Auditor::~Auditor() {}
35fa0734ecSArgyrios Kyrtzidis 
36fa0734ecSArgyrios Kyrtzidis #ifndef NDEBUG
370dbb783cSCraig Topper static ExplodedNode::Auditor* NodeAuditor = nullptr;
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 
53637d1e66SAngel Garcia Gomez 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.
9368a172caSAnton Yartsev   // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or
9468a172caSAnton Yartsev   //      PreImplicitCall (so that we would be able to find it when retrying a
9568a172caSAnton 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 
15768a172caSAnton Yartsev   // Condition 10, continuation.
15868a172caSAnton Yartsev   if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>())
15968a172caSAnton Yartsev     return false;
16068a172caSAnton 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())
2790dbb783cSCraig Topper     return nullptr;
280fa0734ecSArgyrios Kyrtzidis 
28180547386SJordan Rose   const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
28280547386SJordan Rose   if (Storage.isNull())
2830dbb783cSCraig Topper     return nullptr;
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())
2910dbb783cSCraig Topper     return nullptr;
292fa0734ecSArgyrios Kyrtzidis 
29380547386SJordan Rose   const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
29480547386SJordan Rose   if (Storage.isNull())
2950dbb783cSCraig Topper     return nullptr;
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;
3070dbb783cSCraig Topper   void *InsertPos = nullptr;
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 
339*4067e35fSBen Craig ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L,
340*4067e35fSBen Craig                                                 ProgramStateRef State,
341*4067e35fSBen Craig                                                 bool IsSink) {
342*4067e35fSBen Craig   NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>();
343*4067e35fSBen Craig   new (V) NodeTy(L, State, IsSink);
344*4067e35fSBen Craig   return V;
345*4067e35fSBen Craig }
346*4067e35fSBen Craig 
347b564d1fbSDavid Blaikie std::unique_ptr<ExplodedGraph>
34825fac2f6SJordan Rose ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
3490833c84aSJordan Rose                     InterExplodedGraphMap *ForwardMap,
3500833c84aSJordan Rose                     InterExplodedGraphMap *InverseMap) const {
3510833c84aSJordan Rose 
3520833c84aSJordan Rose   if (Nodes.empty())
3530dbb783cSCraig Topper     return nullptr;
354fa0734ecSArgyrios Kyrtzidis 
355fa0734ecSArgyrios Kyrtzidis   typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
356fa0734ecSArgyrios Kyrtzidis   Pass1Ty Pass1;
357fa0734ecSArgyrios Kyrtzidis 
3580833c84aSJordan Rose   typedef InterExplodedGraphMap Pass2Ty;
3590833c84aSJordan Rose   InterExplodedGraphMap Pass2Scratch;
3600833c84aSJordan Rose   Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
361fa0734ecSArgyrios Kyrtzidis 
3620e62c1ccSChris Lattner   SmallVector<const ExplodedNode*, 10> WL1, WL2;
363fa0734ecSArgyrios Kyrtzidis 
364fa0734ecSArgyrios Kyrtzidis   // ===- Pass 1 (reverse DFS) -===
3650833c84aSJordan Rose   for (ArrayRef<const NodeTy *>::iterator I = Sinks.begin(), E = Sinks.end();
3660833c84aSJordan Rose        I != E; ++I) {
3675a751b99SJordan Rose     if (*I)
368fa0734ecSArgyrios Kyrtzidis       WL1.push_back(*I);
369fa0734ecSArgyrios Kyrtzidis   }
370fa0734ecSArgyrios Kyrtzidis 
3710833c84aSJordan Rose   // Process the first worklist until it is empty.
372fa0734ecSArgyrios Kyrtzidis   while (!WL1.empty()) {
37325284cc9SRobert Wilhelm     const ExplodedNode *N = WL1.pop_back_val();
374fa0734ecSArgyrios Kyrtzidis 
375fa0734ecSArgyrios Kyrtzidis     // Have we already visited this node?  If so, continue to the next one.
376ad8e079cSBenjamin Kramer     if (!Pass1.insert(N).second)
377fa0734ecSArgyrios Kyrtzidis       continue;
378fa0734ecSArgyrios Kyrtzidis 
379fa0734ecSArgyrios Kyrtzidis     // If this is a root enqueue it to the second worklist.
380fa0734ecSArgyrios Kyrtzidis     if (N->Preds.empty()) {
381fa0734ecSArgyrios Kyrtzidis       WL2.push_back(N);
382fa0734ecSArgyrios Kyrtzidis       continue;
383fa0734ecSArgyrios Kyrtzidis     }
384fa0734ecSArgyrios Kyrtzidis 
385fa0734ecSArgyrios Kyrtzidis     // Visit our predecessors and enqueue them.
386ad8e079cSBenjamin Kramer     WL1.append(N->Preds.begin(), N->Preds.end());
387fa0734ecSArgyrios Kyrtzidis   }
388fa0734ecSArgyrios Kyrtzidis 
389fa0734ecSArgyrios Kyrtzidis   // We didn't hit a root? Return with a null pointer for the new graph.
390fa0734ecSArgyrios Kyrtzidis   if (WL2.empty())
3910dbb783cSCraig Topper     return nullptr;
392fa0734ecSArgyrios Kyrtzidis 
393fa0734ecSArgyrios Kyrtzidis   // Create an empty graph.
394b564d1fbSDavid Blaikie   std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph();
395fa0734ecSArgyrios Kyrtzidis 
396fa0734ecSArgyrios Kyrtzidis   // ===- Pass 2 (forward DFS to construct the new graph) -===
397fa0734ecSArgyrios Kyrtzidis   while (!WL2.empty()) {
39825284cc9SRobert Wilhelm     const ExplodedNode *N = WL2.pop_back_val();
399fa0734ecSArgyrios Kyrtzidis 
400fa0734ecSArgyrios Kyrtzidis     // Skip this node if we have already processed it.
401fa0734ecSArgyrios Kyrtzidis     if (Pass2.find(N) != Pass2.end())
402fa0734ecSArgyrios Kyrtzidis       continue;
403fa0734ecSArgyrios Kyrtzidis 
404fa0734ecSArgyrios Kyrtzidis     // Create the corresponding node in the new graph and record the mapping
405fa0734ecSArgyrios Kyrtzidis     // from the old node to the new node.
406*4067e35fSBen Craig     ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State, N->isSink());
407fa0734ecSArgyrios Kyrtzidis     Pass2[N] = NewN;
408fa0734ecSArgyrios Kyrtzidis 
409fa0734ecSArgyrios Kyrtzidis     // Also record the reverse mapping from the new node to the old node.
410fa0734ecSArgyrios Kyrtzidis     if (InverseMap) (*InverseMap)[NewN] = N;
411fa0734ecSArgyrios Kyrtzidis 
412fa0734ecSArgyrios Kyrtzidis     // If this node is a root, designate it as such in the graph.
413fa0734ecSArgyrios Kyrtzidis     if (N->Preds.empty())
414fa0734ecSArgyrios Kyrtzidis       G->addRoot(NewN);
415fa0734ecSArgyrios Kyrtzidis 
416fa0734ecSArgyrios Kyrtzidis     // In the case that some of the intended predecessors of NewN have already
417fa0734ecSArgyrios Kyrtzidis     // been created, we should hook them up as predecessors.
418fa0734ecSArgyrios Kyrtzidis 
419fa0734ecSArgyrios Kyrtzidis     // Walk through the predecessors of 'N' and hook up their corresponding
420fa0734ecSArgyrios Kyrtzidis     // nodes in the new graph (if any) to the freshly created node.
42180547386SJordan Rose     for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
42280547386SJordan Rose          I != E; ++I) {
423fa0734ecSArgyrios Kyrtzidis       Pass2Ty::iterator PI = Pass2.find(*I);
424fa0734ecSArgyrios Kyrtzidis       if (PI == Pass2.end())
425fa0734ecSArgyrios Kyrtzidis         continue;
426fa0734ecSArgyrios Kyrtzidis 
4270833c84aSJordan Rose       NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
428fa0734ecSArgyrios Kyrtzidis     }
429fa0734ecSArgyrios Kyrtzidis 
430fa0734ecSArgyrios Kyrtzidis     // In the case that some of the intended successors of NewN have already
431fa0734ecSArgyrios Kyrtzidis     // been created, we should hook them up as successors.  Otherwise, enqueue
432fa0734ecSArgyrios Kyrtzidis     // the new nodes from the original graph that should have nodes created
433fa0734ecSArgyrios Kyrtzidis     // in the new graph.
43480547386SJordan Rose     for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
43580547386SJordan Rose          I != E; ++I) {
436fa0734ecSArgyrios Kyrtzidis       Pass2Ty::iterator PI = Pass2.find(*I);
437fa0734ecSArgyrios Kyrtzidis       if (PI != Pass2.end()) {
4380833c84aSJordan Rose         const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
439fa0734ecSArgyrios Kyrtzidis         continue;
440fa0734ecSArgyrios Kyrtzidis       }
441fa0734ecSArgyrios Kyrtzidis 
442fa0734ecSArgyrios Kyrtzidis       // Enqueue nodes to the worklist that were marked during pass 1.
443fa0734ecSArgyrios Kyrtzidis       if (Pass1.count(*I))
444fa0734ecSArgyrios Kyrtzidis         WL2.push_back(*I);
445fa0734ecSArgyrios Kyrtzidis     }
446fa0734ecSArgyrios Kyrtzidis   }
447fa0734ecSArgyrios Kyrtzidis 
448fa0734ecSArgyrios Kyrtzidis   return G;
449fa0734ecSArgyrios Kyrtzidis }
450fa0734ecSArgyrios Kyrtzidis 
451