public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/47980] New: Inefficient code for local const char arrays
@ 2011-03-03 21:40 rafael.espindola at gmail dot com
  2011-03-03 21:47 ` [Bug c/47980] " ktietz at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: rafael.espindola at gmail dot com @ 2011-03-03 21:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

           Summary: Inefficient code for local const char arrays
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: rafael.espindola@gmail.com


gcc will compile

--------------------
void f(const char *p);

void g(void) {
  const char foo[] = "aoeuaoeuaeouaeouaoeuaoeaoxbxod";
  f(foo);
}
----------------------

to

        .cfi_startproc
        subq    $40, %rsp
        .cfi_def_cfa_offset 48
        movl    $.LC0, %esi
        movl    $31, %ecx
        leaq    1(%rsp), %rdi
        rep movsb
        leaq    1(%rsp), %rdi
        call    f
        addq    $40, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc

No idea why it wants to copy the string before calling f.


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

* [Bug c/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
@ 2011-03-03 21:47 ` ktietz at gcc dot gnu.org
  2011-03-03 21:50 ` rafael.espindola at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: ktietz at gcc dot gnu.org @ 2011-03-03 21:47 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

Kai Tietz <ktietz at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |ktietz at gcc dot gnu.org
         Resolution|                            |INVALID

--- Comment #1 from Kai Tietz <ktietz at gcc dot gnu.org> 2011-03-03 21:46:48 UTC ---
This bug is invalid. The compiler did what you told him here. If you have the
opinion for 'char s[] = "text"' would be a pointer to the constant, you are
wrong.


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

* [Bug c/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
  2011-03-03 21:47 ` [Bug c/47980] " ktietz at gcc dot gnu.org
@ 2011-03-03 21:50 ` rafael.espindola at gmail dot com
  2011-03-03 21:52 ` ktietz at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: rafael.espindola at gmail dot com @ 2011-03-03 21:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

--- Comment #2 from Rafael Avila de Espindola <rafael.espindola at gmail dot com> 2011-03-03 21:50:07 UTC ---
I agree that the code is correct. The bug is because of a missing optimization,
not about wrong behavior.

The only use of foo is passing it function expecting a const pointer. Gcc could
remove the copy and just pass a pointer to the global it created to hold the
string.

p.s.: how do I reopen the bug?


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

