From: Philip Reames Date: Mon, 19 Sep 2016 23:30:23 +0000 (+0000) Subject: [LCSSA] Cache LoopExits to avoid wasted work X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=67197eeb7d7fdebf4dc8f7e476ccb4d6942c9778;p=llvm [LCSSA] Cache LoopExits to avoid wasted work When looking at the scribus_1.3 example from https://llvm.org/bugs/show_bug.cgi?id=10584, I noticed that we were spending a large amount of time computing loop exits in LCSSA. This code appears to be written with the assumption that LoopExits are stored in the Loop and thus cheap to query. This is not true, so we should cache the result across the potentially long running loop which tends to visit a small handful of Loops. On the particular example from 10584, this change drops the time spent in LCSSA computation by about 80%. Differential Revision: https://reviews.llvm.org/D24509 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281949 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Utils/LCSSA.cpp b/lib/Transforms/Utils/LCSSA.cpp index 33c18378cdf..ac403cc72c6 100644 --- a/lib/Transforms/Utils/LCSSA.cpp +++ b/lib/Transforms/Utils/LCSSA.cpp @@ -63,19 +63,25 @@ static bool isExitBlock(BasicBlock *BB, bool llvm::formLCSSAForInstructions(SmallVectorImpl &Worklist, DominatorTree &DT, LoopInfo &LI) { SmallVector UsesToRewrite; - SmallVector ExitBlocks; SmallSetVector PHIsToRemove; PredIteratorCache PredCache; bool Changed = false; + // Cache the Loop ExitBlocks across this loop. We expect to get a lot of + // instructions within the same loops, computing the exit blocks is + // expensive, and we're not mutating the loop structure. + SmallDenseMap> LoopExitBlocks; + while (!Worklist.empty()) { UsesToRewrite.clear(); - ExitBlocks.clear(); Instruction *I = Worklist.pop_back_val(); BasicBlock *InstBB = I->getParent(); Loop *L = LI.getLoopFor(InstBB); - L->getExitBlocks(ExitBlocks); + if (!LoopExitBlocks.count(L)) + L->getExitBlocks(LoopExitBlocks[L]); + assert(LoopExitBlocks.count(L)); + const SmallVectorImpl &ExitBlocks = LoopExitBlocks[L]; if (ExitBlocks.empty()) continue;