Bug 32677 - optimizer fails properly accessing argument
Summary: optimizer fails properly accessing argument
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-08 17:59 UTC by Giuseppe Attardi
Modified: 2007-07-08 18:23 UTC (History)
14 users (show)

See Also:
Host: x86_64 GNU/Linux
Target: x86_64-redhat-linux
Build: 4.2.0
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Giuseppe Attardi 2007-07-08 17:59:29 UTC
The problem occurs when using flags -O3 (or -O2) in gcc 4.0, 4.1 and 4.2.0.

The following program exhibits the bug:

#include <iostream>

inline void store4(char* dst, char* src) {
  *(unsigned*)dst = *(unsigned*)src;
}

void    set(char* dst, long value)  {
  store4(dst, (char*)&value);
}

int main(int argc, char* argv[])
{
  int  value = 0;
  char* data = (char*)&value;
  set(data, 1);
  std::cerr << value << std::endl;
}

Command line for showing the bug:

 > c++ -o gccBug -O3 gccBug.cpp ; gccBug
 0
 > c++ -o gccBug -O gccBug.cpp ; gccBug
 1

The bug also disappears when compiling with option -m32.

From the assembly code for set(), the -O3 optimized version is:

_Z3setPcl:
.LFB1462:
        movl    -8(%rsp), %eax
        movl    %eax, (%rdi)
        ret

instead the -O version is:

_Z3setPcl:
.LFB1462:
        movq    %rsi, -8(%rsp)
        movl    -8(%rsp), %eax
        movl    %eax, (%rdi)
        ret

-- Giuseppe Attardi
University of Pisa
Italy
Comment 1 Andrew Pinski 2007-07-08 18:23:38 UTC
You are violating C/C++ aliasing rules.  You are accessing a long as an unsigned int which is not allowed by C/C++ and is undefined.  It is allowed to access an int as a unsigned int and a long as an unsigned long but not long as an int or an int as a long.

*** This bug has been marked as a duplicate of 21920 ***