Skip to content

Commit de6e539

Browse files
committed
Swift Optimizer: make the set's insert functions return a Bool.
Returning true if the element was not contained in the set before inserting
1 parent fe40707 commit de6e539

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/BasicBlockRange.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ struct BasicBlockRange : CustomStringConvertible, CustomReflectable {
7878

7979
/// Insert a potential end block.
8080
mutating func insert(_ block: BasicBlock) {
81-
if !wasInserted.contains(block) {
82-
wasInserted.insert(block)
81+
if wasInserted.insert(block) {
8382
inserted.append(block)
8483
}
8584
worklist.pushIfNotVisited(block)

SwiftCompilerSources/Sources/Optimizer/DataStructures/BasicBlockWorklist.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ struct BasicBlockWorklist : CustomStringConvertible, CustomReflectable {
3434

3535
/// Pushes \p block onto the worklist if \p block has never been pushed before.
3636
mutating func pushIfNotVisited(_ block: BasicBlock) {
37-
if !pushedBlocks.contains(block) {
38-
pushedBlocks.insert(block);
37+
if pushedBlocks.insert(block) {
3938
worklist.append(block)
4039
}
4140
}

SwiftCompilerSources/Sources/Optimizer/DataStructures/Set.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ struct BasicBlockSet : CustomStringConvertible, CustomReflectable {
3535
BasicBlockSet_contains(bridged, block.bridged) != 0
3636
}
3737

38-
mutating func insert(_ block: BasicBlock) {
39-
BasicBlockSet_insert(bridged, block.bridged)
38+
/// Returns true if `block` was not contained in the set before inserting.
39+
@discardableResult
40+
mutating func insert(_ block: BasicBlock) -> Bool {
41+
BasicBlockSet_insert(bridged, block.bridged) != 0
4042
}
4143

4244
mutating func erase(_ block: BasicBlock) {
@@ -80,8 +82,10 @@ struct ValueSet : CustomStringConvertible, CustomReflectable {
8082
NodeSet_containsValue(bridged, value.bridged) != 0
8183
}
8284

83-
mutating func insert(_ value: Value) {
84-
NodeSet_insertValue(bridged, value.bridged)
85+
/// Returns true if `value` was not contained in the set before inserting.
86+
@discardableResult
87+
mutating func insert(_ value: Value) -> Bool {
88+
NodeSet_insertValue(bridged, value.bridged) != 0
8589
}
8690

8791
mutating func erase(_ value: Value) {
@@ -139,8 +143,10 @@ struct InstructionSet : CustomStringConvertible, CustomReflectable {
139143
NodeSet_containsInstruction(bridged, inst.bridged) != 0
140144
}
141145

142-
mutating func insert(_ inst: Instruction) {
143-
NodeSet_insertInstruction(bridged, inst.bridged)
146+
/// Returns true if `inst` was not contained in the set before inserting.
147+
@discardableResult
148+
mutating func insert(_ inst: Instruction) -> Bool {
149+
NodeSet_insertInstruction(bridged, inst.bridged) != 0
144150
}
145151

146152
mutating func erase(_ inst: Instruction) {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,18 @@ BridgedBasicBlockSet PassContext_allocBasicBlockSet(BridgedPassContext context);
135135
void PassContext_freeBasicBlockSet(BridgedPassContext context,
136136
BridgedBasicBlockSet set);
137137
SwiftInt BasicBlockSet_contains(BridgedBasicBlockSet set, BridgedBasicBlock block);
138-
void BasicBlockSet_insert(BridgedBasicBlockSet set, BridgedBasicBlock block);
138+
SwiftInt BasicBlockSet_insert(BridgedBasicBlockSet set, BridgedBasicBlock block);
139139
void BasicBlockSet_erase(BridgedBasicBlockSet set, BridgedBasicBlock block);
140140
BridgedFunction BasicBlockSet_getFunction(BridgedBasicBlockSet set);
141141

142142
BridgedNodeSet PassContext_allocNodeSet(BridgedPassContext context);
143143
void PassContext_freeNodeSet(BridgedPassContext context,
144144
BridgedNodeSet set);
145145
SwiftInt NodeSet_containsValue(BridgedNodeSet set, BridgedValue value);
146-
void NodeSet_insertValue(BridgedNodeSet set, BridgedValue value);
146+
SwiftInt NodeSet_insertValue(BridgedNodeSet set, BridgedValue value);
147147
void NodeSet_eraseValue(BridgedNodeSet set, BridgedValue value);
148148
SwiftInt NodeSet_containsInstruction(BridgedNodeSet set, BridgedInstruction inst);
149-
void NodeSet_insertInstruction(BridgedNodeSet set, BridgedInstruction inst);
149+
SwiftInt NodeSet_insertInstruction(BridgedNodeSet set, BridgedInstruction inst);
150150
void NodeSet_eraseInstruction(BridgedNodeSet set, BridgedInstruction inst);
151151
BridgedFunction NodeSet_getFunction(BridgedNodeSet set);
152152

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,8 @@ SwiftInt BasicBlockSet_contains(BridgedBasicBlockSet set, BridgedBasicBlock bloc
14691469
return castToBlockSet(set)->contains(castToBasicBlock(block)) ? 1 : 0;
14701470
}
14711471

1472-
void BasicBlockSet_insert(BridgedBasicBlockSet set, BridgedBasicBlock block) {
1473-
castToBlockSet(set)->insert(castToBasicBlock(block));
1472+
SwiftInt BasicBlockSet_insert(BridgedBasicBlockSet set, BridgedBasicBlock block) {
1473+
return castToBlockSet(set)->insert(castToBasicBlock(block)) ? 1 : 0;
14741474
}
14751475

14761476
void BasicBlockSet_erase(BridgedBasicBlockSet set, BridgedBasicBlock block) {
@@ -1494,8 +1494,8 @@ SwiftInt NodeSet_containsValue(BridgedNodeSet set, BridgedValue value) {
14941494
return castToNodeSet(set)->contains(castToSILValue(value)) ? 1 : 0;
14951495
}
14961496

1497-
void NodeSet_insertValue(BridgedNodeSet set, BridgedValue value) {
1498-
castToNodeSet(set)->insert(castToSILValue(value));
1497+
SwiftInt NodeSet_insertValue(BridgedNodeSet set, BridgedValue value) {
1498+
return castToNodeSet(set)->insert(castToSILValue(value)) ? 1 : 0;
14991499
}
15001500

15011501
void NodeSet_eraseValue(BridgedNodeSet set, BridgedValue value) {
@@ -1506,8 +1506,8 @@ SwiftInt NodeSet_containsInstruction(BridgedNodeSet set, BridgedInstruction inst
15061506
return castToNodeSet(set)->contains(castToInst(inst)->asSILNode()) ? 1 : 0;
15071507
}
15081508

1509-
void NodeSet_insertInstruction(BridgedNodeSet set, BridgedInstruction inst) {
1510-
castToNodeSet(set)->insert(castToInst(inst)->asSILNode());
1509+
SwiftInt NodeSet_insertInstruction(BridgedNodeSet set, BridgedInstruction inst) {
1510+
return castToNodeSet(set)->insert(castToInst(inst)->asSILNode()) ? 1 : 0;
15111511
}
15121512

15131513
void NodeSet_eraseInstruction(BridgedNodeSet set, BridgedInstruction inst) {

0 commit comments

Comments
 (0)