<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - SCEV unable to infer loop max bound for remainder loops"
   href="https://llvm.org/bugs/show_bug.cgi?id=31412">31412</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>SCEV unable to infer loop max bound for remainder loops
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>normal
          </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>mkuper@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, sanjoy@playingwithpointers.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider:

void foo0(int* in, int* out, unsigned k) {
  k %= 4;
  for (int i = 0; i < k; ++i) {
    in[i] += out[i];
  }
}

$ bin/clang -m32 -c -S -o - -O2 ~/llvm/temp/smallmax.cpp -emit-llvm | bin/opt
-analyze -scalar-evolution
[...]
Loop %for.body: backedge-taken count is (-1 + (zext i2 (trunc i32 %k to i2) to
i32))<nsw>
Loop %for.body: max backedge-taken count is -1


After talking to Sanjoy, it seems that the main issue is that
max-backedge-taken computation does not take the fact that "if the backedge is
taken, necessarily k > 0" into account.

Indeed:

void foo2(int* in, int* out, unsigned k) {
  k %= 4;
  k += 2;
  for (int i = 0; i < k; ++i) {
    in[i] += out[i];
  }
}

$ bin/clang -m32 -c -S -o - -O2 ~/llvm/temp/smallmax.cpp -emit-llvm | bin/opt
-analyze -scalar-evolution
[...]
Loop %for.body: backedge-taken count is (1 + (zext i2 (trunc i32 %k to i2) to
i32))<nuw><nsw>
Loop %for.body: max backedge-taken count is 4

But I think this is not the only problem.
If it were, adding 1 to k should have been sufficient, but:

void foo1(int* in, int* out, unsigned k) {
  k %= 4;
  k += 1;
  for (int i = 0; i < k; ++i) {
    in[i] += out[i];
  }
}

$ bin/clang -m32 -c -S -o - -O2 ~/llvm/temp/smallmax.cpp -emit-llvm | bin/opt
-analyze -scalar-evolution
[...]
Loop %for.body: backedge-taken count is (zext i2 (trunc i32 %k to i2) to i32)
Loop %for.body: max backedge-taken count is -1</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>