Index: llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp =================================================================== --- llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -1792,10 +1792,10 @@ } while (!DomWorklist.empty()); } -static bool unswitchNontrivialInvariants( +static void unswitchNontrivialInvariants( Loop &L, Instruction &TI, ArrayRef Invariants, - DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, - function_ref)> UnswitchCB, + SmallVectorImpl &ExitBlocks, DominatorTree &DT, LoopInfo &LI, + AssumptionCache &AC, function_ref)> UnswitchCB, ScalarEvolution *SE) { auto *ParentBB = TI.getParent(); BranchInst *BI = dyn_cast(&TI); @@ -1851,17 +1851,6 @@ // whatever reason). assert(LI.getLoopFor(ParentBB) == &L && "Branch in an inner loop!"); - SmallVector ExitBlocks; - L.getUniqueExitBlocks(ExitBlocks); - - // We cannot unswitch if exit blocks contain a cleanuppad instruction as we - // don't know how to split those exit blocks. - // FIXME: We should teach SplitBlock to handle this and remove this - // restriction. - for (auto *ExitBB : ExitBlocks) - if (isa(ExitBB->getFirstNonPHI())) - return false; - // Compute the parent loop now before we start hacking on things. Loop *ParentL = L.getParentLoop(); @@ -2145,7 +2134,6 @@ UnswitchCB(IsStillLoop, SibLoops); ++NumBranches; - return true; } /// Recursively compute the cost of a dominator subtree based on the per-block @@ -2241,6 +2229,19 @@ if (containsIrreducibleCFG(RPOT, LI)) return false; + SmallVector ExitBlocks; + L.getUniqueExitBlocks(ExitBlocks); + + // We cannot unswitch if exit blocks contain a cleanuppad instruction as we + // don't know how to split those exit blocks. + // FIXME: We should teach SplitBlock to handle this and remove this + // restriction. + for (auto *ExitBB : ExitBlocks) + if (isa(ExitBB->getFirstNonPHI())) { + dbgs() << "Cannot unswitch because of cleanuppad in exit block\n"; + return false; + } + LLVM_DEBUG( dbgs() << "Considering " << UnswitchCandidates.size() << " non-trivial loop invariant conditions for unswitching.\n"); @@ -2374,11 +2375,12 @@ return false; } - LLVM_DEBUG(dbgs() << " Trying to unswitch non-trivial (cost = " + LLVM_DEBUG(dbgs() << " Unswitching non-trivial (cost = " << BestUnswitchCost << ") terminator: " << *BestUnswitchTI << "\n"); - return unswitchNontrivialInvariants( - L, *BestUnswitchTI, BestUnswitchInvariants, DT, LI, AC, UnswitchCB, SE); + unswitchNontrivialInvariants(L, *BestUnswitchTI, BestUnswitchInvariants, + ExitBlocks, DT, LI, AC, UnswitchCB, SE); + return true; } /// Unswitch control flow predicated on loop invariant conditions.