[llvm] [LAA] Use computeConstantDifference() (PR #103725)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 05:31:21 PDT 2024


nikic wrote:

> I added the following asserts and checked some other codebases and found no missed cases. Not sure if it might be worth including such an assert to see if there are some cases where this is still causing a difference
> 
> ```
> diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
> index 8c939f261e69..7fbc3ba53292 100644
> --- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
> +++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
> @@ -1601,9 +1601,14 @@ std::optional<int> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
>      const SCEV *PtrSCEVB = SE.getSCEV(PtrB);
>      std::optional<APInt> Diff =
>          SE.computeConstantDifference(PtrSCEVB, PtrSCEVA);
> -    if (!Diff)
> +    const auto *Diff2 =
> +               dyn_cast<SCEVConstant>(SE.getMinusSCEV(PtrSCEVB, PtrSCEVA));
> +    if (!Diff) {
> +      assert(!Diff2);
>        return std::nullopt;
> +    }
>      Val = Diff->getSExtValue();
> +    assert(Val == Diff2->getAPInt().getSExtValue());
>    }
>    int Size = DL.getTypeStoreSize(ElemTyA);
>    int Dist = Val / Size;
> ```

Right, I basically did the same :) I don't think we can include an assert, because there's no hard guarantee that they are the same. E.g. if you end up with a 9-fold nested addrec here, there would be a difference, but at that point we wouldn't care.

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


More information about the llvm-commits mailing list