public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
@ 2023-02-03  8:50 Aldy Hernandez
  2023-02-03  9:16 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Aldy Hernandez @ 2023-02-03  8:50 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Andrew MacLeod, GCC patches, Aldy Hernandez

The problem here is we are trying to compare two ranges with different
precisions and the == operator in wide_int is complaining.

Interestingly, the problem is not the nonzero bits, but the fact that
the entire ranges have different precisions.  The reason we don't ICE
when comparing the sub-ranges, is because the code in
irange::operator== works on trees, and tree_int_cst_equal is
promoting the comparison to a widest int:

  if (TREE_CODE (t1) == INTEGER_CST
      && TREE_CODE (t2) == INTEGER_CST
      && wi::to_widest (t1) == wi::to_widest (t2))
    return 1;

This is why we don't see the ICE until the nonzero bits comparison is
done on wide ints.  I think we should maintain the current equality
behavior, and follow suit in the nonzero bit comparison.

I have also fixed the legacy equality code, even though technically
nonzero bits shouldn't appear in legacy.  But better safe than sorry.

	PR 108639/tree-optimization

Re-running tests with Jakub's testcases for both PR108638 and PR108639.

OK pending tests?

gcc/ChangeLog:

	* value-range.cc (irange::legacy_equal_p): Compare nonzero bits as
	widest_int.
	(irange::operator==): Same.
---
 gcc/testsuite/gcc.c-torture/compile/pr108638.c | 12 ++++++++++++
 gcc/testsuite/gcc.c-torture/compile/pr108639.c | 11 +++++++++++
 gcc/value-range.cc                             | 11 +++++++++--
 3 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr108638.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr108639.c

diff --git a/gcc/testsuite/gcc.c-torture/compile/pr108638.c b/gcc/testsuite/gcc.c-torture/compile/pr108638.c
new file mode 100644
index 00000000000..755c151a09a
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr108638.c
@@ -0,0 +1,12 @@
+/* PR tree-optimization/108638 */
+
+long long a;
+int b;
+
+void
+foo (void)
+{
+  for (a = 0; a < __SIZEOF_LONG_LONG__ * __CHAR_BIT__; a++)
+    if (b)
+      b |= a << a;
+}
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr108639.c b/gcc/testsuite/gcc.c-torture/compile/pr108639.c
new file mode 100644
index 00000000000..ed826cc2f5a
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr108639.c
@@ -0,0 +1,11 @@
+/* PR tree-optimization/108639 */
+
+long long a;
+
+int
+main ()
+{
+  a = a ? 0 || 0 % 0 : 0;
+  a = a << a;
+  return 0;
+}
diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index 26f6f26b01a..a535337c47a 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const
 			       other.tree_lower_bound (0))
 	  && vrp_operand_equal_p (tree_upper_bound (0),
 				  other.tree_upper_bound (0))
-	  && get_nonzero_bits () == other.get_nonzero_bits ());
+	  && (widest_int::from (get_nonzero_bits (),
+				TYPE_SIGN (type ()))
+	      == widest_int::from (other.get_nonzero_bits (),
+				   TYPE_SIGN (other.type ()))));
 }
 
 bool
@@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const
 	  || !operand_equal_p (ub, ub_other, 0))
 	return false;
     }
-  return get_nonzero_bits () == other.get_nonzero_bits ();
+  widest_int nz1 = widest_int::from (get_nonzero_bits (),
+				     TYPE_SIGN (type ()));
+  widest_int nz2 = widest_int::from (other.get_nonzero_bits (),
+				     TYPE_SIGN (other.type ()));
+  return nz1 == nz2;
 }
 
 /* Return TRUE if this is a symbolic range.  */
-- 
2.39.1


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

* Re: [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
  2023-02-03  8:50 [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int Aldy Hernandez
@ 2023-02-03  9:16 ` Jakub Jelinek
  2023-02-03 16:23   ` Andrew MacLeod
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2023-02-03  9:16 UTC (permalink / raw)
  To: Aldy Hernandez; +Cc: Andrew MacLeod, GCC patches

On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote:
> [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.

0 missing in the bug number in the subject line, though the current
recommended formatting of the subject is I think:
value-range: Compare nonzero bits in irange with widest_int [PR180639]
                                                                                                                                                                                      
        PR 108639/tree-optimization

Reversed component and number

> --- a/gcc/value-range.cc
> +++ b/gcc/value-range.cc
> @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const
>  			       other.tree_lower_bound (0))
>  	  && vrp_operand_equal_p (tree_upper_bound (0),
>  				  other.tree_upper_bound (0))
> -	  && get_nonzero_bits () == other.get_nonzero_bits ());
> +	  && (widest_int::from (get_nonzero_bits (),
> +				TYPE_SIGN (type ()))
> +	      == widest_int::from (other.get_nonzero_bits (),
> +				   TYPE_SIGN (other.type ()))));
>  }
>  
>  bool
> @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const
>  	  || !operand_equal_p (ub, ub_other, 0))
>  	return false;
>      }
> -  return get_nonzero_bits () == other.get_nonzero_bits ();
> +  widest_int nz1 = widest_int::from (get_nonzero_bits (),
> +				     TYPE_SIGN (type ()));
> +  widest_int nz2 = widest_int::from (other.get_nonzero_bits (),
> +				     TYPE_SIGN (other.type ()));
> +  return nz1 == nz2;
>  }

