[llvm-commits] [llvm] r94664 - in /llvm/trunk: lib/Target/X86/X86ISelLowering.cpp lib/Target/X86/X86ISelLowering.h test/CodeGen/X86/pic.ll test/CodeGen/X86/tailcall1.ll test/CodeGen/X86/tailcall2.ll

Evan Cheng evan.cheng at apple.com
Tue Jan 26 22:25:16 PST 2010


Author: evancheng
Date: Wed Jan 27 00:25:16 2010
New Revision: 94664

URL: http://llvm.org/viewvc/llvm-project?rev=94664&view=rev
Log:
Perform trivial tail call optimization for callees with "C" ABI. These are done
even when -tailcallopt is not specified and it does not require changing ABI.
First case is the most trivial one. Perform tail call optimization when both
the caller and callee do not return values and when the callee does not take
any input arguments.

Added:
    llvm/trunk/test/CodeGen/X86/tailcall2.ll
Modified:
    llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
    llvm/trunk/lib/Target/X86/X86ISelLowering.h
    llvm/trunk/test/CodeGen/X86/pic.ll
    llvm/trunk/test/CodeGen/X86/tailcall1.ll

Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=94664&r1=94663&r2=94664&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Wed Jan 27 00:25:16 2010
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "x86-isel"
 #include "X86.h"
 #include "X86InstrBuilder.h"
 #include "X86ISelLowering.h"
@@ -39,6 +40,7 @@
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/VectorExtras.h"
 #include "llvm/Support/CommandLine.h"
@@ -48,6 +50,8 @@
 #include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
+STATISTIC(NumTailCalls, "Number of tail calls");
+
 static cl::opt<bool>
 DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
 
@@ -1788,7 +1792,7 @@
   if (isTailCall)
     // Check if it's really possible to do a tail call.
     isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
-                                                   Ins, DAG);
+                                                   Outs, Ins, DAG);
 
   assert(!(isVarArg && CallConv == CallingConv::Fast) &&
          "Var args not supported with calling convention fastcc");
@@ -1806,6 +1810,8 @@
 
   int FPDiff = 0;
   if (isTailCall) {
+    ++NumTailCalls;
+
     // Lower arguments at fp - stackoffset + fpdiff.
     unsigned NumBytesCallerPushed =
       MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
@@ -2237,11 +2243,29 @@
 X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
                                                      CallingConv::ID CalleeCC,
                                                      bool isVarArg,
-                                      const SmallVectorImpl<ISD::InputArg> &Ins,
+                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
+                                    const SmallVectorImpl<ISD::InputArg> &Ins,
                                                      SelectionDAG& DAG) const {
-  if (CalleeCC == CallingConv::Fast &&
-      DAG.getMachineFunction().getFunction()->getCallingConv() == CalleeCC)
+  // If -tailcallopt is specified, make fastcc functions tail-callable.
+  const Function *F = DAG.getMachineFunction().getFunction();
+  if (PerformTailCallOpt &&
+      CalleeCC == CallingConv::Fast && F->getCallingConv() == CalleeCC)
     return true;
+
+  if (CalleeCC != CallingConv::Fast &&
+      CalleeCC != CallingConv::C)
+    return false;
+
+  // Look for obvious safe cases to perform tail call optimization.
+  // For now, only consider callees which take no arguments and no return
+  // values.
+  if (!Outs.empty())
+    return false;
+
+  if (Ins.empty())
+    // If the caller does not return a value, then this is obviously safe.
+    return F->getReturnType()->isVoidTy();
+
   return false;
 }
 

Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.h?rev=94664&r1=94663&r2=94664&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86ISelLowering.h (original)
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.h Wed Jan 27 00:25:16 2010
@@ -630,7 +630,8 @@
     bool IsEligibleForTailCallOptimization(SDValue Callee,
                                            CallingConv::ID CalleeCC,
                                            bool isVarArg,
-                                      const SmallVectorImpl<ISD::InputArg> &Ins,
+                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
+                                    const SmallVectorImpl<ISD::InputArg> &Ins,
                                            SelectionDAG& DAG) const;
     bool IsCalleePop(bool isVarArg, CallingConv::ID CallConv);
     SDValue EmitTailCallLoadRetAddr(SelectionDAG &DAG, SDValue &OutRetAddr,

Modified: llvm/trunk/test/CodeGen/X86/pic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/pic.ll?rev=94664&r1=94663&r2=94664&view=diff

==============================================================================
--- llvm/trunk/test/CodeGen/X86/pic.ll (original)
+++ llvm/trunk/test/CodeGen/X86/pic.ll Wed Jan 27 00:25:16 2010
@@ -190,7 +190,7 @@
 ; LINUX: .L8$pb:
 ; LINUX:   addl	$_GLOBAL_OFFSET_TABLE_+(.Lpicbaseref8-.L8$pb),
 ; LINUX:   addl	.LJTI8_0 at GOTOFF(
-; LINUX:   jmpl	*%ecx
+; LINUX:   jmpl	*
 
 ; LINUX: .LJTI8_0:
 ; LINUX:   .long	 .LBB8_2 at GOTOFF

Modified: llvm/trunk/test/CodeGen/X86/tailcall1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/tailcall1.ll?rev=94664&r1=94663&r2=94664&view=diff

==============================================================================
--- llvm/trunk/test/CodeGen/X86/tailcall1.ll (original)
+++ llvm/trunk/test/CodeGen/X86/tailcall1.ll Wed Jan 27 00:25:16 2010
@@ -4,8 +4,8 @@
 
 define fastcc i32 @tailcaller(i32 %in1, i32 %in2) nounwind {
 entry:
-	%tmp11 = tail call fastcc i32 @tailcallee(i32 %in1, i32 %in2, i32 %in1, i32 %in2)
-	ret i32 %tmp11
+  %tmp11 = tail call fastcc i32 @tailcallee(i32 %in1, i32 %in2, i32 %in1, i32 %in2)
+  ret i32 %tmp11
 }
 
 declare fastcc i8* @alias_callee()

Added: llvm/trunk/test/CodeGen/X86/tailcall2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/tailcall2.ll?rev=94664&view=auto

==============================================================================
--- llvm/trunk/test/CodeGen/X86/tailcall2.ll (added)
+++ llvm/trunk/test/CodeGen/X86/tailcall2.ll Wed Jan 27 00:25:16 2010
@@ -0,0 +1,12 @@
+; RUN: llc < %s -march=x86    -asm-verbose=false | FileCheck %s
+; RUN: llc < %s -march=x86-64 -asm-verbose=false | FileCheck %s
+
+define void @bar(i32 %x) nounwind ssp {
+entry:
+; CHECK: bar:
+; CHECK: jmp _foo
+  tail call void @foo() nounwind
+  ret void
+}
+
+declare void @foo()





More information about the llvm-commits mailing list