public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C and C++ parser performing optimizations
@ 2020-08-02 18:15 Stefan Franke
  2020-08-02 18:40 ` Alexander Monakov
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Franke @ 2020-08-02 18:15 UTC (permalink / raw)
  To: gcc-help

Consider this simple example:

 

  const char * foo() {

    char const * const a = "test";

    char const * const b = a;

    return b;

  }

 

And compile it with

 

gcc -O1 -S test.c -fdump-tree-original

 

you get

 

;; Function foo (null)

;; enabled by -tree-original

 

 

{

  const char * const a = (const char * const) "test";

  const char * const b = (const char * const) "test";

 

    const char * const a = (const char * const) "test";

    const char * const b = (const char * const) "test";

  return (const char *) "test";

}

 

So the parser performs unwanted and uncontrollable optimizations, which I
consider bogus.

 

Any thoughts on this?

 

cheers

 

Stefan


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

* Re: C and C++ parser performing optimizations
  2020-08-02 18:15 C and C++ parser performing optimizations Stefan Franke
@ 2020-08-02 18:40 ` Alexander Monakov
  2020-08-04  6:44   ` AW: " Stefan Franke
  0 siblings, 1 reply; 7+ messages in thread
From: Alexander Monakov @ 2020-08-02 18:40 UTC (permalink / raw)
  To: Stefan Franke; +Cc: gcc-help

On Sun, 2 Aug 2020, Stefan Franke wrote:

> So the parser performs unwanted and uncontrollable optimizations, which I
> consider bogus.

On occasion they are also incorrect.

My (possibly wrong or incomplete) understanding is that GCC does not have
internal separation of mandatory simplifications that need to be done in
the frontend (like constant folding in the context of integer constant
expressions) vs. optional simplifications (optimizing substitutions).
So it just does both at the same time.

Alexander

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

* AW: C and C++ parser performing optimizations
  2020-08-02 18:40 ` Alexander Monakov
@ 2020-08-04  6:44   ` Stefan Franke
  2020-08-04  7:13     ` Alexander Monakov
  2020-08-24 19:06     ` Gunther Nikl
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Franke @ 2020-08-04  6:44 UTC (permalink / raw)
  To: gcc-help



> -----Ursprüngliche Nachricht-----
> Von: Alexander Monakov <amonakov@ispras.ru>
> Gesendet: Sonntag, 2. August 2020 20:41
> An: Stefan Franke <stefan@franke.ms>
> Cc: gcc-help@gcc.gnu.org
> Betreff: Re: C and C++ parser performing optimizations
> 
> On Sun, 2 Aug 2020, Stefan Franke wrote:
> 
> > So the parser performs unwanted and uncontrollable optimizations,
> > which I consider bogus.
> 
> On occasion they are also incorrect.
> 
> My (possibly wrong or incomplete) understanding is that GCC does not have
> internal separation of mandatory simplifications that need to be done in
the
> frontend (like constant folding in the context of integer constant
> expressions) vs. optional simplifications (optimizing substitutions).
> So it just does both at the same time.
> 
> Alexander

Here is an example where gcc creates wrong code:

test.c:
int foo() {
  const char * const txt = "hello";
  register const char * const p asm("ecx") = txt;
  register int dx asm("edx");
  asm(" call _faa" :"=r" (dx) :"rf" (p));
}

gcc -O1 -S test.c -fdump-tree-original

The variable p gets replaced and the asm input parameters are wrong:

__asm__(" call _faa":"=r" dx:"rf" (const char * const) "hello");

Uh - oh!

Stefan


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

* Re: AW: C and C++ parser performing optimizations
  2020-08-04  6:44   ` AW: " Stefan Franke
