]> granicus.if.org Git - llvm/commitdiff
Avoid tail recursion elimination across calls with operand bundles
authorSanjoy Das <sanjoy@playingwithpointers.com>
Mon, 7 Nov 2016 21:01:49 +0000 (21:01 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Mon, 7 Nov 2016 21:01:49 +0000 (21:01 +0000)
Summary:
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.

Reviewers: rnk, majnemer, nlewycky, ahatanak

Subscribers: mcrosier, llvm-commits

Differential Revision: https://reviews.llvm.org/D26270

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286147 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 16c88270a84f86ef2950ff3248be336ae1a50aa3..2c0294ccc385c06acfd662ad25c8c3dab93e11bb 100644 (file)
@@ -236,7 +236,7 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
       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 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
           SafeToTail = false;
           break;
         }
+        SafeToTail &= CI->hasOperandBundles();
         if (SafeToTail) {
           emitOptimizationRemark(
               F.getContext(), "tailcallelim", F, CI->getDebugLoc(),
diff --git a/test/Transforms/TailCallElim/deopt-bundle.ll b/test/Transforms/TailCallElim/deopt-bundle.ll
new file mode 100644 (file)
index 0000000..a98c47e
--- /dev/null
@@ -0,0 +1,57 @@
+; 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
+}
+
+declare void @func()
+
+define void @f_3(i1 %B) personality i8 42 {
+; CHECK-LABEL: @f_3(
+entry:
+  invoke void @func()
+          to label %exit unwind label %merge
+merge:
+  %cs1 = catchswitch within none [label %catch] unwind to caller
+
+catch:
+; CHECK: catch:
+; CHECK: call void @f_3(i1 %B) [ "funclet"(token %cp) ]
+  %cp = catchpad within %cs1 []
+  call void @f_3(i1 %B) [ "funclet"(token %cp) ]
+  ret void
+
+exit:
+  ret void
+}