[clang] 874b0a0 - [CodeGen] Mark calls to objc_autorelease as tail
Akira Hatanaka via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 13:48:44 PST 2020
Author: Akira Hatanaka
Date: 2020-11-10T13:48:25-08:00
New Revision: 874b0a0b9db93f5d3350ffe6b5efda2d908415d0
URL: https://github.com/llvm/llvm-project/commit/874b0a0b9db93f5d3350ffe6b5efda2d908415d0
DIFF: https://github.com/llvm/llvm-project/commit/874b0a0b9db93f5d3350ffe6b5efda2d908415d0.diff
LOG: [CodeGen] Mark calls to objc_autorelease as tail
This enables a method sending an autorelease message to an object and
returning the object in MRR to avoid adding the object to an autorelease
pool if a call to objc_retainAutoreleasedReturnValue in the caller
function accepts the hand off of the retain count.
rdar://problem/50678052
Differential Revision: https://reviews.llvm.org/D91111
Added:
Modified:
clang/lib/CodeGen/CGObjC.cpp
clang/test/CodeGenObjC/convert-messages-to-runtime-calls.m
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp
index 89bf402c19f8..bdb9f4002f3c 100644
--- a/clang/lib/CodeGen/CGObjC.cpp
+++ b/clang/lib/CodeGen/CGObjC.cpp
@@ -2221,6 +2221,12 @@ static llvm::Value *emitObjCValueOperation(CodeGenFunction &CGF,
// Call the function.
llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value);
+ // Mark calls to objc_autorelease as tail on the assumption that methods
+ // overriding autorelease do not touch anything on the stack.
+ if (fnName == "objc_autorelease")
+ if (auto *Call = dyn_cast<llvm::CallInst>(Inst))
+ Call->setTailCall();
+
// Cast the result back to the original type.
return CGF.Builder.CreateBitCast(Inst, origType);
}
diff --git a/clang/test/CodeGenObjC/convert-messages-to-runtime-calls.m b/clang/test/CodeGenObjC/convert-messages-to-runtime-calls.m
index c51b56b66fff..4a415be21cb0 100644
--- a/clang/test/CodeGenObjC/convert-messages-to-runtime-calls.m
+++ b/clang/test/CodeGenObjC/convert-messages-to-runtime-calls.m
@@ -30,7 +30,7 @@ void test1(id x) {
// CALLS: {{call.*@objc_allocWithZone}}
// CALLS: {{call.*@objc_retain}}
// CALLS: {{call.*@objc_release}}
- // CALLS: {{call.*@objc_autorelease}}
+ // CALLS: {{tail call.*@objc_autorelease}}
[NSObject alloc];
[NSObject allocWithZone:nil];
[x retain];
@@ -121,7 +121,7 @@ void test_alloc_instance(A *a) {
// call will return i8* which we have to cast to A*
// CHECK-LABEL: define {{.*}}void @test_autorelease_class_ptr
A* test_autorelease_class_ptr(B *b) {
- // CALLS: {{call.*@objc_autorelease}}
+ // CALLS: {{tail call.*@objc_autorelease}}
// CALLS-NEXT: bitcast i8*
// CALLS-NEXT: ret
return [b autorelease];
More information about the cfe-commits
mailing list