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

            Bug ID: 120132
           Summary: JMP to target function from the middle of the current
                    if the results layout are compatible
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the following example:


struct A {
    int i;
};

const A& f();
A g();

int test1(bool cond) {
    const A& a = cond ? f() : g();
    return a.i;
}


GCC-15 produces the following assembly:

test1(bool):
  sub rsp, 8
  test dil, dil
  je .L2
  call f()
  mov eax, DWORD PTR [rax]
  add rsp, 8
  ret
.L2:
  call g()
  add rsp, 8
  ret


However, a more optimal assembly would be:

test1(bool):
  test dil, dil
  je g()
  sub rsp, 8
  call f()
  mov eax, DWORD PTR [rax]
  add rsp, 8
  ret

There's now less instructions in total and g() is now faster to execute if cond
== false.


Godbolt playground: https://godbolt.org/z/64xsq738s

Reply via email to