Bug 10142

Summary: [SPARC64] gcc produces wrong code when passing structures by value
Product: gcc Reporter: p.van-hoof
Component: targetAssignee: Eric Botcazou <ebotcazou>
Status: RESOLVED FIXED    
Severity: normal CC: briant, ehrhardt, gcc-bugs
Priority: P3 Keywords: wrong-code
Version: 3.2.2   
Target Milestone: 3.3.1   
Host: sparc-sun-solaris2.9 Target: sparc-sun-solaris2.9
Build: sparc-sun-solaris2.9 Known to work:
Known to fail: Last reconfirmed: 2003-06-09 15:11:49

Description p.van-hoof 2003-03-18 23:26:00 UTC
In the program below, a struct is passed as a parameter to a routine.
This is clearly done incorrectly, as demonstrated by the print statements.
This bug is triggered by the -m64 flag on the command line, even in
non-optimized mode.

This bug is present in gcc 3.2.2, as well as 3.3 and 3.4.

Release:
3.2.2

Environment:
System: SunOS scooby 5.9 Generic_112233-04 sun4u sparc SUNW,Sun-Blade-100
Architecture: sun4

	
host: sparc-sun-solaris2.9
build: sparc-sun-solaris2.9
target: sparc-sun-solaris2.9
configured with: /opt/temp/gcc-3.2.2/configure --prefix=/opt/local --enable-threads

How-To-Repeat:
scooby>	gcc h.c
scooby>	a.out 
1.20e+00 25
1.20e+00 25

scooby>	gcc -m64 h.c
scooby>	a.out
1.20e+00 25
1.20e+00 -2147488472

scooby>	cat h.c

typedef struct {
	double m;
	long x;
} mx;

void b(int i1,int i2,int i3,int i4,int i5,int i6,mx m)
{
	printf("%.2e %ld\n",m.m,m.x);
}

int main() {
	mx z={1.2, 25};
	printf( "%.2e %ld\n", z.m, z.x );
	b(1,2,3,4,5,6,z);
	return 0;
}
Comment 1 Eric Botcazou 2003-03-22 15:06:18 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed on all active branches. The obvious workaround is
    to pass the structure by reference. gcc 2.95.3 ICEs on the testcase so this is not a regression.
Comment 2 Christian Ehrhardt 2003-04-14 19:22:49 UTC
From: "Christian Ehrhardt" <ehrhardt@mathematik.uni-ulm.de>
To: p.van-hoof@qub.ac.uk, gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org,
  nobody@gcc.gnu.org, gcc-prs@gcc.gnu.org
Cc:  
Subject: Re: c/10142: [Sparc] gcc -m64 produces wrong code when passing structures by value
Date: Mon, 14 Apr 2003 19:22:49 +0200

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10142
 
 This patch seems to fix the problem for me, but regtests are still in
 progress and I'm not completly sure what the test is supposed to do
 in the first place.
 
 
 Index: calls.c
 ===================================================================
 RCS file: /cvsroot/gcc/gcc/gcc/calls.c,v
 retrieving revision 1.263
 diff -u -r1.263 calls.c
 --- calls.c	14 Mar 2003 01:59:16 -0000	1.263
 +++ calls.c	14 Apr 2003 17:20:36 -0000
 @@ -1317,7 +1317,11 @@
  	 it means that we are to pass this arg in the register(s) designated
  	 by the PARALLEL, but also to pass it in the stack.  */
        if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
 +#if 0
  	  && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
 +#else
 +	)
 +#endif
  	args[i].pass_on_stack = 1;
  
        /* If this is an addressable type, we must preallocate the stack
 
    regards  Christian
 
 -- 
 THAT'S ALL FOLKS!
Comment 3 Eric Botcazou 2003-06-09 15:11:09 UTC
I'll look into this one.
Comment 4 Eric Botcazou 2003-06-09 15:11:49 UTC
Investigating.
Comment 5 Eric Botcazou 2003-06-10 23:27:40 UTC
*** Bug 11136 has been marked as a duplicate of this bug. ***
Comment 6 CVS Commits 2003-06-13 03:56:15 UTC
Subject: Bug 10142

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	ebotcazou@gcc.gnu.org	2003-06-13 03:56:13

Modified files:
	gcc            : ChangeLog 
	gcc/config/sparc: sparc.c 

Log message:
	PR target/10142
	* config/sparc/sparc.c (function_arg_record_value_parms): Add
	new 'stack' field.
	(function_arg_record_value_1): Set 'stack' to 1 if we run out of
	integer slots for an integer field.
	(function_arg_record_value_3): Shift vector index.
	(function_arg_record_value_2): Likewise.
	(function_arg_record_value): Initialize 'stack' to 0.
	Set 'stack' to 1 if we run out of integer slots for an integer field.
	Generate (parallel [(expr_list (nil) ...) ...]) if 'stack' is set to 1.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.18182&r2=1.18183
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/sparc/sparc.c.diff?cvsroot=gcc&r1=1.243&r2=1.244

Comment 7 CVS Commits 2003-06-13 04:07:24 UTC
Subject: Bug 10142

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	ebotcazou@gcc.gnu.org	2003-06-13 04:07:18

Modified files:
	gcc            : ChangeLog 
	gcc/config/sparc: sparc.c 

Log message:
	PR target/10142
	* config/sparc/sparc.c (function_arg_record_value_parms): Add
	new 'stack' field.
	(function_arg_record_value_1): Set 'stack' to 1 if we run out of
	integer slots for an integer field.
	(function_arg_record_value_3): Shift vector index.
	(function_arg_record_value_2): Likewise.
	(function_arg_record_value): Initialize 'stack' to 0.
	Set 'stack' to 1 if we run out of integer slots for an integer field.
	Generate (parallel [(expr_list (nil) ...) ...]) if 'stack' is set to 1.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.576&r2=1.16114.2.577
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/sparc/sparc.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.233.4.3&r2=1.233.4.4

Comment 8 Eric Botcazou 2003-06-13 07:05:14 UTC
See http://gcc.gnu.org/ml/gcc-patches/2003-06/msg01079.html