[llvm-bugs] [Bug 37415] New: Missing optimization: suboptimal code without constexpr

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 11 02:05:54 PDT 2018


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

            Bug ID: 37415
           Summary: Missing optimization: suboptimal code without
                    constexpr
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

Consider the following code snippet:

// Bubble-like sort. Anything complex enough will work
template <class It>
constexpr void sort(It first, It last) {
    for (;first != last; ++first) {
        auto it = first;
        ++it;
        for (; it != last; ++it) {
            if (*it < *first) {
                auto tmp = *it;
                *it = *first;
                *first = tmp;
            }
        }
    }
}

constexpr int generate() {
    int a[7] = {3, 7, 4, 2, 8, 0, 1};
    sort(a + 0, a + 7);
    return a[0] + a[6];
}

int no_constexpr() {
    return generate();
}

int with_constexpr() {
    constexpr auto res =  generate();
    return res;
}



Above code generates ~150 assembly instructions for `no_constexpr()` function
and just 2 instructions for  `with_constexpr()`:

with_constexpr(): # @with_constexpr()
  mov eax, 8
  ret


Could the compiler detect that `a[7]` holds values known at compile time and
force the constexpr on `sort(a + 0, a + 7);` and `generate()` evaluations?

-- 
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/20180511/c6db1860/attachment.html>


More information about the llvm-bugs mailing list