Skip to content

Commit 67425a5

Browse files
committed
Swift Optimizer: introduce InstructionSetWithCount
It's based on `InstructionSet` but also provides a `count` property
1 parent 74e4078 commit 67425a5

File tree

1 file changed

+37
-0
lines changed
  • SwiftCompilerSources/Sources/Optimizer/DataStructures

1 file changed

+37
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/Set.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,44 @@ struct SpecificInstructionSet<InstType: Instruction> : IntrusiveSet {
177177
}
178178
}
179179

180+
/// An `InstructionSet` which also provides a `count` property.
181+
struct SpecificInstructionSetWithCount<InstType: Instruction> : IntrusiveSet {
182+
private(set) var count = 0
183+
private var underlyingSet: SpecificInstructionSet<InstType>
184+
185+
init(_ context: some Context) {
186+
self.underlyingSet = SpecificInstructionSet(context)
187+
}
188+
189+
func contains(_ inst: InstType) -> Bool { underlyingSet.contains(inst) }
190+
191+
var isEmpty: Bool { count == 0 }
192+
193+
/// Returns true if `inst` was not contained in the set before inserting.
194+
@discardableResult
195+
mutating func insert(_ inst: InstType) -> Bool {
196+
if underlyingSet.insert(inst) {
197+
count += 1
198+
return true
199+
}
200+
return false
201+
}
202+
203+
mutating func erase(_ inst: InstType) {
204+
if underlyingSet.contains(inst) {
205+
count -= 1
206+
assert(count >= 0)
207+
}
208+
underlyingSet.erase(inst)
209+
}
210+
211+
var description: String { underlyingSet.description }
212+
213+
mutating func deinitialize() { underlyingSet.deinitialize() }
214+
}
215+
180216
typealias InstructionSet = SpecificInstructionSet<Instruction>
217+
typealias InstructionSetWithCount = SpecificInstructionSetWithCount<Instruction>
181218

182219
/// A set of operands.
183220
///

0 commit comments

Comments
 (0)