Skip to content

Commit b87e203

Browse files
committed
[cast-opt] Hoist the AnyHashable bail-out as early as possible.
We were doing unneeded work and then bailing. This is NFC.
1 parent d00dcaa commit b87e203

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,25 @@ getObjCToSwiftBridgingFunction(SILOptFunctionBuilder &funcBuilder,
7272
/// _ObjectiveCBridgeable.
7373
SILInstruction *
7474
CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
75+
CanType target = dynamicCast.getTargetType();
76+
auto &mod = dynamicCast.getModule();
77+
78+
// AnyHashable is a special case that we do not handle since we only handle
79+
// objc targets in this function. Bailout early.
80+
if (auto dt = target.getNominalOrBoundGenericNominal()) {
81+
if (dt == mod.getASTContext().getAnyHashableDecl()) {
82+
return nullptr;
83+
}
84+
}
7585

7686
SILInstruction *Inst = dynamicCast.getInstruction();
7787
bool isConditional = dynamicCast.isConditional();
7888
SILValue Src = dynamicCast.getSource();
7989
SILValue Dest = dynamicCast.getDest();
80-
CanType Target = dynamicCast.getTargetType();
8190
CanType BridgedTargetTy = dynamicCast.getBridgedTargetType();
8291
SILBasicBlock *SuccessBB = dynamicCast.getSuccessBlock();
8392
SILBasicBlock *FailureBB = dynamicCast.getFailureBlock();
8493
auto *F = Inst->getFunction();
85-
auto &M = Inst->getModule();
8694
auto Loc = Inst->getLoc();
8795

8896
// The conformance to _BridgedToObjectiveC is statically known.
@@ -103,14 +111,6 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
103111
assert(Src->getType().isAddress() && "Source should have an address type");
104112
assert(Dest->getType().isAddress() && "Source should have an address type");
105113

106-
// AnyHashable is a special case - it does not conform to NSObject -
107-
// If AnyHashable - Bail out of the optimization
108-
if (auto DT = Target.getNominalOrBoundGenericNominal()) {
109-
if (DT == M.getASTContext().getAnyHashableDecl()) {
110-
return nullptr;
111-
}
112-
}
113-
114114
// If this is a conditional cast:
115115
// We need a new fail BB in order to add a dealloc_stack to it
116116
SILBasicBlock *ConvFailBB = nullptr;
@@ -130,7 +130,7 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
130130
// - then convert _ObjectiveCBridgeable._ObjectiveCType to
131131
// a Swift type using _forceBridgeFromObjectiveC.
132132

133-
if (!Src->getType().isLoadable(M)) {
133+
if (!Src->getType().isLoadable(mod)) {
134134
// This code path is never reached in current test cases
135135
// If reached, we'd have to convert from an ObjC Any* to a loadable type
136136
// Should use check_addr / make a source we can actually load
@@ -177,25 +177,25 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
177177

178178
// Lookup the _ObjectiveCBridgeable protocol.
179179
auto BridgedProto =
180-
M.getASTContext().getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
181-
auto Conf = *M.getSwiftModule()->lookupConformance(Target, BridgedProto);
180+
mod.getASTContext().getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
181+
auto Conf = *mod.getSwiftModule()->lookupConformance(target, BridgedProto);
182182

183183
auto ParamTypes = bridgingFunc->getLoweredFunctionType()->getParameters();
184184

185185
auto *FuncRef = Builder.createFunctionRef(Loc, bridgingFunc);
186186

187-
auto MetaTy = MetatypeType::get(Target, MetatypeRepresentation::Thick);
187+
auto MetaTy = MetatypeType::get(target, MetatypeRepresentation::Thick);
188188
auto SILMetaTy = F->getTypeLowering(MetaTy).getLoweredType();
189189
auto *MetaTyVal = Builder.createMetatype(Loc, SILMetaTy);
190190
SmallVector<SILValue, 1> Args;
191191

192192
// Add substitutions
193193
auto SubMap = SubstitutionMap::getProtocolSubstitutions(Conf.getRequirement(),
194-
Target, Conf);
194+
target, Conf);
195195

196196
auto SILFnTy = FuncRef->getType();
197-
SILType SubstFnTy = SILFnTy.substGenericArgs(M, SubMap);
198-
SILFunctionConventions substConv(SubstFnTy.castTo<SILFunctionType>(), M);
197+
SILType SubstFnTy = SILFnTy.substGenericArgs(mod, SubMap);
198+
SILFunctionConventions substConv(SubstFnTy.castTo<SILFunctionType>(), mod);
199199

200200
// Temporary to hold the intermediate result.
201201
AllocStackInst *Tmp = nullptr;
@@ -263,7 +263,7 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
263263
SILBasicBlock *ConvSuccessBB = Inst->getFunction()->createBasicBlock();
264264
SmallVector<std::pair<EnumElementDecl *, SILBasicBlock *>, 1> CaseBBs;
265265
CaseBBs.push_back(
266-
std::make_pair(M.getASTContext().getOptionalNoneDecl(), FailureBB));
266+
std::make_pair(mod.getASTContext().getOptionalNoneDecl(), FailureBB));
267267
Builder.createSwitchEnumAddr(Loc, InOutOptionalParam, ConvSuccessBB,
268268
CaseBBs);
269269

0 commit comments

Comments
 (0)