On Thu, Dec 02, 2021 at 11:03:46AM -0500, Vladimir Makarov wrote: > --- 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; > + }
This will require that long long is at least twice as large as int everywhere, I thought you wanted to do that only when __builtin_smul_overflow isn't available. That would be #ifdef __has_builtin #if __has_builtin(__builtin_smul_overflow) #define HAS_SMUL_OVERFLOW #endif #endif #ifdef HAS_SMUL_OVERFLOW if (__builtin_smul_overflow (mult, diff, &priority) || priority <= -INT_MAX) priority = diff >= 0 ? INT_MAX : -INT_MAX; #else static_assert (sizeof (long long) >= 2 * sizeof (int), "overflow code does not work for int wider" "than half of long long"); long long priorityll = (long long) mult * diff; if (priorityll < -INT_MAX || priorityll > INT_MAX) priority = diff >= 0 ? INT_MAX : -INT_MAX; else priority = priorityll; #endif Why priority <= -INT_MAX in the first case though, shouldn't that be < -INT_MAX ? Jakub