[llvm-dev] Computing loop trip counts with Scalar evolution

Friedman, Eli via llvm-dev llvm-dev at lists.llvm.org
Mon May 22 10:33:02 PDT 2017


On 5/18/2017 11:30 AM, Alex Susu via llvm-dev wrote:
>   Hello.
>     I tried to get the trip count of a loop with Scalar evolution. I 
> got inspired from 
> http://stackoverflow.com/questions/13834364/how-to-get-loop-bounds-in-llvm 
> .
>     However the analysis described there doesn't work well for the 
> second inner loop of thes function below (although if we declare Bcols 
> a short it works well):
>       void MatMul(int Arows, int Acols, int Brows, int Bcols) {
>         short i, j, k;
>         for (i = 0; i < Arows; ++i) {
>           for (j = 0; j < Bcols; ++j) {
>             C[i][j] = 0;
>             for (k = 0; k < Acols; ++k) {
>               C[i][j] += A[i][k] * B[j][k];
>             }
>           }
>         }
>       }
>

Your function is weird because the compiler can't prove whether the 
induction variables overflow.  Here's the output of "clang -O2 
-emit-llvm -S | opt -analyze -scalar-evolution":

Determining loop execution counts for: @MatMul
Loop %for.body13: Unpredictable backedge-taken count.
Loop %for.body13: Unpredictable max backedge-taken count.
Loop %for.body13: Predicated backedge-taken count is (-1 + %Acols)
  Predicates:
     {1,+,1}<%for.body13> Added Flags: <nssw>

Loop %for.body6: Unpredictable backedge-taken count.
Loop %for.body6: Unpredictable max backedge-taken count.
Loop %for.body6: Predicated backedge-taken count is (-1 + %Bcols)
  Predicates:
     {1,+,1}<%for.body6> Added Flags: <nssw>

Loop %for.cond2.preheader: Unpredictable backedge-taken count.
Loop %for.cond2.preheader: Unpredictable max backedge-taken count.
Loop %for.cond2.preheader: Predicated backedge-taken count is (-1 + %Arows)
  Predicates:
     {1,+,1}<%for.cond2.preheader> Added Flags: <nssw>

-Eli


-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project



More information about the llvm-dev mailing list