[LLVMbugs] [Bug 20985] New: Failure to peal (potential) last iteration when profitable

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Sep 17 14:50:15 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20985

            Bug ID: 20985
           Summary: Failure to peal (potential) last iteration when
                    profitable
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedbugs at nondot.org
          Reporter: listmail at philipreames.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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.)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140917/00acfee3/attachment.html>


More information about the llvm-bugs mailing list