[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:11 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)
         return false;
+      
+      // If there is a Delta that we can reuse Basis to rewrite C,
+      // drop the poison-generating instructions of Basis to avoid
+      // introducing poison.
+      for (Instruction *I : DropPoisonGeneratingInsts)
+        I->dropPoisonGeneratingAnnotations();
----------------
Artem-B wrote:

I think what we do here is better explained by just "Clean up DropPoisonGeneratingInsts returned by successful SE->canReuseInstruction()". 

Speaking of which, if we do exit because Delta is null, we may be leaking those annotations. I think they must be cleaned before early exit on null delta.

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


More information about the llvm-commits mailing list