Skip to content

Commit 272fd22

Browse files
committed
SILCloner: support cloning the whole function if the client already provided the cloned entry block
1 parent 08696ee commit 272fd22

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
838858
template <typename ImplClass>
839859
void 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>
861885
void 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

883896
template<typename ImplClass>

0 commit comments

Comments
 (0)