@@ -111,55 +111,53 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
111111 Builder.setInsertionPoint (CurrInsPoint);
112112 }
113113
114- if (SILBridgedTy != Src->getType ()) {
115- // Check if we can simplify a cast into:
116- // - ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
117- // - then convert _ObjectiveCBridgeable._ObjectiveCType to
118- // a Swift type using _forceBridgeFromObjectiveC.
119-
120- if (!Src->getType ().isLoadable (M)) {
121- // This code path is never reached in current test cases
122- // If reached, we'd have to convert from an ObjC Any* to a loadable type
123- // Should use check_addr / make a source we can actually load
124- return nullptr ;
125- }
114+ // We know this is always true since SILBridgedTy is an object and Src is an
115+ // address.
116+ assert (SILBridgedTy != Src->getType ());
117+
118+ // Check if we can simplify a cast into:
119+ // - ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
120+ // - then convert _ObjectiveCBridgeable._ObjectiveCType to
121+ // a Swift type using _forceBridgeFromObjectiveC.
122+
123+ if (!Src->getType ().isLoadable (M)) {
124+ // This code path is never reached in current test cases
125+ // If reached, we'd have to convert from an ObjC Any* to a loadable type
126+ // Should use check_addr / make a source we can actually load
127+ return nullptr ;
128+ }
126129
127- // Generate a load for the source argument.
128- auto *Load =
129- Builder.createLoad (Loc, Src, LoadOwnershipQualifier::Unqualified);
130- // Try to convert the source into the expected ObjC type first.
130+ // Generate a load for the source argument.
131+ auto *Load =
132+ Builder.createLoad (Loc, Src, LoadOwnershipQualifier::Unqualified);
133+ // Try to convert the source into the expected ObjC type first.
131134
132- if (Load->getType () == SILBridgedTy) {
133- // If type of the source and the expected ObjC type are
134- // equal, there is no need to generate the conversion
135- // from ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
136- if (isConditional) {
137- SILBasicBlock *CastSuccessBB = F->createBasicBlock ();
138- CastSuccessBB->createPhiArgument (SILBridgedTy,
139- ValueOwnershipKind::Owned);
140- Builder.createBranch (Loc, CastSuccessBB, SILValue (Load));
141- Builder.setInsertionPoint (CastSuccessBB);
142- SrcOp = CastSuccessBB->getArgument (0 );
143- } else {
144- SrcOp = Load;
145- }
146- } else if (isConditional) {
135+ if (Load->getType () == SILBridgedTy) {
136+ // If type of the source and the expected ObjC type are
137+ // equal, there is no need to generate the conversion
138+ // from ObjCTy to _ObjectiveCBridgeable._ObjectiveCType.
139+ if (isConditional) {
147140 SILBasicBlock *CastSuccessBB = F->createBasicBlock ();
148141 CastSuccessBB->createPhiArgument (SILBridgedTy, ValueOwnershipKind::Owned);
149- auto *CCBI = Builder.createCheckedCastBranch (Loc, false , Load,
150- SILBridgedTy, CastSuccessBB, ConvFailBB);
151- NewI = CCBI;
152- splitEdge (CCBI, /* EdgeIdx to ConvFailBB */ 1 );
142+ Builder.createBranch (Loc, CastSuccessBB, SILValue (Load));
153143 Builder.setInsertionPoint (CastSuccessBB);
154144 SrcOp = CastSuccessBB->getArgument (0 );
155145 } else {
156- auto cast =
157- Builder.createUnconditionalCheckedCast (Loc, Load, SILBridgedTy);
158- NewI = cast;
159- SrcOp = cast;
146+ SrcOp = Load;
160147 }
148+ } else if (isConditional) {
149+ SILBasicBlock *CastSuccessBB = F->createBasicBlock ();
150+ CastSuccessBB->createPhiArgument (SILBridgedTy, ValueOwnershipKind::Owned);
151+ auto *CCBI = Builder.createCheckedCastBranch (Loc, false , Load, SILBridgedTy,
152+ CastSuccessBB, ConvFailBB);
153+ NewI = CCBI;
154+ splitEdge (CCBI, /* EdgeIdx to ConvFailBB */ 1 );
155+ Builder.setInsertionPoint (CastSuccessBB);
156+ SrcOp = CastSuccessBB->getArgument (0 );
161157 } else {
162- SrcOp = Src;
158+ auto cast = Builder.createUnconditionalCheckedCast (Loc, Load, SILBridgedTy);
159+ NewI = cast;
160+ SrcOp = cast;
163161 }
164162
165163 // Now emit the a cast from the casted ObjC object into a target type.
0 commit comments