Obtaining bounds of loops

Hi,

I am trying to test out the getBounds() method from LoopInfo. However, the method doesn't seem to obtain the bounds of simple loops like -

        for (int j = 0; j < 10; j ++)
            B[j] = 6;

        int i = 10, j = 2,

        while ( i != j)
              j++;

  My code snippet in the function pass is as follows (which gives the output as "No value for loop bounds" for all the loops):

       LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();

       for (auto loop = LI.begin(), e = LI.end(); loop != e; ++loop )
       {
           ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
           auto b = (*loop)->getBounds(*SE);

           if (!b.hasValue())
                {
                   errs() << "No value for loop bounds\n";
                }
       }

  Is there something that I am missing here?

  Thanks,
  Bodhi

Hi Bodhi,

Well, probably you can’t get the bounds because your loops are not rotated [1]
You can get that with -loop-rotate pass.

FWIW, this is required by getInductionVariable() [2]
as if the loop is not rotated, you won’t have the comparison on the latch. In general, I think that getInductionVariable() is weird, which
results in e.g., some loop optimizations writing their own getInductionVariable() because the “canonical” one can’t do the job. For example, loop interchange [3]

Best,
Stefanos

[1] https://llvm.org/docs/LoopTerminology.html#rotated-loops
[2] https://github.com/llvm/llvm-project/blob/95d13c01ecba5c9dba8ea1bd875c4179cbaea9e2/llvm/lib/Analysis/LoopInfo.cpp#L299
[3] https://github.com/llvm/llvm-project/blob/95d13c01ecba5c9dba8ea1bd875c4179cbaea9e2/llvm/lib/Transforms/Scalar/LoopInterchange.cpp#L298

Στις Δευ, 22 Φεβ 2021 στις 6:48 μ.μ., ο/η bodhisatwa via llvm-dev <llvm-dev@lists.llvm.org> έγραψε:

JFYI, the infrastructure on Loop itself is very very limited. Honestly, it probably shouldn't exist at all.

ScalarEvolution exists to analyze loops. In particular, it can compute symbolic execute counts for a much much broader family of loops.

Without knowing your use case, I can't say for sure, but I suspect you want to be using ScalarEvolution.

Philip