public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110567] New: GCC fails using a register to initialize multiple variables with a constant
@ 2023-07-05 22:09 vincent.riviere at freesbee dot fr
  2023-07-05 22:20 ` [Bug target/110567] " pinskia at gcc dot gnu.org
  2023-07-05 22:54 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: vincent.riviere at freesbee dot fr @ 2023-07-05 22:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110567

            Bug ID: 110567
           Summary: GCC fails using a register to initialize multiple
                    variables with a constant
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent.riviere at freesbee dot fr
  Target Milestone: ---
            Target: m68k-elf

This is about a missed optimization for an obvious case.
GCC fails to use a register to initialize several global variables with the
same constant value.

$ cat foo.c
extern long init;
extern long g1, g2, g3, g4, g5;

void f(void)
{
    long val = 12345678;

    g1 = val;
    g2 = val;
    g3 = val;
    g4 = val;
    g5 = val;
}

$ m68k-elf-gcc -S foo.c -o - -Os
#NO_APP
        .file   "foo.c"
        .text
        .align  2
        .globl  f
        .type   f, @function
f:
        move.l #12345678,g1
        move.l #12345678,g2
        move.l #12345678,g3
        move.l #12345678,g4
        move.l #12345678,g5
        rts
        .size   f, .-f
        .ident  "GCC: (GNU) 13.1.0"

We can see that 12345678 is used many times. This is a waste of space and time.

On the other hand, if I replace 12345678 by "init" (an external global
variable) then GCC produces the expected optimized output:

$ m68k-elf-gcc -S foo.c -o - -Os
#NO_APP
        .file   "foo.c"
        .text
        .align  2
        .globl  f
        .type   f, @function
f:
        move.l init,%d0
        move.l %d0,g1
        move.l %d0,g2
        move.l %d0,g3
        move.l %d0,g4
        move.l %d0,g5
        rts
        .size   f, .-f
        .ident  "GCC: (GNU) 13.1.0"

GCC should be able to optimize a constant initializer, just as it does with a
variable initializer.

I can reproduce the same wrong behaviour with GCC 11.4.0 for x86_64-pc-cygwin
host with -m32:

$ gcc -m32 -S foo.c -o - -Os
        .file   "foo.c"
        .text
        .globl  _f
        .def    _f;     .scl    2;      .type   32;     .endef
_f:
        movl    $12345678, _g1
        movl    $12345678, _g2
        movl    $12345678, _g3
        movl    $12345678, _g4
        movl    $12345678, _g5
        ret
        .ident  "GCC: (GNU) 11.4.0"

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

* [Bug target/110567] GCC fails using a register to initialize multiple variables with a constant
  2023-07-05 22:09 [Bug c/110567] New: GCC fails using a register to initialize multiple variables with a constant vincent.riviere at freesbee dot fr
@ 2023-07-05 22:20 ` pinskia at gcc dot gnu.org
  2023-07-05 22:54 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-05 22:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110567

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |target

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a target issue where the cost model for -Os is still saying constants
are "free".

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

* [Bug target/110567] GCC fails using a register to initialize multiple variables with a constant
  2023-07-05 22:09 [Bug c/110567] New: GCC fails using a register to initialize multiple variables with a constant vincent.riviere at freesbee dot fr
  2023-07-05 22:20 ` [Bug target/110567] " pinskia at gcc dot gnu.org
@ 2023-07-05 22:54 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-05 22:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110567

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=44073

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
x86 bug is PR 44073 .

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

end of thread, other threads:[~2023-07-05 22:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-05 22:09 [Bug c/110567] New: GCC fails using a register to initialize multiple variables with a constant vincent.riviere at freesbee dot fr
2023-07-05 22:20 ` [Bug target/110567] " pinskia at gcc dot gnu.org
2023-07-05 22:54 ` pinskia at gcc dot gnu.org

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