From 68490aec4e19d72b9249f3144380f006df23aac5 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Fri, 13 Nov 2020 17:00:49 +0700 Subject: [PATCH] [NFC] Move lambdae into static functions --- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 84 ++++++++++++++------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index 4c31064..dfcf89f 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1350,6 +1350,41 @@ analyzeCond(const Loop *L, BranchInst *BI, ScalarEvolution *SE, return CanBeReplacedWithInvariant; } +static void replaceExitCond(BranchInst *BI, Value *NewCond, + SmallVectorImpl &DeadInsts) { + auto *OldCond = BI->getCondition(); + BI->setCondition(NewCond); + if (OldCond->use_empty()) + DeadInsts.emplace_back(OldCond); +} + +static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken, + SmallVectorImpl &DeadInsts) { + BranchInst *BI = cast(ExitingBB->getTerminator()); + bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB)); + auto *OldCond = BI->getCondition(); + auto *NewCond = + ConstantInt::get(OldCond->getType(), IsTaken ? ExitIfTrue : !ExitIfTrue); + replaceExitCond(BI, NewCond, DeadInsts); +} + +static void replaceWithInvariantCond( + const Loop *L, BasicBlock *ExitingBB, ICmpInst::Predicate InvariantPred, + const SCEV *InvariantLHS, const SCEV *InvariantRHS, SCEVExpander &Rewriter, + SmallVectorImpl &DeadInsts) { + BranchInst *BI = cast(ExitingBB->getTerminator()); + Rewriter.setInsertPoint(BI); + auto *LHSV = Rewriter.expandCodeFor(InvariantLHS); + auto *RHSV = Rewriter.expandCodeFor(InvariantRHS); + bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB)); + if (ExitIfTrue) + InvariantPred = ICmpInst::getInversePredicate(InvariantPred); + IRBuilder<> Builder(BI); + auto *NewCond = Builder.CreateICmp(InvariantPred, LHSV, RHSV, + BI->getCondition()->getName()); + replaceExitCond(BI, NewCond, DeadInsts); +} + bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { SmallVector ExitingBlocks; L->getExitingBlocks(ExitingBlocks); @@ -1409,38 +1444,6 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { } #endif - auto ReplaceExitCond = [&](BranchInst *BI, Value *NewCond) { - auto *OldCond = BI->getCondition(); - BI->setCondition(NewCond); - if (OldCond->use_empty()) - DeadInsts.emplace_back(OldCond); - }; - - auto FoldExit = [&](BasicBlock *ExitingBB, bool IsTaken) { - BranchInst *BI = cast(ExitingBB->getTerminator()); - bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB)); - auto *OldCond = BI->getCondition(); - auto *NewCond = ConstantInt::get(OldCond->getType(), - IsTaken ? ExitIfTrue : !ExitIfTrue); - ReplaceExitCond(BI, NewCond); - }; - - auto ReplaceWithInvariantCond = [&]( - BasicBlock *ExitingBB, ICmpInst::Predicate InvariantPred, - const SCEV *InvariantLHS, const SCEV *InvariantRHS) { - BranchInst *BI = cast(ExitingBB->getTerminator()); - Rewriter.setInsertPoint(BI); - auto *LHSV = Rewriter.expandCodeFor(InvariantLHS); - auto *RHSV = Rewriter.expandCodeFor(InvariantRHS); - bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB)); - if (ExitIfTrue) - InvariantPred = ICmpInst::getInversePredicate(InvariantPred); - IRBuilder<> Builder(BI); - auto *NewCond = Builder.CreateICmp(InvariantPred, LHSV, RHSV, - BI->getCondition()->getName()); - ReplaceExitCond(BI, NewCond); - }; - bool Changed = false; bool SkipLastIter = false; SmallSet DominatingExitCounts; @@ -1450,9 +1453,8 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { // Okay, we do not know the exit count here. Can we at least prove that it // will remain the same within iteration space? auto *BI = cast(ExitingBB->getTerminator()); - auto OptimizeCond = [this, L, BI, ExitingBB, MaxExitCount, &FoldExit, - &ReplaceWithInvariantCond](bool Inverted, - bool SkipLastIter) { + auto OptimizeCond = [this, L, BI, ExitingBB, MaxExitCount, &Rewriter]( + bool Inverted, bool SkipLastIter) { const SCEV *MaxIter = MaxExitCount; if (SkipLastIter) { const SCEV *One = SE->getOne(MaxIter->getType()); @@ -1463,11 +1465,11 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { switch (analyzeCond(L, BI, SE, Inverted, MaxIter, InvariantPred, InvariantLHS, InvariantRHS)) { case CanBeRemoved: - FoldExit(ExitingBB, Inverted); + foldExit(L, ExitingBB, Inverted, DeadInsts); return true; case CanBeReplacedWithInvariant: { - ReplaceWithInvariantCond(ExitingBB, InvariantPred, InvariantLHS, - InvariantRHS); + replaceWithInvariantCond(L, ExitingBB, InvariantPred, InvariantLHS, + InvariantRHS, Rewriter, DeadInsts); return true; } case CannotOptimize: @@ -1511,7 +1513,7 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { // TODO: Given we know the backedge can't be taken, we should go ahead // and break it. Or at least, kill all the header phis and simplify. if (ExitCount->isZero()) { - FoldExit(ExitingBB, true); + foldExit(L, ExitingBB, true, DeadInsts); Changed = true; continue; } @@ -1533,7 +1535,7 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { // one? if (SE->isLoopEntryGuardedByCond(L, CmpInst::ICMP_ULT, MaxExitCount, ExitCount)) { - FoldExit(ExitingBB, false); + foldExit(L, ExitingBB, false, DeadInsts); Changed = true; continue; } @@ -1543,7 +1545,7 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { // exiting iteration, but (from the visit order) strictly follows another // which does the same and is thus dead. if (!DominatingExitCounts.insert(ExitCount).second) { - FoldExit(ExitingBB, false); + foldExit(L, ExitingBB, false, DeadInsts); Changed = true; continue; } -- 2.7.4