<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Potential code size reduction optimization: reusing function tails"
   href="https://bugs.llvm.org/show_bug.cgi?id=33763">33763</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Potential code size reduction optimization: reusing function tails
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jpakkane@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Suppose you have code that looks like this:

int func1();
int func2();
int func3();
int func4();

int func5() {
  int i = 0;
  i+=func2();
  i+=func3();
  i+=func4();
  return i+func1();
}

int func6() {
  int i=1;
  i+=func3();
  i+=func3();
  i+=func4();
  return i+func1();
}

It gets compiled to the following assembly when using -Os:

func5():
        pushq   %rbx
        call    func2()
        movl    %eax, %ebx
        call    func3()
        addl    %eax, %ebx
        call    func4()
        addl    %eax, %ebx
        call    func1()
        addl    %ebx, %eax
        popq    %rbx
        ret
func6():
        pushq   %rbx
        call    func3()
        leal    1(%rax), %ebx
        call    func3()
        addl    %eax, %ebx
        call    func4()
        addl    %eax, %ebx
        call    func1()
        addl    %ebx, %eax
        popq    %rbx
        ret


However the ends of the two functions are identical. This could be compiled
into the following which is functionally the same but takes less space:

func5():
        pushq   %rbx
        call    func2()
        movl    %eax, %ebx
common_tail:
        call    func3()
        addl    %eax, %ebx
        call    func4()
        addl    %eax, %ebx
        call    func1()
        addl    %ebx, %eax
        popq    %rbx
        ret
func6():
        pushq   %rbx
        call    func3()
        leal    1(%rax), %ebx
        jmp common_tail

Testing with compiler explorer says that neither GCC, Clang, MSVC nor ICC do
this optimization but some embedded compilers do.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>