<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 - Undefined behavior inside constexpr function"
   href="https://bugs.llvm.org/show_bug.cgi?id=39675">39675</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Undefined behavior inside constexpr function
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

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

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>oleksandr.yefremov@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Bug summary:
I create two functions a() and b().
Both are very similar and both invoke undefined behavior using integer
overflow.
clang compiler is able to optimize both functions (using -03 flag) up to single
constant.
Except constant is different in both cases! This is still acceptable because of
UB.

C++ forbids to invoke UB inside constexpr functions.
When making function a() constexpr compiler fails to compile code as expected
while it continue to compile very similar function b() and evaluates wrong
result.

Expected behavior:
compiler must not compile function b() when it is marked as constexpr because
of UB inside this function..

Sample code is available in compiler explorer at <a href="https://godbolt.org/z/tWXnnd">https://godbolt.org/z/tWXnnd</a>

Sample code:

//constexpr
int a()
{
    int i = (1 << 30) + ((1 << 30) - 1);
    return i*2/2;
}

constexpr
int f(int i)
{
    return i*2/2;
}

constexpr
int b()
{
    int i = (1 << 30) + ((1 << 30) - 1);
    return f(i);
}</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>