Skip to content

Commit dd9efa8

Browse files
committed
SIL: add block/instruction/value set variants which maintain their current size.
1 parent ce5e2fb commit dd9efa8

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

include/swift/SIL/BasicBlockBits.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class BasicBlockSet {
8989
BasicBlockFlag flag;
9090

9191
public:
92+
using Element = SILBasicBlock *;
93+
9294
BasicBlockSet(SILFunction *function) : flag(function) {}
9395

9496
SILFunction *getFunction() const { return flag.getFunction(); }
@@ -101,6 +103,8 @@ class BasicBlockSet {
101103
void erase(SILBasicBlock *block) { flag.reset(block); }
102104
};
103105

106+
using BasicBlockSetWithSize = KnownSizeSet<BasicBlockSet>;
107+
104108
} // namespace swift
105109

106110
#endif

include/swift/SIL/NodeBits.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class InstructionSet {
9494
NodeSet nodeSet;
9595

9696
public:
97+
using Element = SILInstruction *;
98+
9799
InstructionSet(SILFunction *function) : nodeSet(function) {}
98100

99101
SILFunction *getFunction() const { return nodeSet.getFunction(); }
@@ -106,13 +108,17 @@ class InstructionSet {
106108
void erase(SILInstruction *inst) { nodeSet.erase(inst->asSILNode()); }
107109
};
108110

111+
using InstructionSetWithSize = KnownSizeSet<InstructionSet>;
112+
109113
/// A set of SILValues.
110114
///
111115
/// For details see NodeBitfield.
112116
class ValueSet {
113117
NodeSet nodeSet;
114118

115119
public:
120+
using Element = SILValue;
121+
116122
ValueSet(SILFunction *function) : nodeSet(function) {}
117123

118124
SILFunction *getFunction() const { return nodeSet.getFunction(); }
@@ -125,6 +131,8 @@ class ValueSet {
125131
void erase(SILValue value) { nodeSet.erase(value); }
126132
};
127133

134+
using ValueSetWithSize = KnownSizeSet<ValueSet>;
135+
128136
} // namespace swift
129137

130138
#endif

include/swift/SIL/SILBitfield.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,43 @@ template <class Impl, class T> class SILBitfield {
108108
}
109109
};
110110

111+
/// A set which knowns its size.
112+
///
113+
/// This template adds a size property to a base `Set`.
114+
template <class Set>
115+
class KnownSizeSet {
116+
Set set;
117+
size_t numElements = 0;
118+
public:
119+
using Element = typename Set::Element;
120+
121+
KnownSizeSet(SILFunction *function) : set(function) {}
122+
123+
SILFunction *getFunction() const { return set.getFunction(); }
124+
125+
bool contains(Element element) const { return set.contains(element); }
126+
127+
/// Returns true if \p value was not contained in the set before inserting.
128+
bool insert(Element element) {
129+
if (set.insert(element)) {
130+
numElements += 1;
131+
return true;
132+
}
133+
return false;
134+
}
135+
136+
void erase(Element element) {
137+
if (contains(element)) {
138+
set.erase(element);
139+
assert(numElements > 0);
140+
numElements -= 1;
141+
}
142+
}
143+
144+
bool empty() const { return numElements == 0; }
145+
146+
size_t size() const { return numElements; }
147+
};
111148

112149
} // namespace swift
113150

0 commit comments

Comments
 (0)