[llvm-bugs] [Bug 33023] New: Clang does not optimize simple code

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 12 05:01:16 PDT 2017


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

            Bug ID: 33023
           Summary: Clang does not optimize simple code
           Product: clang
           Version: 4.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++14
          Assignee: unassignedclangbugs at nondot.org
          Reporter: vladon at yandex-team.ru
                CC: llvm-bugs at lists.llvm.org

===
#include <stdlib.h>

struct Vec {
    int len;
    int* data;
};

inline int get(struct Vec* vec, int i) {
    if (i < vec->len) {
        return vec->data[i];
    } else {
        abort();
    }
}

int sum5(struct Vec* vec) {
    return get(vec, 0) + get(vec, 1) + get(vec, 2) + get(vec, 3) + get(vec, 4)
+ get(vec, 5);
}
===

See https://godbolt.org/g/WywyK3

But it optimizes this:

===
#include <stdlib.h>

struct Vec {
    int len;
    int* data;
};

inline int get(struct Vec* vec, int i) {
    if (i < vec->len) {
        return vec->data[i];
    } else {
        abort();
    }
}

int sum5(struct Vec* vec) {
    return get(vec, 5) + get(vec, 4) + get(vec, 3) + get(vec, 2) + get(vec, 1)
+ get(vec, 0);
}
===

See https://godbolt.org/g/L5TjyT

Or this:

===
#include <stdlib.h>

struct Vec {
    int len;
    int* data;
};

inline int get(struct Vec* vec, int i) {
    if (i < vec->len) {
        return vec->data[i];
    } else {
        abort();
    }
}

int sum5(struct Vec* vec) {
    int sum = 0;
    for (int i = 0; i < 6; ++i)
        sum += get(vec, i);
    return sum;
}
===

See https://godbolt.org/g/gldaXb

-- 
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/20170512/7ff1fbfa/attachment.html>


More information about the llvm-bugs mailing list