https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118678
Bug ID: 118678 Summary: Dubious optimization when compiling with -fpie -Os Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: joshudson at gmail dot com Target Milestone: --- The following code fragment demonstrates the problem: extern int execve2(const char *name, const char**args, const char **environ); int function() { const char *args[] = { "/bin/echo", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", 0 }; return -execve2(args[0], args, 0); } When compiling with -S -Os it finds and optimization with the weight of the code being eight bytes per array member: leaq .LC11(%rip), %rsi movl $22, %ecx xorl %edx, %edx leaq 8(%rsp), %rdi rep movsl It could have been better by saying leaq .LC11(%rip), %rsi movl $11, %ecx xorl %edx, %edx leaq 8(%rsp), %rdi rep movsq However the bug that surprised me is it continues to generate this movs instruction when compiling with -S -Os -fpie ; the optimization is no longer nice because this now requires *eleven* relocations with the weight of the code being 16 bytes per array member; the simple solution generated by -S -O3 -mno-sse only uses 12 bytes per array member. I don't really know how the guts of the optimizer work; but I haven't been able to get it to generate this anywhere other than an array of constants, and it often misses it; so I'm not too sure how hard this is to fix.