Skip to content

Commit 5abe8e5

Browse files
committed
Fix SimplifyCFG::simplifyArgs for OSSA
1 parent 554cd84 commit 5abe8e5

File tree

5 files changed

+274
-122
lines changed

5 files changed

+274
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ struct ShrinkBorrowScopeTest : UnitTest {
270270
}
271271
};
272272

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

446472
// SimplifyCFG unit tests
473+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-argument",
474+
SimplifyCFGSimplifyArgument)
475+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-block-args",
476+
SimplifyCFGSimplifyBlockArgs)
447477
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum",
448478
SimplifyCFGCanonicalizeSwitchEnum)
449479
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)