From: George Karpenkov Date: Thu, 11 Oct 2018 22:59:59 +0000 (+0000) Subject: [analyzer] Experiment with an iteration order only based on location, and not using... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0fe246dfa0c6ff3bab36c0e51ed0d731ad0aa899;p=clang [analyzer] Experiment with an iteration order only based on location, and not using the stack frame Differential Revision: https://reviews.llvm.org/D53058 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@344313 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h index 715cc2bf23..8f2a27dfbf 100644 --- a/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ b/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -186,6 +186,7 @@ public: BFS, UnexploredFirst, UnexploredFirstQueue, + UnexploredFirstLocationQueue, BFSBlockDFSContents, NotSet }; diff --git a/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h b/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h index 07edd35ff9..ef3c2694b2 100644 --- a/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h +++ b/include/clang/StaticAnalyzer/Core/PathSensitive/WorkList.h @@ -85,6 +85,7 @@ public: static std::unique_ptr makeBFSBlockDFSContents(); static std::unique_ptr makeUnexploredFirst(); static std::unique_ptr makeUnexploredFirstPriorityQueue(); + static std::unique_ptr makeUnexploredFirstPriorityLocationQueue(); }; } // end ento namespace diff --git a/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp b/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp index c910d31d4b..ac8d4d6b3e 100644 --- a/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp +++ b/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp @@ -77,6 +77,8 @@ AnalyzerOptions::getExplorationStrategy() { 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); diff --git a/lib/StaticAnalyzer/Core/CoreEngine.cpp b/lib/StaticAnalyzer/Core/CoreEngine.cpp index e5a5296e02..e76314972f 100644 --- a/lib/StaticAnalyzer/Core/CoreEngine.cpp +++ b/lib/StaticAnalyzer/Core/CoreEngine.cpp @@ -53,7 +53,8 @@ STATISTIC(NumPathsExplored, // 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 @@ static std::unique_ptr generateWorkList(AnalyzerOptions &Opts) { 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 @@ static std::unique_ptr generateWorkList(AnalyzerOptions &Opts) { 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. diff --git a/lib/StaticAnalyzer/Core/WorkList.cpp b/lib/StaticAnalyzer/Core/WorkList.cpp index 4b227375da..9eb3ee87fb 100644 --- a/lib/StaticAnalyzer/Core/WorkList.cpp +++ b/lib/StaticAnalyzer/Core/WorkList.cpp @@ -252,3 +252,63 @@ public: 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(); +}