public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* working around "QImode can alias everything" missed optimizations
@ 2000-05-26 15:44 Doug Evans
  2000-05-27 15:15 ` Richard Henderson
  0 siblings, 1 reply; 13+ messages in thread
From: Doug Evans @ 2000-05-26 15:44 UTC (permalink / raw)
  To: gcc

version: current cvs sources as of around noon today
target: i386-linux

If you make `buf' in this program a short*, gcc -O2 -fomit-frame-pointer
optimizes `foo' just fine.  If `buf' is a char*, gcc does poorly
(I'm guessing because it can't know that buf may point to itself
and QImode values are allowed to alias anything).

So how do I work around this?
How do I rewrite this code so that I get something like the following?

foo:
	movl	4(%esp), %edx
	movl	(%edx), %eax
	movw	$1, (%eax)
	movw	$2, 1(%eax)
	movw	$3, 2(%eax)
	addl	$3, %eax
	movl	%eax, (%edx)
	ret

typedef struct {
    char* buf;
} tc_t;

static inline void
emit_1 (tc_t* tc, int c)
{
    *tc->buf = c;
    ++tc->buf;
}

void
foo (tc_t* p)
{
    emit_1 (p, 1);
    emit_1 (p, 2);
    emit_1 (p, 3);
}

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-26 15:44 working around "QImode can alias everything" missed optimizations Doug Evans
@ 2000-05-27 15:15 ` Richard Henderson
  2000-05-29  2:06   ` Doug Evans
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Henderson @ 2000-05-27 15:15 UTC (permalink / raw)
  To: Doug Evans; +Cc: gcc

On Fri, May 26, 2000 at 03:43:55PM -0700, Doug Evans wrote:
> (I'm guessing because it can't know that buf may point to itself
> and QImode values are allowed to alias anything).

Yep.

> So how do I work around this?

You can't.  :-(



r~

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-27 15:15 ` Richard Henderson
@ 2000-05-29  2:06   ` Doug Evans
  2000-05-29  8:20     ` working around "QImode can alias everything" missedoptimizations Mark Mitchell
  0 siblings, 1 reply; 13+ messages in thread
From: Doug Evans @ 2000-05-29  2:06 UTC (permalink / raw)
  To: Richard Henderson; +Cc: gcc

Richard Henderson writes:
 > On Fri, May 26, 2000 at 03:43:55PM -0700, Doug Evans wrote:
 > > (I'm guessing because it can't know that buf may point to itself
 > > and QImode values are allowed to alias anything).
 > 
 > Yep.
 > 
 > > So how do I work around this?
 > 
 > You can't.  :-(

I was afraid you were going to say that.

Any interest in fixing this [dunno how yet]?
GCC produces great code for non-QImode values (well done!).
It has to be agreed that QImode values shouldn't be penalized (right?).
[not that I would change behaviour of course, but rather that some
new way of specifying a "ya, I'm a QImode (ptr) value but, gosh darn it,
I don't alias everything" value needs to be found.  Alternatively, one
might come up with another way, but this way seems at first glance
like the right way to go.  I tried playing with `restrict' but I
don't know enough about it yet to say whether that has a chance
of working - I reread jfc's original email regarding his `restrict'
patch and he said there'd be problems with inline fns].

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

