Path: blob/main/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_bvgraph.h
35233 views
//===-- sanitizer_bvgraph.h -------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of Sanitizer runtime.9// BVGraph -- a directed graph.10//11//===----------------------------------------------------------------------===//1213#ifndef SANITIZER_BVGRAPH_H14#define SANITIZER_BVGRAPH_H1516#include "sanitizer_common.h"17#include "sanitizer_bitvector.h"1819namespace __sanitizer {2021// Directed graph of fixed size implemented as an array of bit vectors.22// Not thread-safe, all accesses should be protected by an external lock.23template<class BV>24class BVGraph {25public:26enum SizeEnum : uptr { kSize = BV::kSize };27uptr size() const { return kSize; }28// No CTOR.29void clear() {30for (uptr i = 0; i < size(); i++)31v[i].clear();32}3334bool empty() const {35for (uptr i = 0; i < size(); i++)36if (!v[i].empty())37return false;38return true;39}4041// Returns true if a new edge was added.42bool addEdge(uptr from, uptr to) {43check(from, to);44return v[from].setBit(to);45}4647// Returns true if at least one new edge was added.48uptr addEdges(const BV &from, uptr to, uptr added_edges[],49uptr max_added_edges) {50uptr res = 0;51t1.copyFrom(from);52while (!t1.empty()) {53uptr node = t1.getAndClearFirstOne();54if (v[node].setBit(to))55if (res < max_added_edges)56added_edges[res++] = node;57}58return res;59}6061// *EXPERIMENTAL*62// Returns true if an edge from=>to exist.63// This function does not use any global state except for 'this' itself,64// and thus can be called from different threads w/o locking.65// This would be racy.66// FIXME: investigate how much we can prove about this race being "benign".67bool hasEdge(uptr from, uptr to) { return v[from].getBit(to); }6869// Returns true if the edge from=>to was removed.70bool removeEdge(uptr from, uptr to) {71return v[from].clearBit(to);72}7374// Returns true if at least one edge *=>to was removed.75bool removeEdgesTo(const BV &to) {76bool res = 0;77for (uptr from = 0; from < size(); from++) {78if (v[from].setDifference(to))79res = true;80}81return res;82}8384// Returns true if at least one edge from=>* was removed.85bool removeEdgesFrom(const BV &from) {86bool res = false;87t1.copyFrom(from);88while (!t1.empty()) {89uptr idx = t1.getAndClearFirstOne();90if (!v[idx].empty()) {91v[idx].clear();92res = true;93}94}95return res;96}9798void removeEdgesFrom(uptr from) {99return v[from].clear();100}101102bool hasEdge(uptr from, uptr to) const {103check(from, to);104return v[from].getBit(to);105}106107// Returns true if there is a path from the node 'from'108// to any of the nodes in 'targets'.109bool isReachable(uptr from, const BV &targets) {110BV &to_visit = t1,111&visited = t2;112to_visit.copyFrom(v[from]);113visited.clear();114visited.setBit(from);115while (!to_visit.empty()) {116uptr idx = to_visit.getAndClearFirstOne();117if (visited.setBit(idx))118to_visit.setUnion(v[idx]);119}120return targets.intersectsWith(visited);121}122123// Finds a path from 'from' to one of the nodes in 'target',124// stores up to 'path_size' items of the path into 'path',125// returns the path length, or 0 if there is no path of size 'path_size'.126uptr findPath(uptr from, const BV &targets, uptr *path, uptr path_size) {127if (path_size == 0)128return 0;129path[0] = from;130if (targets.getBit(from))131return 1;132// The function is recursive, so we don't want to create BV on stack.133// Instead of a getAndClearFirstOne loop we use the slower iterator.134for (typename BV::Iterator it(v[from]); it.hasNext(); ) {135uptr idx = it.next();136if (uptr res = findPath(idx, targets, path + 1, path_size - 1))137return res + 1;138}139return 0;140}141142// Same as findPath, but finds a shortest path.143uptr findShortestPath(uptr from, const BV &targets, uptr *path,144uptr path_size) {145for (uptr p = 1; p <= path_size; p++)146if (findPath(from, targets, path, p) == p)147return p;148return 0;149}150151private:152void check(uptr idx1, uptr idx2) const {153CHECK_LT(idx1, size());154CHECK_LT(idx2, size());155}156BV v[kSize];157// Keep temporary vectors here since we can not create large objects on stack.158BV t1, t2;159};160161} // namespace __sanitizer162163#endif // SANITIZER_BVGRAPH_H164165166