From bd374da130b9f1835e666088bb9c241e2bfce74b Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 8 Feb 2010 20:34:14 +0000 Subject: [PATCH] In guaranteed tailcall mode, don't decline the tailcall optimization for blocks ending in "unreachable". llvm-svn: 95565 --- .../SelectionDAG/SelectionDAGBuilder.cpp | 17 ++++++++++------- llvm/test/CodeGen/X86/tailcall1.ll | 5 ++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index cf342c793b9d..93ae043dbc14 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -4205,13 +4205,16 @@ isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr, const ReturnInst *Ret = dyn_cast(Term); const Function *F = ExitBB->getParent(); - // The block must end in a return statement. - // FIXME: Disallow tailcall if the block ends in an unreachable for now. - // The way tailcall optimization is currently implemented means it will - // add an epilogue followed by a jump. That is not profitable. Also, if - // the callee is a special function (e.g. longjmp on x86), it can end up - // causing miscompilation that has not been fully understood. - if (!Ret) return false; + // The block must end in a return statement or unreachable. + // + // FIXME: Decline tailcall if it's not guaranteed and if the block ends in + // an unreachable, for now. The way tailcall optimization is currently + // implemented means it will add an epilogue followed by a jump. That is + // not profitable. Also, if the callee is a special function (e.g. + // longjmp on x86), it can end up causing miscompilation that has not + // been fully understood. + if (!Ret && + (!GuaranteedTailCallOpt || !isa(Term))) return false; // If I will have a chain, make sure no other instruction that will have a // chain interposes between I and the return. diff --git a/llvm/test/CodeGen/X86/tailcall1.ll b/llvm/test/CodeGen/X86/tailcall1.ll index d08919e668cf..f7ff5d5308d6 100644 --- a/llvm/test/CodeGen/X86/tailcall1.ll +++ b/llvm/test/CodeGen/X86/tailcall1.ll @@ -1,4 +1,7 @@ -; RUN: llc < %s -march=x86 -tailcallopt | grep TAILCALL | count 4 +; RUN: llc < %s -march=x86 -tailcallopt | grep TAILCALL | count 5 + +; With -tailcallopt, CodeGen guarantees a tail call optimization +; for all of these. declare fastcc i32 @tailcallee(i32 %a1, i32 %a2, i32 %a3, i32 %a4)