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

            Bug ID: 94122
           Summary: Wrong optimization: reading value of a decimal FP
                    variable changes its representation for optimizer
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Split from bug 94103, comment 1.

It seems the optimizer sometimes computes the representation of variables from
its value instead of tracking it directly. This is wrong when the value admits
different representations.
(Given that the value is used, the representation should be valid (non-trap).)

Example with decimal floating-point:

----------------------------------------------------------------------
#include <string.h>
#include <stdio.h>

int main()
{
    _Decimal32 x = 9999999; // maximum significand
    unsigned u;

    memcpy(&u, &x, sizeof u);
    printf("%#08x\n", u);

    ++*(unsigned char *)&x; // create non-canonical representation of 0
    (void)-x;

    memcpy(&u, &x, sizeof u);
    printf("%#08x\n", u);
}
----------------------------------------------------------------------
$ gcc -std=c2x -pedantic -Wall -Wextra test.c && ./a.out
0x6cb8967f
0x6cb89680
$ gcc -std=c2x -pedantic -Wall -Wextra -O3 test.c && ./a.out
0x6cb8967f
0x32800000
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200305 (experimental)
----------------------------------------------------------------------

Unoptimized results are right, optimized -- wrong.

Reply via email to