Prevent tailcallelim from breaking "recursive" calls to builtins.
authorChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2007 20:58:55 +0000 (20:58 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2007 20:58:55 +0000 (20:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41804 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/TailRecursionElimination.cpp
test/Transforms/TailCallElim/inf-recursion.ll [new file with mode: 0644]

index 497b81fecdeb0d40bc9e59b8ea911c44fbcf450d..7eb47ddaa4fc54b28c359bb2918c79d19b5e0adf 100644 (file)
@@ -302,6 +302,15 @@ bool TailCallElim::ProcessReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry,
 
   if (&BB->front() == Ret) // Make sure there is something before the ret...
     return false;
+  
+  // If the return is in the entry block, then making this transformation would
+  // turn infinite recursion into an infinite loop.  This transformation is ok
+  // in theory, but breaks some code like:
+  //   double fabs(double f) { return __builtin_fabs(f); } // a 'fabs' call
+  // disable this xform in this case, because the code generator will lower the
+  // call to fabs into inline code.
+  if (BB == &F->getEntryBlock())
+    return false;
 
   // Scan backwards from the return, checking to see if there is a tail call in
   // this block.  If so, set CI to it.
diff --git a/test/Transforms/TailCallElim/inf-recursion.ll b/test/Transforms/TailCallElim/inf-recursion.ll
new file mode 100644 (file)
index 0000000..d346171
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | opt -tailcallelim | grep call
+; Don't turn this into an infinite loop, this is probably the implementation
+; of fabs and we expect the codegen to lower fabs.
+
+define double @fabs(double %f) {
+entry:
+        %tmp2 = call double @fabs( double %f )          ; <double> [#uses=1]
+        ret double %tmp2
+}
+