[llvm-bugs] [Bug 34538] New: Failure to optimize range loops

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Sep 9 06:44:52 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34538

            Bug ID: 34538
           Summary: Failure to optimize range loops
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedbugs at nondot.org
          Reporter: gonzalobg88 at gmail.com
                CC: llvm-bugs at lists.llvm.org

Given the following modern C++ range-like code using optionals (see it here in
action: https://godbolt.org/g/Lz1xNK):

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

-- 
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/20170909/777bdd13/attachment.html>


More information about the llvm-bugs mailing list