sanitizer_bvgraph.h 4.6 KB

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