Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 1 | //===-- sanitizer_bvgraph.h -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of Sanitizer runtime. |
| 11 | // BVGraph -- a directed graph. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SANITIZER_BVGRAPH_H |
| 16 | #define SANITIZER_BVGRAPH_H |
| 17 | |
| 18 | #include "sanitizer_common.h" |
| 19 | #include "sanitizer_bitvector.h" |
| 20 | |
| 21 | namespace __sanitizer { |
| 22 | |
| 23 | // Directed graph of fixed size implemented as an array of bit vectors. |
Kostya Serebryany | e233d86 | 2014-02-14 12:08:23 +0000 | [diff] [blame] | 24 | // Not thread-safe, all accesses should be protected by an external lock. |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 25 | template<class BV> |
| 26 | class BVGraph { |
| 27 | public: |
| 28 | enum SizeEnum { kSize = BV::kSize }; |
| 29 | uptr size() const { return kSize; } |
| 30 | // No CTOR. |
| 31 | void clear() { |
| 32 | for (uptr i = 0; i < size(); i++) |
| 33 | v[i].clear(); |
| 34 | } |
| 35 | |
Kostya Serebryany | 7332558 | 2014-02-17 11:21:52 +0000 | [diff] [blame^] | 36 | bool empty() const { |
| 37 | for (uptr i = 0; i < size(); i++) |
| 38 | if (!v[i].empty()) |
| 39 | return false; |
| 40 | return true; |
| 41 | } |
| 42 | |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 43 | // Returns true if a new edge was added. |
| 44 | bool addEdge(uptr from, uptr to) { |
Kostya Serebryany | 07526fb | 2014-02-13 12:39:21 +0000 | [diff] [blame] | 45 | check(from, to); |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 46 | return v[from].setBit(to); |
| 47 | } |
| 48 | |
Kostya Serebryany | ec68429 | 2014-02-17 08:47:48 +0000 | [diff] [blame] | 49 | // Returns true if at least one new edge was added. |
| 50 | bool addEdges(const BV &from, uptr to) { |
| 51 | bool res = false; |
| 52 | t1.copyFrom(from); |
| 53 | while (!t1.empty()) |
| 54 | if (v[t1.getAndClearFirstOne()].setBit(to)) |
| 55 | res = true; |
| 56 | return res; |
| 57 | } |
| 58 | |
Kostya Serebryany | 7332558 | 2014-02-17 11:21:52 +0000 | [diff] [blame^] | 59 | // Returns true if the edge from=>to was removed. |
| 60 | bool removeEdge(uptr from, uptr to) { |
| 61 | return v[from].clearBit(to); |
| 62 | } |
| 63 | |
| 64 | // Returns true if at least one edge *=>to was removed. |
| 65 | bool removeEdgesTo(const BV &to) { |
| 66 | bool res = 0; |
| 67 | for (uptr from = 0; from < size(); from++) { |
| 68 | if (v[from].setDifference(to)) |
| 69 | res = true; |
| 70 | } |
| 71 | return res; |
| 72 | } |
| 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 | |
| 88 | void removeEdgesFrom(uptr from) { |
| 89 | return v[from].clear(); |
| 90 | } |
| 91 | |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 92 | bool hasEdge(uptr from, uptr to) const { |
Kostya Serebryany | 07526fb | 2014-02-13 12:39:21 +0000 | [diff] [blame] | 93 | check(from, to); |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 94 | return v[from].getBit(to); |
| 95 | } |
| 96 | |
| 97 | // Returns true if there is a path from the node 'from' |
Kostya Serebryany | 5e52d48 | 2014-02-13 07:44:51 +0000 | [diff] [blame] | 98 | // to any of the nodes in 'targets'. |
| 99 | bool isReachable(uptr from, const BV &targets) { |
Kostya Serebryany | e233d86 | 2014-02-14 12:08:23 +0000 | [diff] [blame] | 100 | BV &to_visit = t1, |
| 101 | &visited = t2; |
Kostya Serebryany | f6cb35a | 2014-02-13 09:52:15 +0000 | [diff] [blame] | 102 | to_visit.copyFrom(v[from]); |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 103 | visited.clear(); |
| 104 | visited.setBit(from); |
| 105 | while (!to_visit.empty()) { |
| 106 | uptr idx = to_visit.getAndClearFirstOne(); |
| 107 | if (visited.setBit(idx)) |
| 108 | to_visit.setUnion(v[idx]); |
| 109 | } |
Kostya Serebryany | 5e52d48 | 2014-02-13 07:44:51 +0000 | [diff] [blame] | 110 | return targets.intersectsWith(visited); |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kostya Serebryany | f6cb35a | 2014-02-13 09:52:15 +0000 | [diff] [blame] | 113 | // Finds a path from 'from' to one of the nodes in 'target', |
| 114 | // stores up to 'path_size' items of the path into 'path', |
| 115 | // returns the path length, or 0 if there is no path of size 'path_size'. |
| 116 | uptr findPath(uptr from, const BV &targets, uptr *path, uptr path_size) { |
| 117 | if (path_size == 0) |
| 118 | return 0; |
| 119 | path[0] = from; |
| 120 | if (targets.getBit(from)) |
| 121 | return 1; |
Kostya Serebryany | e233d86 | 2014-02-14 12:08:23 +0000 | [diff] [blame] | 122 | // The function is recursive, so we don't want to create BV on stack. |
| 123 | // Instead of a getAndClearFirstOne loop we use the slower iterator. |
| 124 | for (typename BV::Iterator it(v[from]); it.hasNext(); ) { |
| 125 | uptr idx = it.next(); |
Kostya Serebryany | f6cb35a | 2014-02-13 09:52:15 +0000 | [diff] [blame] | 126 | if (uptr res = findPath(idx, targets, path + 1, path_size - 1)) |
| 127 | return res + 1; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 132 | private: |
Kostya Serebryany | 67d4197 | 2014-02-13 15:45:20 +0000 | [diff] [blame] | 133 | void check(uptr idx1, uptr idx2) const { |
| 134 | CHECK_LT(idx1, size()); |
| 135 | CHECK_LT(idx2, size()); |
| 136 | } |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 137 | BV v[kSize]; |
Kostya Serebryany | e233d86 | 2014-02-14 12:08:23 +0000 | [diff] [blame] | 138 | // Keep temporary vectors here since we can not create large objects on stack. |
| 139 | BV t1, t2; |
Kostya Serebryany | be1d22b | 2014-02-12 11:28:09 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // namespace __sanitizer |
| 143 | |
| 144 | #endif // SANITIZER_BVGRAPH_H |