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]

[PATCH] Adds generic option -fsort-data to sort variables based on their alignment


Hi All,

Please find following patch which adds generic option -fsort-data to
sort variables and constants based on their alignment. This option gets
enabled with -Os switch.

Please comment on patch.

========================Start Of Patch==================================
ChangeLog
2005-07-20  Anil Paranjpe <anilp1@kpitcummins.com>
	* varasm.c (align_rodata_section) : New function
	 	     (align_data_section) : New function
	           (enum section_category) : Define new categories like
SECCAT_RODATA_ALIGN,
	
SECCAT_DATA_ALIGN.
	* output.h : Declare functions align_rodata_section and
align_data_section.
	* common.opt : Declare new option fsort-data.
	* opts.c (decode_options) : The fsort-data option is set under
optimize_size option.

========================================================================
--- gcc-4.1-20050604/gcc/common.opt.orig	2005-06-04
22:37:55.000000000 +0530
+++ gcc-4.1-20050604/gcc/common.opt	2005-06-27 15:11:02.000000000
+0530
@@ -935,6 +935,10 @@ funroll-all-loops
 Common Report Var(flag_unroll_all_loops)  Perform loop unrolling for
all loops
 
+fsort-data
+Common Report Var(flag_sort_data)
+Sort variables based on their alignment and place in separate sections
+
 ; Nonzero means that unsafe floating-point math optimizations are
allowed  ; for the sake of speed.  IEEE compliance is not guaranteed,
and operations  ; are allowed to assume that their arguments and results
are "normal"
--- gcc-4.1-20050604/gcc/opts.c.orig	2005-06-01 12:32:17.000000000
+0530
+++ gcc-4.1-20050604/gcc/opts.c	2005-06-27 18:02:44.000000000 +0530
@@ -612,6 +612,7 @@ decode_options (unsigned int argc, const
       set_param_value ("max-inline-insns-single", 5);
       set_param_value ("max-inline-insns-auto", 5);
       flag_inline_functions = 1;
+      flag_sort_data = 1;
 
       /* We want to crossjump as much as possible.  */
       set_param_value ("min-crossjump-insns", 1);
--- gcc-4.1-20050604/gcc/output.h.orig	2005-04-28 03:05:20.000000000
+0530
+++ gcc-4.1-20050604/gcc/output.h	2005-06-28 10:29:20.000000000
+0530
@@ -220,6 +220,12 @@ extern void mergeable_string_section (tr  extern
void mergeable_constant_section (enum machine_mode,
 					unsigned HOST_WIDE_INT, unsigned
int);
 
+/* enabled with -Os (fsort-data) */
+extern void align_rodata_section (tree, unsigned HOST_WIDE_INT,
+				      unsigned int);
+extern void align_data_section (tree, unsigned HOST_WIDE_INT,
+				      unsigned int);
+
 /* Declare DECL to be a weak symbol.  */  extern void declare_weak
(tree);
 /* Merge weak status.  */
--- gcc-4.1-20050604/gcc/varasm.c.orig	2005-05-25 09:46:42.000000000
+0530
+++ gcc-4.1-20050604/gcc/varasm.c	2005-07-19 14:35:02.000000000
+0530
@@ -770,6 +770,49 @@ mergeable_constant_section (enum machine
 
   readonly_data_section ();
 }
+
+void
+align_rodata_section(tree decl ATTRIBUTE_UNUSED,
+			  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
+			  unsigned int flags ATTRIBUTE_UNUSED) {
+   char name[35];
+
+   if ( TREE_CODE(TREE_TYPE(decl)) == ARRAY_TYPE )
+   {
+	sprintf (name, ".rodata.align%d", (int)
((TYPE_ALIGN(TREE_TYPE(TREE_TYPE(decl)))) / 8));
+   }
+   else
+   {
+	sprintf (name, ".rodata.align%d", (int)
((TYPE_ALIGN(TREE_TYPE(decl))) / 8));
+   }
+   
+   flags |= SECTION_NOTYPE ;     
+   named_section_flags (name, flags);
+   return;
+}
+
+void
+align_data_section(tree decl ATTRIBUTE_UNUSED,
+			  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
+			  unsigned int flags ATTRIBUTE_UNUSED) {
+   char name[35];
+
+   if ( TREE_CODE(TREE_TYPE(decl)) == ARRAY_TYPE )
+   {
+      sprintf (name, ".data.align%d", (int)
((TYPE_ALIGN(TREE_TYPE(TREE_TYPE(decl)))) / 8));
+   }
+   else
+   {
+      sprintf (name, ".data.align%d", (int)
((TYPE_ALIGN(TREE_TYPE(decl))) / 8));
+   }
+
+   flags |= SECTION_NOTYPE | SECTION_WRITE;
+   named_section_flags (name, flags);
+   return;
+}
+
 
 /* Given NAME, a putative register name, discard any customary
prefixes.  */
 
@@ -5029,6 +5072,7 @@ enum section_category
   SECCAT_RODATA_MERGE_STR_INIT,
   SECCAT_RODATA_MERGE_CONST,
   SECCAT_SRODATA,
+  SECCAT_RODATA_ALIGN,
 
   SECCAT_DATA,
 
@@ -5045,7 +5089,8 @@ enum section_category
   SECCAT_DATA_REL_LOCAL,
   SECCAT_DATA_REL_RO,
   SECCAT_DATA_REL_RO_LOCAL,
-
+  SECCAT_DATA_ALIGN,
+ 
   SECCAT_SDATA,
   SECCAT_TDATA,
 
@@ -5073,6 +5118,13 @@ categorize_decl_for_section (tree decl, 
     }
   else if (TREE_CODE (decl) == VAR_DECL)
     {
+      if (flag_sort_data)   /* Enabled only with -Os */
+      {
+	  if (! TREE_READONLY (decl))
+		return SECCAT_DATA_ALIGN;  
+	  else
+		return SECCAT_RODATA_ALIGN;  
+      }
       if (DECL_INITIAL (decl) == NULL
 	  || DECL_INITIAL (decl) == error_mark_node
 	  || (flag_zero_initialized_in_bss
@@ -5199,6 +5251,12 @@ default_elf_select_section_1 (tree decl,
     case SECCAT_RODATA_MERGE_CONST:
       mergeable_constant_section (DECL_MODE (decl), align, 0);
       return;
+    case SECCAT_RODATA_ALIGN:
+      align_rodata_section(decl, align, 0);
+      return;
+    case SECCAT_DATA_ALIGN:
+      align_data_section(decl, align, 0);  
+      return;
     case SECCAT_SRODATA:
       sname = ".sdata2";
       break;

========================End Of Patch==================================

Regards,
Anil Paranjpe
KPIT Cummins InfoSystems Ltd.
Pune, India

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Free download of GNU based tool-chains for Renesas' SH and H8 Series.
The following site also offers free technical support to its users. 
Visit http://www.kpitgnutools.com for details. 
Latest versions of KPIT GNU tools were released on June 1, 2005.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


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