Index: clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h =================================================================== --- clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -186,6 +186,7 @@ BFS, UnexploredFirst, UnexploredFirstQueue, + UnexploredFirstLocationQueue, BFSBlockDFSContents, NotSet }; Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h =================================================================== --- clang/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h +++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h @@ -85,6 +85,7 @@ static std::unique_ptr makeBFSBlockDFSContents(); static std::unique_ptr makeUnexploredFirst(); static std::unique_ptr makeUnexploredFirstPriorityQueue(); + static std::unique_ptr makeUnexploredFirstPriorityLocationQueue(); }; } // end ento namespace Index: clang/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp +++ clang/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp @@ -77,6 +77,8 @@ ExplorationStrategyKind::UnexploredFirst) .Case("unexplored_first_queue", ExplorationStrategyKind::UnexploredFirstQueue) + .Case("unexplored_first_location_queue", + ExplorationStrategyKind::UnexploredFirstLocationQueue) .Case("bfs_block_dfs_contents", ExplorationStrategyKind::BFSBlockDFSContents) .Default(ExplorationStrategyKind::NotSet); Index: clang/lib/StaticAnalyzer/Core/CoreEngine.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/CoreEngine.cpp +++ clang/lib/StaticAnalyzer/Core/CoreEngine.cpp @@ -53,7 +53,8 @@ // Core analysis engine. //===----------------------------------------------------------------------===// -static std::unique_ptr generateWorkList(AnalyzerOptions &Opts) { +static std::unique_ptr generateWorkList(AnalyzerOptions &Opts, + SubEngine &subengine) { switch (Opts.getExplorationStrategy()) { case AnalyzerOptions::ExplorationStrategyKind::DFS: return WorkList::makeDFS(); @@ -65,6 +66,8 @@ return WorkList::makeUnexploredFirst(); case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstQueue: return WorkList::makeUnexploredFirstPriorityQueue(); + case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstLocationQueue: + return WorkList::makeUnexploredFirstPriorityLocationQueue(); default: llvm_unreachable("Unexpected case"); } @@ -72,7 +75,7 @@ CoreEngine::CoreEngine(SubEngine &subengine, FunctionSummariesTy *FS, AnalyzerOptions &Opts) - : SubEng(subengine), WList(generateWorkList(Opts)), + : SubEng(subengine), WList(generateWorkList(Opts, subengine)), BCounterFactory(G.getAllocator()), FunctionSummaries(FS) {} /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps. Index: clang/lib/StaticAnalyzer/Core/WorkList.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/WorkList.cpp +++ clang/lib/StaticAnalyzer/Core/WorkList.cpp @@ -252,3 +252,63 @@ std::unique_ptr WorkList::makeUnexploredFirstPriorityQueue() { return llvm::make_unique(); } + +namespace { +class UnexploredFirstPriorityLocationQueue : public WorkList { + using LocIdentifier = int; + + // How many times each location was visited. + // Is signed because we negate it later in order to have a reversed + // comparison. + using VisitedTimesMap = llvm::DenseMap; + + // 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). + using QueuePriority = std::pair; + using QueueItem = std::pair; + + 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, 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()) + NumVisited = NumReached[BE->getBlock()->getBlockID()]++; + + 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::makeUnexploredFirstPriorityLocationQueue() { + return llvm::make_unique(); +}