In guaranteed tailcall mode, don't decline the tailcall optimization
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGBuilder.cpp
index 3a25714f07c0a5b996f320b5e17b980edcde02dd..93ae043dbc14435c5f3c203b971d7481c2372b78 100644 (file)
@@ -4205,19 +4205,16 @@ isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr,
   const ReturnInst *Ret = dyn_cast<ReturnInst>(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;
-
-  // Unless we are explicitly forcing tailcall optimization do not tailcall if
-  // the called function is bitcast'ed. The analysis may not be entirely
-  // accurate.
-  if (!PerformTailCallOpt && isa<BitCastInst>(CS.getCalledValue()))
-    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<UnreachableInst>(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.
@@ -4246,6 +4243,10 @@ isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr,
   if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
     return false;
 
+  // It's not safe to eliminate the sign / zero extension of the return value.
+  if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
+    return false;
+
   // Otherwise, make sure the unmodified return value of I is the return value.
   for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
        U = dyn_cast<Instruction>(U->getOperand(0))) {