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] reg-stack.c: Use VEC instead of VARRAY.


Hi,

Attached is a patch to use VEC instead of VARRAY.

A couple of notes.

Once we remove the use of VARRAY in reg-stack.c, the file does not
define any garbage collected objects, so the patch removes the
mentions of gt-reg-stack.h in Makefile.in and reg-stack.c.

Without this patch, there was a bug that accesses an array beyond its
size.  Consider:

  max = VARRAY_SIZE (...);
  if (uid >= max)
    {
      max = uid + uid / 20;
      VARRAY_GROW (..., max);
    }
  ... = VARRAY_CHAR (..., uid);

The code is intended to increase the size of the array if uid >= max.
However, note that there are cases where uid == max even after the
"if" statement.  For example, if uid == 6, then we have max == 6,
causing access to the element just after the last element of the
array.  The patch guarantees uid < max after the "if" statement by
increasing max at least by 1.

Tested on x86_64-pc-linux-gnu.  I'll check in this patch in 24 hours
or so as preapproved.

Kazu Hirata

2006-03-13  Kazu Hirata  <kazu@codesourcery.com>

	* Makefile.in (reg-stack.o): Don't depend on gt-reg-stack.h.
	* reg-stack.c (stack_regs_mentioned_data): Change the type to
	VEC(char,heap) *.
	(stack_regs_mentioned): Update the uses of
	stack_regs_mentioned_data.  Don't access the array beyond its
	end.
	(reg_to_stack): Update the uses of stack_regs_mentioned_data.
	Don't include gt-reg-stack.h.

Index: Makefile.in
===================================================================
--- Makefile.in	(revision 111980)
+++ Makefile.in	(working copy)
@@ -2544,7 +2544,7 @@ recog.o : recog.c $(CONFIG_H) $(SYSTEM_H
 reg-stack.o : reg-stack.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(RTL_H) $(TREE_H) $(RECOG_H) $(REGS_H) hard-reg-set.h $(FLAGS_H) \
    insn-config.h toplev.h reload.h $(FUNCTION_H) $(TM_P_H) $(GGC_H) \
-   gt-reg-stack.h $(BASIC_BLOCK_H) output.h $(VARRAY_H) timevar.h tree-pass.h \
+   $(BASIC_BLOCK_H) output.h $(VARRAY_H) timevar.h tree-pass.h \
    target.h
 sreal.o: sreal.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) sreal.h
 predict.o: predict.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
@@ -2845,7 +2845,7 @@ gt-function.h gt-integrate.h gt-tree.h g
 gt-emit-rtl.h gt-explow.h gt-stor-layout.h gt-regclass.h \
 gt-lists.h gt-alias.h gt-cselib.h gt-gcse.h \
 gt-expr.h gt-sdbout.h gt-optabs.h gt-bitmap.h gt-dojump.h \
-gt-dwarf2out.h gt-reg-stack.h gt-dwarf2asm.h \
+gt-dwarf2out.h gt-dwarf2asm.h \
 gt-dbxout.h \
 gtype-c.h gt-cfglayout.h \
 gt-tree-mudflap.h gt-tree-vect-generic.h \
Index: reg-stack.c
===================================================================
--- reg-stack.c	(revision 111980)
+++ reg-stack.c	(working copy)
@@ -174,13 +174,16 @@
 #include "tree-pass.h"
 #include "target.h"
 
+DEF_VEC_I(char);
+DEF_VEC_ALLOC_I(char,heap);
+
 /* We use this array to cache info about insns, because otherwise we
    spend too much time in stack_regs_mentioned_p.
 
    Indexed by insn UIDs.  A value of zero is uninitialized, one indicates
    the insn uses stack registers, two indicates the insn does not use
    stack registers.  */
-static GTY(()) varray_type stack_regs_mentioned_data;
+static VEC(char,heap) *stack_regs_mentioned_data;
 
 #ifdef STACK_REGS
 
@@ -309,21 +312,27 @@ stack_regs_mentioned (rtx insn)
     return 0;
 
   uid = INSN_UID (insn);
-  max = VARRAY_SIZE (stack_regs_mentioned_data);
+  max = VEC_length (char, stack_regs_mentioned_data);
   if (uid >= max)
     {
+      char *p;
+      unsigned int old_max = max;
+
       /* Allocate some extra size to avoid too many reallocs, but
 	 do not grow too quickly.  */
-      max = uid + uid / 20;
-      VARRAY_GROW (stack_regs_mentioned_data, max);
+      max = uid + uid / 20 + 1;
+      VEC_safe_grow (char, heap, stack_regs_mentioned_data, max);
+      p = VEC_address (char, stack_regs_mentioned_data);
+      memset (&p[old_max], 0,
+	      sizeof (char) * (max - old_max));
     }
 
-  test = VARRAY_CHAR (stack_regs_mentioned_data, uid);
+  test = VEC_index (char, stack_regs_mentioned_data, uid);
   if (test == 0)
     {
       /* This insn has yet to be examined.  Do so now.  */
       test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
-      VARRAY_CHAR (stack_regs_mentioned_data, uid) = test;
+      VEC_replace (char, stack_regs_mentioned_data, uid, test);
     }
 
   return test == 1;
@@ -3031,7 +3040,8 @@ reg_to_stack (void)
   int max_uid;
 
   /* Clean up previous run.  */
-  stack_regs_mentioned_data = 0;
+  if (stack_regs_mentioned_data != NULL)
+    VEC_free (char, heap, stack_regs_mentioned_data);
 
   /* See if there is something to do.  Flow analysis is quite
      expensive so we might save some compilation time.  */
@@ -3114,8 +3124,9 @@ reg_to_stack (void)
 
   /* Allocate a cache for stack_regs_mentioned.  */
   max_uid = get_max_uid ();
-  VARRAY_CHAR_INIT (stack_regs_mentioned_data, max_uid + 1,
-		    "stack_regs_mentioned cache");
+  stack_regs_mentioned_data = VEC_alloc (char, heap, max_uid + 1);
+  memset (VEC_address (char, stack_regs_mentioned_data),
+	  0, sizeof (char) * max_uid + 1);
 
   convert_regs ();
 
@@ -3171,5 +3182,3 @@ struct tree_opt_pass pass_stack_regs =
   TODO_ggc_collect,                     /* todo_flags_finish */
   'k'                                   /* letter */
 };
-
-#include "gt-reg-stack.h"


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