Skip to content

Commit b948ccf

Browse files
committed
SwiftCompilerSources: add more APIs to create new basic blocks
* rename `Context.splitBlock(at:)` -> `Context.splitBlock(before:)` * add `Context.splitBlock(after:)` * add `Context.createBlock(after:)`
1 parent 6f6c0a5 commit b948ccf

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
185185
// empty value.
186186
// Create the needed blocks of the `switch_enum` CFG diamond.
187187
let origBlock = bridgeToSwiftCall.parentBlock
188-
let someBlock = context.splitBlock(at: bridgeToSwiftCall)
189-
let noneBlock = context.splitBlock(at: bridgeToSwiftCall)
190-
let continueBlock = context.splitBlock(at: bridgeToSwiftCall)
188+
let someBlock = context.splitBlock(before: bridgeToSwiftCall)
189+
let noneBlock = context.splitBlock(before: bridgeToSwiftCall)
190+
let continueBlock = context.splitBlock(before: bridgeToSwiftCall)
191191

192192

193193
let builder = Builder(atEndOf: origBlock, location: bridgeToSwiftCall.location, context)

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@ extension MutatingContext {
5555
///
5656
/// `inst` and all subsequent instructions are moved to the new block, while all
5757
/// instructions _before_ `inst` remain in the original block.
58-
func splitBlock(at inst: Instruction) -> BasicBlock {
58+
func splitBlock(before inst: Instruction) -> BasicBlock {
5959
notifyBranchesChanged()
60-
return _bridged.splitBlock(inst.bridged).block
60+
return _bridged.splitBlockBefore(inst.bridged).block
61+
}
62+
63+
/// Splits the basic block, which contains `inst`, after `inst` and returns the
64+
/// new block.
65+
///
66+
/// All subsequent instructions after `inst` are moved to the new block, while `inst` and all
67+
/// instructions _before_ `inst` remain in the original block.
68+
func splitBlock(after inst: Instruction) -> BasicBlock {
69+
notifyBranchesChanged()
70+
return _bridged.splitBlockAfter(inst.bridged).block
71+
}
72+
73+
func createBlock(after block: BasicBlock) -> BasicBlock {
74+
notifyBranchesChanged()
75+
return _bridged.createBlockAfter(block.bridged).block
6176
}
6277

6378
func erase(instruction: Instruction) {

SwiftCompilerSources/Sources/Optimizer/TestPasses/TestInstructionIteration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private func handle(instruction: Instruction, _ context: FunctionPassContext) {
6161
case "delete_branches":
6262
deleteAllInstructions(ofType: BranchInst.self, in: instruction.parentBlock, context)
6363
case "split_block":
64-
_ = context.splitBlock(at: instruction)
64+
_ = context.splitBlock(before: instruction)
6565
default:
6666
break
6767
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ struct BridgedPassContext {
190190
bool cfgChanged;
191191
};
192192

193-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock splitBlock(BridgedInstruction bridgedInst) const;
193+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock splitBlockBefore(BridgedInstruction bridgedInst) const;
194+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock splitBlockAfter(BridgedInstruction bridgedInst) const;
195+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock createBlockAfter(BridgedBasicBlock bridgedBlock) const;
194196
BRIDGED_INLINE void eraseInstruction(BridgedInstruction inst) const;
195197
BRIDGED_INLINE void eraseBlock(BridgedBasicBlock block) const;
196198
bool tryOptimizeApplyOfPartialApply(BridgedInstruction closure) const;

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,21 @@ BridgedDiagnosticEngine BridgedPassContext::getDiagnosticEngine() const {
183183

184184
// SIL modifications
185185

186-
BridgedBasicBlock BridgedPassContext::splitBlock(BridgedInstruction bridgedInst) const {
186+
BridgedBasicBlock BridgedPassContext::splitBlockBefore(BridgedInstruction bridgedInst) const {
187187
auto *block = bridgedInst.unbridged()->getParent();
188188
return {block->split(bridgedInst.unbridged()->getIterator())};
189189
}
190190

191+
BridgedBasicBlock BridgedPassContext::splitBlockAfter(BridgedInstruction bridgedInst) const {
192+
auto *block = bridgedInst.unbridged()->getParent();
193+
return {block->split(std::next(bridgedInst.unbridged()->getIterator()))};
194+
}
195+
196+
BridgedBasicBlock BridgedPassContext::createBlockAfter(BridgedBasicBlock bridgedBlock) const {
197+
swift::SILBasicBlock *block = bridgedBlock.unbridged();
198+
return {block->getParent()->createBasicBlockAfter(block)};
199+
}
200+
191201
void BridgedPassContext::eraseInstruction(BridgedInstruction inst) const {
192202
invocation->eraseInstruction(inst.unbridged());
193203
}

0 commit comments

Comments
 (0)