1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00
llvm-mirror/test/CodeGen/AArch64/call-rv-marker.ll
Akira Hatanaka 4055195f29 [ObjC][ARC] Use operand bundle 'clang.arc.attachedcall' instead of
explicitly emitting retainRV or claimRV calls in the IR

This reapplies ed4718eccb12bd42214ca4fb17d196d49561c0c7, which was reverted
because it was causing a miscompile. The bug that was causing the miscompile
has been fixed in 75805dce5ff874676f3559c069fcd6737838f5c0.

Original commit message:

Background:

This fixes a longstanding problem where llvm breaks ARC's autorelease
optimization (see the link below) by separating calls from the marker
instructions or retainRV/claimRV calls. The backend changes are in
https://reviews.llvm.org/D92569.

https://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-autoreleasereturnvalue

What this patch does to fix the problem:

- The front-end adds operand bundle "clang.arc.attachedcall" to calls,
  which indicates the call is implicitly followed by a marker
  instruction and an implicit retainRV/claimRV call that consumes the
  call result. In addition, it emits a call to
  @llvm.objc.clang.arc.noop.use, which consumes the call result, to
  prevent the middle-end passes from changing the return type of the
  called function. This is currently done only when the target is arm64
  and the optimization level is higher than -O0.

- ARC optimizer temporarily emits retainRV/claimRV calls after the calls
  with the operand bundle in the IR and removes the inserted calls after
  processing the function.

- ARC contract pass emits retainRV/claimRV calls after the call with the
  operand bundle. It doesn't remove the operand bundle on the call since
  the backend needs it to emit the marker instruction. The retainRV and
  claimRV calls are emitted late in the pipeline to prevent optimization
  passes from transforming the IR in a way that makes it harder for the
  ARC middle-end passes to figure out the def-use relationship between
  the call and the retainRV/claimRV calls (which is the cause of
  PR31925).

- The function inliner removes an autoreleaseRV call in the callee if
  nothing in the callee prevents it from being paired up with the
  retainRV/claimRV call in the caller. It then inserts a release call if
  claimRV is attached to the call since autoreleaseRV+claimRV is
  equivalent to a release. If it cannot find an autoreleaseRV call, it
  tries to transfer the operand bundle to a function call in the callee.
  This is important since the ARC optimizer can remove the autoreleaseRV
  returning the callee result, which makes it impossible to pair it up
  with the retainRV/claimRV call in the caller. If that fails, it simply
  emits a retain call in the IR if retainRV is attached to the call and
  does nothing if claimRV is attached to it.

- SCCP refrains from replacing the return value of a call with a
  constant value if the call has the operand bundle. This ensures the
  call always has at least one user (the call to
  @llvm.objc.clang.arc.noop.use).

- This patch also fixes a bug in replaceUsesOfNonProtoConstant where
  multiple operand bundles of the same kind were being added to a call.

Future work:

- Use the operand bundle on x86-64.

- Fix the auto upgrader to convert call+retainRV/claimRV pairs into
  calls with the operand bundles.

rdar://71443534

Differential Revision: https://reviews.llvm.org/D92808
2021-03-04 11:22:30 -08:00

150 lines
4.6 KiB
LLVM