@ 2020-08-04  7:13     ` Alexander Monakov
  2020-08-04 15:33       ` Segher Boessenkool
  2020-08-24 19:06     ` Gunther Nikl
  1 sibling, 1 reply; 7+ messages in thread
From: Alexander Monakov @ 2020-08-04  7:13 UTC (permalink / raw)
  To: Stefan Franke; +Cc: gcc-help

On Tue, 4 Aug 2020, Stefan Franke wrote:

> Here is an example where gcc creates wrong code:
> 
> test.c:
> int foo() {
>   const char * const txt = "hello";
>   register const char * const p asm("ecx") = txt;
>   register int dx asm("edx");
>   asm(" call _faa" :"=r" (dx) :"rf" (p));
> }
> 
> gcc -O1 -S test.c -fdump-tree-original
> 
> The variable p gets replaced and the asm input parameters are wrong:
> 
> __asm__(" call _faa":"=r" dx:"rf" (const char * const) "hello");

I think it's a separate issue: even if the frontend wouldn't do such
propagation, I'm pretty sure later passes would. const and volatile
qualifiers just do not work as expected with register variables, and
since recently the documentation mentions that:

6.47.5.2 Specifying Registers for Local Variables

Do not use type qualifiers such as const and volatile, as the outcome may be
contrary to expectations. In particular, when the const qualifier is used, the
compiler may substitute the variable with its initializer in asm statements,
which may cause the corresponding operand to appear in a different register. 


6.47.5.1 Defining Global Register Variables

Do not use type qualifiers such as const and volatile, as the outcome may be
contrary to expectations. In particular, using the volatile qualifier does not
fully prevent the compiler from optimizing accesses to the register. 

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

* Re: AW: C and C++ parser performing optimizations
  2020-08-04  7:13     ` Alexander Monakov
@ 2020-08-04 15:33       ` Segher Boessenkool
  0 siblings, 0 replies; 7+ messages in thread
From: Segher Boessenkool @ 2020-08-04 15:33 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: Stefan Franke, gcc-help

Hi!

On Tue, Aug 04, 2020 at 10:13:12AM +0300, Alexander Monakov via Gcc-help wrote:
> On Tue, 4 Aug 2020, Stefan Franke wrote:
> > Here is an example where gcc creates wrong code:
> > 
> > test.c:
> > int foo() {
> >   const char * const txt = "hello";
> >   register const char * const p asm("ecx") = txt;
> >   register int dx asm("edx");
> >   asm(" call _faa" :"=r" (dx) :"rf" (p));

  return dx; // and then it is valid code, even

> > }
> > 
> > gcc -O1 -S test.c -fdump-tree-original
> > 
> > The variable p gets replaced and the asm input parameters are wrong:
> > 
> > __asm__(" call _faa":"=r" dx:"rf" (const char * const) "hello");
> 
> I think it's a separate issue: even if the frontend wouldn't do such
> propagation, I'm pretty sure later passes would.

I don't think so?  That would be separate extra bugs.

> const and volatile
> qualifiers just do not work as expected with register variables, and
> since recently the documentation mentions that:
> 
> 6.47.5.2 Specifying Registers for Local Variables
> 
> Do not use type qualifiers such as const and volatile, as the outcome may be
> contrary to expectations. In particular, when the const qualifier is used, the
> compiler may substitute the variable with its initializer in asm statements,
> which may cause the corresponding operand to appear in a different register. 

Either we should warn for this, or we should fix this to behave as
expected for "const".  "volatile" otoh, well, what would that even mean
here, maytbe we should just error for that.

(Global register vars are different still...  together with -ffixed-*
that should be pretty easy, but *without* -ffixed-*, global register
vars aren't very well defined anyway).


Segher

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

* Re: C and C++ parser performing optimizations
  2020-08-04  6:44   ` AW: " Stefan Franke
  2020-08-04  7:13     ` Alexander Monakov
@ 2020-08-24 19:06     ` Gunther Nikl
  2020-08-24 19:54       ` AW: " Stefan Franke
  1 sibling, 1 reply; 7+ messages in thread
From: Gunther Nikl @ 2020-08-24 19:06 UTC (permalink / raw)
  To: Stefan Franke, gcc-help

stefan@franke.ms (Stefan Franke) wrote:

> > Von: Alexander Monakov <amonakov@ispras.ru>
> > Betreff: Re: C and C++ parser performing optimizations
> > 
> > On Sun, 2 Aug 2020, Stefan Franke wrote:
> > 
> > > So the parser performs unwanted and uncontrollable optimizations,
> > > which I consider bogus.
> > 
> > On occasion they are also incorrect.
> > 
> > My (possibly wrong or incomplete) understanding is that GCC does
> > not have internal separation of mandatory simplifications that need
> > to be done in
> the
> > frontend (like constant folding in the context of integer constant
> > expressions) vs. optional simplifications (optimizing
> > substitutions). So it just does both at the same time.
> > 
> > Alexander
> 
> Here is an example where gcc creates wrong code:
> 
> test.c:
> int foo() {
>   const char * const txt = "hello";
>   register const char * const p asm("ecx") = txt;
>   register int dx asm("edx");
>   asm(" call _faa" :"=r" (dx) :"rf" (p));
> }

Let me guess: this is about the m68k-amigaos LP macros? Can you show an
example for that target?

Gunther

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

* AW: C and C++ parser performing optimizations
  2020-08-24 19:06     ` Gunther Nikl
