https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113676

            Bug ID: 113676
           Summary: Miscompilation tree-vrp __builtin_unreachable
           Product: gcc
           Version: 12.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: magnus.hegdahl at gmail dot com
  Target Milestone: ---

Compiling the following code with -O1 -ftree-vrp or higher on GCC 10 or 12
results in a program that segfaults when argc >= 2.

#include <bit>
#include <vector>

auto main(int argc, char **) -> int {
    auto rounded_n = std::bit_ceil(static_cast<unsigned>(argc));
    auto a = std::vector<int>(2UL * rounded_n);

    for (std::size_t i = rounded_n; i-- > 1;) {
        if (!(0 < i && i < rounded_n)) __builtin_unreachable();
        a[i] = 0;
    }
}

If __builtin_unreachable is replaced by for example __builtin_trap, there is no
bug.
I could not reproduce this in GCC 11 or 13.

https://godbolt.org/z/dvEWKzWTh

Disassembly of the loop body:
.L6:
        mov     DWORD PTR [rdi+rdx*4], 0
        sub     rdx, 1
        jmp     .L6

Reply via email to