TRE: make TRE a bit more aggressive
authorSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 16 Oct 2014 03:27:30 +0000 (03:27 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 16 Oct 2014 03:27:30 +0000 (03:27 +0000)
Make tail recursion elimination a bit more aggressive.  This allows us to get
tail recursion on functions that are just branches to a different function.  The
fact that the function takes a byval argument does not restrict it from being
optimised into just a tail call.

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

lib/Transforms/Scalar/TailRecursionElimination.cpp
test/Transforms/Inline/byval-tail-call.ll
test/Transforms/TailCallElim/basic.ll
test/Transforms/TailCallElim/byval.ll [new file with mode: 0644]

index b7580255150c48fae28ff2dbb02a62bb6ffafcca..f8eafc845db5f8a8ead61071cc7405ff1583dc41 100644 (file)
@@ -249,12 +249,7 @@ bool TailCallElim::markTails(Function &F, bool &AllCallsAreTailCalls) {
     return false;
   AllCallsAreTailCalls = true;
 
-  // The local stack holds all alloca instructions and all byval arguments.
   AllocaDerivedValueTracker Tracker;
-  for (Argument &Arg : F.args()) {
-    if (Arg.hasByValAttr())
-      Tracker.walk(&Arg);
-  }
   for (auto &BB : F) {
     for (auto &I : BB)
       if (AllocaInst *AI = dyn_cast<AllocaInst>(&I))
@@ -310,9 +305,8 @@ bool TailCallElim::markTails(Function &F, bool &AllCallsAreTailCalls) {
         for (auto &Arg : CI->arg_operands()) {
           if (isa<Constant>(Arg.getUser()))
             continue;
-          if (Argument *A = dyn_cast<Argument>(Arg.getUser()))
-            if (!A->hasByValAttr())
-              continue;
+          if (isa<Argument>(Arg.getUser()))
+            continue;
           SafeToTail = false;
           break;
         }
index 3a8906aa210f6a25dd779d2ba249961fc9137265..e80bae1696e59b401a499eb8d8fc0e3e2cef7a9b 100644 (file)
@@ -27,10 +27,11 @@ define internal void @qux(i32* byval %x) {
   tail call void @ext(i32* null)
   ret void
 }
+
 define void @frob(i32* %x) {
 ; CHECK-LABEL: define void @frob(
 ; CHECK: alloca i32
-; CHECK: {{^ *}}call void @ext(
+; CHECK: {{^ *}}tail call void @ext(
 ; CHECK: tail call void @ext(i32* null)
 ; CHECK: ret void
   tail call void @qux(i32* byval %x)
index 8e9814b52bbce90e60bf5b6291bc639cef5c71e8..c7af9af5be75b63e42f1ef5b85a0833768942e1e 100644 (file)
@@ -147,7 +147,7 @@ cond_false:
 ; Don't tail call if a byval arg is captured.
 define void @test9(i32* byval %a) {
 ; CHECK-LABEL: define void @test9(
-; CHECK: {{^ *}}call void @use(
+; CHECK: {{^ *}}tail call void @use(
   call void @use(i32* %a)
   ret void
 }
diff --git a/test/Transforms/TailCallElim/byval.ll b/test/Transforms/TailCallElim/byval.ll
new file mode 100644 (file)
index 0000000..1150f76
--- /dev/null
@@ -0,0 +1,34 @@
+; RUN: opt -mtriple i386 -Os -S %s -o - | FileCheck %s
+; RUN: opt -mtriple x86_64 -Os -S %s -o - | FileCheck %s
+; RUN: opt -mtriple armv7 -Os -S %s -o - | FileCheck %s
+
+%struct.D16 = type { [16 x double] }
+
+declare void @_Z2OpP3D16PKS_S2_(%struct.D16*, %struct.D16*, %struct.D16*)
+
+define void @_Z7TestRefRK3D16S1_(%struct.D16* noalias sret %agg.result, %struct.D16* %RHS, %struct.D16* %LHS) {
+  %1 = alloca %struct.D16*, align 8
+  %2 = alloca %struct.D16*, align 8
+  store %struct.D16* %RHS, %struct.D16** %1, align 8
+  store %struct.D16* %LHS, %struct.D16** %2, align 8
+  %3 = load %struct.D16** %1, align 8
+  %4 = load %struct.D16** %2, align 8
+  call void @_Z2OpP3D16PKS_S2_(%struct.D16* %agg.result, %struct.D16* %3, %struct.D16* %4)
+  ret void
+}
+
+; CHECK: define void @_Z7TestRefRK3D16S1_({{.*}}) {
+; CHECK:   tail call void @_Z2OpP3D16PKS_S2_(%struct.D16* %agg.result, %struct.D16* %RHS, %struct.D16* %LHS)
+; CHECK:   ret void
+; CHECK: }
+
+define void @_Z7TestVal3D16S_(%struct.D16* noalias sret %agg.result, %struct.D16* byval align 8 %RHS, %struct.D16* byval align 8 %LHS) {
+  call void @_Z2OpP3D16PKS_S2_(%struct.D16* %agg.result, %struct.D16* %RHS, %struct.D16* %LHS)
+  ret void
+}
+
+; CHECK: define void @_Z7TestVal3D16S_({{.*}}) {
+; CHECK:   tail call void @_Z2OpP3D16PKS_S2_(%struct.D16* %agg.result, %struct.D16* %RHS, %struct.D16* %LHS)
+; CHECK:   ret void
+; CHECK: }
+