[llvm] Reland "Redesign Straight-Line Strength Reduction (SLSR) (#162930)" (PR #169614)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 26 12:20:31 PST 2025


================
@@ -677,61 +688,37 @@ void StraightLineStrengthReduce::setBasisAndDeltaFor(Candidate &C) {
     return false;
   };
 
-  // Priority:
-  // Constant Delta from Index > Constant Delta from Base >
-  // Constant Delta from Stride > Variable Delta from Base or Stride
-  // TODO: Change the priority to align with the cost model.
-
-  // First, look for a constant index-diff basis
-  if (const auto *IndexDeltaCandidates =
-          CandidateDict.getCandidatesWithDeltaKind(C, Candidate::IndexDelta)) {
-    bool FoundConstDelta =
-        SearchFrom(*IndexDeltaCandidates, [&DT = DT, &C](Candidate *Basis) {
-          if (isSimilar(C, *Basis, Candidate::IndexDelta)) {
-            assert(DT->dominates(Basis->Ins, C.Ins));
-            auto *Delta = getIndexDelta(C, *Basis);
-            if (!C.isProfitableRewrite(Delta, Candidate::IndexDelta))
-              return false;
-            C.Basis = Basis;
-            C.DeltaKind = Candidate::IndexDelta;
-            C.Delta = Delta;
-            LLVM_DEBUG(dbgs() << "Found delta from Index " << *C.Delta << "\n");
-            return true;
-          }
-          return false;
-        });
-    if (FoundConstDelta)
-      return;
-  }
-
-  // No constant-index-diff basis found. look for the best possible base-diff
-  // or stride-diff basis
-  // Base/Stride diffs not supported for form (B + i) * S
-  if (C.CandidateKind == Candidate::Mul)
-    return;
-
   auto For = [this, &C](Candidate::DKind K) {
     // return true if find a Basis with constant delta and stop searching,
     // return false if did not find a Basis or the delta is not a constant
     // and continue searching for a Basis with constant delta
     return [K, this, &C](Candidate *Basis) -> bool {
-      if (!isSimilar(C, *Basis, K))
+      SmallVector<Instruction *> DropPoisonGeneratingInsts;
+      // Ensure the IR of Basis->Ins is not more poisonous than its SCEV.
+      if (!isSimilar(C, *Basis, K) ||
+          !SE->canReuseInstruction(SE->getSCEV(Basis->Ins), Basis->Ins,
+                                   DropPoisonGeneratingInsts))
         return false;
 
       assert(DT->dominates(Basis->Ins, C.Ins));
-      const SCEV *BasisPart =
-          (K == Candidate::BaseDelta) ? Basis->Base : Basis->StrideSCEV;
-      const SCEV *CandPart =
-          (K == Candidate::BaseDelta) ? C.Base : C.StrideSCEV;
-      const SCEV *Diff = SE->getMinusSCEV(CandPart, BasisPart);
-      Value *AvailableVal = getNearestValueOfSCEV(Diff, C.Ins);
-      if (!AvailableVal)
+      Value *Delta = getDelta(C, *Basis, K);
+      if (K == Candidate::IndexDelta &&
+          !C.isProfitableRewrite(Delta, Candidate::IndexDelta))
+        return false;
+
+      if (!Delta)
----------------
Artem-B wrote:

Should we check `Delta`  before we use it in `isProfitableRewrite` ? AFAICT, isProfitableRewrite() ends up dereferencing it without checking for NULL. Speaking of which, if we do not expect to ever see NULL, perhaps it would make sense to pass Delta by reference to make it clear.

https://github.com/llvm/llvm-project/pull/169614


More information about the llvm-commits mailing list