[PATCH] D52895: [TailCallElim] Enable marking of calls with byval as tails

Robert Lougher via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 4 10:54:30 PDT 2018


rob.lougher created this revision.
rob.lougher added reviewers: rnk, nicholas, hfinkel.
Herald added a subscriber: eraman.

In https://reviews.llvm.org/D50679 the alias analysis rules were changed with regards to tail calls and byval arguments. Previously, tail calls were assumed not to alias allocas from the current frame. This has been updated, to not assune this for arguments with the byval attribute.

With this change, TailCallElim can now be more aggressive and mark more calls as tails, e.g.:

define void @test() {

  %f = alloca %struct.foo
  call void @bar(%struct.foo* byval %f)
  ret void

}

define void @test2(%struct.foo* byval %f) {

  call void @bar(%struct.foo* byval %f)
  ret void

}

define void @test3(%struct.foo* byval %f) {

  %agg.tmp = alloca %struct.foo
  %0 = bitcast %struct.foo* %agg.tmp to i8*
  %1 = bitcast %struct.foo* %f to i8*
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 40, i1 false)
  call void @bar(%struct.foo* byval %agg.tmp)
  ret void

}

The problematic case where a byval parameter is captured by a call is still handled correctly, and will not be marked as a tail (see PR7272).

After this change the following code:

- test.cpp --------------

struct foo {

  int i[10];

};

extern void bar(struct foo f);

void foo(struct foo f) {

  bar(f);

}
-

Now produces on Liinux x86-64:

_Z3foo3foo:                             # @_Z3foo3foo
	jmp	_Z3bar3foo              # TAILCALL

Previously it gave:

_Z3foo3foo:                             # @_Z3foo3foo
	subq		$40, %rsp
	movq	80(%rsp), %rax
	movq	%rax, 32(%rsp)
	movaps	48(%rsp), %xmm0
	movaps	64(%rsp), %xmm1
	movups	%xmm1, 16(%rsp)
	movups	%xmm0, (%rsp)
	callq	_Z3bar3foo
	addq	$40, %rsp
retq

The new output matches gcc.


Repository:
  rL LLVM

https://reviews.llvm.org/D52895

Files:
  lib/Transforms/Scalar/TailRecursionElimination.cpp
  test/Transforms/Inline/byval-tail-call.ll
  test/Transforms/TailCallElim/basic.ll


Index: test/Transforms/TailCallElim/basic.ll
===================================================================
--- test/Transforms/TailCallElim/basic.ll
+++ test/Transforms/TailCallElim/basic.ll
@@ -198,3 +198,42 @@
   call void undef(i8* undef) [ "foo"(i8* %e) ]
   unreachable
 }
+
+%struct.foo = type { [10 x i32] }
+
+; If an alloca is passed byval it is not an escape point and the call can
+; be marked as a tail.
+define void @test13() {
+; CHECK-LABEL: @test13
+; CHECK: tail call void @bar
+entry:
+  %f = alloca %struct.foo
+  call void @bar(%struct.foo* byval %f)
+  ret void
+}
+
+; A call which passes a byval parameter using byval can be marked tail.
+define void @test14(%struct.foo* byval %f) {
+; CHECK-LABEL: @test14
+; CHECK: tail call void @bar
+entry:
+  call void @bar(%struct.foo* byval %f)
+  ret void
+}
+
+; If a byval parameter is copied into an alloca and passed byval the call can
+; be marked tail.
+define void @test15(%struct.foo* byval %f) {
+; CHECK-LABEL: @test15
+; CHECK: tail call void @bar
+entry:
+  %agg.tmp = alloca %struct.foo
+  %0 = bitcast %struct.foo* %agg.tmp to i8*
+  %1 = bitcast %struct.foo* %f to i8*
+  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 40, i1 false)
+  call void @bar(%struct.foo* byval %agg.tmp)
+  ret void
+}
+
+declare void @bar(%struct.foo* byval)
+declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1)
Index: test/Transforms/Inline/byval-tail-call.ll
===================================================================
--- test/Transforms/Inline/byval-tail-call.ll
+++ test/Transforms/Inline/byval-tail-call.ll
@@ -40,3 +40,24 @@
   tail call void @qux(i32* byval %x)
   ret void
 }
+
+; If a call passes a byval parameter as byval the call can be marked as a
+; tail.
+
+declare void @ext2(i32* byval)
+
+define void @bar2(i32* byval %x) {
+  call void @ext2(i32* byval %x)
+  ret void
+}
+
+define void @foobar(i32* %x) {
+; CHECK-LABEL: define void @foobar(
+; CHECK: %[[POS:.*]] = alloca i32
+; CHECK: %[[VAL:.*]] = load i32, i32* %x
+; CHECK: store i32 %[[VAL]], i32* %[[POS]]
+; CHECK: tail call void @ext2(i32* byval nonnull %[[POS]]
+; CHECK: ret void
+  tail call void @bar2(i32* byval %x)
+  ret void
+}
Index: lib/Transforms/Scalar/TailRecursionElimination.cpp
===================================================================
--- lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -127,6 +127,12 @@
       case Instruction::Call:
       case Instruction::Invoke: {
         CallSite CS(I);
+        // If the alloca-derived argument is passed byval it is not an escape
+        // point, or a use of an alloca. Calling with byval copies the contents
+        // of the alloca into argument registers or stack slots, which exist
+        // beyond the lifetime of the current frame.
+        if (CS.isArgOperand(U) && CS.isByValArgument(CS.getArgumentNo(U)))
+          continue;
         bool IsNocapture =
             CS.isDataOperand(U) && CS.doesNotCapture(CS.getDataOperandNo(U));
         callUsesLocalStack(CS, IsNocapture);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52895.168323.patch
Type: text/x-patch
Size: 3134 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181004/cc8e0634/attachment.bin>


More information about the llvm-commits mailing list