Skip to content

Commit cdbd0f4

Browse files
committed
Fix SimplifyCFG::simplifyArgs for OSSA
1 parent 3660a1e commit cdbd0f4

File tree

5 files changed

+272
-122
lines changed

5 files changed

+272
-122
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,16 +3811,21 @@ bool SimplifyCFG::simplifyArgument(SILBasicBlock *BB, unsigned i) {
38113811
// the uses in this block, and then rewrite the branch operands.
38123812
LLVM_DEBUG(llvm::dbgs() << "unwrap argument:" << *A);
38133813
A->replaceAllUsesWith(SILUndef::get(A->getType(), *BB->getParent()));
3814-
auto *NewArg =
3815-
BB->replacePhiArgument(i, proj->getType(), OwnershipKind::Owned);
3814+
auto *NewArg = BB->replacePhiArgument(i, proj->getType(),
3815+
BB->getArgument(i)->getOwnershipKind());
38163816
proj->replaceAllUsesWith(NewArg);
38173817

38183818
// Rewrite the branch operand for each incoming branch.
38193819
for (auto *Pred : BB->getPredecessorBlocks()) {
38203820
if (auto *Branch = cast<BranchInst>(Pred->getTerminator())) {
3821+
auto *BranchOpValue = cast<SingleValueInstruction>(Branch->getOperand(i));
38213822
auto V = getInsertedValue(cast<SingleValueInstruction>(Branch->getArg(i)),
38223823
proj);
38233824
Branch->setOperand(i, V);
3825+
if (isInstructionTriviallyDead(BranchOpValue)) {
3826+
BranchOpValue->replaceAllUsesWithUndef();
3827+
BranchOpValue->eraseFromParent();
3828+
}
38243829
addToWorklist(Pred);
38253830
}
38263831
}
@@ -3866,15 +3871,6 @@ bool SimplifyCFG::simplifyArgs(SILBasicBlock *BB) {
38663871
if (BB->pred_empty())
38673872
return false;
38683873

3869-
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
3870-
// TODO: OSSA phi support
3871-
if (llvm::any_of(BB->getArguments(), [this](SILArgument *arg) {
3872-
return !arg->getType().isTrivial(Fn);
3873-
})) {
3874-
return false;
3875-
}
3876-
}
3877-
38783874
// Ignore blocks that are successors of terminators with mandatory args.
38793875
for (SILBasicBlock *pred : BB->getPredecessorBlocks()) {
38803876
if (hasMandatoryArgument(pred->getTerminator()))

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ struct ShrinkBorrowScopeTest : UnitTest {
270270
}
271271
};
272272

273+
struct SimplifyCFGSimplifyArgument : UnitTest {
274+
SimplifyCFGSimplifyArgument(UnitTestRunner *pass) : UnitTest(pass) {}
275+
void invoke(Arguments &arguments) override {
276+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
277+
passToRun->injectPassManager(getPass()->getPassManager());
278+
passToRun->injectFunction(getFunction());
279+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
280+
/*EnableJumpThread=*/false)
281+
.simplifyArgument(arguments.takeBlock(), arguments.takeUInt());
282+
}
283+
};
284+
285+
struct SimplifyCFGSimplifyBlockArgs : UnitTest {
286+
SimplifyCFGSimplifyBlockArgs(UnitTestRunner *pass) : UnitTest(pass) {}
287+
void invoke(Arguments &arguments) override {
288+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
289+
passToRun->injectPassManager(getPass()->getPassManager());
290+
passToRun->injectFunction(getFunction());
291+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
292+
/*EnableJumpThread=*/false)
293+
.simplifyBlockArgs();
294+
}
295+
};
296+
273297
struct SimplifyCFGCanonicalizeSwitchEnum : UnitTest {
274298
SimplifyCFGCanonicalizeSwitchEnum(UnitTestRunner *pass) : UnitTest(pass) {}
275299
void invoke(Arguments &arguments) override {
@@ -478,6 +502,10 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
478502
ADD_UNIT_TEST_SUBCLASS("shrink-borrow-scope", ShrinkBorrowScopeTest)
479503

480504
// SimplifyCFG unit tests
505+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-argument",
506+
SimplifyCFGSimplifyArgument)
507+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-block-args",
508+
SimplifyCFGSimplifyBlockArgs)
481509
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum",
482510
SimplifyCFGCanonicalizeSwitchEnum)
483511
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-switch-enum-block",

test/SILOptimizer/simplify_bb_args.sil

Lines changed: 0 additions & 77 deletions
This file was deleted.

test/SILOptimizer/simplify_cfg_args_ossa_disabled.sil

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)