Patch to move explicit zero initialization to bss section [take 2]

Kaveh R. Ghazi ghazi@caip.rutgers.edu
Fri Mar 1 20:04:00 GMT 2002


Based on this approval:
http://gcc.gnu.org/ml/gcc-patches/2002-01/msg00418.html
I've installed my -fzero-initialized-in-bss patch on the trunk.

I incorporated the feedback from the last round, including rth's
request to make it on by default, tweeked it to apply cleanly and
redid bootstrap and regtest on solaris2.7 before putting it in.

Here's the final patch.

		--Kaveh


2002-01-06  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* Makefile.in (CRTSTUFF_CFLAGS): Add -fno-zero-initialized-in-bss.
	* doc/invoke.texi (-fno-zero-initialized-in-bss): Document.
	* flags.h (flag_zero_initialized_in_bss): Declare.
	* toplev.c (flag_zero_initialized_in_bss): New flag.
	(lang_independent_options): Add flag_zero_initialized_in_bss.
	* tree.c (initializer_zerop): New function.
	* tree.h (initializer_zerop): Declare.
	* varasm.c (assemble_variable): If we can emit bss, put zero
	initializers in the bss section.
	
diff -rup orig/egcc-CVS20020228/gcc/Makefile.in egcc-CVS20020228/gcc/Makefile.in
--- orig/egcc-CVS20020228/gcc/Makefile.in	Thu Feb 28 07:33:18 2002
+++ egcc-CVS20020228/gcc/Makefile.in	Thu Feb 28 21:37:34 2002
@@ -372,7 +372,8 @@ TARGET_LIBGCC2_CFLAGS =
 
 # Options to use when compiling crtbegin/end.
 CRTSTUFF_CFLAGS = -O2 $(GCC_CFLAGS) $(INCLUDES) $(MULTILIB_CFLAGS) -g0 \
-  -finhibit-size-directive -fno-inline-functions -fno-exceptions
+  -finhibit-size-directive -fno-inline-functions -fno-exceptions \
+  -fno-zero-initialized-in-bss
 
 # Additional sources to handle exceptions; overridden on ia64.
 LIB2ADDEH = $(srcdir)/unwind-dw2.c $(srcdir)/unwind-dw2-fde.c \
diff -rup orig/egcc-CVS20020228/gcc/doc/invoke.texi egcc-CVS20020228/gcc/doc/invoke.texi
--- orig/egcc-CVS20020228/gcc/doc/invoke.texi	Thu Feb 28 07:38:47 2002
+++ egcc-CVS20020228/gcc/doc/invoke.texi	Thu Feb 28 21:46:24 2002
@@ -273,6 +273,7 @@ in the following sections.
 -fno-function-cse  -fno-guess-branch-probability @gol
 -fno-inline  -fno-math-errno  -fno-peephole  -fno-peephole2 @gol
 -funsafe-math-optimizations -fno-trapping-math @gol
+-fno-zero-initialized-in-bss @gol
 -fomit-frame-pointer  -foptimize-register-move @gol
 -foptimize-sibling-calls  -fprefetch-loop-arrays @gol
 -freduce-all-givs -fregmove  -frename-registers @gol
@@ -3407,6 +3408,19 @@ an exact implementation of IEEE or ISO r
 math functions.
 
 The default is @option{-ftrapping-math}.
+
+@item -fno-zero-initialized-in-bss
+@opindex fno-zero-initialized-in-bss
+If the target supports a BSS section, GCC by default puts variables that
+are initialized to zero into BSS@.  This can save space in the resulting
+code.
+
+This option turns off this behavior because some programs explicitly
+rely on variables going to the data section.  E.g., so that the
+resulting executable can find the beginning of that section and/or make
+assumptions based on that.
+
+The default is @option{-fzero-initialized-in-bss}.
 @end table
 
 The following options control specific optimizations.  The @option{-O2}
diff -rup orig/egcc-CVS20020228/gcc/flags.h egcc-CVS20020228/gcc/flags.h
--- orig/egcc-CVS20020228/gcc/flags.h	Thu Jan 10 16:30:56 2002
+++ egcc-CVS20020228/gcc/flags.h	Thu Feb 28 21:36:12 2002
@@ -636,4 +636,7 @@ extern int flag_detailed_statistics;
 /* Nonzero means enable synchronous exceptions for non-call instructions.  */
 extern int flag_non_call_exceptions;
 
+/* Nonzero means put zero initialized data in the bss section.  */
+extern int flag_zero_initialized_in_bss;
+
 #endif /* ! GCC_FLAGS_H */
