@@ -1939,89 +1939,94 @@ static void prepareCallArguments(ApplySite AI, SILBuilder &Builder,
1939
1939
1940
1940
// / Create a new apply based on an old one, but with a different
1941
1941
// / function being applied.
1942
- static ApplySite replaceWithSpecializedCallee (ApplySite AI ,
1943
- SILValue Callee ,
1944
- const ReabstractionInfo &ReInfo ) {
1945
- SILBuilderWithScope Builder (AI .getInstruction ());
1946
- SILLocation Loc = AI .getLoc ();
1947
- SmallVector<SILValue, 4 > Arguments ;
1948
- SILValue StoreResultTo ;
1942
+ static ApplySite replaceWithSpecializedCallee (ApplySite applySite ,
1943
+ SILValue callee ,
1944
+ const ReabstractionInfo &reInfo ) {
1945
+ SILBuilderWithScope builder (applySite .getInstruction ());
1946
+ SILLocation loc = applySite .getLoc ();
1947
+ SmallVector<SILValue, 4 > arguments ;
1948
+ SILValue resultOut ;
1949
1949
1950
- prepareCallArguments (AI, Builder, ReInfo, Arguments, StoreResultTo );
1950
+ prepareCallArguments (applySite, builder, reInfo, arguments, resultOut );
1951
1951
1952
1952
// Create a substituted callee type.
1953
- auto CanFnTy = Callee->getType ().castTo <SILFunctionType>();
1954
- SubstitutionMap Subs;
1955
- if (ReInfo.getSpecializedType ()->isPolymorphic ()) {
1956
- Subs = ReInfo.getCallerParamSubstitutionMap ();
1957
- Subs = SubstitutionMap::get (CanFnTy->getSubstGenericSignature (), Subs);
1958
- }
1959
-
1960
- auto CalleeSubstFnTy =
1961
- CanFnTy->substGenericArgs (*Callee->getModule (), Subs,
1962
- ReInfo.getResilienceExpansion ());
1963
- auto CalleeSILSubstFnTy = SILType::getPrimitiveObjectType (CalleeSubstFnTy);
1964
- SILFunctionConventions substConv (CalleeSubstFnTy, Builder.getModule ());
1965
-
1966
- if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
1967
- SILBasicBlock *ResultBB = TAI->getNormalBB ();
1968
- assert (ResultBB->getSinglePredecessorBlock () == TAI->getParent ());
1969
- auto *NewTAI = Builder.createTryApply (Loc, Callee, Subs, Arguments,
1970
- ResultBB, TAI->getErrorBB ());
1971
- if (StoreResultTo) {
1953
+ auto canFnTy = callee->getType ().castTo <SILFunctionType>();
1954
+ SubstitutionMap subs;
1955
+ if (reInfo.getSpecializedType ()->isPolymorphic ()) {
1956
+ subs = reInfo.getCallerParamSubstitutionMap ();
1957
+ subs = SubstitutionMap::get (canFnTy->getSubstGenericSignature (), subs);
1958
+ }
1959
+
1960
+ auto calleeSubstFnTy = canFnTy->substGenericArgs (
1961
+ *callee->getModule (), subs, reInfo.getResilienceExpansion ());
1962
+ auto calleeSILSubstFnTy = SILType::getPrimitiveObjectType (calleeSubstFnTy);
1963
+ SILFunctionConventions substConv (calleeSubstFnTy, builder.getModule ());
1964
+
1965
+ switch (applySite.getKind ()) {
1966
+ case ApplySiteKind::TryApplyInst: {
1967
+ auto *tai = cast<TryApplyInst>(applySite);
1968
+ SILBasicBlock *resultBlock = tai->getNormalBB ();
1969
+ assert (resultBlock->getSinglePredecessorBlock () == tai->getParent ());
1970
+ auto *newTAI = builder.createTryApply (loc, callee, subs, arguments,
1971
+ resultBlock, tai->getErrorBB ());
1972
+ if (resultOut) {
1972
1973
assert (substConv.useLoweredAddresses ());
1973
1974
// The original normal result of the try_apply is an empty tuple.
1974
- assert (ResultBB ->getNumArguments () == 1 );
1975
- Builder .setInsertionPoint (ResultBB ->begin ());
1976
- fixUsedVoidType (ResultBB ->getArgument (0 ), Loc, Builder );
1975
+ assert (resultBlock ->getNumArguments () == 1 );
1976
+ builder .setInsertionPoint (resultBlock ->begin ());
1977
+ fixUsedVoidType (resultBlock ->getArgument (0 ), loc, builder );
1977
1978
1978
- SILArgument *Arg = ResultBB->replacePhiArgument (
1979
- 0 , StoreResultTo->getType ().getObjectType (),
1980
- ValueOwnershipKind::Owned);
1979
+ SILArgument *arg = resultBlock->replacePhiArgument (
1980
+ 0 , resultOut->getType ().getObjectType (), ValueOwnershipKind::Owned);
1981
1981
// Store the direct result to the original result address.
1982
- Builder .createStore (Loc, Arg, StoreResultTo ,
1982
+ builder .createStore (loc, arg, resultOut ,
1983
1983
StoreOwnershipQualifier::Unqualified);
1984
1984
}
1985
- return NewTAI ;
1985
+ return newTAI ;
1986
1986
}
1987
- if (auto *A = dyn_cast<ApplyInst>(AI)) {
1988
- auto *NewAI = Builder.createApply (Loc, Callee, Subs, Arguments,
1989
- A->isNonThrowing ());
1990
- if (StoreResultTo) {
1987
+ case ApplySiteKind::ApplyInst: {
1988
+ auto *ai = cast<ApplyInst>(applySite);
1989
+ auto *newAI =
1990
+ builder.createApply (loc, callee, subs, arguments, ai->isNonThrowing ());
1991
+ if (resultOut) {
1991
1992
assert (substConv.useLoweredAddresses ());
1992
- if (!CalleeSILSubstFnTy .isNoReturnFunction (
1993
- Builder .getModule (), Builder .getTypeExpansionContext ())) {
1993
+ if (!calleeSILSubstFnTy .isNoReturnFunction (
1994
+ builder .getModule (), builder .getTypeExpansionContext ())) {
1994
1995
// Store the direct result to the original result address.
1995
- fixUsedVoidType (A, Loc, Builder );
1996
- Builder .createStore (Loc, NewAI, StoreResultTo ,
1996
+ fixUsedVoidType (ai, loc, builder );
1997
+ builder .createStore (loc, newAI, resultOut ,
1997
1998
StoreOwnershipQualifier::Unqualified);
1998
1999
} else {
1999
- Builder .createUnreachable (Loc );
2000
+ builder .createUnreachable (loc );
2000
2001
// unreachable should be the terminator instruction.
2001
2002
// So, split the current basic block right after the
2002
2003
// inserted unreachable instruction.
2003
- Builder .getInsertionPoint ()->getParent ()->split (
2004
- Builder .getInsertionPoint ());
2004
+ builder .getInsertionPoint ()->getParent ()->split (
2005
+ builder .getInsertionPoint ());
2005
2006
}
2006
2007
}
2007
- A ->replaceAllUsesWith (NewAI );
2008
- return NewAI ;
2008
+ ai ->replaceAllUsesWith (newAI );
2009
+ return newAI ;
2009
2010
}
2010
- if (auto *A = dyn_cast<BeginApplyInst>(AI)) {
2011
- assert (!StoreResultTo);
2012
- auto *NewAI = Builder.createBeginApply (Loc, Callee, Subs, Arguments,
2013
- A->isNonThrowing ());
2014
- A->replaceAllUsesPairwiseWith (NewAI);
2015
- return NewAI;
2011
+ case ApplySiteKind::BeginApplyInst: {
2012
+ auto *bai = cast<BeginApplyInst>(applySite);
2013
+ assert (!resultOut);
2014
+ auto *newBAI = builder.createBeginApply (loc, callee, subs, arguments,
2015
+ bai->isNonThrowing ());
2016
+ bai->replaceAllUsesPairwiseWith (newBAI);
2017
+ return newBAI;
2016
2018
}
2017
- if (auto *PAI = dyn_cast<PartialApplyInst>(AI)) {
2018
- auto *NewPAI = Builder.createPartialApply (
2019
- Loc, Callee, Subs, Arguments,
2020
- PAI->getType ().getAs <SILFunctionType>()->getCalleeConvention (),
2021
- PAI->isOnStack ());
2022
- PAI->replaceAllUsesWith (NewPAI);
2023
- return NewPAI;
2019
+ case ApplySiteKind::PartialApplyInst: {
2020
+ auto *pai = cast<PartialApplyInst>(applySite);
2021
+ auto *newPAI = builder.createPartialApply (
2022
+ loc, callee, subs, arguments,
2023
+ pai->getType ().getAs <SILFunctionType>()->getCalleeConvention (),
2024
+ pai->isOnStack ());
2025
+ pai->replaceAllUsesWith (newPAI);
2026
+ return newPAI;
2024
2027
}
2028
+ }
2029
+
2025
2030
llvm_unreachable (" unhandled kind of apply" );
2026
2031
}
2027
2032
0 commit comments