public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* SRA problem with uninitialzed fields
@ 2004-09-24 16:01 Richard Kenner
  2004-09-24 16:16 ` Andrew Pinski
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Kenner @ 2004-09-24 16:01 UTC (permalink / raw)
  To: rth; +Cc: dewar, gcc

Robert and I were just talking on the phone about implementation issues of
packed arrays in Ada and I realized there might be a problem with SRA.
Indeed there is and I can show it with a trivial C program. 

Consider:

struct foo {unsigned int f: 2;};

int
sub1 ()
{
  struct foo x;

  x.f |= 3;
  return x.f == 3;
}

SRA makes an x$f and here's the resulting .vars file:

sub1 ()
{
  <unnamed type> x$f;

  return (int) (<unnamed type>) (unsigned char) ((signed char) x$f | 3) == 3;
}

In the original C, the value of x.f is clearly uninitialized, but the
assignment statement forces it to be 3.  So the comparison is always true.

But in the resulting code, x$f is a full byte and so the upper six bits
remain uninitialized.  Thus the comparison above may or not may be true,
depending on how things happened to be initialized.

So clearly this is an unsafe optimization, but it's not clear what the
exact condition for safety is.  I suspect the point is that the SRA'ed
variable can't ever be uninitialized for this to work.

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: SRA problem with uninitialzed fields
@ 2004-09-24 16:55 Richard Kenner
  2004-09-24 18:23 ` Joseph S. Myers
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Kenner @ 2004-09-24 16:55 UTC (permalink / raw)
  To: pinskia; +Cc: dewar, gcc, rth

    This looks like a bug in the expanders than the SRA as the <unnamed
    type> is the right type in the sense it say it is only a certain
    number of bits in this case 2.

Indeed I stopped when I looked at the .vars since I "knew" we didn't do
that sort of masking.  But now I see that we actually do that masking
operation whenever TYPE_PRECISION is less than the size of the mode, if it's
required by the language (the default is false, but set true for C/C++/Objc).

I'd guess that logic was added for precisely this case and the generated
code omits the comparison since it's always known to be true.

I clearly have to turn that flag on for Ada and then revisit some of the
discussions I had this morning in this light.

Thanks!

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: SRA problem with uninitialzed fields
@ 2004-09-24 18:43 Richard Kenner
  2004-09-24 18:50 ` Joseph S. Myers
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Kenner @ 2004-09-24 18:43 UTC (permalink / raw)
  To: jsm; +Cc: gcc

    C++ enums were one case of restricted-precision types for which this 
    masking was not wanted, but for now 
    <http://gcc.gnu.org/ml/gcc-patches/2004-08/msg01985.html> and until a C++ 
    DR about this is resolved (see the long discussions on the gcc list last 
    month) they don't have the restricted precision.  

But what about the C test case I sent?  Isn't that valid C++ also and
wouldn't it have the problem I mentioned if that hook is false?

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: SRA problem with uninitialzed fields
@ 2004-09-24 18:53 Richard Kenner
  2004-09-24 19:02 ` Joseph S. Myers
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Kenner @ 2004-09-24 18:53 UTC (permalink / raw)
  To: jsm; +Cc: gcc

    A proper implementation that keeps the types those that accord with
    the language semantics would be for the GIMPLE generated to include
    explicit mask operations for assignments to bit-fields.  If it doesn't
    represent that assignment to a bit-field from the same type may change
    the value, then there are likely to be problems.  At one point there
    were, but I don't see such problems at present in some quick tests.

Well, look at the test case I sent. A mask is clearly required on the
comparison (at least, I think so!), but the only mechanism to generate it
is the hook, which is false for C++.

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

end of thread, other threads:[~2004-09-24 22:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-24 16:01 SRA problem with uninitialzed fields Richard Kenner
2004-09-24 16:16 ` Andrew Pinski
2004-09-24 16:55 Richard Kenner
2004-09-24 18:23 ` Joseph S. Myers
2004-09-24 18:43 Richard Kenner
2004-09-24 18:50 ` Joseph S. Myers
2004-09-24 18:53 Richard Kenner
2004-09-24 19:02 ` Joseph S. Myers
2004-09-25  2:38   ` Robert Dewar
2004-09-25  2:59     ` Joseph S. Myers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).