|
14 | 14 | //
|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 |
|
17 |
| -#ifndef SWIFT_SIL_BASICBLOCKBITS_H |
18 |
| -#define SWIFT_SIL_BASICBLOCKBITS_H |
| 17 | +#ifndef SWIFT_SIL_SILBITFIELD_H |
| 18 | +#define SWIFT_SIL_SILBITFIELD_H |
19 | 19 |
|
20 | 20 | #include "swift/SIL/SILFunction.h"
|
21 | 21 | #include "llvm/ADT/SmallVector.h"
|
@@ -173,6 +173,101 @@ class BasicBlockSet {
|
173 | 173 | void erase(SILBasicBlock *block) { flag.reset(block); }
|
174 | 174 | };
|
175 | 175 |
|
| 176 | +/// An implementation of `llvm::SetVector<SILBasicBlock *, |
| 177 | +/// SmallVector<SILBasicBlock *, N>, |
| 178 | +/// BasicBlockSet>`. |
| 179 | +/// |
| 180 | +/// Unfortunately it's not possible to use `llvm::SetVector` directly because |
| 181 | +/// the BasicBlockSet constructor needs a `SILFunction` argument. |
| 182 | +/// |
| 183 | +/// Note: This class does not provide a `remove` method intentinally, because |
| 184 | +/// it would have a O(n) complexity. |
| 185 | +template <unsigned N> class BasicBlockSetVector { |
| 186 | + using Vector = llvm::SmallVector<SILBasicBlock *, N>; |
| 187 | + |
| 188 | + Vector vector; |
| 189 | + BasicBlockSet set; |
| 190 | + |
| 191 | +public: |
| 192 | + using iterator = typename Vector::const_iterator; |
| 193 | + |
| 194 | + BasicBlockSetVector(SILFunction *function) : set(function) {} |
| 195 | + |
| 196 | + iterator begin() const { return vector.begin(); } |
| 197 | + iterator end() const { return vector.end(); } |
| 198 | + |
| 199 | + unsigned size() const { return vector.size(); } |
| 200 | + bool empty() const { return vector.empty(); } |
| 201 | + |
| 202 | + bool contains(SILBasicBlock *block) const { return set.contains(block); } |
| 203 | + |
| 204 | + /// Returns true if \p block was not contained in the set before inserting. |
| 205 | + bool insert(SILBasicBlock *block) { |
| 206 | + if (set.insert(block)) { |
| 207 | + vector.push_back(block); |
| 208 | + return true; |
| 209 | + } |
| 210 | + return false; |
| 211 | + } |
| 212 | +}; |
| 213 | + |
| 214 | +/// A utility for processing basic blocks in a worklist. |
| 215 | +/// |
| 216 | +/// It is basically a combination of a block vector and a block set. It can be |
| 217 | +/// used for typical worklist-processing algorithms. |
| 218 | +template <unsigned N> class BasicBlockWorklist { |
| 219 | + llvm::SmallVector<SILBasicBlock *, N> worklist; |
| 220 | + BasicBlockSet visited; |
| 221 | + |
| 222 | +public: |
| 223 | + /// Construct an empty worklist. |
| 224 | + BasicBlockWorklist(SILFunction *function) : visited(function) {} |
| 225 | + |
| 226 | + /// Initialize the worklist with \p initialBlock. |
| 227 | + BasicBlockWorklist(SILBasicBlock *initialBlock) |
| 228 | + : visited(initialBlock->getParent()) { |
| 229 | + push(initialBlock); |
| 230 | + } |
| 231 | + |
| 232 | + /// Pops the last added element from the worklist or returns null, if the |
| 233 | + /// worklist is empty. |
| 234 | + SILBasicBlock *pop() { |
| 235 | + if (worklist.empty()) |
| 236 | + return nullptr; |
| 237 | + return worklist.pop_back_val(); |
| 238 | + } |
| 239 | + |
| 240 | + /// Pushes \p block onto the worklist if \p block has never been push before. |
| 241 | + bool pushIfNotVisited(SILBasicBlock *block) { |
| 242 | + if (visited.insert(block)) { |
| 243 | + worklist.push_back(block); |
| 244 | + return true; |
| 245 | + } |
| 246 | + return false; |
| 247 | + } |
| 248 | + |
| 249 | + /// Like `pushIfNotVisited`, but requires that \p block has never been on the |
| 250 | + /// worklist before. |
| 251 | + void push(SILBasicBlock *block) { |
| 252 | + assert(!visited.contains(block)); |
| 253 | + visited.insert(block); |
| 254 | + worklist.push_back(block); |
| 255 | + } |
| 256 | + |
| 257 | + /// Like `pop`, but marks the returned block as "unvisited". This means, that |
| 258 | + /// the block can be pushed onto the worklist again. |
| 259 | + SILBasicBlock *popAndForget() { |
| 260 | + if (worklist.empty()) |
| 261 | + return nullptr; |
| 262 | + SILBasicBlock *block = worklist.pop_back_val(); |
| 263 | + visited.erase(block); |
| 264 | + return block; |
| 265 | + } |
| 266 | + |
| 267 | + /// Returns true if \p block was visited, i.e. has been added to the worklist. |
| 268 | + bool isVisited(SILBasicBlock *block) const { return visited.contains(block); } |
| 269 | +}; |
| 270 | + |
176 | 271 | } // namespace swift
|
177 | 272 |
|
178 | 273 | #endif
|
0 commit comments