<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 - Simple no-op loops with a termination flag are not optimized out"
   href="https://bugs.llvm.org/show_bug.cgi?id=34171">34171</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Simple no-op loops with a termination flag are not optimized out
          </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>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>geoffrey@allott.email
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following C code results in a loop with many iterations even with -O3; I
would expect it to be optimized out:

------------------------

void do_nothing() {
    int i=200, q=0;
    while(!q) if (!--i) i=q=1;
}

------------------------

; Function Attrs: norecurse nounwind readnone sspstrong uwtable
define void @do_nothing() local_unnamed_addr #0 {
  br label %1

; <label>:1:                                      ; preds = %1, %0
  %2 = phi i32 [ 200, %0 ], [ %3, %1 ]
  %3 = add nsw i32 %2, -25
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %5, label %1

; <label>:5:                                      ; preds = %1
  ret void
}

------------------------

    movl    $-200, %eax
.LBB0_1:
    addl    $25, %eax
    jne .LBB0_1
    retq

------------------------

(Any number low enough seems to optimize out - I assume because of some
hardcoded limits that simply evaluate some number of loops.)

It seems as though the problem is the fact that `i` is set to 1 on exit of the
loop - if this is removed then the loop is optimized away no matter the inital
value of i.

This situation can come up very often whenever a flag is used as the condition
to exit a loop. For instance, no-op loops in rust are not optimized out by llvm
because they terminate in such a way.</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>