|
| 1 | +//===--- DAGNodeWorklist.h --------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +#ifndef SWIFT_BASIC_DAGNODEWORKLIST_H |
| 14 | +#define SWIFT_BASIC_DAGNODEWORKLIST_H |
| 15 | + |
| 16 | +#include "swift/Basic/LLVM.h" |
| 17 | +#include "llvm/ADT/SmallPtrSet.h" |
| 18 | +#include "llvm/ADT/SmallVector.h" |
| 19 | + |
| 20 | +/// Worklist of pointer-like things that have an invalid default value. This not |
| 21 | +/// only avoids duplicates in the worklist, but also avoids revisiting |
| 22 | +/// already-popped nodes. This makes it suitable for DAG traversal. This can |
| 23 | +/// also be used within hybrid worklist/recursive traversal by recording the |
| 24 | +/// size of the worklist at each level of recursion. |
| 25 | +/// |
| 26 | +/// The primary API has two methods: intialize() and pop(). Others are provided |
| 27 | +/// for flexibility. |
| 28 | +template <typename T, unsigned SmallSize> struct DAGNodeWorklist { |
| 29 | + llvm::SmallPtrSet<T, SmallSize> nodeVisited; |
| 30 | + llvm::SmallVector<T, SmallSize> nodeVector; |
| 31 | + |
| 32 | + DAGNodeWorklist() = default; |
| 33 | + |
| 34 | + DAGNodeWorklist(const DAGNodeWorklist &) = delete; |
| 35 | + |
| 36 | + void initialize(T t) { |
| 37 | + clear(); |
| 38 | + insert(t); |
| 39 | + } |
| 40 | + |
| 41 | + template <typename R> void initializeRange(R &&range) { |
| 42 | + clear(); |
| 43 | + nodeVisited.insert(range.begin(), range.end()); |
| 44 | + nodeVector.append(range.begin(), range.end()); |
| 45 | + } |
| 46 | + |
| 47 | + T pop() { return empty() ? T() : nodeVector.pop_back_val(); } |
| 48 | + |
| 49 | + bool empty() const { return nodeVector.empty(); } |
| 50 | + |
| 51 | + unsigned size() const { return nodeVector.size(); } |
| 52 | + |
| 53 | + void clear() { |
| 54 | + nodeVector.clear(); |
| 55 | + nodeVisited.clear(); |
| 56 | + } |
| 57 | + |
| 58 | + void insert(T t) { |
| 59 | + if (nodeVisited.insert(t).second) |
| 60 | + nodeVector.push_back(t); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +#endif // SWIFT_BASIC_DAGNODEWORKLIST_H |
0 commit comments