ExplodedGraph.cpp revision 28038f33aa2db4833881fea757a1f0daf85ac02b
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)//=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=// 28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// The LLVM Compiler Infrastructure 48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// License. See LICENSE.TXT for details. 75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// 88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)//===----------------------------------------------------------------------===// 98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// This file defines the template classes ExplodedNode and ExplodedGraph, 118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// which represent a path-sensitive, intra-procedural "exploded graph." 128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h" 178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clang/AST/Stmt.h" 198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clang/AST/ParentMap.h" 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/DenseSet.h" 218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "llvm/ADT/DenseMap.h" 228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "llvm/ADT/SmallVector.h" 238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "llvm/ADT/Statistic.h" 248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <vector> 2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using namespace clang; 2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using namespace ento; 285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Node auditing. 318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)//===----------------------------------------------------------------------===// 328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// An out of line virtual method to provide a home for the class vtable. 348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExplodedNode::Auditor::~Auditor() {} 355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef NDEBUG 378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static ExplodedNode::Auditor* NodeAuditor = 0; 388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif 398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { 418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#ifndef NDEBUG 428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) NodeAuditor = A; 438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif 448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Cleanup. 485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static const unsigned CounterTop = 1000; 518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExplodedGraph::ExplodedGraph() 535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) : NumNodes(0), reclaimNodes(false), reclaimCounter(CounterTop) {} 545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 55effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochExplodedGraph::~ExplodedGraph() {} 5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//===----------------------------------------------------------------------===// 58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Node reclamation. 59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//===----------------------------------------------------------------------===// 60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool ExplodedGraph::shouldCollect(const ExplodedNode *node) { 628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Reclaim all nodes that match *all* the following criteria: 635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // 645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (1) 1 predecessor (that has one successor) 655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (2) 1 successor (that has one predecessor) 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (3) The ProgramPoint is for a PostStmt. 675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (4) There is no 'tag' for the ProgramPoint. 685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (5) The 'store' is the same as the predecessor. 695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (6) The 'GDM' is the same as the predecessor. 705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (7) The LocationContext is the same as the predecessor. 715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (8) The PostStmt is for a non-consumed Stmt or Expr. 725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // (9) The successor is not a CallExpr StmtPoint (so that we would be able to 735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // find it when retrying a call with no inlining). 745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well. 755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Conditions 1 and 2. 778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (node->pred_size() != 1 || node->succ_size() != 1) 788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const ExplodedNode *pred = *(node->pred_begin()); 818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (pred->succ_size() != 1) 828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const ExplodedNode *succ = *(node->succ_begin()); 855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (succ->pred_size() != 1) 868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Condition 3. 898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ProgramPoint progPoint = node->getLocation(); 908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (!isa<PostStmt>(progPoint)) 918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Condition 4. 945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) PostStmt ps = cast<PostStmt>(progPoint); 958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (ps.getTag()) 968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (isa<BinaryOperator>(ps.getStmt())) 998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return false; 1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Conditions 5, 6, and 7. 1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ProgramStateRef state = node->getState(); 1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ProgramStateRef pred_state = pred->getState(); 1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (state->store != pred_state->store || state->GDM != pred_state->GDM || 1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) progPoint.getLocationContext() != pred->getLocationContext()) 1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return false; 1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Condition 8. 1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (const Expr *Ex = dyn_cast<Expr>(ps.getStmt())) { 1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ParentMap &PM = progPoint.getLocationContext()->getParentMap(); 1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (!PM.isConsumedExpr(Ex)) 1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return false; 1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } 1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Condition 9. 1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const ProgramPoint SuccLoc = succ->getLocation(); 1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (const StmtPoint *SP = dyn_cast<StmtPoint>(&SuccLoc)) 1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (CallEvent::mayBeInlined(SP->getStmt())) 119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return false; 1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return true; 12246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 12346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 12446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void ExplodedGraph::collectNode(ExplodedNode *node) { 12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Removing a node means: 1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // (a) changing the predecessors successor to the successor of this node 1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // (b) changing the successors predecessor to the predecessor of this node 1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // (c) Putting 'node' onto freeNodes. 1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert(node->pred_size() == 1 || node->succ_size() == 1); 1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ExplodedNode *pred = *(node->pred_begin()); 131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ExplodedNode *succ = *(node->succ_begin()); 13246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) pred->replaceSuccessor(succ); 1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) succ->replacePredecessor(pred); 13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) FreeNodes.push_back(node); 1358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) Nodes.RemoveNode(node); 136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) --NumNodes; 1378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) node->~ExplodedNode(); 1388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 1398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExplodedGraph::reclaimRecentlyAllocatedNodes() { 1418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (ChangedNodes.empty()) 1428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return; 1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Only periodically relcaim nodes so that we can build up a set of 1458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // nodes that meet the reclamation criteria. Freshly created nodes 1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // by definition have no successor, and thus cannot be reclaimed (see below). 1478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert(reclaimCounter > 0); 1488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (--reclaimCounter != 0) 1498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return; 1508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) reclaimCounter = CounterTop; 1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) for (NodeVector::iterator it = ChangedNodes.begin(), et = ChangedNodes.end(); 1538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) it != et; ++it) { 154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ExplodedNode *node = *it; 1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (shouldCollect(node)) 1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) collectNode(node); 1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } 1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ChangedNodes.clear(); 1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)//===----------------------------------------------------------------------===// 1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// ExplodedNode. 1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)//===----------------------------------------------------------------------===// 1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static inline BumpVector<ExplodedNode*>& getVector(void *P) { 16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P); 1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { 170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) assert (!V->isSink()); 1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) Preds.addNode(V, G); 1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) V->Succs.addNode(this, G); 1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#ifndef NDEBUG 1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (NodeAuditor) NodeAuditor->AddEdge(V, this); 1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif 176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { 1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert(getKind() == Size1); 1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) P = reinterpret_cast<uintptr_t>(node); 1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert(getKind() == Size1); 1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { 1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); 1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) assert(!getFlag()); 1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (getKind() == Size1) { 1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (ExplodedNode *NOld = getNode()) { 1908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) BumpVectorContext &Ctx = G.getNodeAllocator(); 1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) BumpVector<ExplodedNode*> *V = 1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) G.getAllocator().Allocate<BumpVector<ExplodedNode*> >(); 1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) new (V) BumpVector<ExplodedNode*>(Ctx, 4); 1945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); 1965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) V->push_back(NOld, Ctx); 1975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) V->push_back(N, Ctx); 1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) P = reinterpret_cast<uintptr_t>(V) | SizeOther; 1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) assert(getPtr() == (void*) V); 2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) assert(getKind() == SizeOther); 2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) else { 2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) P = reinterpret_cast<uintptr_t>(N); 2045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) assert(getKind() == Size1); 2055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 2065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) else { 2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) assert(getKind() == SizeOther); 2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) getVector(getPtr()).push_back(N, G.getNodeAllocator()); 2105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 2115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)unsigned ExplodedNode::NodeGroup::size() const { 2145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (getFlag()) 2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return 0; 2165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (getKind() == Size1) 2188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return getNode() ? 1 : 0; 2198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) else 220 return getVector(getPtr()).size(); 221} 222 223ExplodedNode **ExplodedNode::NodeGroup::begin() const { 224 if (getFlag()) 225 return NULL; 226 227 if (getKind() == Size1) 228 return (ExplodedNode**) (getPtr() ? &P : NULL); 229 else 230 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin())); 231} 232 233ExplodedNode** ExplodedNode::NodeGroup::end() const { 234 if (getFlag()) 235 return NULL; 236 237 if (getKind() == Size1) 238 return (ExplodedNode**) (getPtr() ? &P+1 : NULL); 239 else { 240 // Dereferencing end() is undefined behaviour. The vector is not empty, so 241 // we can dereference the last elem and then add 1 to the result. 242 return const_cast<ExplodedNode**>(getVector(getPtr()).end()); 243 } 244} 245 246ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, 247 ProgramStateRef State, 248 bool IsSink, 249 bool* IsNew) { 250 // Profile 'State' to determine if we already have an existing node. 251 llvm::FoldingSetNodeID profile; 252 void *InsertPos = 0; 253 254 NodeTy::Profile(profile, L, State, IsSink); 255 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); 256 257 if (!V) { 258 if (!FreeNodes.empty()) { 259 V = FreeNodes.back(); 260 FreeNodes.pop_back(); 261 } 262 else { 263 // Allocate a new node. 264 V = (NodeTy*) getAllocator().Allocate<NodeTy>(); 265 } 266 267 new (V) NodeTy(L, State, IsSink); 268 269 if (reclaimNodes) 270 ChangedNodes.push_back(V); 271 272 // Insert the node into the node set and return it. 273 Nodes.InsertNode(V, InsertPos); 274 ++NumNodes; 275 276 if (IsNew) *IsNew = true; 277 } 278 else 279 if (IsNew) *IsNew = false; 280 281 return V; 282} 283 284std::pair<ExplodedGraph*, InterExplodedGraphMap*> 285ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, 286 llvm::DenseMap<const void*, const void*> *InverseMap) const { 287 288 if (NBeg == NEnd) 289 return std::make_pair((ExplodedGraph*) 0, 290 (InterExplodedGraphMap*) 0); 291 292 assert (NBeg < NEnd); 293 294 OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap()); 295 296 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap); 297 298 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take()); 299} 300 301ExplodedGraph* 302ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources, 303 const ExplodedNode* const* EndSources, 304 InterExplodedGraphMap* M, 305 llvm::DenseMap<const void*, const void*> *InverseMap) const { 306 307 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; 308 Pass1Ty Pass1; 309 310 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty; 311 Pass2Ty& Pass2 = M->M; 312 313 SmallVector<const ExplodedNode*, 10> WL1, WL2; 314 315 // ===- Pass 1 (reverse DFS) -=== 316 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) { 317 assert(*I); 318 WL1.push_back(*I); 319 } 320 321 // Process the first worklist until it is empty. Because it is a std::list 322 // it acts like a FIFO queue. 323 while (!WL1.empty()) { 324 const ExplodedNode *N = WL1.back(); 325 WL1.pop_back(); 326 327 // Have we already visited this node? If so, continue to the next one. 328 if (Pass1.count(N)) 329 continue; 330 331 // Otherwise, mark this node as visited. 332 Pass1.insert(N); 333 334 // If this is a root enqueue it to the second worklist. 335 if (N->Preds.empty()) { 336 WL2.push_back(N); 337 continue; 338 } 339 340 // Visit our predecessors and enqueue them. 341 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) 342 WL1.push_back(*I); 343 } 344 345 // We didn't hit a root? Return with a null pointer for the new graph. 346 if (WL2.empty()) 347 return 0; 348 349 // Create an empty graph. 350 ExplodedGraph* G = MakeEmptyGraph(); 351 352 // ===- Pass 2 (forward DFS to construct the new graph) -=== 353 while (!WL2.empty()) { 354 const ExplodedNode *N = WL2.back(); 355 WL2.pop_back(); 356 357 // Skip this node if we have already processed it. 358 if (Pass2.find(N) != Pass2.end()) 359 continue; 360 361 // Create the corresponding node in the new graph and record the mapping 362 // from the old node to the new node. 363 ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(), 0); 364 Pass2[N] = NewN; 365 366 // Also record the reverse mapping from the new node to the old node. 367 if (InverseMap) (*InverseMap)[NewN] = N; 368 369 // If this node is a root, designate it as such in the graph. 370 if (N->Preds.empty()) 371 G->addRoot(NewN); 372 373 // In the case that some of the intended predecessors of NewN have already 374 // been created, we should hook them up as predecessors. 375 376 // Walk through the predecessors of 'N' and hook up their corresponding 377 // nodes in the new graph (if any) to the freshly created node. 378 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { 379 Pass2Ty::iterator PI = Pass2.find(*I); 380 if (PI == Pass2.end()) 381 continue; 382 383 NewN->addPredecessor(PI->second, *G); 384 } 385 386 // In the case that some of the intended successors of NewN have already 387 // been created, we should hook them up as successors. Otherwise, enqueue 388 // the new nodes from the original graph that should have nodes created 389 // in the new graph. 390 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { 391 Pass2Ty::iterator PI = Pass2.find(*I); 392 if (PI != Pass2.end()) { 393 PI->second->addPredecessor(NewN, *G); 394 continue; 395 } 396 397 // Enqueue nodes to the worklist that were marked during pass 1. 398 if (Pass1.count(*I)) 399 WL2.push_back(*I); 400 } 401 } 402 403 return G; 404} 405 406void InterExplodedGraphMap::anchor() { } 407 408ExplodedNode* 409InterExplodedGraphMap::getMappedNode(const ExplodedNode *N) const { 410 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::const_iterator I = 411 M.find(N); 412 413 return I == M.end() ? 0 : I->second; 414} 415 416