diff -rup orig/egcc-CVS20020228/gcc/toplev.c egcc-CVS20020228/gcc/toplev.c
--- orig/egcc-CVS20020228/gcc/toplev.c	Thu Feb 28 07:33:23 2002
+++ egcc-CVS20020228/gcc/toplev.c	Thu Feb 28 21:47:23 2002
@@ -803,6 +803,9 @@ int flag_gnu_linker = 0;
 int flag_gnu_linker = 1;
 #endif
 
+/* Nonzero means put zero initialized data in the bss section.  */
+int flag_zero_initialized_in_bss = 1;
+
 /* Enable SSA.  */
 int flag_ssa = 0;
 
@@ -1135,6 +1138,8 @@ static const lang_independent_options f_
    N_("Suppress output of instruction numbers and line number notes in debugging dumps") },
   {"instrument-functions", &flag_instrument_function_entry_exit, 1,
    N_("Instrument function entry/exit with profiling calls") },
+  {"zero-initialized-in-bss", &flag_zero_initialized_in_bss, 1,
+   N_("Put zero initialized data in the bss section") },
   {"ssa", &flag_ssa, 1,
    N_("Enable SSA optimizations") },
   {"ssa-ccp", &flag_ssa_ccp, 1,
diff -rup orig/egcc-CVS20020228/gcc/tree.c egcc-CVS20020228/gcc/tree.c
--- orig/egcc-CVS20020228/gcc/tree.c	Thu Feb 21 07:30:52 2002
+++ egcc-CVS20020228/gcc/tree.c	Thu Feb 28 21:56:48 2002
@@ -4975,3 +4975,45 @@ make_vector (mode, innertype, unsignedp)
 
   return t;
 }
+
+/* Given an initializer INIT, return TRUE if INIT is zero or some
+   aggregate of zeros.  Otherwise return FALSE.  */
+
+bool
+initializer_zerop (init)
+     tree init;
+{
+  STRIP_NOPS (init);
+
+  switch (TREE_CODE (init))
+    {
+    case INTEGER_CST:
+      return integer_zerop (init);
+    case REAL_CST:
+      return real_zerop (init)
+	&& ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
+    case COMPLEX_CST:
+      return integer_zerop (init)
+	|| (real_zerop (init)
+	    && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
+	    && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
+    case CONSTRUCTOR:
+      {
+	if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
+	{
+	  tree aggr_init = TREE_OPERAND (init, 1);
+	  
+	  while (aggr_init)
+	    {
+	      if (! initializer_zerop (TREE_VALUE (aggr_init)))
+		return false;
+	      aggr_init = TREE_CHAIN (aggr_init);
+	    }
+	  return true;
+	}
+	return false;
+      }
+    default:
+      return false;
+    }
+}
diff -rup orig/egcc-CVS20020228/gcc/tree.h egcc-CVS20020228/gcc/tree.h
--- orig/egcc-CVS20020228/gcc/tree.h	Thu Feb 28 21:31:18 2002
+++ egcc-CVS20020228/gcc/tree.h	Thu Feb 28 21:36:12 2002
@@ -2495,6 +2495,11 @@ extern int list_length			PARAMS ((tree))
 
 extern int fields_length		PARAMS ((tree));
 
+/* Given an initializer INIT, return TRUE if INIT is zero or some
+   aggregate of zeros.  Otherwise return FALSE.  */
+
+extern bool initializer_zerop		PARAMS ((tree));
+
 /* integer_zerop (tree x) is nonzero if X is an integer constant of value 0 */
 
 extern int integer_zerop		PARAMS ((tree));
diff -rup orig/egcc-CVS20020228/gcc/varasm.c egcc-CVS20020228/gcc/varasm.c
--- orig/egcc-CVS20020228/gcc/varasm.c	Tue Feb 26 16:30:17 2002
+++ egcc-CVS20020228/gcc/varasm.c	Thu Feb 28 21:36:12 2002
@@ -1602,7 +1602,12 @@ assemble_variable (decl, top_level, at_e
 
   /* Handle uninitialized definitions.  */
 
-  if ((DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node)
+  if ((DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node
+#if defined ASM_EMIT_BSS
+       || (flag_zero_initialized_in_bss
+	   && initializer_zerop (DECL_INITIAL (decl)))
+#endif
+       )
       /* If the target can't output uninitialized but not common global data
 	 in .bss, then we have to use .data.  */
 #if ! defined ASM_EMIT_BSS



More information about the Gcc-patches mailing list