[llvm-commits] [llvm] r94804 - in /llvm/trunk: lib/Target/X86/X86ISelLowering.cpp test/CodeGen/X86/tailcall2.ll

Evan Cheng evan.cheng at apple.com
Thu Jan 28 22:45:59 PST 2010


Author: evancheng
Date: Fri Jan 29 00:45:59 2010
New Revision: 94804

URL: http://llvm.org/viewvc/llvm-project?rev=94804&view=rev
Log:
Catch more trivial tail call opportunities: no inputs and output types match.

Modified:
    llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
    llvm/trunk/test/CodeGen/X86/tailcall2.ll

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

==============================================================================
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Fri Jan 29 00:45:59 2010
@@ -2246,27 +2246,35 @@
                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
                                     const SmallVectorImpl<ISD::InputArg> &Ins,
                                                      SelectionDAG& DAG) const {
-  // 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;
 
+  // If -tailcallopt is specified, make fastcc functions tail-callable.
+  const Function *CallerF = DAG.getMachineFunction().getFunction();
+  if (PerformTailCallOpt &&
+      CalleeCC == CallingConv::Fast &&
+      CallerF->getCallingConv() == CalleeCC)
+    return true;
+
   // Look for obvious safe cases to perform tail call optimization.
-  // For now, only consider callees which take no arguments and no return
-  // values.
+  // For now, only consider callees which take no arguments.
   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();
+  // If the caller does not return a value, then this is obviously safe.
+  // This is one case where it's safe to perform this optimization even
+  // if the return types do not match.
+  const Type *CallerRetTy = CallerF->getReturnType();
+  if (CallerRetTy->isVoidTy())
+    return true;
 
-  return false;
+  // If the return types match, then it's safe.
+  GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
+  if (!G) return false;  // FIXME: common external symbols?
+  Function *CalleeF = cast<Function>(G->getGlobal());
+  const Type *CalleeRetTy = CalleeF->getReturnType();
+  return CallerRetTy == CalleeRetTy;
 }
 
 FastISel *

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

==============================================================================
--- llvm/trunk/test/CodeGen/X86/tailcall2.ll (original)
+++ llvm/trunk/test/CodeGen/X86/tailcall2.ll Fri Jan 29 00:45:59 2010
@@ -1,12 +1,32 @@
 ; 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 {
+define void @t1(i32 %x) nounwind ssp {
 entry:
-; CHECK: bar:
+; CHECK: t1:
 ; CHECK: jmp {{_?}}foo
   tail call void @foo() nounwind
   ret void
 }
 
 declare void @foo()
+
+define void @t2() nounwind ssp {
+entry:
+; CHECK: t2:
+; CHECK: jmp {{_?}}foo2
+  %0 = tail call i32 @foo2() nounwind
+  ret void
+}
+
+declare i32 @foo2()
+
+define void @t3() nounwind ssp {
+entry:
+; CHECK: t3:
+; CHECK: jmp {{_?}}foo3
+  %0 = tail call i32 @foo3() nounwind
+  ret void
+}
+
+declare i32 @foo3()





More information about the llvm-commits mailing list