Hi,<br><br>Seems pass scalar-evolution+indvars fail to get the loop trip count of the following case:<br><br>int foo(int x, int y, int lam[256], int alp[256]) {<br>  int i;<br>  int z = y;<br><br>  for (i = 255; i >= 0; i--) {<br>
    z += x;<br>    lam[i] = alp[i];<br>  }<br>  return z;<br>}<br><br><br>The final optimized ll code is :<br><br>define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind {<br>entry:<br>        br label %bb<br><br>bb:             ; preds = %bb, %entry<br>
        %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb ]          ; <i32> [#uses=4]<br>        %i.0.reg2mem.0 = sub i32 255, %indvar           ; <i32> [#uses=2]<br>        %tmp7 = getelementptr i32* %alp, i32 %i.0.reg2mem.0             ; <i32*> [#uses=1]<br>
        %tmp8 = load i32* %tmp7, align 4                ; <i32> [#uses=1]<br>        %tmp11 = getelementptr i32* %lam, i32 %i.0.reg2mem.0            ; <i32*> [#uses=1]<br>        store i32 %tmp8, i32* %tmp11, align 4<br>
        %tmp13 = sub i32 254, %indvar           ; <i32> [#uses=1]<br>        %tmp16 = icmp slt i32 %tmp13, 0         ; <i1> [#uses=1]<br>        %indvar.next = add i32 %indvar, 1               ; <i32> [#uses=1]<br>
        br i1 %tmp16, label %bb17, label %bb<br><br>bb17:           ; preds = %bb<br>        %indvar.lcssa = phi i32 [ %indvar, %bb ]                ; <i32> [#uses=1]<br>        %tmp28 = mul i32 %indvar.lcssa, %x              ; <i32> [#uses=1]<br>
        %z.0.reg2mem.0 = add i32 %y, %x         ; <i32> [#uses=1]<br>        %tmp4 = add i32 %z.0.reg2mem.0, %tmp28          ; <i32> [#uses=1]<br>        ret i32 %tmp4<br>}<br><br>Surely the loop trip count is 256, but the Loop::getTripCount() will return NULL.<br>
This is due to the exit condition:  <br><br>%tmp16 = icmp slt i32 %tmp13, 0         ; <i1> [#uses=1]<br><br>pass indvars should transform it into a canonical exit condtion EQ or NE, but as scalarevolution returned NonComputedItCount, it failed to do that.<br>
<br>In pass ScalarEvolution three algorithms failed to compute the trip count:<br><br>1. In SCEVAddRecExpr::getNumIterationsInRange(), as the loop stride is negative, the exit value expression will be zero: (Here A is the stride value)<br>
<br>        // The exit value should be (End+A)/A.<br>      APInt ExitVal = (End + A).udiv(A);<br>      ConstantInt *ExitValue = ConstantInt::get(ExitVal);         <br><br>2. In Line 1953,   the switch-case computes the trip count due to different Cond code.<br>
    But in this case, as it exits on true, the slt is converted to sge, not switch-case for this condtion.<br><br><br>3. As the loop count is over 100, the Brute-Force algorithm also doent work<br><br>Can we improve this?<br>
<br><br>Thanks,<br>Sheng.<br><br>    <br>