@@ -217,6 +217,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
217217 ArrayRef<SILValue> entryArgs,
218218 bool replaceOriginalFunctionInPlace = false );
219219
220+ void cloneFunctionBody (SILFunction *F);
221+
220222 // / Clone all blocks in this function and all instructions in those
221223 // / blocks.
222224 // /
@@ -835,9 +837,31 @@ void SILCloner<ImplClass>::cloneFunctionBody(SILFunction *F,
835837 commonFixUp (F);
836838}
837839
840+ template <typename ImplClass>
841+ void SILCloner<ImplClass>::cloneFunctionBody(SILFunction *F) {
842+ assert (!Builder.getFunction ().empty () && " Expect the entry block to already be created" );
843+
844+ assert (F != &Builder.getFunction () && " Must clone into a new function." );
845+ assert (BBMap.empty () && " This API does not allow clients to map blocks." );
846+
847+ SILBasicBlock *clonedEntryBB = Builder.getFunction ().getEntryBlock ();
848+ BBMap.insert (std::make_pair (&*F->begin (), clonedEntryBB));
849+
850+ Builder.setInsertionPoint (clonedEntryBB);
851+
852+ // This will layout all newly cloned blocks immediate after clonedEntryBB.
853+ visitBlocksDepthFirst (&*F->begin ());
854+
855+ commonFixUp (F);
856+ }
857+
838858template <typename ImplClass>
839859void SILCloner<ImplClass>::cloneFunction(SILFunction *origF) {
840860 SILFunction *newF = &Builder.getFunction ();
861+ if (!newF->empty ()) {
862+ cloneFunctionBody (origF);
863+ return ;
864+ }
841865
842866 auto *newEntryBB = newF->createBasicBlock ();
843867
@@ -861,23 +885,12 @@ template <typename ImplClass>
861885void SILCloner<ImplClass>::cloneFunctionBody(
862886 SILFunction *F, SILBasicBlock *clonedEntryBB, ArrayRef<SILValue> entryArgs,
863887 llvm::function_ref<SILValue(SILValue)> entryArgIndexToOldArgIndex) {
864- assert (F != clonedEntryBB->getParent () && " Must clone into a new function." );
865- assert (BBMap.empty () && " This API does not allow clients to map blocks." );
866888 assert (ValueMap.empty () && " Stale ValueMap." );
867-
868889 assert (entryArgs.size () == F->getArguments ().size ());
869890 for (unsigned i = 0 , e = entryArgs.size (); i != e; ++i) {
870891 ValueMap[entryArgIndexToOldArgIndex (entryArgs[i])] = entryArgs[i];
871892 }
872-
873- BBMap.insert (std::make_pair (&*F->begin (), clonedEntryBB));
874-
875- Builder.setInsertionPoint (clonedEntryBB);
876-
877- // This will layout all newly cloned blocks immediate after clonedEntryBB.
878- visitBlocksDepthFirst (&*F->begin ());
879-
880- commonFixUp (F);
893+ cloneFunctionBody (F);
881894}
882895
883896template <typename ImplClass>
0 commit comments