|
| 1 | +//===--- OperanDatastructures.h -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 Operands. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_SIL_OPERANDDATASTRUCTURES_H |
| 18 | +#define SWIFT_SIL_OPERANDDATASTRUCTURES_H |
| 19 | + |
| 20 | +#include "swift/SIL/OperandBits.h" |
| 21 | +#include "swift/SIL/StackList.h" |
| 22 | + |
| 23 | +namespace swift { |
| 24 | + |
| 25 | +/// An implementation of `llvm::SetVector<Operand *, |
| 26 | +/// StackList<Operand *>, |
| 27 | +/// OperandSet>`. |
| 28 | +/// |
| 29 | +/// Unfortunately it's not possible to use `llvm::SetVector` directly because |
| 30 | +/// the OperandSet and StackList constructors needs a `SILFunction` |
| 31 | +/// argument. |
| 32 | +/// |
| 33 | +/// Note: This class does not provide a `remove` method intentionally, because |
| 34 | +/// it would have a O(n) complexity. |
| 35 | +class OperandSetVector { |
| 36 | + StackList<Operand *> vector; |
| 37 | + OperandSet set; |
| 38 | + |
| 39 | +public: |
| 40 | + using iterator = typename StackList<Operand *>::iterator; |
| 41 | + |
| 42 | + OperandSetVector(SILFunction *function) : vector(function), set(function) {} |
| 43 | + |
| 44 | + iterator begin() const { return vector.begin(); } |
| 45 | + iterator end() const { return vector.end(); } |
| 46 | + |
| 47 | + llvm::iterator_range<iterator> getRange() const { |
| 48 | + return llvm::make_range(begin(), end()); |
| 49 | + } |
| 50 | + |
| 51 | + bool empty() const { return vector.empty(); } |
| 52 | + |
| 53 | + bool contains(Operand *instruction) const { |
| 54 | + return set.contains(instruction); |
| 55 | + } |
| 56 | + |
| 57 | + /// Returns true if \p instruction was not contained in the set before |
| 58 | + /// inserting. |
| 59 | + bool insert(Operand *instruction) { |
| 60 | + if (set.insert(instruction)) { |
| 61 | + vector.push_back(instruction); |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +/// A utility for processing instructions in a worklist. |
| 69 | +/// |
| 70 | +/// It is basically a combination of an instruction vector and an instruction |
| 71 | +/// set. It can be used for typical worklist-processing algorithms. |
| 72 | +class OperandWorklist { |
| 73 | + StackList<Operand *> worklist; |
| 74 | + OperandSet visited; |
| 75 | + |
| 76 | +public: |
| 77 | + /// Construct an empty worklist. |
| 78 | + OperandWorklist(SILFunction *function) |
| 79 | + : worklist(function), visited(function) {} |
| 80 | + |
| 81 | + /// Initialize the worklist with \p initialOperand. |
| 82 | + OperandWorklist(Operand *initialOperand) |
| 83 | + : OperandWorklist(initialOperand->getUser()->getFunction()) { |
| 84 | + push(initialOperand); |
| 85 | + } |
| 86 | + |
| 87 | + /// Pops the last added element from the worklist or returns null, if the |
| 88 | + /// worklist is empty. |
| 89 | + Operand *pop() { |
| 90 | + if (worklist.empty()) |
| 91 | + return nullptr; |
| 92 | + return worklist.pop_back_val(); |
| 93 | + } |
| 94 | + |
| 95 | + /// Pushes \p operand onto the worklist if \p operand has never been |
| 96 | + /// push before. |
| 97 | + bool pushIfNotVisited(Operand *operand) { |
| 98 | + if (visited.insert(operand)) { |
| 99 | + worklist.push_back(operand); |
| 100 | + return true; |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + /// Pushes the operands of all uses of \p instruction onto the worklist if the |
| 106 | + /// operands have never been pushed before. Returns \p true if we inserted |
| 107 | + /// /any/ values. |
| 108 | + /// |
| 109 | + /// This is a bulk convenience API. |
| 110 | + bool pushResultOperandsIfNotVisited(SILInstruction *inst) { |
| 111 | + bool insertedOperand = false; |
| 112 | + for (auto result : inst->getResults()) { |
| 113 | + for (auto *use : result->getUses()) { |
| 114 | + insertedOperand |= pushIfNotVisited(use); |
| 115 | + } |
| 116 | + } |
| 117 | + return insertedOperand; |
| 118 | + } |
| 119 | + |
| 120 | + /// Like `pushIfNotVisited`, but requires that \p operand has never been |
| 121 | + /// on the worklist before. |
| 122 | + void push(Operand *operand) { |
| 123 | + assert(!visited.contains(operand)); |
| 124 | + visited.insert(operand); |
| 125 | + worklist.push_back(operand); |
| 126 | + } |
| 127 | + |
| 128 | + /// Like `pop`, but marks the returned operand as "unvisited". This means, |
| 129 | + /// that the operand can be pushed onto the worklist again. |
| 130 | + Operand *popAndForget() { |
| 131 | + if (worklist.empty()) |
| 132 | + return nullptr; |
| 133 | + Operand *operand = worklist.pop_back_val(); |
| 134 | + visited.erase(operand); |
| 135 | + return operand; |
| 136 | + } |
| 137 | + |
| 138 | + /// Returns true if \p operand was visited, i.e. has been added to the |
| 139 | + /// worklist. |
| 140 | + bool isVisited(Operand *operand) const { return visited.contains(operand); } |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace swift |
| 144 | + |
| 145 | +#endif |
0 commit comments