[llvm] 6cf7f32 - [IndVars][NFC] Separate invariant condition creation and cond replacement

Max Kazantsev via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 12 02:16:35 PST 2022


Author: Max Kazantsev
Date: 2022-12-12T17:16:22+07:00
New Revision: 6cf7f32ab5bc0ace281c8fa92b0b217be6aad07e

URL: https://github.com/llvm/llvm-project/commit/6cf7f32ab5bc0ace281c8fa92b0b217be6aad07e
DIFF: https://github.com/llvm/llvm-project/commit/6cf7f32ab5bc0ace281c8fa92b0b217be6aad07e.diff

LOG: [IndVars][NFC] Separate invariant condition creation and cond replacement

This separation is a preparatory step for further improvements in this code.
Also simplifies this function's API.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index d4e247a3d522..9d666f76cb0c 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1346,21 +1346,21 @@ static void replaceLoopPHINodesWithPreheaderValues(
   }
 }
 
-static void replaceWithInvariantCond(
-    const Loop *L, BasicBlock *ExitingBB, ICmpInst::Predicate InvariantPred,
-    const SCEV *InvariantLHS, const SCEV *InvariantRHS, SCEVExpander &Rewriter,
-    SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
+static Value *
+createInvariantCond(const Loop *L, BasicBlock *ExitingBB,
+                    const ScalarEvolution::LoopInvariantPredicate &LIP,
+                    SCEVExpander &Rewriter) {
+  ICmpInst::Predicate InvariantPred = LIP.Pred;
   BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
   Rewriter.setInsertPoint(BI);
-  auto *LHSV = Rewriter.expandCodeFor(InvariantLHS);
-  auto *RHSV = Rewriter.expandCodeFor(InvariantRHS);
+  auto *LHSV = Rewriter.expandCodeFor(LIP.LHS);
+  auto *RHSV = Rewriter.expandCodeFor(LIP.RHS);
   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);
+  return Builder.CreateICmp(InvariantPred, LHSV, RHSV,
+                            BI->getCondition()->getName());
 }
 
 static bool optimizeLoopExitWithUnknownExitCount(
@@ -1415,9 +1415,10 @@ static bool optimizeLoopExitWithUnknownExitCount(
   // Can we prove it to be trivially true?
   if (SE->isKnownPredicateAt(LIP->Pred, LIP->LHS, LIP->RHS, BI))
     foldExit(L, ExitingBB, /*IsTaken*/ false, DeadInsts);
-  else
-    replaceWithInvariantCond(L, ExitingBB, LIP->Pred, LIP->LHS, LIP->RHS,
-                             Rewriter, DeadInsts);
+  else {
+    auto *NewCond = createInvariantCond(L, ExitingBB, *LIP, Rewriter);
+    replaceExitCond(BI, NewCond, DeadInsts);
+  }
 
   return true;
 }


        


More information about the llvm-commits mailing list