* [Bug c/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
  2011-03-03 21:47 ` [Bug c/47980] " ktietz at gcc dot gnu.org
  2011-03-03 21:50 ` rafael.espindola at gmail dot com
@ 2011-03-03 21:52 ` ktietz at gcc dot gnu.org
  2011-03-03 22:00 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: ktietz at gcc dot gnu.org @ 2011-03-03 21:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

Kai Tietz <ktietz at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
   Last reconfirmed|                            |2011.03.03 21:52:14
         Resolution|INVALID                     |
     Ever Confirmed|0                           |1

--- Comment #3 from Kai Tietz <ktietz at gcc dot gnu.org> 2011-03-03 21:52:14 UTC ---
As missed optimization it is valid. so reopened


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

* [Bug middle-end/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
                   ` (3 preceding siblings ...)
  2011-03-03 22:00 ` jakub at gcc dot gnu.org
@ 2011-03-03 22:00 ` pinskia at gcc dot gnu.org
  2011-03-03 23:07 ` pinskia at gcc dot gnu.org
  2011-03-04 16:26 ` hubicka at ucw dot cz
  6 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-03-03 22:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
          Component|c                           |middle-end
         Resolution|                            |INVALID

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-03-03 22:00:36 UTC ---
Actually the copying is correct.
Take:
void f(const char *p);

void g(int t) {
  const char foo[] = "aoeuaoeuaeouaeouaoeuaoeaoxbxod";
  f(foo, t);
  if (!t)
    g(1);
}

const char *a;

void f(const char *p, int t)
{
  if (!t)
    a = p;
  else if (a == p)
    abort ();
}

void main(void)
{
  g(0);
}

This program should not abort with your "optimization", it will.


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

* [Bug c/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
                   ` (2 preceding siblings ...)
  2011-03-03 21:52 ` ktietz at gcc dot gnu.org
@ 2011-03-03 22:00 ` jakub at gcc dot gnu.org
  2011-03-04 16:26   ` Jan Hubicka
  2011-03-03 22:00 ` [Bug middle-end/47980] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-03-03 22:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-03-03 21:59:50 UTC ---
I believe f could do:
  assert (arg != "aoeuaoeuaeouaeouaoeuaoeaoxbxod");
which would then fail with the proposed optimization.  It is unspecified if
two string literals with the same content are distinct objects, but foo must be
a distinct object (ok, with static const char foo[] =
"aoeuaoeuaeouaeouaoeuaoeaoxbxod"; and -fmerge-all-constants which ignores some
C requirements it doesn't have to).


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

* [Bug middle-end/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
                   ` (4 preceding siblings ...)
  2011-03-03 22:00 ` [Bug middle-end/47980] " pinskia at gcc dot gnu.org
@ 2011-03-03 23:07 ` pinskia at gcc dot gnu.org
  2011-03-04 16:26 ` hubicka at ucw dot cz
  6 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-03-03 23:07 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-03-03 23:07:01 UTC ---
In fact my testcase in comment #5 is an exact duplicate of bug 38615.

*** This bug has been marked as a duplicate of bug 38615 ***


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

* Re: [Bug c/47980] Inefficient code for local const char arrays
  2011-03-03 22:00 ` jakub at gcc dot gnu.org
@ 2011-03-04 16:26   ` Jan Hubicka
  0 siblings, 0 replies; 9+ messages in thread
From: Jan Hubicka @ 2011-03-04 16:26 UTC (permalink / raw)
  To: jakub at gcc dot gnu.org; +Cc: gcc-bugs

> I believe f could do:
>   assert (arg != "aoeuaoeuaeouaeouaoeuaoeaoxbxod");
> which would then fail with the proposed optimization.  It is unspecified if
> two string literals with the same content are distinct objects, but foo must be
> a distinct object (ok, with static const char foo[] =
> "aoeuaoeuaeouaeouaoeuaoeaoxbxod"; and -fmerge-all-constants which ignores some
> C requirements it doesn't have to).
Hmm, I was not aware of this.

I've seen quite few cases of real world code where such local arrays was used only
for direct references.  It is valid to do the transform in all cases when address
is not escaping and it is not used for inequality tests.  We do not track the second,
but we could.

It is however bit tricky to do the actual promotion of automatic scalar var to
static as gimplification lower initializers very early and thus we will have to
reconstruct it from gimple code.

Worthwhile optimization however I think.

Honza


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

* [Bug middle-end/47980] Inefficient code for local const char arrays
  2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
                   ` (5 preceding siblings ...)
  2011-03-03 23:07 ` pinskia at gcc dot gnu.org
@ 2011-03-04 16:26 ` hubicka at ucw dot cz
  6 siblings, 0 replies; 9+ messages in thread
From: hubicka at ucw dot cz @ 2011-03-04 16:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47980

--- Comment #7 from Jan Hubicka <hubicka at ucw dot cz> 2011-03-04 16:26:06 UTC ---
> I believe f could do:
>   assert (arg != "aoeuaoeuaeouaeouaoeuaoeaoxbxod");
> which would then fail with the proposed optimization.  It is unspecified if
> two string literals with the same content are distinct objects, but foo must be
> a distinct object (ok, with static const char foo[] =
> "aoeuaoeuaeouaeouaoeuaoeaoxbxod"; and -fmerge-all-constants which ignores some
> C requirements it doesn't have to).
Hmm, I was not aware of this.

I've seen quite few cases of real world code where such local arrays was used
only
for direct references.  It is valid to do the transform in all cases when
address
is not escaping and it is not used for inequality tests.  We do not track the
second,
but we could.

It is however bit tricky to do the actual promotion of automatic scalar var to
static as gimplification lower initializers very early and thus we will have to
reconstruct it from gimple code.

Worthwhile optimization however I think.

Honza


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

end of thread, other threads:[~2011-03-04 16:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-03 21:40 [Bug c/47980] New: Inefficient code for local const char arrays rafael.espindola at gmail dot com
2011-03-03 21:47 ` [Bug c/47980] " ktietz at gcc dot gnu.org
2011-03-03 21:50 ` rafael.espindola at gmail dot com
2011-03-03 21:52 ` ktietz at gcc dot gnu.org
2011-03-03 22:00 ` jakub at gcc dot gnu.org
2011-03-04 16:26   ` Jan Hubicka
2011-03-03 22:00 ` [Bug middle-end/47980] " pinskia at gcc dot gnu.org
2011-03-03 23:07 ` pinskia at gcc dot gnu.org
2011-03-04 16:26 ` hubicka at ucw dot cz

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