Skip to content

Commit 9311e94

Browse files
committed
Avoid unnecessary block arguments in SimplifyCFG::threadEdge and SimplifyCFG::simplifyThreadedTerminators
1 parent bbd6a87 commit 9311e94

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,13 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
233233
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, {UED});
234234
} else {
235235
assert(SEI->getDefaultBB() == ThreadedSuccessorBlock);
236-
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock,
237-
SEI->getOperand());
236+
auto *OldBlockArg = ThreadedSuccessorBlock->getArgument(0);
237+
OldBlockArg->replaceAllUsesWith(SEI->getOperand());
238+
ThreadedSuccessorBlock->eraseArgument(0);
239+
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock);
238240
}
239241
} else {
240-
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock,
241-
ArrayRef<SILValue>());
242+
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock);
242243
}
243244
SEI->eraseFromParent();
244245
}
@@ -502,11 +503,12 @@ bool SimplifyCFG::simplifyThreadedTerminators() {
502503
if (auto *EI = dyn_cast<EnumInst>(SEI->getOperand())) {
503504
LLVM_DEBUG(llvm::dbgs() << "simplify threaded " << *SEI);
504505
auto *LiveBlock = SEI->getCaseDestination(EI->getElement());
505-
if (EI->hasOperand() && !LiveBlock->args_empty()) {
506-
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock,
507-
EI->getOperand());
508-
} else if (!LiveBlock->args_empty()) {
509-
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock, {EI});
506+
if (!LiveBlock->args_empty()) {
507+
auto *LiveBlockArg = LiveBlock->getArgument(0);
508+
auto NewValue = EI->hasOperand() ? EI->getOperand() : EI;
509+
LiveBlockArg->replaceAllUsesWith(NewValue);
510+
LiveBlock->eraseArgument(0);
511+
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock);
510512
} else {
511513
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock);
512514
}

0 commit comments

Comments
 (0)