Skip to content

Commit 825185d

Browse files
committed
Move PtrWorklist into DAGNodeWorklist.h for use across files.
1 parent 803d96f commit 825185d

File tree

2 files changed

+69
-48
lines changed

2 files changed

+69
-48
lines changed

include/swift/Basic/DAGNodeWorklist.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#ifndef SWIFT_SILOPTIMIZER_UTILS_CANONICALOSSALIFETIME_H
9494
#define SWIFT_SILOPTIMIZER_UTILS_CANONICALOSSALIFETIME_H
9595

96+
#include "swift/Basic/DAGNodeWorklist.h"
9697
#include "swift/Basic/SmallPtrSetVector.h"
9798
#include "swift/SIL/SILInstruction.h"
9899
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
@@ -220,50 +221,6 @@ class CanonicalOSSAConsumeInfo {
220221
SWIFT_ASSERT_ONLY_DECL(void dump() const LLVM_ATTRIBUTE_USED);
221222
};
222223

223-
// Worklist of pointer-like things that have an invalid default value. Avoid
224-
// revisiting nodes--suitable for DAGs, but pops finished nodes without
225-
// preserving them in the vector.
226-
//
227-
// The primary API has two methods: intialize() and pop(). Others are provided
228-
// for flexibility.
229-
//
230-
// TODO: make this a better utility.
231-
template <typename T, unsigned SmallSize> struct PtrWorklist {
232-
SmallPtrSet<T, SmallSize> ptrVisited;
233-
SmallVector<T, SmallSize> ptrVector;
234-
235-
PtrWorklist() = default;
236-
237-
PtrWorklist(const PtrWorklist &) = delete;
238-
239-
void initialize(T t) {
240-
clear();
241-
insert(t);
242-
}
243-
244-
template <typename R> void initializeRange(R &&range) {
245-
clear();
246-
ptrVisited.insert(range.begin(), range.end());
247-
ptrVector.append(range.begin(), range.end());
248-
}
249-
250-
T pop() { return empty() ? T() : ptrVector.pop_back_val(); }
251-
252-
bool empty() const { return ptrVector.empty(); }
253-
254-
unsigned size() const { return ptrVector.size(); }
255-
256-
void clear() {
257-
ptrVector.clear();
258-
ptrVisited.clear();
259-
}
260-
261-
void insert(T t) {
262-
if (ptrVisited.insert(t).second)
263-
ptrVector.push_back(t);
264-
}
265-
};
266-
267224
/// Canonicalize OSSA lifetimes.
268225
///
269226
/// Allows the allocation of analysis state to be reused across calls to
@@ -315,11 +272,11 @@ class CanonicalizeOSSALifetime {
315272
/// outisde the pruned liveness at the time it is discovered.
316273
llvm::SmallPtrSet<DebugValueInst *, 8> debugValues;
317274

318-
/// Reuse a general visited set for def-use traversal.
319-
PtrWorklist<SILValue, 8> defUseWorklist;
275+
/// Visited set for general def-use traversal that prevents revisiting values.
276+
DAGNodeWorklist<SILValue, 8> defUseWorklist;
320277

321-
/// Reuse a general worklist for CFG traversal.
322-
PtrWorklist<SILBasicBlock *, 8> blockWorklist;
278+
/// Visited set general CFG traversal that prevents revisiting blocks.
279+
DAGNodeWorklist<SILBasicBlock *, 8> blockWorklist;
323280

324281
/// Pruned liveness for the extended live range including copies. For this
325282
/// purpose, only consuming instructions are considered "lifetime

0 commit comments

Comments
 (0)