[Bug target/31830] New: Input parameter trashed with optimization -O when using a union and bit field

2007-05-04 Thread gmorain at gmail dot com
This occurs with a cross compilation from i386-linux to powerpc-elf.  The input
parameter in r3 gets overwritten by the return value from a function call. 
This only occurs when optimization is enabled (-O1 or higher).  This has
something to do with unions and bit fields.  A simple test case follows.

foo.c:
extern int bar1(void);
extern void bar2(int);

void foo(char x) 
{
union {
int val;
struct {
unsigned a: 8;
} f;
} u;

u.val = bar1();
u.f.a = x;
bar2(u.val);
}

Here is the assembly:

foo:
stwu 1,-16(1)
mflr 0
stw 0,20(1)
bl bar1
rlwimi 3,3,24,0,7
bl bar2
lwz 0,20(1)
mtlr 0
addi 1,1,16
blr
The input paramter to foo() is passed in r3.  r3 is not saved before calling
bar1(), and bar1() returns its value in r3, thereby overwriting the input value
of r3.  But the input value of r3 is required in the next instruction 'rlwimi
3,3,24,0,7'.

This works with gcc 3.3.1.  It also works with gcc 4.1.2 targetted for
mipsisa32-elf.  It fails with gcc 4.1.2 targetted for powerpc-elf.  My target
is an embedded system with no OS.

Here is how I build this:

$ /usr/local/trpz/trpz4.1/bin/powerpc-elf-gcc -v -save-temps -Wall -c -O -o
foo.o foo.c
Using built-in specs.
Target: powerpc-elf
Configured with: ../gcc-4.1.2/configure --prefix=/usr/local/trpz/trpz4.1
--target=powerpc-elf --enable-64-bit-bfd --disable-libssp
Thread model: single
gcc version 4.1.2

/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../libexec/gcc/powerpc-elf/4.1.2/cc1
-E -quiet -v -iprefix
/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../lib/gcc/powerpc-elf/4.1.2/
foo.c -Wall -O -fpch-preprocess -o foo.i
ignoring nonexistent directory
"/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../lib/gcc/powerpc-elf/4.1.2/../../../../powerpc-elf/sys-include"
ignoring nonexistent directory
"/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../lib/gcc/powerpc-elf/4.1.2/../../../../powerpc-elf/include"
ignoring duplicate directory
"/usr/local/trpz/trpz4.1/lib/gcc/powerpc-elf/4.1.2/include"
ignoring nonexistent directory
"/usr/local/trpz/trpz4.1/lib/gcc/powerpc-elf/4.1.2/../../../../powerpc-elf/sys-include"
ignoring nonexistent directory
"/usr/local/trpz/trpz4.1/lib/gcc/powerpc-elf/4.1.2/../../../../powerpc-elf/include"
#include "..." search starts here:
#include <...> search starts here:

/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../lib/gcc/powerpc-elf/4.1.2/include
End of search list.

/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../libexec/gcc/powerpc-elf/4.1.2/cc1
-fpreprocessed foo.i -quiet -dumpbase foo.c -auxbase-strip foo.o -O -Wall
-version -o foo.s
GNU C version 4.1.2 (powerpc-elf)
compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=129090
Compiler executable checksum: bd93f7309b7da302bc5e08433888e36b

/home/gary/trpz/tools/i386-redhat-linux/trpz4.1/bin/../lib/gcc/powerpc-elf/4.1.2/../../../../powerpc-elf/bin/as
-mppc -many -V -Qy -o foo.o foo.s
GNU assembler version 2.17 (powerpc-elf) using BFD version 2.17

My host system is:
$ uname -por
2.6.20-1.2948.fc6 i686 GNU/Linux


-- 
   Summary: Input parameter trashed with optimization -O when using
a union and bit field
   Product: gcc
   Version: 4.1.2
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: target
    AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: gmorain at gmail dot com
  GCC host triplet: i386-linux-gnu
GCC target triplet: powerpc-elf


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



[Bug target/31830] Input parameter trashed with optimization -O when using a union and bit field

2007-05-04 Thread gmorain at gmail dot com


--- Comment #1 from gmorain at gmail dot com  2007-05-05 00:47 ---
Created an attachment (id=13513)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=13513&action=view)
Source that demonstrates the problem.


-- 


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



[Bug rtl-optimization/31830] Input parameter trashed with optimization -O when using a union and bit field

2007-05-18 Thread gmorain at gmail dot com


--- Comment #3 from gmorain at gmail dot com  2007-05-19 02:17 ---
(In reply to comment #2)
> I can reproduce the failure on powerpc-linux-gnu with 4.1.0 but it works on 
> the
> trunk. combine is doing something wrong.
> 

I tested it on 4.2.0, and it works OK.  Except, I can't understand why the
stack frame is 32 bytes instead of 16.

powerpc-elf-objdump -S foo.o

foo.o: file format elf32-powerpc

Disassembly of section .text:

 :
   0:   94 21 ff e0 stwur1,-32(r1)
   4:   7c 08 02 a6 mflrr0
   8:   93 a1 00 14 stw r29,20(r1)
   c:   90 01 00 24 stw r0,36(r1)
  10:   7c 7d 1b 78 mr  r29,r3
  14:   48 00 00 01 bl  14 
  18:   53 a3 c0 0e rlwimi  r3,r29,24,0,7
  1c:   48 00 00 01 bl  1c 
  20:   80 01 00 24 lwz r0,36(r1)
  24:   7c 08 03 a6 mtlrr0
  28:   83 a1 00 14 lwz r29,20(r1)
  2c:   38 21 00 20 addir1,r1,32
  30:   4e 80 00 20 blr


-- 


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