#include "clang/StaticAnalyzer/Core/PathSensitive/WorkList.h"
#include "llvm/ADT/PriorityQueue.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include <deque>
#include <vector>
using namespace clang;
using namespace ento;
#define DEBUG_TYPE "WorkList"
STATISTIC(MaxQueueSize, "Maximum size of the worklist");
STATISTIC(MaxReachableSize, "Maximum size of auxiliary worklist set");
namespace {
class DFS : public WorkList {
SmallVector<WorkListUnit, 20> Stack;
public:
bool hasWork() const override {
return !Stack.empty();
}
void enqueue(const WorkListUnit& U) override {
Stack.push_back(U);
}
WorkListUnit dequeue() override {
assert(!Stack.empty());
const WorkListUnit& U = Stack.back();
Stack.pop_back(); return U;
}
};
class BFS : public WorkList {
std::deque<WorkListUnit> Queue;
public:
bool hasWork() const override {
return !Queue.empty();
}
void enqueue(const WorkListUnit& U) override {
Queue.push_back(U);
}
WorkListUnit dequeue() override {
WorkListUnit U = Queue.front();
Queue.pop_front();
return U;
}
};
}
WorkList::~WorkList() = default;
std::unique_ptr<WorkList> WorkList::makeDFS() {
return std::make_unique<DFS>();
}
std::unique_ptr<WorkList> WorkList::makeBFS() {
return std::make_unique<BFS>();
}
namespace {
class BFSBlockDFSContents : public WorkList {
std::deque<WorkListUnit> Queue;
SmallVector<WorkListUnit, 20> Stack;
public:
bool hasWork() const override {
return !Queue.empty() || !Stack.empty();
}
void enqueue(const WorkListUnit& U) override {
if (U.getNode()->getLocation().getAs<BlockEntrance>())
Queue.push_front(U);
else
Stack.push_back(U);
}
WorkListUnit dequeue() override {
if (!Stack.empty()) {
const WorkListUnit& U = Stack.back();
Stack.pop_back(); return U;
}
assert(!Queue.empty());
WorkListUnit U = Queue.front();
Queue.pop_front();
return U;
}
};
}
std::unique_ptr<WorkList> WorkList::makeBFSBlockDFSContents() {
return std::make_unique<BFSBlockDFSContents>();
}
namespace {
class UnexploredFirstStack : public WorkList {
SmallVector<WorkListUnit, 20> StackUnexplored;
SmallVector<WorkListUnit, 20> StackOthers;
using BlockID = unsigned;
using LocIdentifier = std::pair<BlockID, const StackFrameContext *>;
llvm::DenseSet<LocIdentifier> Reachable;
public:
bool hasWork() const override {
return !(StackUnexplored.empty() && StackOthers.empty());
}
void enqueue(const WorkListUnit &U) override {
const ExplodedNode *N = U.getNode();
auto BE = N->getLocation().getAs<BlockEntrance>();
if (!BE) {
StackUnexplored.push_back(U);
} else {
LocIdentifier LocId = std::make_pair(
BE->getBlock()->getBlockID(),
N->getLocationContext()->getStackFrame());
auto InsertInfo = Reachable.insert(LocId);
if (InsertInfo.second) {
StackUnexplored.push_back(U);
} else {
StackOthers.push_back(U);
}
}
MaxReachableSize.updateMax(Reachable.size());
MaxQueueSize.updateMax(StackUnexplored.size() + StackOthers.size());
}
WorkListUnit dequeue() override {
if (!StackUnexplored.empty()) {
WorkListUnit &U = StackUnexplored.back();
StackUnexplored.pop_back();
return U;
} else {
WorkListUnit &U = StackOthers.back();
StackOthers.pop_back();
return U;
}
}
};
}
std::unique_ptr<WorkList> WorkList::makeUnexploredFirst() {
return std::make_unique<UnexploredFirstStack>();
}
namespace {
class UnexploredFirstPriorityQueue : public WorkList {
using BlockID = unsigned;
using LocIdentifier = std::pair<BlockID, const StackFrameContext *>;
using VisitedTimesMap = llvm::DenseMap<LocIdentifier, int>;
using QueuePriority = std::pair<int, unsigned long>;
using QueueItem = std::pair<WorkListUnit, QueuePriority>;
unsigned long Counter = 0;
VisitedTimesMap NumReached;
llvm::PriorityQueue<QueueItem, std::vector<QueueItem>, llvm::less_second>
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->getLocationContext()->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 std::make_unique<UnexploredFirstPriorityQueue>();
}
namespace {
class UnexploredFirstPriorityLocationQueue : public WorkList {
using LocIdentifier = const CFGBlock *;
using VisitedTimesMap = llvm::DenseMap<LocIdentifier, int>;
using QueuePriority = std::pair<int, unsigned long>;
using QueueItem = std::pair<WorkListUnit, QueuePriority>;
unsigned long Counter = 0;
VisitedTimesMap NumReached;
llvm::PriorityQueue<QueueItem, std::vector<QueueItem>, llvm::less_second>
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>())
NumVisited = NumReached[BE->getBlock()]++;
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::makeUnexploredFirstPriorityLocationQueue() {
return std::make_unique<UnexploredFirstPriorityLocationQueue>();
}