<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 - Missing optimization: suboptimal code without constexpr"
   href="https://bugs.llvm.org/show_bug.cgi?id=37415">37415</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missing optimization: suboptimal code without constexpr
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>antoshkka@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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?</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>