* Re: working around "QImode can alias everything" missedoptimizations
  2000-05-29  2:06   ` Doug Evans
@ 2000-05-29  8:20     ` Mark Mitchell
  2000-05-29 13:53       ` working around "QImode can alias everything" missed optimizations Toon Moene
  0 siblings, 1 reply; 13+ messages in thread
From: Mark Mitchell @ 2000-05-29  8:20 UTC (permalink / raw)
  To: dje; +Cc: rth, gcc

>>>>> "Doug" == Doug Evans <dje@transmeta.com> writes:

    Doug> right way to go.  I tried playing with `restrict' but I
    Doug> don't know enough about it yet to say whether that has a
    Doug> chance of working - I reread jfc's original email regarding
    Doug> his `restrict' patch and he said there'd be problems with
    Doug> inline fns].

I think `restrict' will do what you want -- in theory.  Whether the
GCC implementation (which is, I believe, based more on my work that
John Carr's) will do it or not is unclear.  The current implementation
of restrict is, shall we way, limited.  Information is not propogated
around the compiler as widely as it should be.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-29  8:20     ` working around "QImode can alias everything" missedoptimizations Mark Mitchell
@ 2000-05-29 13:53       ` Toon Moene
  0 siblings, 0 replies; 13+ messages in thread
From: Toon Moene @ 2000-05-29 13:53 UTC (permalink / raw)
  To: Mark Mitchell; +Cc: dje, rth, gcc

Mark Mitchell wrote:

> I think `restrict' will do what you want -- in theory.  Whether the
> GCC implementation (which is, I believe, based more on my work that
> John Carr's) will do it or not is unclear.

The original work on alias analysis by John Carr contained an
implementation of `restrict'.  I know, because when we used his work as
an additional patch to the gcc backend (delivered "with" g77 back in
March 1997), we had to be careful to remove the C frontend stuff that
implemented `restrict' from our patch ...

-- 
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
GNU Fortran 95: http://g95.sourceforge.net/ (under construction)

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-31 12:30 Mike Stump
@ 2000-05-31 12:37 ` Jamie Lokier
  0 siblings, 0 replies; 13+ messages in thread
From: Jamie Lokier @ 2000-05-31 12:37 UTC (permalink / raw)
  To: Mike Stump; +Cc: dje, gcc, rth

Mike Stump wrote:
> Anyway, all of this discussion is pointless anyway, as I already
> mentioned a more direct solution to the problem.

Interesting more general problems include "how do I declare a type that
nothing else will alias?"  For a specific global variable that nothing
ever aliases, does __restrict work?

-- Jamie

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

* Re: working around "QImode can alias everything" missed optimizations
@ 2000-05-31 12:30 Mike Stump
  2000-05-31 12:37 ` Jamie Lokier
  0 siblings, 1 reply; 13+ messages in thread
From: Mike Stump @ 2000-05-31 12:30 UTC (permalink / raw)
  To: egcs; +Cc: dje, gcc, rth

> Date: Wed, 31 May 2000 20:32:03 +0200
> From: Jamie Lokier <egcs@tantalophile.demon.co.uk>

> Mike Stump wrote:
> > > Is it possible to use an anonymous struct containing a char?
> > 
> > No.

> <raises eyebrow>

Welcoe to C/C++.

> Why not?  I thought a `struct X { int_type y; }'

int_type != char.

> would not type-alias anything except certain struct types related to
> X.

If you think it will work, show us the code, and I can try and cite
the rules.  I'll start you off:

struct X { char x; } a, b;

int main() {
       char *cp = &a.x;
       *cp = 1;
       a.x = 2;
       a = b;
}

Think about that can alias what under the ANSI rules.  The _point_ of
the exercise was to avoid aliasing, just in case you forget.  Be sure
to read and understand:

       [#7] An object shall have its stored value accessed only  by
       an lvalue expression that has one of the following types:59

          - an aggregate or union type that  includes  one  of  the
            aforementioned  types  among  its  members  (including,
            recursively, a member of a  subaggregate  or  contained
            union), or

          - a character type.

Anyway, all of this discussion is pointless anyway, as I already
mentioned a more direct solution to the problem.

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-31 11:32 ` Jamie Lokier
@ 2000-05-31 12:27   ` Andi Kleen
  0 siblings, 0 replies; 13+ messages in thread
From: Andi Kleen @ 2000-05-31 12:27 UTC (permalink / raw)
  To: Jamie Lokier; +Cc: gcc

Jamie Lokier <egcs@tantalophile.demon.co.uk> writes:

> Mike Stump wrote:
> > > Is it possible to use an anonymous struct containing a char?
> > 
> > No.
> 
> <raises eyebrow>
> 
> Why not?  I thought a `struct X { int_type y; }' would not type-alias
> anything except certain struct types related to X.

I tried it when the original question came up, but it seems to generate
a lot worse code (at least with egcs 1.1) than a simple char.

-Andi

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-31 11:25 Mike Stump
@ 2000-05-31 11:32 ` Jamie Lokier
  2000-05-31 12:27   ` Andi Kleen
  0 siblings, 1 reply; 13+ messages in thread
From: Jamie Lokier @ 2000-05-31 11:32 UTC (permalink / raw)
  To: Mike Stump; +Cc: dje, gcc, rth

Mike Stump wrote:
> > Is it possible to use an anonymous struct containing a char?
> 
> No.

<raises eyebrow>

Why not?  I thought a `struct X { int_type y; }' would not type-alias
anything except certain struct types related to X.

-- Jamie

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

* Re: working around "QImode can alias everything" missed optimizations
@ 2000-05-31 11:25 Mike Stump
  2000-05-31 11:32 ` Jamie Lokier
  0 siblings, 1 reply; 13+ messages in thread
From: Mike Stump @ 2000-05-31 11:25 UTC (permalink / raw)
  To: egcs; +Cc: dje, gcc, rth

> Date: Wed, 31 May 2000 19:39:58 +0200
> From: Jamie Lokier <egcs@tantalophile.demon.co.uk>

> Is it possible to use an anonymous struct containing a char?

No.

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

* Re: working around "QImode can alias everything" missed optimizations
  2000-05-30 14:58 Mike Stump
@ 2000-05-31 10:40 ` Jamie Lokier
  0 siblings, 0 replies; 13+ messages in thread
From: Jamie Lokier @ 2000-05-31 10:40 UTC (permalink / raw)
  To: Mike Stump; +Cc: dje, rth, gcc

Mike Stump wrote:
> The standard already has this.  It is called int8_t.  It is not a
> character type, and the alias stuff makes an exception only for
> character type (s/u/plain char).  So there you have it.  We just need
> to fix an brokenness in the compiler that doesn't respect this fact.

Is it possible to use an anonymous struct containing a char?

-- Jamie

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

* Re: working around "QImode can alias everything" missed optimizations
@ 2000-05-30 14:58 Mike Stump
  2000-05-31 10:40 ` Jamie Lokier
  0 siblings, 1 reply; 13+ messages in thread
From: Mike Stump @ 2000-05-30 14:58 UTC (permalink / raw)
  To: dje, rth; +Cc: gcc

> From: Doug Evans <dje@transmeta.com>
> Date: Mon, 29 May 2000 02:06:38 -0700 (PDT)
> To: Richard Henderson <rth@cygnus.com>
> Cc: gcc@gcc.gnu.org

> Richard Henderson writes:
>  > On Fri, May 26, 2000 at 03:43:55PM -0700, Doug Evans wrote:
>  > > (I'm guessing because it can't know that buf may point to itself
>  > > and QImode values are allowed to alias anything).
>  > 
>  > Yep.
>  > 
>  > > So how do I work around this?
>  > 
>  > You can't.  :-(

> I was afraid you were going to say that.

> Any interest in fixing this [dunno how yet]?  GCC produces great
> code for non-QImode values (well done!).  It has to be agreed that
> QImode values shouldn't be penalized (right?).  [not that I would
> change behaviour of course, but rather that some new way of
> specifying a "ya, I'm a QImode (ptr) value but, gosh darn it, I
> don't alias everything" value needs to be found.  Alternatively, one
> might come up with another way, but this way seems at first glance
> like the right way to go.

The standard already has this.  It is called int8_t.  It is not a
character type, and the alias stuff makes an exception only for
character type (s/u/plain char).  So there you have it.  We just need
to fix an brokenness in the compiler that doesn't respect this fact.

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

* Re: working around "QImode can alias everything" missed optimizations
@ 2000-05-29  8:27 Richard Kenner
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Kenner @ 2000-05-29  8:27 UTC (permalink / raw)
  To: mark; +Cc: gcc

    I think `restrict' will do what you want -- in theory.  Whether the
    GCC implementation (which is, I believe, based more on my work that
    John Carr's) will do it or not is unclear.  The current implementation
    of restrict is, shall we way, limited.  Information is not propogated
    around the compiler as widely as it should be.

I think it's actually pretty complete, but I'm doing work in that area
right now and it'll be a bit better once I'm done.

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

end of thread, other threads:[~2000-05-31 12:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-26 15:44 working around "QImode can alias everything" missed optimizations Doug Evans
2000-05-27 15:15 ` Richard Henderson
2000-05-29  2:06   ` Doug Evans
2000-05-29  8:20     ` working around "QImode can alias everything" missedoptimizations Mark Mitchell
2000-05-29 13:53       ` working around "QImode can alias everything" missed optimizations Toon Moene
2000-05-29  8:27 Richard Kenner
2000-05-30 14:58 Mike Stump
2000-05-31 10:40 ` Jamie Lokier
2000-05-31 11:25 Mike Stump
2000-05-31 11:32 ` Jamie Lokier
2000-05-31 12:27   ` Andi Kleen
2000-05-31 12:30 Mike Stump
2000-05-31 12:37 ` Jamie Lokier

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).