From cb06b6ab0018d9aa07bf9fec0052a9f8c921a593 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Mon, 12 Dec 2022 15:21:33 +0700 Subject: [PATCH] [IndVars][NFC] Remove redundant param in optimizeLoopExitWithUnknownExitCount There was a crippled version of this transform for Inverted predicate, so the same query was done twice. Advanced version of this transform wasn't implemented for inverted condition. Thus, the code was hard to read. The only real purpose of the Inverted param was to make a simple isKnownPredicateAt query. Instead if this, use evaluatePredicateAt to solve the task for both inverted and non-inverted predicate. This slightly changes the order of queries, but effectively it should save some time by avoiding duplicating queries, and simplifies the code a lot. I also could not find any evidence that we ever eliminate anything with Inverted = true, but conservatively preserved the current behavior. Maybe we can remove it and save some compile time. Differential Revision: https://reviews.llvm.org/D139814 Reviewed By: nikic --- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 35 ++++++++++----------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index 1bfecb8..d4e247a 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1364,9 +1364,8 @@ static void replaceWithInvariantCond( } static bool optimizeLoopExitWithUnknownExitCount( - const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, - const SCEV *MaxIter, bool Inverted, bool SkipLastIter, - ScalarEvolution *SE, SCEVExpander &Rewriter, + const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, const SCEV *MaxIter, + bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter, SmallVectorImpl &DeadInsts) { ICmpInst::Predicate Pred; Value *LHS, *RHS; @@ -1382,20 +1381,13 @@ static bool optimizeLoopExitWithUnknownExitCount( if (L->contains(FalseSucc)) Pred = CmpInst::getInversePredicate(Pred); - // If we are proving loop exit, invert the predicate. - if (Inverted) - Pred = CmpInst::getInversePredicate(Pred); - const SCEV *LHSS = SE->getSCEVAtScope(LHS, L); const SCEV *RHSS = SE->getSCEVAtScope(RHS, L); - // Can we prove it to be trivially true? - if (SE->isKnownPredicateAt(Pred, LHSS, RHSS, BI)) { - foldExit(L, ExitingBB, Inverted, DeadInsts); + // Can we prove it to be trivially true or false? + if (auto EV = SE->evaluatePredicateAt(Pred, LHSS, RHSS, BI)) { + foldExit(L, ExitingBB, /*IsTaken*/ !*EV, DeadInsts); return true; } - // Further logic works for non-inverted condition only. - if (Inverted) - return false; auto *ARTy = LHSS->getType(); auto *MaxIterTy = MaxIter->getType(); @@ -1422,7 +1414,7 @@ static bool optimizeLoopExitWithUnknownExitCount( // Can we prove it to be trivially true? if (SE->isKnownPredicateAt(LIP->Pred, LIP->LHS, LIP->RHS, BI)) - foldExit(L, ExitingBB, Inverted, DeadInsts); + foldExit(L, ExitingBB, /*IsTaken*/ false, DeadInsts); else replaceWithInvariantCond(L, ExitingBB, LIP->Pred, LIP->LHS, LIP->RHS, Rewriter, DeadInsts); @@ -1638,10 +1630,10 @@ 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 = [&](bool Inverted, bool SkipLastIter) { - return optimizeLoopExitWithUnknownExitCount( - L, BI, ExitingBB, MaxBECount, Inverted, SkipLastIter, SE, Rewriter, - DeadInsts); + auto OptimizeCond = [&](bool SkipLastIter) { + return optimizeLoopExitWithUnknownExitCount(L, BI, ExitingBB, + MaxBECount, SkipLastIter, + SE, Rewriter, DeadInsts); }; // TODO: We might have proved that we can skip the last iteration for @@ -1660,11 +1652,10 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { // hope that we will be able to prove triviality for at least one of // them. We can stop querying MaxBECount for this case once SCEV // understands that (MaxBECount - 1) will not overflow here. - if (OptimizeCond(false, false) || OptimizeCond(true, false)) + if (OptimizeCond(false)) + Changed = true; + else if (SkipLastIter && OptimizeCond(true)) Changed = true; - else if (SkipLastIter) - if (OptimizeCond(false, true) || OptimizeCond(true, true)) - Changed = true; if (MaxBECount == MaxExitCount) // If the loop has more than 1 iteration, all further checks will be // executed 1 iteration less. -- 2.7.4