Bug 7515 - sim: mips: uses lhs casts which makes strict-aliasing angry
Summary: sim: mips: uses lhs casts which makes strict-aliasing angry
Status: ASSIGNED
Alias: None
Product: gdb
Classification: Unclassified
Component: sim (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-03-08 14:38 UTC by ac131313
Modified: 2021-10-31 17:26 UTC (History)
2 users (show)

See Also:
Host:
Target: mips-*
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ac131313 2002-03-08 22:38:01 UTC
[Converted from Gnats 410]

Below is a reply from an e-mail I sent to gcc@.  This was apparently discovered ~two years ago.  I'm not sure any current src/sim simulators suffer this problem.  I'm mainly adding it for reference.

GDB, is also very much exposed to the same problem.

Release:
unknown

How-To-Repeat:
To: Andrew Cagney
CC: gcc@
Subject: Re: -fstrict-aliasing and naughty code?
From: Geoff Keating

Hello,
> 
> I'm trying to understand how to write ``bad'' (host dependant) code
> that doesn't get screwed by strict aliasing.
> 
> For instance, the code snipit:
> 

> > unsigned i;
> > unsigned64 tmp_reg, tmp_reg1;
> > for (i = 0; i < 4; i++)
> > *( (i < 2 ? (unsigned32 *) &tmp_reg
> > : (unsigned32 *) &tmp_reg1)
> > + (1 - i % 2) ) = ...;
> > cpu->registers[...] = tmp_reg;
> >

> 
> I'm told, is bad because:
> 

> > apparently, when -fstrict-aliasing is in effect, gcc is
> > allowed to assume that the expression inside the for loop
> > has no effect on the value of tmp_reg and tmp_reg1, since
> > the assignment is to an object of dissimilar type.

> 
> Provided I make (wild?) assumptions about the host and compiler, can I
> instead write the above to use something like:
> 
> union {
> unsigned64 u64;
> unsigned32 u32[2];
> } tmp_reg, tmp_reg1;
> 
> for (i = 0; i < 4; i++)
> if (i < 2)
> tmp_reg.u32[1 - i % 2] = ...
> else
> tmp_reg1.u32[1 - i %2] = ...;
> cpu->registers[...] = tmp_reg.u64;


Yes, this is documented to work:

     The practice of reading from a different union member than the one
     most recently written to (called "type-punning") is common.  Even
     with `-fstrict-aliasing', type-punning is allowed, provided the
     memory is accessed through the union type.

However, it will be no more efficient than the more portable

  unsigned32 tmp_reg[2], tmp_reg1[2];

  for (i = 0; i < 4; i++)
  if (i < 2)
    tmp_reg[1 - i % 2] = ...
  else
    tmp_reg1[1 - i %2] = ...;
cpu->registers[...] = (unsigned64)tmp_reg[0] << 32 | tmp_reg[1];

in fact it will usually be less efficient because GCC will allocate
registers better for the second example.
Comment 1 Mike Frysinger 2011-11-06 07:02:57 UTC
i'm guessing you mean the code in sim/mips/interp.c:sim_fetch_register()
...
*(unsigned64*)memory = H2T_8 ((unsigned32) (cpu->fgr[rn - FGR_BASE]));
...