All of lore.kernel.org
 help / color / mirror / Atom feed
* Error in gcc version 2.96 20000731
@ 2001-09-13 14:15 Kjeld Borch Egevang
  2001-09-13 15:10 ` H . J . Lu
  2001-09-13 15:14 ` Maciej W. Rozycki
  0 siblings, 2 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 14:15 UTC (permalink / raw)
  To: linux-mips

Hi all.

I discovered an optimization error in the current gcc for MIPS.

When I compile the code below with -O2 it clears the code-field just 
after setting it. The instructions are mixed up. It works fine with -O1 
and -O0.

If the "//" is removed in front of the first printf, it works too.


/Kjeld

--------------- cut here -----------------
#include <stdio.h>


typedef struct rtx_def
{
 short code;
 int dummy;
} rtx;


void put_code (rtx *rt, short code)
{
 register int length;

 length = 1;
 for (; length >= 0; length--)
   ((int *) rt)[length] = 0;

 // printf("put_code before\n");
 rt->code = code;
 printf("put_code after, code=%d %d\n", code, rt->code);
}


int main()
{
 rtx rt;

 put_code(&rt, 5);
 printf("gen_rtx, code=%d\n", rt.code);
}
--------------- cut here -----------------


The assembler looks like:

       .rdata
       .align  2
$LC0:
       .ascii  "put_code after, code=%d %d\n\000"
       .text
       .align  2
       .globl  put_code
       .ent    put_code
       .type    put_code,@function
put_code:
       .frame  $sp,32,$31              # vars= 0, regs= 2/0, args= 16, 
extra= 8
       .mask   0x90000000,-4
       .fmask  0x00000000,0
       .set    noreorder
       .cpload $25
       .set    reorder
       subu    $sp,$sp,32
       .cprestore 16
       move    $2,$5
       sll     $2,$2,16
       sra     $2,$2,16
       move    $5,$2
       la      $3,$LC0
       sw      $31,28($sp)
       sw      $28,24($sp)
       move    $6,$5
       sh      $2,0($4)     <----- sets code field
       sw      $0,4($4)     <----- clears structure
       sw      $0,0($4)     <----- clears structure
       move    $4,$3
       la      $25,printf
       jal     $31,$25
       lw      $31,28($sp)
       #nop
       .set    noreorder
       .set    nomacro
       j       $31
       addu    $sp,$sp,32
       .set    macro
       .set    reorder

       .end    put_code

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Error in gcc version 2.96 20000731
  2001-09-13 14:15 Error in gcc version 2.96 20000731 Kjeld Borch Egevang
@ 2001-09-13 15:10 ` H . J . Lu
  2001-09-13 21:09     ` Kjeld Borch Egevang
  2001-09-13 15:14 ` Maciej W. Rozycki
  1 sibling, 1 reply; 13+ messages in thread
From: H . J . Lu @ 2001-09-13 15:10 UTC (permalink / raw)
  To: Kjeld Borch Egevang; +Cc: linux-mips

On Thu, Sep 13, 2001 at 04:15:10PM +0200, Kjeld Borch Egevang wrote:
> Hi all.
> 
> I discovered an optimization error in the current gcc for MIPS.
> 
> When I compile the code below with -O2 it clears the code-field just 
> after setting it. The instructions are mixed up. It works fine with -O1 
> and -O0.
> 
> If the "//" is removed in front of the first printf, it works too.
> 

The code isn't ISO C. You cannot declare something as short and then
access it as int. On x86:

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 0
gen_rtx, code=0

On mips,

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 5
gen_rtx, code=0

You can fix the code or add -fno-strict-aliasing

# gcc alias.c -O2 -fno-strict-aliasing
put_code after, code=5 5
gen_rtx, code=5



H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Error in gcc version 2.96 20000731
  2001-09-13 14:15 Error in gcc version 2.96 20000731 Kjeld Borch Egevang
  2001-09-13 15:10 ` H . J . Lu
