public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PR103437] [committed] IRA: Process multiplication overflow in priority calculation for allocno assignments
@ 2021-12-02 13:53 Vladimir Makarov
  2021-12-02 14:00 ` Jakub Jelinek
  0 siblings, 1 reply; 11+ messages in thread
From: Vladimir Makarov @ 2021-12-02 13:53 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 233 bytes --]

The following patch fixes

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103437

The patch was successfully bootstrapped and tested on x86-64. There is 
no test as the bug occurs on GCC built with sanitizing for an existing 
go test.

[-- Attachment #2: pr103437.patch --]
[-- Type: text/x-patch, Size: 1970 bytes --]

commit c6cf5ac1522c54b2ced98fc687e973a9ff17ba1e
Author: Vladimir N. Makarov <vmakarov@redhat.com>
Date:   Thu Dec 2 08:29:45 2021 -0500

    [PR103437] Process multiplication overflow in priority calculation for allocno assignments
    
    We process overflows in cost calculations but for huge functions
    priority calculation can overflow as priority can be bigger the cost
    used for it.  The patch fixes the problem.
    
    gcc/ChangeLog:
    
            PR rtl-optimization/103437
            * ira-color.c (setup_allocno_priorities): Process multiplication
            overflow.

diff --git a/gcc/ira-color.c b/gcc/ira-color.c
index 3d01c60800c..1f80cbea0e2 100644
--- a/gcc/ira-color.c
+++ b/gcc/ira-color.c
@@ -2796,7 +2796,7 @@ static int *allocno_priorities;
 static void
 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
 {
-  int i, length, nrefs, priority, max_priority, mult;
+  int i, length, nrefs, priority, max_priority, mult, diff;
   ira_allocno_t a;
 
   max_priority = 0;
@@ -2807,11 +2807,14 @@ setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
       ira_assert (nrefs >= 0);
       mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
       ira_assert (mult >= 0);
-      allocno_priorities[ALLOCNO_NUM (a)]
-	= priority
-	= (mult
-	   * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
-	   * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
+      mult *= ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
+      diff = ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
+      /* Multiplication can overflow for very large functions.
+	 Check the overflow and constrain the result if necessary: */
+      if (__builtin_smul_overflow (mult, diff, &priority)
+	  || priority <= -INT_MAX)
+	priority = diff >= 0 ? INT_MAX : -INT_MAX;
+      allocno_priorities[ALLOCNO_NUM (a)] = priority;
       if (priority < 0)
 	priority = -priority;
       if (max_priority < priority)

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

end of thread, other threads:[~2021-12-02 17:44 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02 13:53 [PR103437] [committed] IRA: Process multiplication overflow in priority calculation for allocno assignments Vladimir Makarov
2021-12-02 14:00 ` Jakub Jelinek
2021-12-02 14:23   ` Vladimir Makarov
2021-12-02 14:29     ` Jakub Jelinek
2021-12-02 14:38       ` Vladimir Makarov
2021-12-02 15:52         ` Christophe Lyon
2021-12-02 16:03           ` Vladimir Makarov
2021-12-02 16:13             ` Jakub Jelinek
2021-12-02 17:06               ` Vladimir Makarov
2021-12-02 17:21                 ` Vladimir Makarov
2021-12-02 17:44                   ` 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).