@ 2020-08-24 19:54       ` Stefan Franke
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Franke @ 2020-08-24 19:54 UTC (permalink / raw)
  To: gcc-help



> -----Ursprüngliche Nachricht-----
> Von: Gunther Nikl <gnikl@justmail.de>
> Gesendet: Montag, 24. August 2020 21:06
> An: Stefan Franke <stefan@franke.ms>; gcc-help@gcc.gnu.org
> Betreff: Re: C and C++ parser performing optimizations
> 
> stefan@franke.ms (Stefan Franke) wrote:
> 
> > > Von: Alexander Monakov <amonakov@ispras.ru>
> > > Betreff: Re: C and C++ parser performing optimizations
> > >
> > > On Sun, 2 Aug 2020, Stefan Franke wrote:
> > >
> > > > So the parser performs unwanted and uncontrollable optimizations,
> > > > which I consider bogus.
> > >
> > > On occasion they are also incorrect.
> > >
> > > My (possibly wrong or incomplete) understanding is that GCC does not
> > > have internal separation of mandatory simplifications that need to
> > > be done in
> > the
> > > frontend (like constant folding in the context of integer constant
> > > expressions) vs. optional simplifications (optimizing
> > > substitutions). So it just does both at the same time.
> > >
> > > Alexander
> >
> > Here is an example where gcc creates wrong code:
> >
> > test.c:
> > int foo() {
> >   const char * const txt = "hello";
> >   register const char * const p asm("ecx") = txt;
> >   register int dx asm("edx");
> >   asm(" call _faa" :"=r" (dx) :"rf" (p)); }
> 
> Let me guess: this is about the m68k-amigaos LP macros? Can you show an
> example for that target?
> 
> Gunther

Your guess is correct, the m68k-amigaos LP macros are affected. And you may
choose any bogus defined lib function. E.g.

#define InitStruct(___initTable, ___memory, ___size) \
      LP3NR(0x4e, InitStruct , const APTR, ___initTable, a1, APTR,
___memory, a2, ULONG, ___size, d0,\
      , EXEC_BASE_NAME)

What's wrong here? It yields
	register const APTR  __initTable asm("a1");
which is
	register void * const __initTable asm("a1");
(some might expect
	register const void * __initTable asm("a1");
but that's not the case).

So __initTable is const which triggers the bogus optimization in the parsers
(c and c++ don't share the code - they reimplemented that bug).

I patched the parsers to omit that optimization. There are many other passes
which handle const propagation without dropping the register information.

One could fix the headers to circumvent that bug, all 'const <typedef>'
locations need a fix, but that's another story...

Stefan



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

end of thread, other threads:[~2020-08-24 19:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-02 18:15 C and C++ parser performing optimizations Stefan Franke
2020-08-02 18:40 ` Alexander Monakov
2020-08-04  6:44   ` AW: " Stefan Franke
2020-08-04  7:13     ` Alexander Monakov
2020-08-04 15:33       ` Segher Boessenkool
2020-08-24 19:06     ` Gunther Nikl
2020-08-24 19:54       ` AW: " Stefan Franke

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