From 45ffe6341d9642487785b0d0028166e6fbdbe5d7 Mon Sep 17 00:00:00 2001 From: Dave MacLachlan Date: Tue, 13 Jul 2021 09:21:25 -0400 Subject: [PATCH] [clang/objc] Optimize getters for non-atomic, copied properties Properties that were declared `@property(copy, nonatomic) id foo` make an unnecessary call to objc_get_property(). This call can be replaced with a direct access to the backing variable identical to how a `@property(nonatomic) id foo` would do it. This reduces codegen by 4 bytes (x86_64/arm64) and removes a cross linkage unit function call per property declared as copy/nonatomic. Differential Revision: https://reviews.llvm.org/D105311 --- clang/lib/CodeGen/CGObjC.cpp | 7 ++++--- clang/test/CodeGenObjC/arc-blocks.m | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp index b865780..2f0acd4 100644 --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -925,10 +925,11 @@ PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, IvarSize = TInfo.Width; IvarAlignment = TInfo.Align; - // If we have a copy property, we always have to use getProperty/setProperty. - // TODO: we could actually use setProperty and an expression for non-atomics. + // If we have a copy property, we always have to use setProperty. + // If the property is atomic we need to use getProperty, but in + // the nonatomic case we can just use expression. if (IsCopy) { - Kind = GetSetProperty; + Kind = IsAtomic ? GetSetProperty : SetPropertyAndExpressionGet; return; } diff --git a/clang/test/CodeGenObjC/arc-blocks.m b/clang/test/CodeGenObjC/arc-blocks.m index 078408b..ab0ee37 100644 --- a/clang/test/CodeGenObjC/arc-blocks.m +++ b/clang/test/CodeGenObjC/arc-blocks.m @@ -477,7 +477,7 @@ void test11b(void) { // CHECK: call void @objc_setProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i8* {{%.*}}, i1 zeroext true, i1 zeroext true) // CHECK: define internal void ()* @"\01-[Test12 nblock]"( -// CHECK: call i8* @objc_getProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i1 zeroext false) +// CHECK: %add.ptr = getelementptr inbounds i8, i8* %1, i64 %ivar // CHECK: define internal void @"\01-[Test12 setNblock:]"( // CHECK: call void @objc_setProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i8* {{%.*}}, i1 zeroext false, i1 zeroext true) -- 2.7.4