@ 2001-09-13 15:14 ` Maciej W. Rozycki
  2001-09-13 15:16   ` H . J . Lu
  2001-09-13 20:47     ` Kjeld Borch Egevang
  1 sibling, 2 replies; 13+ messages in thread
From: Maciej W. Rozycki @ 2001-09-13 15:14 UTC (permalink / raw)
  To: Kjeld Borch Egevang; +Cc: linux-mips

On Thu, 13 Sep 2001, Kjeld Borch Egevang wrote:

> I discovered an optimization error in the current gcc for MIPS.

 Is 2.96 20000731 current?  I thought 3.0.1 is.

> When I compile the code below with -O2 it clears the code-field just 
> after setting it. The instructions are mixed up. It works fine with -O1 
> and -O0.

 Use "-fno-strict-aliasing"?

> If the "//" is removed in front of the first printf, it works too.

 Why don't you use memset() to clear the struct in the first place?

-- 
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Error in gcc version 2.96 20000731
  2001-09-13 15:14 ` Maciej W. Rozycki
@ 2001-09-13 15:16   ` H . J . Lu
  2001-09-13 20:47     ` Kjeld Borch Egevang
  1 sibling, 0 replies; 13+ messages in thread
From: H . J . Lu @ 2001-09-13 15:16 UTC (permalink / raw)
  To: Maciej W. Rozycki; +Cc: Kjeld Borch Egevang, linux-mips

On Thu, Sep 13, 2001 at 05:14:45PM +0200, Maciej W. Rozycki wrote:
> On Thu, 13 Sep 2001, Kjeld Borch Egevang wrote:
> 
> > I discovered an optimization error in the current gcc for MIPS.
> 
>  Is 2.96 20000731 current?  I thought 3.0.1 is.
> 

gcc 3.0.1 lacks many patches in my gcc 2.96 and gcc 3.x. I am trying to
get them backported to 3.0.x.


H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 20:47     ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 20:47 UTC (permalink / raw)
  To: linux-mips

Thanks for your reply.

I was trying to port SPEC CPU 2000 (benchmarks), and one of the tests
failed. I boiled it down to the attached program. For performance reasons it
could be perfectly valid not to use memset(), but I agree that the code
looks a bit odd.


/Kjeld

-----Original Message-----
From: Maciej W. Rozycki [mailto:macro@ds2.pg.gda.pl]
Sent: 13. september 2001 17:15
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, 13 Sep 2001, Kjeld Borch Egevang wrote:

> I discovered an optimization error in the current gcc for MIPS.

 Is 2.96 20000731 current?  I thought 3.0.1 is.

> When I compile the code below with -O2 it clears the code-field just
> after setting it. The instructions are mixed up. It works fine with -O1
> and -O0.

 Use "-fno-strict-aliasing"?

> If the "//" is removed in front of the first printf, it works too.

 Why don't you use memset() to clear the struct in the first place?

--
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 20:47     ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 20:47 UTC (permalink / raw)
  To: linux-mips

Thanks for your reply.

I was trying to port SPEC CPU 2000 (benchmarks), and one of the tests
failed. I boiled it down to the attached program. For performance reasons it
could be perfectly valid not to use memset(), but I agree that the code
looks a bit odd.


/Kjeld

-----Original Message-----
From: Maciej W. Rozycki [mailto:macro@ds2.pg.gda.pl]
Sent: 13. september 2001 17:15
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, 13 Sep 2001, Kjeld Borch Egevang wrote:

> I discovered an optimization error in the current gcc for MIPS.

 Is 2.96 20000731 current?  I thought 3.0.1 is.

> When I compile the code below with -O2 it clears the code-field just
> after setting it. The instructions are mixed up. It works fine with -O1
> and -O0.

 Use "-fno-strict-aliasing"?

> If the "//" is removed in front of the first printf, it works too.

 Why don't you use memset() to clear the struct in the first place?

--
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 21:09     ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 21:09 UTC (permalink / raw)
  To: H . J . Lu; +Cc: linux-mips

Perhaps I don't get you point, but I get the same with:

int main()
{
  rtx rt;

  put_code(&rt, (short)5);
  printf("gen_rtx, code=%d\n", (int)rt.code);
}


/Kjeld

