[PATCH] fix crash in tail call elimination (PR22704)

Sanjay Patel spatel at rotateright.com
Fri Feb 27 10:34:46 PST 2015


Hi hfinkel, aadg, nicholas,

This is a trivial fix (I hope), but I'm sending this patch for review to draw the attention of people more familiar with TailRecursionElimination.

I suspect there are other similar dyn_cast bugs lurking in this pass.

http://reviews.llvm.org/D7944

Files:
  lib/Transforms/Scalar/TailRecursionElimination.cpp
  test/Transforms/TailCallElim/inf-recursion.ll

Index: lib/Transforms/Scalar/TailRecursionElimination.cpp
===================================================================
--- lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -523,7 +523,7 @@
   Value *ReturnedValue = nullptr;
 
   for (Function::iterator BBI = F->begin(), E = F->end(); BBI != E; ++BBI) {
-    ReturnInst *RI = dyn_cast<ReturnInst>(BBI->getTerminator());
+    ReturnInst *RI = dyn_cast_or_null<ReturnInst>(BBI->getTerminator());
     if (RI == nullptr || RI == IgnoreRI) continue;
 
     // We can only perform this transformation if the value returned is
Index: test/Transforms/TailCallElim/inf-recursion.ll
===================================================================
--- test/Transforms/TailCallElim/inf-recursion.ll
+++ test/Transforms/TailCallElim/inf-recursion.ll
@@ -31,3 +31,27 @@
 }
 
 declare x86_fp80 @fabsl(x86_fp80 %f)
+
+
+; Don't crash while transforming a function with infinite recursion.
+
+define i32 @PR22704(i1 %bool) {
+entry:
+  br i1 %bool, label %t, label %f
+
+t:
+  %call1 = call i32 @PR22704(i1 1)
+  br label %return
+
+f:
+  %call = call i32 @PR22704(i1 1)
+  br label %return
+
+return:
+  ret i32 0
+
+; CHECK-LABEL: @PR22704(
+; CHECK:       %bool.tr = phi i1 [ %bool, %entry ], [ true, %t ], [ true, %f ]
+; CHECK:       br i1 %bool.tr, label %t, label %f
+}
+

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7944.20864.patch
Type: text/x-patch
Size: 1384 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150227/9ced5a92/attachment.bin>


More information about the llvm-commits mailing list