public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94103] New: Wrong optimization: reading value of a variable changes its representation for optimizer
@ 2020-03-09 17:45 ch3root at openwall dot com
  2020-03-09 17:46 ` [Bug middle-end/94103] " ch3root at openwall dot com
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: ch3root at openwall dot com @ 2020-03-09 17:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94103
           Summary: Wrong optimization: reading value of a 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: ---

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 lost padding in x86-64 long double:

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

int main()
{
    long double x;

    // fill x including the padding
    unsigned long u[2] = {0xEEEEEEEEEEEEEEEE, 0xEEEEEEEEEEEEEEEE};
    memcpy(&x, &u, sizeof x);

    // print the representation of x (initial)
    memcpy(&u, &x, sizeof u);
    printf("%016lX %016lX\n", u[1], u[0]);

    // change the representation of x a bit
    ++*(unsigned char *)&x;
    (void)-x; // use the value of x but don't write it

    // print the representation of x (resulting)
    memcpy(&u, &x, sizeof u);
    printf("%016lX %016lX\n", u[1], u[0]);
}
----------------------------------------------------------------------
$ gcc -std=c2x -pedantic -Wall -Wextra test.c && ./a.out
EEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEF
$ gcc -std=c2x -pedantic -Wall -Wextra -O3 test.c && ./a.out
EEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEE
000000000000EEEE EEEEEEEEEEEEEEEF
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200305 (experimental)
----------------------------------------------------------------------

Zeros in the last output line are wrong.

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2020-04-15  9:58 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09 17:45 [Bug middle-end/94103] New: Wrong optimization: reading value of a variable changes its representation for optimizer ch3root at openwall dot com
2020-03-09 17:46 ` [Bug middle-end/94103] " ch3root at openwall dot com
2020-03-09 17:50 ` pinskia at gcc dot gnu.org
2020-03-09 22:30 ` ch3root at openwall dot com
2020-03-10  9:39 ` [Bug target/94103] " rguenth at gcc dot gnu.org
2020-03-10 13:41 ` ch3root at openwall dot com
2020-03-12  4:13 ` law at redhat dot com
2020-03-12 13:21 ` rguenth at gcc dot gnu.org
2020-03-12 13:23 ` rguenth at gcc dot gnu.org
2020-03-12 13:36 ` marxin at gcc dot gnu.org
2020-03-20 12:32 ` ch3root at openwall dot com
2020-03-20 14:25 ` rguenther at suse dot de
2020-03-20 14:44 ` schwab@linux-m68k.org
2020-03-20 16:29 ` ch3root at openwall dot com
2020-03-20 16:41 ` rguenther at suse dot de
2020-03-20 17:47 ` ch3root at openwall dot com
2020-03-20 17:53 ` rguenther at suse dot de
2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
2020-04-15  9:58 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).