-----Original Message-----
From: owner-linux-mips@oss.sgi.com
[mailto:owner-linux-mips@oss.sgi.com]On Behalf Of H . J . Lu
Sent: 13. september 2001 17:11
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, Sep 13, 2001 at 04:15:10PM +0200, Kjeld Borch Egevang wrote:
> Hi all.
>
> I discovered an optimization error in the current gcc for MIPS.
>
> When I compile the code below with -O2 it clears the code-field just
> after setting it. The instructions are mixed up. It works fine with -O1
> and -O0.
>
> If the "//" is removed in front of the first printf, it works too.
>

The code isn't ISO C. You cannot declare something as short and then
access it as int. On x86:

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 0
gen_rtx, code=0

On mips,

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 5
gen_rtx, code=0

You can fix the code or add -fno-strict-aliasing

# gcc alias.c -O2 -fno-strict-aliasing
put_code after, code=5 5
gen_rtx, code=5



H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 21:09     ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 21:09 UTC (permalink / raw)
  To: H . J . Lu; +Cc: linux-mips

Perhaps I don't get you point, but I get the same with:

int main()
{
  rtx rt;

  put_code(&rt, (short)5);
  printf("gen_rtx, code=%d\n", (int)rt.code);
}


/Kjeld

-----Original Message-----
From: owner-linux-mips@oss.sgi.com
[mailto:owner-linux-mips@oss.sgi.com]On Behalf Of H . J . Lu
Sent: 13. september 2001 17:11
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, Sep 13, 2001 at 04:15:10PM +0200, Kjeld Borch Egevang wrote:
> Hi all.
>
> I discovered an optimization error in the current gcc for MIPS.
>
> When I compile the code below with -O2 it clears the code-field just
> after setting it. The instructions are mixed up. It works fine with -O1
> and -O0.
>
> If the "//" is removed in front of the first printf, it works too.
>

The code isn't ISO C. You cannot declare something as short and then
access it as int. On x86:

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 0
gen_rtx, code=0

On mips,

# gcc alias.c -O
put_code after, code=5 5
gen_rtx, code=5
# gcc alias.c -O2
put_code after, code=5 5
gen_rtx, code=0

You can fix the code or add -fno-strict-aliasing

# gcc alias.c -O2 -fno-strict-aliasing
put_code after, code=5 5
gen_rtx, code=5



H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Error in gcc version 2.96 20000731
  2001-09-13 21:09     ` Kjeld Borch Egevang
  (?)
@ 2001-09-13 21:14     ` H . J . Lu
  2001-09-13 21:22         ` Kjeld Borch Egevang
  -1 siblings, 1 reply; 13+ messages in thread
From: H . J . Lu @ 2001-09-13 21:14 UTC (permalink / raw)
  To: Kjeld Borch Egevang; +Cc: linux-mips

On Thu, Sep 13, 2001 at 11:09:04PM +0200, Kjeld Borch Egevang wrote:
> Perhaps I don't get you point, but I get the same with:
> 
> int main()
> {
>   rtx rt;
> 
>   put_code(&rt, (short)5);
>   printf("gen_rtx, code=%d\n", (int)rt.code);
> }
> 

That is not what I meant. You have

typedef struct rtx_def
{
  short code;
  int dummy;
} rtx;
 
The first field of rtx_def is short.

  for (; length >= 0; length--)
    ((int *) rt)[length] = 0;

But you access it as int. If you do

  for (; length >= 0; length--)
    ((short *) rt)[length] = 0;

It will be ok.


H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 21:22         ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 21:22 UTC (permalink / raw)
  To: H . J . Lu; +Cc: linux-mips

I get your point. A bit suprising though. Do you consider it a bug?

/Kjeld

-----Original Message-----
From: owner-linux-mips@oss.sgi.com
[mailto:owner-linux-mips@oss.sgi.com]On Behalf Of H . J . Lu
Sent: 13. september 2001 23:15
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, Sep 13, 2001 at 11:09:04PM +0200, Kjeld Borch Egevang wrote:
> Perhaps I don't get you point, but I get the same with:
> 
> int main()
> {
>   rtx rt;
> 
>   put_code(&rt, (short)5);
>   printf("gen_rtx, code=%d\n", (int)rt.code);
> }
> 

That is not what I meant. You have

typedef struct rtx_def
{
  short code;
  int dummy;
} rtx;
 
The first field of rtx_def is short.

  for (; length >= 0; length--)
    ((int *) rt)[length] = 0;

But you access it as int. If you do

  for (; length >= 0; length--)
    ((short *) rt)[length] = 0;

It will be ok.


H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
@ 2001-09-13 21:22         ` Kjeld Borch Egevang
  0 siblings, 0 replies; 13+ messages in thread
