[PATCH] D26270: Avoid tail recursion elimination across calls with operand bundles

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 2 19:37:30 PDT 2016


sanjoy created this revision.
sanjoy added reviewers: rnk, majnemer, nlewycky, ahatanak.
sanjoy added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

In some specific scenarios with well understood operand bundle types
(like `"deopt"`) it may be possible to go ahead and convert recursion to
iteration, but TailRecursionElimination does not have that logic today
so avoid doing the right thing for now.

I need some input on whether `"funclet"` operand bundles should also
block tail recursion elimination.  If not, I'll allow TRE across calls
with `"funclet"` operand bundles and add a test case.


https://reviews.llvm.org/D26270

Files:
  lib/Transforms/Scalar/TailRecursionElimination.cpp
  test/Transforms/TailCallElim/deopt-bundle.ll


Index: test/Transforms/TailCallElim/deopt-bundle.ll
===================================================================
--- /dev/null
+++ test/Transforms/TailCallElim/deopt-bundle.ll
@@ -0,0 +1,36 @@
+; RUN: opt < %s -tailcallelim -S | FileCheck %s
+
+define i32 @f_1(i32 %x) {
+; CHECK-LABEL: @f_1(
+wentry:
+  %cond = icmp ugt i32 %x, 0
+  br i1 %cond, label %return, label %body
+
+body:
+; CHECK: body:
+; CHECK: call i32 @f_1(i32 %y) [ "deopt"() ]
+  %y = add i32 %x, 1
+  %tmp = call i32 @f_1(i32 %y) [ "deopt"() ]
+  ret i32 0
+
+return:
+  ret i32 1
+}
+
+define i32 @f_2(i32 %x) {
+; CHECK-LABEL: @f_2
+
+entry:
+  %cond = icmp ugt i32 %x, 0
+  br i1 %cond, label %return, label %body
+
+body:
+; CHECK: body:
+; CHECK: call i32 @f_2(i32 %y) [ "unknown"() ]
+  %y = add i32 %x, 1
+  %tmp = call i32 @f_2(i32 %y) [ "unknown"() ]
+  ret i32 0
+
+return:
+  ret i32 1
+}
Index: lib/Transforms/Scalar/TailRecursionElimination.cpp
===================================================================
--- lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -236,7 +236,7 @@
       if (!CI || CI->isTailCall())
         continue;
 
-      bool IsNoTail = CI->isNoTailCall();
+      bool IsNoTail = CI->isNoTailCall() || CI->hasOperandBundles();
 
       if (!IsNoTail && CI->doesNotAccessMemory()) {
         // A call to a readnone function whose arguments are all things computed
@@ -256,6 +256,7 @@
           SafeToTail = false;
           break;
         }
+        SafeToTail &= CI->hasOperandBundles();
         if (SafeToTail) {
           emitOptimizationRemark(
               F.getContext(), "tailcallelim", F, CI->getDebugLoc(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26270.76811.patch
Type: text/x-patch
Size: 1699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161103/1aaf924c/attachment.bin>


More information about the llvm-commits mailing list