Skip to content

[cast-opt] Eliminate always true if statement. #23086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions lib/SILOptimizer/Utils/CastOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,55 +111,53 @@ SILInstruction *CastOptimizer::optimizeBridgedObjCToSwiftCast(
Builder.setInsertionPoint(CurrInsPoint);
}

if (SILBridgedTy != Src->getType()) {
// Check if we can simplify a cast into:
// - ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
// - then convert _ObjectiveCBridgeable._ObjectiveCType to
// a Swift type using _forceBridgeFromObjectiveC.

if (!Src->getType().isLoadable(M)) {
// This code path is never reached in current test cases
// If reached, we'd have to convert from an ObjC Any* to a loadable type
// Should use check_addr / make a source we can actually load
return nullptr;
}
// We know this is always true since SILBridgedTy is an object and Src is an
// address.
assert(SILBridgedTy != Src->getType());

// Check if we can simplify a cast into:
// - ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
// - then convert _ObjectiveCBridgeable._ObjectiveCType to
// a Swift type using _forceBridgeFromObjectiveC.

if (!Src->getType().isLoadable(M)) {
// This code path is never reached in current test cases
// If reached, we'd have to convert from an ObjC Any* to a loadable type
// Should use check_addr / make a source we can actually load
return nullptr;
}

// Generate a load for the source argument.
auto *Load =
Builder.createLoad(Loc, Src, LoadOwnershipQualifier::Unqualified);
// Try to convert the source into the expected ObjC type first.
// Generate a load for the source argument.
auto *Load =
Builder.createLoad(Loc, Src, LoadOwnershipQualifier::Unqualified);
// Try to convert the source into the expected ObjC type first.

if (Load->getType() == SILBridgedTy) {
// If type of the source and the expected ObjC type are
// equal, there is no need to generate the conversion
// from ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
if (isConditional) {
SILBasicBlock *CastSuccessBB = F->createBasicBlock();
CastSuccessBB->createPhiArgument(SILBridgedTy,
ValueOwnershipKind::Owned);
Builder.createBranch(Loc, CastSuccessBB, SILValue(Load));
Builder.setInsertionPoint(CastSuccessBB);
SrcOp = CastSuccessBB->getArgument(0);
} else {
SrcOp = Load;
}
} else if (isConditional) {
if (Load->getType() == SILBridgedTy) {
// If type of the source and the expected ObjC type are
// equal, there is no need to generate the conversion
// from ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
if (isConditional) {
SILBasicBlock *CastSuccessBB = F->createBasicBlock();
CastSuccessBB->createPhiArgument(SILBridgedTy, ValueOwnershipKind::Owned);
auto *CCBI = Builder.createCheckedCastBranch(Loc, false, Load,
SILBridgedTy, CastSuccessBB, ConvFailBB);
NewI = CCBI;
splitEdge(CCBI, /* EdgeIdx to ConvFailBB */ 1);
Builder.createBranch(Loc, CastSuccessBB, SILValue(Load));
Builder.setInsertionPoint(CastSuccessBB);
SrcOp = CastSuccessBB->getArgument(0);
} else {
auto cast =
Builder.createUnconditionalCheckedCast(Loc, Load, SILBridgedTy);
NewI = cast;
SrcOp = cast;
SrcOp = Load;
}
} else if (isConditional) {
SILBasicBlock *CastSuccessBB = F->createBasicBlock();
CastSuccessBB->createPhiArgument(SILBridgedTy, ValueOwnershipKind::Owned);
auto *CCBI = Builder.createCheckedCastBranch(Loc, false, Load, SILBridgedTy,
CastSuccessBB, ConvFailBB);
NewI = CCBI;
splitEdge(CCBI, /* EdgeIdx to ConvFailBB */ 1);
Builder.setInsertionPoint(CastSuccessBB);
SrcOp = CastSuccessBB->getArgument(0);
} else {
SrcOp = Src;
auto cast = Builder.createUnconditionalCheckedCast(Loc, Load, SILBridgedTy);
NewI = cast;
SrcOp = cast;
}

// Now emit the a cast from the casted ObjC object into a target type.
Expand Down