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"