While the above avoids the ICE (and would be certainly correct for
the bounds, depending on the sign of their type sign or zero extended
to widest int), but is the above what we want for non-zero bits
to be considered equal?  The wide_ints (which ought to have precision
of the corresponding type) don't represent normal numbers but bitmasks,
0 - this bit is known to be zero, 1 - nothing is known about this bit).
So, if there are different precisions and the narrower value has 0
in the MSB of the bitmask (so MSB is known to be zero), the above requires
for equality that in the other range all upper bits are known to be zero
too for both signed and unsigned.  That is ok.  Similarly for MSB set
if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we
require on the wider to have all the upper bits cleared.  But for signed
narrower type with MSB set, i.e. it is unknown if it is positive or
negative, the above requires that all the above bits are unknown too.
And that is the case I'm not sure about, whether in that case the
upper bits of the wider wide_int should be checked at all.
Though, perhaps from the POV of nonzero bits derived from the sign-extended
values in the ranges sign bit copies (so all above bits 1) is what one would
get, so maybe it is ok.  Just food for thought.

As for retesting, if you have done full bootstrap/regtest with the patch
without the testcases in it, it should be more than enough to test just
make check-gcc \
RUNTESTFLAGS='--target_board=unix\{-m32,-m64\} compile.exp=pr10863*.c'
You don't really need to rerun all tests just for it.

	Jakub


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

* Re: [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
  2023-02-03  9:16 ` Jakub Jelinek
@ 2023-02-03 16:23   ` Andrew MacLeod
  2023-02-03 16:25     ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew MacLeod @ 2023-02-03 16:23 UTC (permalink / raw)
  To: Jakub Jelinek, Aldy Hernandez; +Cc: GCC patches


On 2/3/23 04:16, Jakub Jelinek wrote:
> On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote:
>> [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
> 0 missing in the bug number in the subject line, though the current
> recommended formatting of the subject is I think:
> value-range: Compare nonzero bits in irange with widest_int [PR180639]
>                                                                                                                                                                                        
>          PR 108639/tree-optimization
>
> Reversed component and number
>
>> --- a/gcc/value-range.cc
>> +++ b/gcc/value-range.cc
>> @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const
>>   			       other.tree_lower_bound (0))
>>   	  && vrp_operand_equal_p (tree_upper_bound (0),
>>   				  other.tree_upper_bound (0))
>> -	  && get_nonzero_bits () == other.get_nonzero_bits ());
>> +	  && (widest_int::from (get_nonzero_bits (),
>> +				TYPE_SIGN (type ()))
>> +	      == widest_int::from (other.get_nonzero_bits (),
>> +				   TYPE_SIGN (other.type ()))));
>>   }
>>   
>>   bool
>> @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const
>>   	  || !operand_equal_p (ub, ub_other, 0))
>>   	return false;
>>       }
>> -  return get_nonzero_bits () == other.get_nonzero_bits ();
>> +  widest_int nz1 = widest_int::from (get_nonzero_bits (),
>> +				     TYPE_SIGN (type ()));
>> +  widest_int nz2 = widest_int::from (other.get_nonzero_bits (),
>> +				     TYPE_SIGN (other.type ()));
>> +  return nz1 == nz2;
>>   }
> While the above avoids the ICE (and would be certainly correct for
> the bounds, depending on the sign of their type sign or zero extended
> to widest int), but is the above what we want for non-zero bits
> to be considered equal?  The wide_ints (which ought to have precision
> of the corresponding type) don't represent normal numbers but bitmasks,
> 0 - this bit is known to be zero, 1 - nothing is known about this bit).
> So, if there are different precisions and the narrower value has 0
> in the MSB of the bitmask (so MSB is known to be zero), the above requires
> for equality that in the other range all upper bits are known to be zero
> too for both signed and unsigned.  That is ok.  Similarly for MSB set
> if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we
> require on the wider to have all the upper bits cleared.  But for signed
> narrower type with MSB set, i.e. it is unknown if it is positive or
> negative, the above requires that all the above bits are unknown too.
> And that is the case I'm not sure about, whether in that case the
> upper bits of the wider wide_int should be checked at all.
> Though, perhaps from the POV of nonzero bits derived from the sign-extended
> values in the ranges sign bit copies (so all above bits 1) is what one would
> get, so maybe it is ok.  Just food for thought.
>
if the bits match exactly along with everything else, then we can be 
sure the ranges are truly equal.  If for some reason the numbers are all 
the same but the non-zero bits don't compare equal,  then I can't think 
of what harm it could cause to compare unequal..  Worst case is we dont 
perform some optimization in this extremely rare scenario of differing 
precisions.  And in fact they could actually be unequal...

