public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] ira-color: fix allocno_priority_compare_func for qsort (PR 82395)
@ 2017-10-05 15:18 Alexander Monakov
  2017-10-19 10:51 ` Alexander Monakov
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2017-10-05 15:18 UTC (permalink / raw)
  To: gcc-patches; +Cc: Vladimir N. Makarov

Hello,

In ira-color.c, qsort comparator allocno_priority_compare_func lacks anti-
commutativity and can indicate A < B < A if boths allocnos satisfy
non_spilled_static_chain_regno_p.  It should fall down to following
sub-comparisons in that case.

There is another issue: the comment doesn't match the code.  We are sorting
allocnos by priority, higher first, and the comment says that allocnos
corresponding to static chain pointer register should go first. However,
the code implements the opposite ordering.

I think the comment documents the intended behavior and the code is wrong.
Thus, the patch changes comparison value to match the comment.

A similar issue was present in lra-assigns.c and was fixed by this patch:
https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00893.html

Bootstrapped and regtested on x86-64, OK for trunk?

Thanks.
Alexander

	PR rtl-optimization/82395
	* ira-color.c (allocno_priority_compare_func): Fix comparison step
	based on non_spilled_static_chain_regno_p.

diff --git a/gcc/ira-color.c b/gcc/ira-color.c
index 22fdb88274d..31a4a8074d1 100644
--- a/gcc/ira-color.c
+++ b/gcc/ira-color.c
@@ -3005,14 +3005,13 @@ allocno_priority_compare_func (const void *v1p, const void *v2p)
 {
   ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
   ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
-  int pri1, pri2;
+  int pri1, pri2, diff;
 
   /* Assign hard reg to static chain pointer pseudo first when
      non-local goto is used.  */
-  if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
-    return 1;
-  else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
-    return -1;
+  if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
+	       - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
+    return diff;
   pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
   pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
   if (pri2 != pri1)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ira-color: fix allocno_priority_compare_func for qsort (PR 82395)
  2017-10-05 15:18 [PATCH] ira-color: fix allocno_priority_compare_func for qsort (PR 82395) Alexander Monakov
@ 2017-10-19 10:51 ` Alexander Monakov
  2017-10-19 16:13   ` Vladimir Makarov
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2017-10-19 10:51 UTC (permalink / raw)
  To: gcc-patches; +Cc: Vladimir N. Makarov

Ping.

On Thu, 5 Oct 2017, Alexander Monakov wrote:
> In ira-color.c, qsort comparator allocno_priority_compare_func lacks anti-
> commutativity and can indicate A < B < A if boths allocnos satisfy
> non_spilled_static_chain_regno_p.  It should fall down to following
> sub-comparisons in that case.
> 
> There is another issue: the comment doesn't match the code.  We are sorting
> allocnos by priority, higher first, and the comment says that allocnos
> corresponding to static chain pointer register should go first. However,
> the code implements the opposite ordering.
> 
> I think the comment documents the intended behavior and the code is wrong.
> Thus, the patch changes comparison value to match the comment.
> 
> A similar issue was present in lra-assigns.c and was fixed by this patch:
> https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00893.html
> 
> Bootstrapped and regtested on x86-64, OK for trunk?
> 
> Thanks.
> Alexander
> 
> 	PR rtl-optimization/82395
> 	* ira-color.c (allocno_priority_compare_func): Fix comparison step
> 	based on non_spilled_static_chain_regno_p.
> 
> diff --git a/gcc/ira-color.c b/gcc/ira-color.c
> index 22fdb88274d..31a4a8074d1 100644
> --- a/gcc/ira-color.c
> +++ b/gcc/ira-color.c
> @@ -3005,14 +3005,13 @@ allocno_priority_compare_func (const void *v1p, const void *v2p)
>  {
>    ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
>    ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
> -  int pri1, pri2;
> +  int pri1, pri2, diff;
>  
>    /* Assign hard reg to static chain pointer pseudo first when
>       non-local goto is used.  */
> -  if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
> -    return 1;
> -  else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
> -    return -1;
> +  if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
> +	       - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
> +    return diff;
>    pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
>    pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
>    if (pri2 != pri1)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ira-color: fix allocno_priority_compare_func for qsort (PR 82395)
  2017-10-19 10:51 ` Alexander Monakov
@ 2017-10-19 16:13   ` Vladimir Makarov
  0 siblings, 0 replies; 3+ messages in thread
From: Vladimir Makarov @ 2017-10-19 16:13 UTC (permalink / raw)
  To: Alexander Monakov, gcc-patches

On 10/19/2017 06:37 AM, Alexander Monakov wrote:
> Ping.
>
> On Thu, 5 Oct 2017, Alexander Monakov wrote:
> Bootstrapped and regtested on x86-64, OK for trunk?
>
OK.  Alexander, sorry for the delay with the answer and thank you for 
finding and fixing the problem.
> 	PR rtl-optimization/82395
> 	* ira-color.c (allocno_priority_compare_func): Fix comparison step
> 	based on non_spilled_static_chain_regno_p.
>
> diff --git a/gcc/ira-color.c b/gcc/ira-color.c
> index 22fdb88274d..31a4a8074d1 100644
> --- a/gcc/ira-color.c
> +++ b/gcc/ira-color.c
> @@ -3005,14 +3005,13 @@ allocno_priority_compare_func (const void *v1p, const void *v2p)
>   {
>     ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
>     ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
> -  int pri1, pri2;
> +  int pri1, pri2, diff;
>   
>     /* Assign hard reg to static chain pointer pseudo first when
>        non-local goto is used.  */
> -  if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
> -    return 1;
> -  else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
> -    return -1;
> +  if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
> +	       - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
> +    return diff;
>     pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
>     pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
>     if (pri2 != pri1)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-10-19 15:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-05 15:18 [PATCH] ira-color: fix allocno_priority_compare_func for qsort (PR 82395) Alexander Monakov
2017-10-19 10:51 ` Alexander Monakov
2017-10-19 16:13   ` Vladimir Makarov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).