This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

-finit-local-vars option


The attached crude patch as an -finit-local-vars option to the C front end. If active, all local variables are initialized to zero. It is part of an experiment to assess the performance impact of local variable initialization.

This is not a real patch submission because the way the flag is implemented, it interferes with unused-variable warnings. I just want to archive it for posterity.

--
Florian Weimer / Red Hat Product Security Team
commit dbb1a7d8ee583466f00502848866fbfe5f5e2ca8
Author: Florian Weimer <fweimer@redhat.com>
Date:   Thu Jun 5 10:16:34 2014 +0200

    Add -finit-local-vars

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 5d36a80..54d7244 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1074,6 +1074,10 @@ Enum(ivar_visibility) String(public) Value(IVAR_VISIBILITY_PUBLIC)
 EnumValue
 Enum(ivar_visibility) String(package) Value(IVAR_VISIBILITY_PACKAGE)
 
+finit-local-vars
+C Var(flag_init_local_vars)
+Initialize local variables to zero
+
 fnonansi-builtins
 C++ ObjC++ Var(flag_no_nonansi_builtin, 0)
 
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index dc8dbc2..52efd56 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -4627,6 +4627,19 @@ finish_decl (tree decl, location_t init_loc, tree init,
 	diagnose_uninitialized_cst_member (decl, type);
     }
 
+  if (flag_init_local_vars
+      && TREE_CODE (decl) == VAR_DECL
+      && !DECL_EXTERNAL (decl)
+      && init == NULL_TREE
+      && DECL_INITIAL (decl) == NULL_TREE
+      && !global_bindings_p ())
+    {
+      if (AGGREGATE_TYPE_P (type))
+	DECL_INITIAL (decl) = build_constructor (type, NULL);
+      else
+	DECL_INITIAL (decl) = fold_convert (type, integer_zero_node);
+    }
+
 	invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
 }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]