public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-5717] [PR103437] Use long long multiplication as backup for overflow processing
@ 2021-12-02 16:01 Vladimir Makarov
  0 siblings, 0 replies; only message in thread
From: Vladimir Makarov @ 2021-12-02 16:01 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:7d02c8bf75980fa2468f4167a82dd3a619e35cb4

commit r12-5717-g7d02c8bf75980fa2468f4167a82dd3a619e35cb4
Author: Vladimir N. Makarov <vmakarov@redhat.com>
Date:   Thu Dec 2 10:55:59 2021 -0500

    [PR103437] Use long long multiplication as backup for overflow processing
    
    __builtin_smul_overflow can be unavailable for some C++ compilers.
    Add long long multiplication as backup for overflow processing.
    
    gcc/ChangeLog:
            PR rtl-optimization/103437
            * ira-color.c (setup_allocno_priorities): Use long long
            multiplication as backup for overflow processing.

Diff:
---
 gcc/ira-color.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/gcc/ira-color.c b/gcc/ira-color.c
index 1f80cbea0e2..3b19a58e1f0 100644
--- a/gcc/ira-color.c
+++ b/gcc/ira-color.c
@@ -2797,6 +2797,7 @@ static void
 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
 {
   int i, length, nrefs, priority, max_priority, mult, diff;
+  bool overflow_backup_p = true;
   ira_allocno_t a;
 
   max_priority = 0;
@@ -2811,9 +2812,25 @@ setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
       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: */
+#ifdef __has_builtin
+#if __has_builtin(__builtin_smul_overflow)
+      overflow_backup_p = false;
       if (__builtin_smul_overflow (mult, diff, &priority)
 	  || priority <= -INT_MAX)
 	priority = diff >= 0 ? INT_MAX : -INT_MAX;
+#endif
+#endif
+      if (overflow_backup_p)
+	{
+	  static_assert
+	    (sizeof (long long) >= 2 * sizeof (int),
+	     "overflow code does not work for such int and long long sizes");
+	  long long priorityll = (long long) mult * diff;
+	  if (priorityll < -INT_MAX || priorityll > INT_MAX)
+	    priority = diff >= 0 ? INT_MAX : -INT_MAX;
+	  else
+	    priority = priorityll;
+	}
       allocno_priorities[ALLOCNO_NUM (a)] = priority;
       if (priority < 0)
 	priority = -priority;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-02 16:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02 16:01 [gcc r12-5717] [PR103437] Use long long multiplication as backup for overflow processing 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).