diff mbox

-finit-local-vars option

Message ID 5391D79D.7020603@redhat.com
State New
Headers show

Commit Message

Florian Weimer June 6, 2014, 3 p.m. UTC
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.

Comments

Jeff Law June 9, 2014, 4:19 p.m. UTC | #1
On 06/06/14 09:00, Florian Weimer wrote:
> 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.
I'd expect impact to be minimal -- especially for objects which are 
SSA_NAMEs.


>
> 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.
This is the big issue.  As is the fact that initializing to zero isn't 
necessarily is good/safe thing to do and may ultimately end up masking 
real bugs.

To know the right initialization value you have to know how the object 
is used and it's probably outside the scope of what the compiler can 
determine.

Really the way forward is to continue to improve the uninit variable 
warnings to give fewer false positives *and* to find a sane way to give 
uninit warnings for objects that aren't SSA_NAMEs.

Jeff
diff mbox

Patch

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);
 }