@@ -603,6 +603,23 @@ SILCombiner::optimizeConcatenationOfStringLiterals(ApplyInst *AI) {
603
603
return tryToConcatenateStrings (AI, Builder);
604
604
}
605
605
606
+ // / This routine replaces the old witness method inst with a new one.
607
+ void SILCombiner::replaceWitnessMethodInst (
608
+ FullApplySite Apply, WitnessMethodInst *WMI, SILBuilderContext &BuilderCtx,
609
+ const CanType &ConcreteType, const ProtocolConformanceRef ConformanceRef) {
610
+ SILBuilderWithScope WMIBuilder (WMI, BuilderCtx);
611
+ auto *NewWMI = WMIBuilder.createWitnessMethod (
612
+ WMI->getLoc (), ConcreteType, ConformanceRef, WMI->getMember (),
613
+ WMI->getType ());
614
+ MutableArrayRef<Operand> Operands = Apply.getInstruction ()->getAllOperands ();
615
+ for (auto &Op : Operands) {
616
+ if (Op.get () == WMI)
617
+ Op.set (NewWMI);
618
+ }
619
+ if (WMI->use_empty ())
620
+ eraseInstFromFunction (*WMI);
621
+ }
622
+
606
623
// / Given an Apply and an argument value produced by InitExistentialAddrInst,
607
624
// / return true if the argument can be replaced by a copy of its value.
608
625
// /
@@ -712,8 +729,10 @@ SILCombiner::createApplyWithConcreteType(FullApplySite Apply,
712
729
}
713
730
// The apply can only be rewritten in terms of the concrete value if it is
714
731
// legal to pass that value as the self argument.
715
- if (CEI.isCopied && !canReplaceCopiedSelf (Apply, CEI.InitExistential , DA))
732
+ if (CEI.isCopied && (!CEI.InitExistential ||
733
+ !canReplaceCopiedSelf (Apply, CEI.InitExistential , DA))) {
716
734
return nullptr ;
735
+ }
717
736
718
737
// Create a set of arguments.
719
738
SmallVector<SILValue, 8 > NewArgs;
@@ -819,24 +838,10 @@ SILCombiner::propagateConcreteTypeOfInitExistential(FullApplySite Apply,
819
838
// are stuck:
820
839
// We will re-create the same instruction and re-populate the worklist
821
840
// with it.
822
- if (CEI.ConcreteType != WMI->getLookupType ()
823
- || SelfConformance != WMI->getConformance ()) {
824
- SILBuilderWithScope WMIBuilder (WMI, BuilderCtx);
825
- // Keep around the dependence on the open instruction unless we've
826
- // actually eliminated the use.
827
- auto *NewWMI = WMIBuilder.createWitnessMethod (
828
- WMI->getLoc (), CEI.ConcreteType , SelfConformance, WMI->getMember (),
829
- WMI->getType ());
830
- // Replace only uses of the witness_method in the apply that was analyzed by
831
- // ConcreteExistentialInfo.
832
- MutableArrayRef<Operand> Operands =
833
- Apply.getInstruction ()->getAllOperands ();
834
- for (auto &Op : Operands) {
835
- if (Op.get () == WMI)
836
- Op.set (NewWMI);
837
- }
838
- if (WMI->use_empty ())
839
- eraseInstFromFunction (*WMI);
841
+ if (CEI.ConcreteType != WMI->getLookupType () ||
842
+ SelfConformance != WMI->getConformance ()) {
843
+ replaceWitnessMethodInst (Apply, WMI, BuilderCtx, CEI.ConcreteType ,
844
+ SelfConformance);
840
845
}
841
846
// Try to rewrite the apply.
842
847
return createApplyWithConcreteType (Apply, CEI, BuilderCtx);
0 commit comments