Setup:
   gcc version: 4.2.3
   system: Linux RedHat 4, x86-64 CPU, kernel 2.6.9-67.ELsmp

To reproduce:
Compile attached program as follows: "g++ -m32 -O2 -g a.ii" and run a.out.
You'll see assert failure on line 11669 of a.ii. If you use -O1 instead of -O2,
the program passes. 

I verified that the correct behaviour for the program is to pass. When I traced
the execution in the debugger, I found that a wrong register (edx instead of
eax) is used to access a variable. It seems GCC has lost track what edx
register refers to when it calls get_r_or_f() for the second time.

On a high level, the program iterates over a two-element linked list. It looks
at the first element, decides to increment the iterator, then looks at the
second element. In the correct behaviour, the iterator (B::list_iter) is left
pointing to the second element. In buggy behaviour, the iterator is incremented
twice, left pointing to the end iterator.

In buggy version, while looking at the content of the second list element, the
program uses wrong register to access one of the values (bsi.m_type on line
11,627). Instead of getting second list element's m_type, it gets it from the
first!

This bug reproduces exactly on 4.2.1. It does not reproduce on 4.3.0. However,
the assembly code on 4.3.0 looks very different from 4.2.3. This bug shows up
and disappears with tiniest unrelated changes to the code. E.g. printing if
list_iter == end_iter at the beginning of is_legal_position() would make the
bug go away.

Please let me know if there is a work-around for the issue with gcc 4.2.3. I'm
currently using -O1 and don't mind using it if I'm sure that the bug is caused
by a -O2-specific optimization (and not simply re-jigs the code).


-- 
           Summary: Wrong register used to get struct information
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ddenisen at altera dot com


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

Reply via email to