]> granicus.if.org Git - clang/commitdiff
[CodeGen][ObjC] Annotate retain-agnostic ObjC globals with attribute
authorAkira Hatanaka <ahatanaka@apple.com>
Fri, 14 Jun 2019 22:06:28 +0000 (22:06 +0000)
committerAkira Hatanaka <ahatanaka@apple.com>
Fri, 14 Jun 2019 22:06:28 +0000 (22:06 +0000)
'objc_arc_inert'

The attribute enables the ARC optimizer to delete ObjC ARC runtime calls
on the annotated globals (see https://reviews.llvm.org/D62433). We
currently only annotate global variables for string literals and global
blocks with the attribute.

rdar://problem/49839633

Differential Revision: https://reviews.llvm.org/D62831

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@363467 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBlocks.cpp
lib/CodeGen/CodeGenModule.cpp
test/CodeGenCXX/static-local-in-local-class.cpp
test/CodeGenObjC/attr-objc-arc-inert.m [new file with mode: 0644]

index 2a317fc956d0cb49524653b68512737bb25fd86f..c3ee7129d9d78b67d556ffe82b114636992dc7e9 100644 (file)
@@ -1434,10 +1434,12 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
   if (CGM.getContext().getLangOpts().OpenCL)
     AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
 
-  llvm::Constant *literal = fields.finishAndCreateGlobal(
+  llvm::GlobalVariable *literal = fields.finishAndCreateGlobal(
       "__block_literal_global", blockInfo.BlockAlign,
       /*constant*/ !IsWindows, llvm::GlobalVariable::InternalLinkage, AddrSpace);
 
+  literal->addAttribute("objc_arc_inert");
+
   // Windows does not allow globals to be initialised to point to globals in
   // different DLLs.  Any such variables must run code to initialise them.
   if (IsWindows) {
index bd44a493a284481bf9646b9bb931fef1488b5739..7edac4d0114be7bdc32f5c4763f8e333bae2a528 100644 (file)
@@ -4652,6 +4652,7 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
   GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
                                     /*isConstant=*/false,
                                     llvm::GlobalVariable::PrivateLinkage);
+  GV->addAttribute("objc_arc_inert");
   switch (Triple.getObjectFormat()) {
   case llvm::Triple::UnknownObjectFormat:
     llvm_unreachable("unknown file format");
index a70afcdc3883e7749c1c907f062211591da0ae3d..2a82c8ff7daef838a79299551fa52027304b9697 100644 (file)
@@ -127,7 +127,7 @@ extern "C" int call_block_deduced_return() {
 // CHECK: load i32, i32* %
 // CHECK: ret i32 %
 
-// CHECK-LABEL: define internal i32* @_ZZZL20block_deduced_returnvEUb_EN1SclEv(%struct.S.6* %this) #0 align 2 {
+// CHECK-LABEL: define internal i32* @_ZZZL20block_deduced_returnvEUb_EN1SclEv(%struct.S.6* %this) #1 align 2 {
 // CHECK: ret i32* @_ZZZL20block_deduced_returnvEUb_E1n
 
 inline auto static_local_label(void *p) {
diff --git a/test/CodeGenObjC/attr-objc-arc-inert.m b/test/CodeGenObjC/attr-objc-arc-inert.m
new file mode 100644 (file)
index 0000000..eddccf5
--- /dev/null
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fblocks -emit-llvm -o - %s | FileCheck %s
+
+// CHECK: @{{.*}} = private global %{{.*}} { i32* getelementptr inbounds ([0 x i32], [0 x i32]* @__CFConstantStringClassReference, i32 0, i32 0), i32 1992, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @{{.*}}, i32 0, i32 0), i64 3 }, section "__DATA,__cfstring", align 8 #[[ATTRNUM0:[0-9]+]]
+// CHECK: @{{.*}} = internal constant { i8**, i32, i32, i8*, %{{.*}}* } { i8** @_NSConcreteGlobalBlock, i32 1342177280, i32 0, i8* bitcast (i32 (i8*)* @{{.*}} to i8*), %{{.*}}* bitcast ({ i64, i64, i8*, i8* }* @{{.*}} to %{{.*}}*) }, align 8 #[[ATTRNUM0]]
+
+@class NSString;
+
+NSString *testStringLiteral(void) {
+  return @"abc";
+}
+
+int testGlobalBlock(int a) {
+  return ^{ return 123; }();
+}
+
+// CHECK: attributes #[[ATTRNUM0]] = { "objc_arc_inert" }