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


-- 
           Summary: optimizer fails properly accessing argument
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: attardi at di dot unipi dot it
 GCC build triplet: 4.2.0
  GCC host triplet: x86_64 GNU/Linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32677

Reply via email to