; RUN: llc -o - %s | FileCheck --check-prefix=SELDAG --check-prefix=CHECK %s
; RUN: llc -global-isel -o - %s | FileCheck --check-prefix=GISEL --check-prefix=CHECK %s
; TODO: support marker generation with GlobalISel
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-iphoneos"
declare i8* @foo0(i32)
declare i8* @foo1()
declare void @llvm.objc.release(i8*)
declare void @objc_object(i8*)
declare void @foo2(i8*)
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
declare %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1))
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
%struct.S = type { i8 }
@g = dso_local global i8* null, align 8
@fptr = dso_local global i8* ()* null, align 8
define dso_local i8* @rv_marker_1() {
; CHECK-LABEL: rv_marker_1:
; CHECK: .cfi_offset w30, -16
; CHECK-NEXT: bl foo1
; SELDAG-NEXT: mov x29, x29
; GISEL-NOT: mov x29, x29
;
entry:
%call = call i8* @foo1() [ "clang.arc.attachedcall"(i64 0) ]
ret i8* %call
}
define dso_local void @rv_marker_2_select(i32 %c) {
; CHECK-LABEL: rv_marker_2_select:
; SELDAG: cinc w0, w8, eq
; GISEL: csinc w0, w8, wzr, eq
; CHECK-NEXT: bl foo0
; SELDAG-NEXT: mov x29, x29
; CHECK-NEXT: ldr x30, [sp], #16
; CHECK-NEXT: b foo2
;
entry:
%tobool.not = icmp eq i32 %c, 0
%.sink = select i1 %tobool.not, i32 2, i32 1
%call1 = call i8* @foo0(i32 %.sink) [ "clang.arc.attachedcall"(i64 0) ]
tail call void @foo2(i8* %call1)
ret void
}
define dso_local void @rv_marker_3() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
; CHECK-LABEL: rv_marker_3
; CHECK: .cfi_offset w30, -32
; CHECK-NEXT: bl foo1
; SELDAG-NEXT: mov x29, x29
;
entry:
%call = call i8* @foo1() [ "clang.arc.attachedcall"(i64 0) ]
invoke void @objc_object(i8* %call) #5
to label %invoke.cont unwind label %lpad
invoke.cont: ; preds = %entry
tail call void @llvm.objc.release(i8* %call)
ret void
lpad: ; preds = %entry
%0 = landingpad { i8*, i32 }
cleanup
tail call void @llvm.objc.release(i8* %call)
resume { i8*, i32 } %0
}
define dso_local void @rv_marker_4() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
; CHECK-LABEL: rv_marker_4
; CHECK: .Ltmp3:
; CHECK-NEXT: bl foo1
; SELDAG-NEXT: mov x29, x29
; CHECK-NEXT: .Ltmp4:
;
entry:
%s = alloca %struct.S, align 1
%0 = getelementptr inbounds %struct.S, %struct.S* %s, i64 0, i32 0
call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %0) #2
%call = invoke i8* @foo1() [ "clang.arc.attachedcall"(i64 0) ]
to label %invoke.cont unwind label %lpad
invoke.cont: ; preds = %entry
invoke void @objc_object(i8* %call) #5
to label %invoke.cont2 unwind label %lpad1
invoke.cont2: ; preds = %invoke.cont
tail call void @llvm.objc.release(i8* %call)
%call3 = call %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1) %s)
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %0)
ret void
lpad: ; preds = %entry
%1 = landingpad { i8*, i32 }
cleanup
br label %ehcleanup
lpad1: ; preds = %invoke.cont
%2 = landingpad { i8*, i32 }
cleanup
tail call void @llvm.objc.release(i8* %call)
br label %ehcleanup
ehcleanup: ; preds = %lpad1, %lpad
%.pn = phi { i8*, i32 } [ %2, %lpad1 ], [ %1, %lpad ]
%call4 = call %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1) %s)
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %0)
resume { i8*, i32 } %.pn
}
define dso_local i8* @rv_marker_5_indirect_call() {
; CHECK-LABEL: rv_marker_5_indirect_call
; CHECK: ldr [[ADDR:x[0-9]+]], [
; CHECK-NEXT: blr [[ADDR]]
; SLEDAG-NEXT: mov x29, x29
; GISEL-NOT: mov x29, x29
;
entry:
%0 = load i8* ()*, i8* ()** @fptr, align 8
%call = call i8* %0() [ "clang.arc.attachedcall"(i64 0) ]
tail call void @foo2(i8* %call)
ret i8* %call
}
declare i8* @foo(i64, i64, i64)
define dso_local void @rv_marker_multiarg(i64 %a, i64 %b, i64 %c) {
; CHECK-LABEL: rv_marker_multiarg
; CHECK: mov [[TMP:x[0-9]+]], x0
; CHECK-NEXT: mov x0, x2
; CHECK-NEXT: mov x2, [[TMP]]
; CHECK-NEXT: bl foo
; SELDAG-NEXT: mov x29, x29
; GISEL-NOT: mov x29, x29
call i8* @foo(i64 %c, i64 %b, i64 %a) [ "clang.arc.attachedcall"(i64 0) ]
ret void
}
declare i32 @__gxx_personality_v0(...)