From: Kjeld Borch Egevang @ 2001-09-13 21:22 UTC (permalink / raw)
  To: H . J . Lu; +Cc: linux-mips

I get your point. A bit suprising though. Do you consider it a bug?

/Kjeld

-----Original Message-----
From: owner-linux-mips@oss.sgi.com
[mailto:owner-linux-mips@oss.sgi.com]On Behalf Of H . J . Lu
Sent: 13. september 2001 23:15
To: Kjeld Borch Egevang
Cc: linux-mips@oss.sgi.com
Subject: Re: Error in gcc version 2.96 20000731


On Thu, Sep 13, 2001 at 11:09:04PM +0200, Kjeld Borch Egevang wrote:
> Perhaps I don't get you point, but I get the same with:
> 
> int main()
> {
>   rtx rt;
> 
>   put_code(&rt, (short)5);
>   printf("gen_rtx, code=%d\n", (int)rt.code);
> }
> 

That is not what I meant. You have

typedef struct rtx_def
{
  short code;
  int dummy;
} rtx;
 
The first field of rtx_def is short.

  for (; length >= 0; length--)
    ((int *) rt)[length] = 0;

But you access it as int. If you do

  for (; length >= 0; length--)
    ((short *) rt)[length] = 0;

It will be ok.


H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Error in gcc version 2.96 20000731
  2001-09-13 21:22         ` Kjeld Borch Egevang
  (?)
@ 2001-09-13 21:26         ` H . J . Lu
  -1 siblings, 0 replies; 13+ messages in thread
From: H . J . Lu @ 2001-09-13 21:26 UTC (permalink / raw)
  To: Kjeld Borch Egevang; +Cc: linux-mips

On Thu, Sep 13, 2001 at 11:22:46PM +0200, Kjeld Borch Egevang wrote:
> I get your point. A bit suprising though. Do you consider it a bug?

It is an alias bug in your example.


H.J.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Error in gcc version 2.96 20000731
  2001-09-13 20:47     ` Kjeld Borch Egevang
  (?)
@ 2001-09-14 14:42     ` Maciej W. Rozycki
  -1 siblings, 0 replies; 13+ messages in thread
From: Maciej W. Rozycki @ 2001-09-14 14:42 UTC (permalink / raw)
  To: Kjeld Borch Egevang; +Cc: linux-mips

On Thu, 13 Sep 2001, Kjeld Borch Egevang wrote:

> I was trying to port SPEC CPU 2000 (benchmarks), and one of the tests
> failed. I boiled it down to the attached program. For performance reasons it
> could be perfectly valid not to use memset(), but I agree that the code
> looks a bit odd.

 With a high optimization level and a constant size of the area to set
(you'd likely use sizeof here, which is constant) gcc will happily inline
a memset() yielding no worse performance but no bugs and better
readability.  You may have no loop at all as the result.  Check yourself! 

-- 
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2001-09-14 15:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-09-13 14:15 Error in gcc version 2.96 20000731 Kjeld Borch Egevang
2001-09-13 15:10 ` H . J . Lu
2001-09-13 21:09   ` Kjeld Borch Egevang
2001-09-13 21:09     ` Kjeld Borch Egevang
2001-09-13 21:14     ` H . J . Lu
2001-09-13 21:22       ` Kjeld Borch Egevang
2001-09-13 21:22         ` Kjeld Borch Egevang
2001-09-13 21:26         ` H . J . Lu
2001-09-13 15:14 ` Maciej W. Rozycki
2001-09-13 15:16   ` H . J . Lu
2001-09-13 20:47   ` Kjeld Borch Egevang
2001-09-13 20:47     ` Kjeld Borch Egevang
2001-09-14 14:42     ` Maciej W. Rozycki

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.