paquette created this revision.
paquette added reviewers: rsmith, jyknight.
Herald added a project: clang.

Since these are only ever run once, they can be marked as cold. On top of that, 
since they're only ever run once, they might as well be minsize.

I observed a 0.21% code size improvement in a standard release build of clang 
for x86 with this change.


Repository:
  rC Clang

https://reviews.llvm.org/D59509

Files:
  clang/lib/CodeGen/CGDeclCXX.cpp
  clang/test/CodeGen/address-safety-attr.cpp
  clang/test/CodeGen/static-attr.cpp

Index: clang/test/CodeGen/static-attr.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/static-attr.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -disable-O0-optnone -emit-llvm %s -o - | FileCheck -check-prefix=WITH %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - | FileCheck -check-prefix=WITHOUT %s
+
+// WITHOUT-NOT: cold minsize noinline
+// WITH: define internal void @__cxx_global_var_init() [[ATTR:#[0-9]]]
+
+extern int baz(void);
+
+int foo(void) {
+  return baz();
+}
+
+static struct StaticInitializer {
+  StaticInitializer() {
+    foo();
+  }
+} I;
+
+int bar() {
+  StaticInitializer I;
+  return 0;
+}
+
+// WITH: attributes [[ATTR]] = { {{.*}}cold minsize noinline nounwind optsize{{.*}} }
Index: clang/test/CodeGen/address-safety-attr.cpp
===================================================================
--- clang/test/CodeGen/address-safety-attr.cpp
+++ clang/test/CodeGen/address-safety-attr.cpp
@@ -26,17 +26,16 @@
 
 // Check that functions generated for global in different source file are
 // not blacklisted.
-// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
-// WITHOUT: @__cxx_global_array_dtor{{.*}}[[NOATTR]]
-// BLFILE: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
-// BLFILE: @__cxx_global_array_dtor{{.*}}[[WITH]]
-// BLFUNC: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
-// BLFUNC: @__cxx_global_array_dtor{{.*}}[[WITH]]
-// ASAN: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
-// ASAN: @__cxx_global_array_dtor{{.*}}[[WITH]]
-
-
-// WITHOUT:  NoAddressSafety1{{.*}}) [[NOATTR]]
+// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR_STATIC:#[0-9]+]]
+// WITHOUT: @__cxx_global_array_dtor{{.*}}[[NOATTR_STATIC]]
+// BLFILE: @__cxx_global_var_init{{.*}}[[WITH_STATIC:#[0-9]+]]
+// BLFILE: @__cxx_global_array_dtor{{.*}}[[WITH_STATIC]]
+// BLFUNC: @__cxx_global_var_init{{.*}}[[WITH_STATIC:#[0-9]+]]
+// BLFUNC: @__cxx_global_array_dtor{{.*}}[[WITH_STATIC]]
+// ASAN: @__cxx_global_var_init{{.*}}[[WITH_STATIC:#[0-9]+]]
+// ASAN: @__cxx_global_array_dtor{{.*}}[[WITH_STATIC]]
+
+// WITHOUT:  NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
 // BLFILE:  NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
 // BLFUNC:  NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
 // ASAN:  NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
@@ -138,18 +137,23 @@
 // Check that __cxx_global_var_init* get the sanitize_address attribute.
 int global1 = 0;
 int global2 = *(int*)((char*)&global1+1);
-// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR]]
-// BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
-// BLFUNC: @__cxx_global_var_init{{.*}}[[WITH]]
-// ASAN: @__cxx_global_var_init{{.*}}[[WITH]]
+// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR_STATIC]]
+// BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR_STATIC:#[0-9]+]]
+// BLFUNC: @__cxx_global_var_init{{.*}}[[WITH_STATIC]]
+// ASAN: @__cxx_global_var_init{{.*}}[[WITH_STATIC]]
 
 // WITHOUT: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
+// WITHOUT: attributes [[NOATTR_STATIC]] = { cold minsize noinline nounwind optsize{{.*}} }
 
 // BLFILE: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
+// BLFILE: attributes [[WITH_STATIC]] = { cold minsize noinline nounwind optsize sanitize_address{{.*}} }
 // BLFILE: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
+// BLFILE: attributes [[NOATTR_STATIC]] = { cold minsize noinline nounwind optsize{{.*}} }
 
 // BLFUNC: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
+// BLFUNC: attributes [[WITH_STATIC]] = { cold minsize noinline nounwind optsize sanitize_address{{.*}} }
 // BLFUNC: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
 
 // ASAN: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
+// ASAN: attributes [[WITH_STATIC]] = { cold minsize noinline nounwind optsize sanitize_address{{.*}} }
 // ASAN: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
Index: clang/lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGDeclCXX.cpp
+++ clang/lib/CodeGen/CGDeclCXX.cpp
@@ -391,6 +391,15 @@
   if (getCodeGenOpts().BranchTargetEnforcement)
     Fn->addFnAttr("branch-target-enforcement");
 
+  // If we're optimizing, then we can make these small, since they're only ever
+  // run once.
+  if (getCodeGenOpts().DisableO0ImplyOptNone ||
+      getCodeGenOpts().OptimizationLevel != 0) {
+    Fn->addFnAttr(llvm::Attribute::OptimizeForSize);
+    Fn->addFnAttr(llvm::Attribute::MinSize);
+    Fn->addFnAttr(llvm::Attribute::Cold);
+  }
+
   return Fn;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to