Bug 108386

Summary: Missed optimization with -fno-omit-frame-pointer on x86
Product: gcc Reporter: Jakub Jelinek <jakub>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: enhancement CC: fw, sjames
Priority: P3 Keywords: missed-optimization
Version: 13.0   
Target Milestone: ---   
Host: Target: x86_64-linux, i686-linux
Build: Known to work:
Known to fail: Last reconfirmed: 2025-10-30 00:00:00

Description Jakub Jelinek 2023-01-12 17:25:36 UTC
void bar (char *);

void
foo (void)
{
  char buf[384];
  bar (&buf[0]);
  bar (&buf[16]);
  bar (&buf[127]);
  bar (&buf[128]);
  bar (&buf[256]);
  bar (&buf[380]);
  bar (&buf[384]);
}

compiles with -O2 -fno-omit-frame-pointer to:
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $384, %rsp
        leaq    -384(%rbp), %rdi
        call    bar
        leaq    -368(%rbp), %rdi
        call    bar
        leaq    -257(%rbp), %rdi
        call    bar
        leaq    -256(%rbp), %rdi
        call    bar
        leaq    -128(%rbp), %rdi
        call    bar
        leaq    -4(%rbp), %rdi
        call    bar
        movq    %rbp, %rdi
        call    bar
        leave
        ret
but this is unnecessarily large.  As frame pointer is here only because user asked for it, the compiler knows there is always constant difference between the stack pointer and frame pointer and perhaps in machine reorg could interchange those cases which would be smaller and not slower.
For these particular leaq/movq instructions, movq %r{sp,bp}, %rdi is 3 bytes,
leaq SIMM8(%rbp), %rdi 4 bytes, leaq SIMM8(%rsp), %rdi 5 bytes, leaq SIMM32(%rbp), %rdi 7 bytes and leaq SIMM32(%rsp), %rdi 8 bytes.
So at least from code size POV, movq %rsp, %rdi is smaller than any %rbp based leaq,
and similarly leaq SIMM8(%rbp), %rdi 2 bytes smaller than leaq SIMM32(%rbp), %rdi.
So, the mov would be a win always, and for frame sizes of more than 128 bytes %rsp up to 127 offset too.
Though, I think the aliasing code hardcodes frame pointer knowledge and ditto I think
the unwinder would be quite upset if we did such changes early.
Comment 1 Drea Pinski 2025-10-30 06:01:12 UTC
.
Comment 2 Drea Pinski 2025-10-30 06:07:02 UTC
https://hachyderm.io/@meowray/115461439076733718 mentions this issue too when it comes to looking into if SFRAME or no-omit-frame-pointer .