X86: Fold tail calls into conditional branches where possible (PR26302)
authorHans Wennborg <hans@hanshq.net>
Wed, 7 Sep 2016 17:52:14 +0000 (17:52 +0000)
committerHans Wennborg <hans@hanshq.net>
Wed, 7 Sep 2016 17:52:14 +0000 (17:52 +0000)
commit75e25f6812da5e46a2a8f8dbbeae0f0f3df832d9
tree4aae7345b3f2ee264c337342b41f7d2e3d4c77fc
parent5ad1cbeecb07c02d5c9e9f27410815556d488452
X86: Fold tail calls into conditional branches where possible (PR26302)

When branching to a block that immediately tail calls, it is possible to fold
the call directly into the branch if the call is direct and there is no stack
adjustment, saving one byte.

Example:

  define void @f(i32 %x, i32 %y) {
  entry:
    %p = icmp eq i32 %x, %y
    br i1 %p, label %bb1, label %bb2
  bb1:
    tail call void @foo()
    ret void
  bb2:
    tail call void @bar()
    ret void
  }

before:

  f:
          movl    4(%esp), %eax
          cmpl    8(%esp), %eax
          jne     .LBB0_2
          jmp     foo
  .LBB0_2:
          jmp     bar

after:

  f:
          movl    4(%esp), %eax
          cmpl    8(%esp), %eax
          jne     bar
  .LBB0_1:
          jmp     foo

I don't expect any significant size savings from this (on a Clang bootstrap I
saw 288 bytes), but it does make the code a little tighter.

This patch only does 32-bit, but 64-bit would work similarly.

Differential Revision: https://reviews.llvm.org/D24108

llvm-svn: 280832
llvm/include/llvm/Target/TargetInstrInfo.h
llvm/lib/CodeGen/BranchFolding.cpp
llvm/lib/Target/X86/X86ExpandPseudo.cpp
llvm/lib/Target/X86/X86InstrControl.td
llvm/lib/Target/X86/X86InstrInfo.cpp
llvm/lib/Target/X86/X86InstrInfo.h
llvm/lib/Target/X86/X86MCInstLower.cpp
llvm/test/CodeGen/X86/conditional-tailcall.ll [new file with mode: 0644]