From 75e25f6812da5e46a2a8f8dbbeae0f0f3df832d9 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Wed, 7 Sep 2016 17:52:14 +0000 Subject: 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/test/CodeGen/X86/conditional-tailcall.ll | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 llvm/test/CodeGen/X86/conditional-tailcall.ll (limited to 'llvm/test/CodeGen/X86/conditional-tailcall.ll') diff --git a/llvm/test/CodeGen/X86/conditional-tailcall.ll b/llvm/test/CodeGen/X86/conditional-tailcall.ll new file mode 100644 index 00000000000..00e8f9f52e9 --- /dev/null +++ b/llvm/test/CodeGen/X86/conditional-tailcall.ll @@ -0,0 +1,24 @@ +; RUN: llc < %s -march=x86 -show-mc-encoding | FileCheck %s + +declare void @foo() +declare void @bar() + +define void @f(i32 %x, i32 %y) optsize { +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 +} + +; CHECK-LABEL: f: +; CHECK: cmp +; CHECK: jne bar +; Check that the asm doesn't just look good, but uses the correct encoding. +; CHECK: encoding: [0x75,A] + +; CHECK: jmp foo -- cgit v1.2.3