|
| 1 | +//===--- BasicBlockDatastructures.h -----------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines efficient data structures for working with BasicBlocks. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_SIL_BASICBLOCKDATASTRUCTURES_H |
| 18 | +#define SWIFT_SIL_BASICBLOCKDATASTRUCTURES_H |
| 19 | + |
| 20 | +#include "swift/SIL/StackList.h" |
| 21 | +#include "swift/SIL/BasicBlockBits.h" |
| 22 | + |
| 23 | +namespace swift { |
| 24 | + |
| 25 | +/// An implementation of `llvm::SetVector<SILBasicBlock *, |
| 26 | +/// StackList<SILBasicBlock *>, |
| 27 | +/// BasicBlockSet>`. |
| 28 | +/// |
| 29 | +/// Unfortunately it's not possible to use `llvm::SetVector` directly because |
| 30 | +/// the BasicBlockSet and StackList constructors needs a `SILFunction` argument. |
| 31 | +/// |
| 32 | +/// Note: This class does not provide a `remove` method intentinally, because |
| 33 | +/// it would have a O(n) complexity. |
| 34 | +class BasicBlockSetVector { |
| 35 | + StackList<SILBasicBlock *> vector; |
| 36 | + BasicBlockSet set; |
| 37 | + |
| 38 | +public: |
| 39 | + using iterator = typename StackList<SILBasicBlock *>::iterator; |
| 40 | + |
| 41 | + BasicBlockSetVector(SILFunction *function) : vector(function), set(function) {} |
| 42 | + |
| 43 | + iterator begin() const { return vector.begin(); } |
| 44 | + iterator end() const { return vector.end(); } |
| 45 | + |
| 46 | + bool empty() const { return vector.empty(); } |
| 47 | + |
| 48 | + bool contains(SILBasicBlock *block) const { return set.contains(block); } |
| 49 | + |
| 50 | + /// Returns true if \p block was not contained in the set before inserting. |
| 51 | + bool insert(SILBasicBlock *block) { |
| 52 | + if (set.insert(block)) { |
| 53 | + vector.push_back(block); |
| 54 | + return true; |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +/// A utility for processing basic blocks in a worklist. |
| 61 | +/// |
| 62 | +/// It is basically a combination of a block vector and a block set. It can be |
| 63 | +/// used for typical worklist-processing algorithms. |
| 64 | +class BasicBlockWorklist { |
| 65 | + StackList<SILBasicBlock *> worklist; |
| 66 | + BasicBlockSet visited; |
| 67 | + |
| 68 | +public: |
| 69 | + /// Construct an empty worklist. |
| 70 | + BasicBlockWorklist(SILFunction *function) |
| 71 | + : worklist(function), visited(function) {} |
| 72 | + |
| 73 | + /// Initialize the worklist with \p initialBlock. |
| 74 | + BasicBlockWorklist(SILBasicBlock *initialBlock) |
| 75 | + : BasicBlockWorklist(initialBlock->getParent()) { |
| 76 | + push(initialBlock); |
| 77 | + } |
| 78 | + |
| 79 | + /// Pops the last added element from the worklist or returns null, if the |
| 80 | + /// worklist is empty. |
| 81 | + SILBasicBlock *pop() { |
| 82 | + if (worklist.empty()) |
| 83 | + return nullptr; |
| 84 | + return worklist.pop_back_val(); |
| 85 | + } |
| 86 | + |
| 87 | + /// Pushes \p block onto the worklist if \p block has never been push before. |
| 88 | + bool pushIfNotVisited(SILBasicBlock *block) { |
| 89 | + if (visited.insert(block)) { |
| 90 | + worklist.push_back(block); |
| 91 | + return true; |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /// Like `pushIfNotVisited`, but requires that \p block has never been on the |
| 97 | + /// worklist before. |
| 98 | + void push(SILBasicBlock *block) { |
| 99 | + assert(!visited.contains(block)); |
| 100 | + visited.insert(block); |
| 101 | + worklist.push_back(block); |
| 102 | + } |
| 103 | + |
| 104 | + /// Like `pop`, but marks the returned block as "unvisited". This means, that |
| 105 | + /// the block can be pushed onto the worklist again. |
| 106 | + SILBasicBlock *popAndForget() { |
| 107 | + if (worklist.empty()) |
| 108 | + return nullptr; |
| 109 | + SILBasicBlock *block = worklist.pop_back_val(); |
| 110 | + visited.erase(block); |
| 111 | + return block; |
| 112 | + } |
| 113 | + |
| 114 | + /// Returns true if \p block was visited, i.e. has been added to the worklist. |
| 115 | + bool isVisited(SILBasicBlock *block) const { return visited.contains(block); } |
| 116 | +}; |
| 117 | + |
| 118 | +} // namespace swift |
| 119 | + |
| 120 | +#endif |
0 commit comments