Skip to content

Commit 08b5c5c

Browse files
authored
Merge pull request #61290 from atrick/valuesetvector
Add NodeSetVector and ValueSetVector
2 parents ee773d9 + 578de7b commit 08b5c5c

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===--- NodeDatastructures.h -----------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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 Nodes.
14+
//
15+
// TODO: Add an InstructionWorklist similar to BasicBlockWorklist.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_SIL_NODEDATASTRUCTURES_H
20+
#define SWIFT_SIL_NODEDATASTRUCTURES_H
21+
22+
#include "swift/SIL/NodeBits.h"
23+
#include "swift/SIL/StackList.h"
24+
25+
namespace swift {
26+
27+
/// An implementation of `llvm::SetVector<SILNode *,
28+
/// StackList<SILNode *>,
29+
/// NodeSet>`.
30+
///
31+
/// Unfortunately it's not possible to use `llvm::SetVector` directly because
32+
/// the ValueSet and StackList constructors needs a `SILFunction` argument.
33+
///
34+
/// Note: This class does not provide a `remove` method intentionally, because
35+
/// it would have a O(n) complexity.
36+
class NodeSetVector {
37+
StackList<SILNode *> vector;
38+
NodeSet set;
39+
40+
public:
41+
using iterator = typename StackList<SILNode *>::iterator;
42+
43+
NodeSetVector(SILFunction *function) : vector(function), set(function) {}
44+
45+
iterator begin() const { return vector.begin(); }
46+
iterator end() const { return vector.end(); }
47+
48+
llvm::iterator_range<iterator> getRange() const {
49+
return llvm::make_range(begin(), end());
50+
}
51+
52+
bool empty() const { return vector.empty(); }
53+
54+
bool contains(SILNode *node) const { return set.contains(node); }
55+
56+
/// Returns true if \p value was not contained in the set before inserting.
57+
bool insert(SILNode *node) {
58+
if (set.insert(node)) {
59+
vector.push_back(node);
60+
return true;
61+
}
62+
return false;
63+
}
64+
};
65+
66+
/// An implementation of `llvm::SetVector<SILValue,
67+
/// StackList<SILValue>,
68+
/// ValueSet>`.
69+
///
70+
/// Unfortunately it's not possible to use `llvm::SetVector` directly because
71+
/// the ValueSet and StackList constructors needs a `SILFunction` argument.
72+
///
73+
/// Note: This class does not provide a `remove` method intentionally, because
74+
/// it would have a O(n) complexity.
75+
class ValueSetVector {
76+
StackList<SILValue> vector;
77+
ValueSet set;
78+
79+
public:
80+
using iterator = typename StackList<SILValue>::iterator;
81+
82+
ValueSetVector(SILFunction *function) : vector(function), set(function) {}
83+
84+
iterator begin() const { return vector.begin(); }
85+
iterator end() const { return vector.end(); }
86+
87+
llvm::iterator_range<iterator> getRange() const {
88+
return llvm::make_range(begin(), end());
89+
}
90+
91+
bool empty() const { return vector.empty(); }
92+
93+
bool contains(SILValue value) const { return set.contains(value); }
94+
95+
/// Returns true if \p value was not contained in the set before inserting.
96+
bool insert(SILValue value) {
97+
if (set.insert(value)) {
98+
vector.push_back(value);
99+
return true;
100+
}
101+
return false;
102+
}
103+
};
104+
105+
} // namespace swift
106+
107+
#endif

0 commit comments

Comments
 (0)