<html>
    <head>
      <base href="http://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 --- - Failure to peal (potential) last iteration when profitable"
   href="http://llvm.org/bugs/show_bug.cgi?id=20985">20985</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to peal (potential) last iteration when profitable
          </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>listmail@philipreames.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Clang -O3 currently fails to optimize the following loop:
void test_unrelated_nontrivial_return(int N, int M) {
  for(int i = 0; i < N; i++) {
    if( i > M ) {
      // this can be any non-trivial code which exits the loop
      counter = -1;
      return;
    }
    counter++;
  }
}

This loop can be transformed into the following:
int i;
for(i = 0; i < min(N,M); i++) {
  counter++;
}
if( i < N ) {
  // in the original loop, we would have taken an alternate loop exit
  // We now need to execute the part of the iteration which completes
  // before the loop exit is taken...
  if( i > M ) {
    counter = -1;
    return;
  }
  unreachable();
}

Running this through opt repeatly does not influence the result.  

This pattern appears frequently in languages with array bounds checks.  M & N
would be the length of two arrays.  A simple motivating example would be:
for(int i = 0; i < len(a); i++) {
  a[i] = b[i]; //with bounds checks!
}

The downside to this optimization is possible code size increase.  This
increase is limited to 2x and will likely be smaller in practice due to
simplifications in the loop (and potential removal of the loop entirely.)</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>