[PATCH] D56802: [CodeGenObjC] Treat ivar offsets variables as constant if they refer to ivars of a direct subclass of NSObject with an @implementation

Erik Pilkington via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 16 13:01:37 PST 2019


erik.pilkington created this revision.
erik.pilkington added reviewers: rjmccall, pete, ahatanak.
Herald added subscribers: dexonsmith, jkorous.

This patch was originally written by Pete Cooper.

This is possible because the size of NSObject is effectively ABI, and will not change in the future. Doing this allows LLVM to avoid loading the offset.

Thanks, 
Erik


Repository:
  rC Clang

https://reviews.llvm.org/D56802

Files:
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m


Index: clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
===================================================================
--- /dev/null
+++ clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -emit-llvm %s -o - | FileCheck %s --dump-input-on-failure
+
+ at interface NSObject {
+  int these, will, never, change, ever;
+}
+ at end
+
+ at interface Sub : NSObject
+ at end
+
+ at implementation Sub {
+  int sub_ivar;
+}
+ at end
+
+// CHECK: @"OBJC_IVAR_$_Sub.sub_ivar" = hidden constant i64 20
+
+ at interface NotNSObject {
+  int these, might, change;
+}
+ at end
+
+ at interface Sub2 : NotNSObject
+ at end
+
+ at implementation Sub2 {
+  int sub2_ivar;
+}
+ at end
+
+// CHECK: @"OBJC_IVAR_$_Sub2.sub2_ivar" = hidden global i64 12
+
Index: clang/lib/CodeGen/CGObjCMac.cpp
===================================================================
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -6702,6 +6702,15 @@
       IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
   }
 
+  // NSObject is a fixed size. If we can see the @implementation of a class
+  // which inherits from NSObject then we know that all it's offsets also must
+  // be fixed, so we can set this as a constant global.
+  // FIXME: Can we do this if see a chain of super classes with implementations
+  // leading to NSObject?
+  if (ID->getImplementation() && ID->getSuperClass() &&
+      ID->getSuperClass()->getName() == "NSObject")
+    IvarOffsetGV->setConstant(true);
+
   if (CGM.getTriple().isOSBinFormatMachO())
     IvarOffsetGV->setSection("__DATA, __objc_ivar");
   return IvarOffsetGV;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56802.182126.patch
Type: text/x-patch
Size: 1670 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190116/6477b770/attachment.bin>


More information about the cfe-commits mailing list