[PATCH] D139814: [IndVars][NFC] Remove redundant param in optimizeLoopExitWithUnknownExitCount

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 12 01:28:37 PST 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcb06b6ab0018: [IndVars][NFC] Remove redundant param in optimizeLoopExitWithUnknownExitCount (authored by mkazantsev).

Changed prior to commit:
  https://reviews.llvm.org/D139814?vs=482020&id=482029#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139814/new/

https://reviews.llvm.org/D139814

Files:
  llvm/lib/Transforms/Scalar/IndVarSimplify.cpp


Index: llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1364,9 +1364,8 @@
 }
 
 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<WeakTrackingVH> &DeadInsts) {
   ICmpInst::Predicate Pred;
   Value *LHS, *RHS;
@@ -1382,20 +1381,13 @@
   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 @@
 
   // 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 @@
       // 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<BranchInst>(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 @@
       // 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139814.482029.patch
Type: text/x-patch
Size: 3490 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221212/94080179/attachment.bin>


More information about the llvm-commits mailing list