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]

Re: RFA: GCC 4.2.1: Stabalizing coalesce_list's qsort


Hi Richard,

Since stabilizing the sort would slow it down, I thought it best to check to
make sure that it is still OK to apply the patch.  ie are we preferring host
independence over compile speed ?

Yes. I don't think the slowdown will be noticable.

OK, I have checked in the attached version of the patch.


Cheers
  Nick

gcc/ChangeLog
2007-08-31  Nick Clifton  <nickc@redhat.com>

	* tree-ssa-coalesce.c (compare_pairs): Use the elements as
	secondary keys in order to obtain a stable sort.

Index: gcc/tree-ssa-coalesce.c
===================================================================
--- gcc/tree-ssa-coalesce.c	(revision 127921)
+++ gcc/tree-ssa-coalesce.c	(working copy)
@@ -314,8 +314,22 @@ add_coalesce (coalesce_list_p cl, int p1
 static int 
 compare_pairs (const void *p1, const void *p2)
 {
-  return (*(const_coalesce_pair_p const*)p1)->cost
-    - (*(const_coalesce_pair_p const*)p2)->cost;
+  const_coalesce_pair_p const * pp1 = p1;
+  const_coalesce_pair_p const * pp2 = p2;
+  int result;
+
+  result = (* pp2)->cost - (* pp1)->cost;
+  /* Since qsort does not guarantee stability we use the elements
+     as a secondary key.  This provides us with independence from
+     the host's implementation of the sorting algorithm.  */
+  if (result == 0)
+    {
+      result = (* pp2)->first_element - (* pp1)->first_element;
+      if (result == 0)
+	result = (* pp2)->second_element - (* pp1)->second_element;
+    }
+
+  return result;
 }
 
 

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