<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 - Failure to optimize range loops"
   href="https://bugs.llvm.org/show_bug.cgi?id=34538">34538</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to optimize range loops
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>gonzalobg88@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Given the following modern C++ range-like code using optionals (see it here in
action: <a href="https://godbolt.org/g/Lz1xNK">https://godbolt.org/g/Lz1xNK</a>):

struct range { int start; int end; };
struct option { int ret; int tag; };

inline 
option next(range *range) {
    if (range->start < range->end) {
        int n = range->start;
        range->start++;
        option o{n, 1};
        return o;
    } else {
        option o{0, 0};
        return o;
    }
}

int loop() {
    range rng = {0, 100};
    for(;;) {
        option o = next(&rng);
        if (o.tag) {
            // nothing
        } else {
            break;
        }
    }
    return 0;
}

GCC generates the following assembly:

loop():
  xor eax, eax
  ret

while clang fails to optimize the loop, generating:

loop(): # @loop()
  xor eax, eax
.LBB0_1: # =>This Inner Loop Header: Depth=1
  xor ecx, ecx
  cmp eax, 100
  setl cl
  add ecx, eax
  cmp eax, 100
  mov eax, ecx
  jl .LBB0_1
  xor eax, eax
  ret</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>