[llvm] Fix exact backedge count algorithm in Scalar-Evolution (PR #92560)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 18 06:00:19 PDT 2024


mrdaybird wrote:

@efriedma-quic Thanks for answering the questions and reviewing my code! 
 
I tried another approach, with the idea that if we can show that Start of RHS >= Start of LHS, and stride of LHS is positive and stride of RHS is negative, then the variables do not overflow(atleast before the last iteration) thus the exact backedge-count is computable(and the loop is finite).
So, I tried something like this, in place of previous code:
```
 if (!isLoopInvariant(RHS, L)) {
    // If RHS is an add recurrence, try again with lhs=lhs-rhs and rhs=0
    if(auto RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS)){
      auto RHSStart = RHSAddRec->getStart();
      auto RHSStride = RHSAddRec->getStepRecurrence(*this);
      auto RHSStartMinusStride = getMinusSCEV(RHSStart, RHSStride);

      auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
      auto *StartMinusOne = getAddExpr(Start,
                                      getMinusOne(Start->getType() ));
      if(isLoopEntryGuardedByCond(L, CondGT, RHSStartMinusStride, StartMinusOne)){
        // Now, we calculate BECount using RHSStart, RHSStride and Start(lhs) and Stride(lhs) 
      }
    }
....
```
I tried this on a sample code([CE](https://llvm.godbolt.org/z/PPo6zKWeK)), but I don't know why but `isLoopEntryGuardedByCond` is not true, eventhough I think it should be. Any idea, what's wrong? @efriedma-quic 

Thanks again for the help!

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


More information about the llvm-commits mailing list