From 600edc32d28884f06039061eccfbcadddf64712d Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Sat, 9 Sep 2017 16:06:24 -0700 Subject: [PATCH] [mandatory-inline] Extract out tryDevirtualizeCode into its own helper function. NFC. Just trying to slim down/refactor this huge loop. rdar://31521023 --- .../Mandatory/MandatoryInlining.cpp | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/SILOptimizer/Mandatory/MandatoryInlining.cpp b/lib/SILOptimizer/Mandatory/MandatoryInlining.cpp index 0a2baf85d8ac3..058669534e56a 100644 --- a/lib/SILOptimizer/Mandatory/MandatoryInlining.cpp +++ b/lib/SILOptimizer/Mandatory/MandatoryInlining.cpp @@ -311,6 +311,28 @@ getCalleeFunction(FullApplySite AI, bool &IsThick, return CalleeFunction; } +static std::tuple +tryDevirtualizeApplyHelper(FullApplySite InnerAI, SILBasicBlock::iterator I, + ClassHierarchyAnalysis *CHA) { + auto NewInstPair = tryDevirtualizeApply(InnerAI, CHA); + auto *NewInst = NewInstPair.first; + if (!NewInst) + return std::make_tuple(InnerAI, I); + + replaceDeadApply(InnerAI, NewInst); + if (auto *II = dyn_cast(NewInst)) + I = II->getIterator(); + else + I = NewInst->getParentBlock()->begin(); + auto NewAI = FullApplySite::isa(NewInstPair.second.getInstruction()); + // *NOTE*, it is important that we return I here since we may have + // devirtualized but not have a full apply site anymore. + if (!NewAI) + return std::make_tuple(FullApplySite(), I); + + return std::make_tuple(NewAI, I); +} + /// \brief Inlines all mandatory inlined functions into the body of a function, /// first recursively inlining all mandatory apply instructions in those /// functions into their bodies if necessary. @@ -363,19 +385,9 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI, auto *ApplyBlock = InnerAI.getParent(); - auto NewInstPair = tryDevirtualizeApply(InnerAI, CHA); - if (auto *NewInst = NewInstPair.first) { - replaceDeadApply(InnerAI, NewInst); - if (auto *II = dyn_cast(NewInst)) - I = II->getIterator(); - else - I = NewInst->getParentBlock()->begin(); - auto NewAI = FullApplySite::isa(NewInstPair.second.getInstruction()); - if (!NewAI) - continue; - - InnerAI = NewAI; - } + std::tie(InnerAI, I) = tryDevirtualizeApplyHelper(InnerAI, I, CHA); + if (!InnerAI) + continue; SILLocation Loc = InnerAI.getLoc(); SILValue CalleeValue = InnerAI.getCallee();