[llvm] 6dac170 - [SCEVExpander] Support cost evaluation of several SCEVs with same budget

Max Kazantsev via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 6 02:02:41 PST 2022


Author: Max Kazantsev
Date: 2022-12-06T17:02:26+07:00
New Revision: 6dac170140d09c23b305558939dc45416f0ec865

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

LOG: [SCEVExpander] Support cost evaluation of several SCEVs with same budget

This is a follow-up from discussion in D138412. Sometimes we want to evaluate
the cost of expansion of several SCEVs together with same budget. For example,
if one of them is a bit above cheap limit, and the second one is free, then
we still want to expand. Checking each of them with "cheap" limit is a bit more
pessimistic.

Differential Revision: https://reviews.llvm.org/D138475
Reviewed By: lebedev.ri

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
    llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index aa4e0ac7001dd..131e24f685e89 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -219,11 +219,11 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
   /// Return true for expressions that can't be evaluated at runtime
   /// within given \b Budget.
   ///
-  /// At is a parameter which specifies point in code where user is going to
-  /// expand this expression. Sometimes this knowledge can lead to
+  /// \p At is a parameter which specifies point in code where user is going to
+  /// expand these expressions. Sometimes this knowledge can lead to
   /// a less pessimistic cost estimation.
-  bool isHighCostExpansion(const SCEV *Expr, Loop *L, unsigned Budget,
-                           const TargetTransformInfo *TTI,
+  bool isHighCostExpansion(ArrayRef<const SCEV *> Exprs, Loop *L,
+                           unsigned Budget, const TargetTransformInfo *TTI,
                            const Instruction *At) {
     assert(TTI && "This function requires TTI to be provided.");
     assert(At && "This function requires At instruction to be provided.");
@@ -233,7 +233,8 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
     SmallPtrSet<const SCEV *, 8> Processed;
     InstructionCost Cost = 0;
     unsigned ScaledBudget = Budget * TargetTransformInfo::TCC_Basic;
-    Worklist.emplace_back(-1, -1, Expr);
+    for (auto *Expr : Exprs)
+      Worklist.emplace_back(-1, -1, Expr);
     while (!Worklist.empty()) {
       const SCEVOperand WorkItem = Worklist.pop_back_val();
       if (isHighCostExpansionHelper(WorkItem, L, *At, Cost, ScaledBudget, *TTI,

diff  --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index 84adb6af0c53b..9f0ad99f3220a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -221,11 +221,8 @@ bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
 
   // Do not generate something ridiculous.
   auto *PHTerm = Preheader->getTerminator();
-  if (Rewriter.isHighCostExpansion(InvariantLHS, L, SCEVCheapExpansionBudget,
-                                   TTI, PHTerm))
-    return false;
-  if (Rewriter.isHighCostExpansion(InvariantRHS, L, SCEVCheapExpansionBudget,
-                                   TTI, PHTerm))
+  if (Rewriter.isHighCostExpansion({ InvariantLHS, InvariantRHS }, L,
+                                   2 * SCEVCheapExpansionBudget, TTI, PHTerm))
     return false;
   auto *NewLHS =
       Rewriter.expandCodeFor(InvariantLHS, IVOperand->getType(), PHTerm);


        


More information about the llvm-commits mailing list