Author: george.karpenkov Date: Mon Feb 26 14:14:18 2018 New Revision: 326136
URL: http://llvm.org/viewvc/llvm-project?rev=326136&view=rev Log: [analyzer] Exploration strategy prioritizing unexplored nodes first See D42775 for discussion. Turns out, just exploring nodes which weren't explored first is not quite enough, as e.g. the first quick traversal resulting in a report can mark everything as "visited", and then subsequent traversals of the same region will get all the pitfalls of DFS. Priority queue-based approach in comparison shows much greater increase in coverage and even performance, without sacrificing memory. Differential Revision: https://reviews.llvm.org/D43354 Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp cfe/trunk/test/Analysis/exploration_order/prefer_unexplored.cc Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h?rev=326136&r1=326135&r2=326136&view=diff ============================================================================== --- cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h (original) +++ cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h Mon Feb 26 14:14:18 2018 @@ -192,6 +192,7 @@ public: DFS, BFS, UnexploredFirst, + UnexploredFirstQueue, BFSBlockDFSContents, NotSet }; Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h?rev=326136&r1=326135&r2=326136&view=diff ============================================================================== --- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h (original) +++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h Mon Feb 26 14:14:18 2018 @@ -84,6 +84,7 @@ public: static std::unique_ptr<WorkList> makeBFS(); static std::unique_ptr<WorkList> makeBFSBlockDFSContents(); static std::unique_ptr<WorkList> makeUnexploredFirst(); + static std::unique_ptr<WorkList> makeUnexploredFirstPriorityQueue(); }; } // end GR namespace Modified: cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp?rev=326136&r1=326135&r2=326136&view=diff ============================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp (original) +++ cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp Mon Feb 26 14:14:18 2018 @@ -68,6 +68,8 @@ AnalyzerOptions::getExplorationStrategy( .Case("bfs", ExplorationStrategyKind::BFS) .Case("unexplored_first", ExplorationStrategyKind::UnexploredFirst) + .Case("unexplored_first_queue", + ExplorationStrategyKind::UnexploredFirstQueue) .Case("bfs_block_dfs_contents", ExplorationStrategyKind::BFSBlockDFSContents) .Default(ExplorationStrategyKind::NotSet); Modified: cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp?rev=326136&r1=326135&r2=326136&view=diff ============================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp (original) +++ cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp Mon Feb 26 14:14:18 2018 @@ -20,7 +20,9 @@ #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/Support/Casting.h" +#include "llvm/ADT/PriorityQueue.h" using namespace clang; using namespace ento; @@ -192,6 +194,66 @@ std::unique_ptr<WorkList> WorkList::make return llvm::make_unique<UnexploredFirstStack>(); } +class UnexploredFirstPriorityQueue : public WorkList { + typedef unsigned BlockID; + typedef std::pair<BlockID, const StackFrameContext *> LocIdentifier; + + // How many times each location was visited. + // Is signed because we negate it later in order to have a reversed + // comparison. + typedef llvm::DenseMap<LocIdentifier, int> VisitedTimesMap; + + // Compare by number of times the location was visited first (negated + // to prefer less often visited locations), then by insertion time (prefer + // expanding nodes inserted sooner first). + typedef std::pair<int, unsigned long> QueuePriority; + typedef std::pair<WorkListUnit, QueuePriority> QueueItem; + + struct ExplorationComparator { + bool operator() (const QueueItem &LHS, const QueueItem &RHS) { + return LHS.second < RHS.second; + } + }; + + // Number of inserted nodes, used to emulate DFS ordering in the priority + // queue when insertions are equal. + unsigned long Counter = 0; + + // Number of times a current location was reached. + VisitedTimesMap NumReached; + + // The top item is the largest one. + llvm::PriorityQueue<QueueItem, std::vector<QueueItem>, ExplorationComparator> + queue; + +public: + bool hasWork() const override { + return !queue.empty(); + } + + void enqueue(const WorkListUnit &U) override { + const ExplodedNode *N = U.getNode(); + unsigned NumVisited = 0; + if (auto BE = N->getLocation().getAs<BlockEntrance>()) { + LocIdentifier LocId = std::make_pair( + BE->getBlock()->getBlockID(), N->getStackFrame()); + NumVisited = NumReached[LocId]++; + } + + queue.push(std::make_pair(U, std::make_pair(-NumVisited, ++Counter))); + } + + WorkListUnit dequeue() override { + QueueItem U = queue.top(); + queue.pop(); + return U.first; + } +}; + +std::unique_ptr<WorkList> WorkList::makeUnexploredFirstPriorityQueue() { + return llvm::make_unique<UnexploredFirstPriorityQueue>(); +} + //===----------------------------------------------------------------------===// // Core analysis engine. //===----------------------------------------------------------------------===// @@ -206,6 +268,8 @@ static std::unique_ptr<WorkList> generat return WorkList::makeBFSBlockDFSContents(); case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirst: return WorkList::makeUnexploredFirst(); + case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstQueue: + return WorkList::makeUnexploredFirstPriorityQueue(); default: llvm_unreachable("Unexpected case"); } Modified: cfe/trunk/test/Analysis/exploration_order/prefer_unexplored.cc URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/exploration_order/prefer_unexplored.cc?rev=326136&r1=326135&r2=326136&view=diff ============================================================================== --- cfe/trunk/test/Analysis/exploration_order/prefer_unexplored.cc (original) +++ cfe/trunk/test/Analysis/exploration_order/prefer_unexplored.cc Mon Feb 26 14:14:18 2018 @@ -1,4 +1,5 @@ // RUN: %clang_analyze_cc1 -w -analyzer-checker=core -analyzer-config exploration_strategy=unexplored_first -analyzer-output=text -verify %s | FileCheck %s +// RUN: %clang_analyze_cc1 -w -analyzer-checker=core -analyzer-config exploration_strategy=unexplored_first_queue -analyzer-output=text -verify %s | FileCheck %s extern int coin(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits