<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - IRCE wouldn't work when trip count is unsigned and br condition is equality."
   href="https://bugs.llvm.org/show_bug.cgi?id=49014">49014</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>IRCE wouldn't work when trip count is unsigned and br condition is equality.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>11.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Loop Optimizer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jie.he.cn@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>please refer to <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - IRCE wouldn't deal with range checks which predicate is uge."
   href="show_bug.cgi?id=49012">Bug 49012</a> for the story.

this time, I made a little change in the previous code. I updated the exit
conditon to "NE" from "ULT", like below:

void testIRCE(unsigned int * buf, unsigned int len, unsigned int
iteration_count) {
    if (iteration_count > 0) {
        unsigned int i = 0;
        do {
            if (i >= len) { // range check
                printf("overflow\n");
                return;
            }

            buf[i] = i;

            i ++;

        } while (i != iteration_count);
    }
}

from C code, we know iteration_count is always an non-negative value (it's an
unsigned int and I add a condition guard outside the loop), and the loop will
exit as expected. but IRCE doesn't work because it thinks iteration_count will
overflow. see the code in function LoopStructure::parseLoopStructure():

if (ICI->isEquality() && !HasNoSignedWrap(IndVarBase)) {
    FailureReason = "LHS in icmp needs nsw for equality predicates";
    return None;
  }

then I skip the check and go to the next. soon,I meet another check when try to
replace NE to ULT, it seems still can't prove iteration_count is non-negative.
see the code in  function LoopStructure::parseLoopStructure():

if (isKnownNonNegativeInLoop(IndVarStart, &L, SE) &&
    isKnownNonNegativeInLoop(RightSCEV, &L, SE))
    Pred = ICmpInst::ICMP_ULT;
else
    Pred = ICmpInst::ICMP_SLT;

the function isKnownNonNegativeInLoop is intended to prove iteration_count SGE
0. from C code, iteration_count is an unsigned int and "> 0", but at LLVM IR
code, it's hard to prove iteration_count SGE 0, LLVM IR doesn't care if it's an
unsigned int. I don't know if there is better way to let the compiler knows
iteration_count is great than 0.

finally, I have to change the type of iteration_count from unsigned to signed,
the optimization IRCE works.

I don't know if there is better idea to let the optimzation</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>