So I suspect this is fine...

Andrew



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

* Re: [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
  2023-02-03 16:23   ` Andrew MacLeod
@ 2023-02-03 16:25     ` Jakub Jelinek
  0 siblings, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2023-02-03 16:25 UTC (permalink / raw)
  To: Andrew MacLeod; +Cc: Aldy Hernandez, GCC patches

On Fri, Feb 03, 2023 at 11:23:28AM -0500, Andrew MacLeod wrote:
> 
> On 2/3/23 04:16, Jakub Jelinek wrote:
> > On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote:
> > > [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int.
> > 0 missing in the bug number in the subject line, though the current
> > recommended formatting of the subject is I think:
> > value-range: Compare nonzero bits in irange with widest_int [PR180639]
> >          PR 108639/tree-optimization
> > 
> > Reversed component and number
> > 
> > > --- a/gcc/value-range.cc
> > > +++ b/gcc/value-range.cc
> > > @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const
> > >   			       other.tree_lower_bound (0))
> > >   	  && vrp_operand_equal_p (tree_upper_bound (0),
> > >   				  other.tree_upper_bound (0))
> > > -	  && get_nonzero_bits () == other.get_nonzero_bits ());
> > > +	  && (widest_int::from (get_nonzero_bits (),
> > > +				TYPE_SIGN (type ()))
> > > +	      == widest_int::from (other.get_nonzero_bits (),
> > > +				   TYPE_SIGN (other.type ()))));
> > >   }
> > >   bool
> > > @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const
> > >   	  || !operand_equal_p (ub, ub_other, 0))
> > >   	return false;
> > >       }
> > > -  return get_nonzero_bits () == other.get_nonzero_bits ();
> > > +  widest_int nz1 = widest_int::from (get_nonzero_bits (),
> > > +				     TYPE_SIGN (type ()));
> > > +  widest_int nz2 = widest_int::from (other.get_nonzero_bits (),
> > > +				     TYPE_SIGN (other.type ()));
> > > +  return nz1 == nz2;
> > >   }
> > While the above avoids the ICE (and would be certainly correct for
> > the bounds, depending on the sign of their type sign or zero extended
> > to widest int), but is the above what we want for non-zero bits
> > to be considered equal?  The wide_ints (which ought to have precision
> > of the corresponding type) don't represent normal numbers but bitmasks,
> > 0 - this bit is known to be zero, 1 - nothing is known about this bit).
> > So, if there are different precisions and the narrower value has 0
> > in the MSB of the bitmask (so MSB is known to be zero), the above requires
> > for equality that in the other range all upper bits are known to be zero
> > too for both signed and unsigned.  That is ok.  Similarly for MSB set
> > if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we
> > require on the wider to have all the upper bits cleared.  But for signed
> > narrower type with MSB set, i.e. it is unknown if it is positive or
> > negative, the above requires that all the above bits are unknown too.
> > And that is the case I'm not sure about, whether in that case the
> > upper bits of the wider wide_int should be checked at all.
> > Though, perhaps from the POV of nonzero bits derived from the sign-extended
> > values in the ranges sign bit copies (so all above bits 1) is what one would
> > get, so maybe it is ok.  Just food for thought.
> > 
> if the bits match exactly along with everything else, then we can be sure
> the ranges are truly equal.  If for some reason the numbers are all the same
> but the non-zero bits don't compare equal,  then I can't think of what harm
> it could cause to compare unequal..  Worst case is we dont perform some
> optimization in this extremely rare scenario of differing precisions.  And
> in fact they could actually be unequal...
> 
> So I suspect this is fine...

Ok then.

	Jakub


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

end of thread, other threads:[~2023-02-03 20:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-03  8:50 [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int Aldy Hernandez
2023-02-03  9:16 ` Jakub Jelinek
2023-02-03 16:23   ` Andrew MacLeod
2023-02-03 16:25     ` Jakub Jelinek

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).