[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 13:05: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)
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:
Where do we do those rewrites? It would be great to drop those annotations close to/in sync with the rewrites. If rewrites happen somewhere else, we have no way to guarantee that they do happen. I.e. that disappearing `inbounds` attribute I commented on in the tests appears to be dropped without any other changes to the instruction. Did we actually rewrite it? Or did we just drop the attribute when we should not have. At the moment I can't tell.
https://github.com/llvm/llvm-project/pull/169614
More information about the llvm-commits
mailing list