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

            Bug ID: 39675
           Summary: Undefined behavior inside constexpr function
           Product: clang
           Version: trunk
          Hardware: Other
                OS: other
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangb...@nondot.org
          Reporter: oleksandr.yefre...@gmail.com
                CC: blitzrak...@gmail.com, dgre...@apple.com,
                    erik.pilking...@gmail.com, llvm-bugs@lists.llvm.org,
                    richard-l...@metafoo.co.uk

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 https://godbolt.org/z/tWXnnd

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);
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to