[2/2] Add assert for type on tree_nonzero_bits

Message ID 1667425595-2654-3-git-send-email-apinski@marvell.com
State New
Headers
Series tree_nonzero_bits vs vector and complex types |

Commit Message

Li, Pan2 via Gcc-patches Nov. 2, 2022, 9:46 p.m. UTC
  From: Andrew Pinski <apinski@marvell.com>

Right now anyone could call tree_nonzero_bits with
either complex or vector types and this will return
the wrong thing. So just assert that nobody calls
it with this.

OK? Bootstrapped and tested with no regressions on x86_64-linux-gnu.

gcc/ChangeLog:

	* fold-const.cc (tree_nonzero_bits): Add
	assert.
---
 gcc/fold-const.cc | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Richard Biener Nov. 5, 2022, 11:47 a.m. UTC | #1
On Wed, Nov 2, 2022 at 10:48 PM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> Right now anyone could call tree_nonzero_bits with
> either complex or vector types and this will return
> the wrong thing. So just assert that nobody calls
> it with this.
>
> OK? Bootstrapped and tested with no regressions on x86_64-linux-gnu.

The function comment needs adjustment.  To me, nonzero bits of
a v4si would simply include all bits of v4si - isn't that what happens?
I see the SSA get_nonzero_bits uses element_precision but
tree_nonzero_bits uses TYPE_PRECISON as fallback, so I guess
the assert is correct.

Still the function comment should be amended, also ...

> gcc/ChangeLog:
>
>         * fold-const.cc (tree_nonzero_bits): Add
>         assert.
> ---
>  gcc/fold-const.cc | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
> index 7e1ea58518b..3ccac9b28df 100644
> --- a/gcc/fold-const.cc
> +++ b/gcc/fold-const.cc
> @@ -16567,6 +16567,9 @@ c_getstr (tree str)
>  wide_int
>  tree_nonzero_bits (const_tree t)
>  {
> +  gcc_assert (TREE_CODE (TREE_TYPE (t)) != VECTOR_TYPE
> +             && TREE_CODE (TREE_TYPE (t)) != COMPLEX_TYPE);

... please perform a "positive" test.  Like INTEGRAL_TYPE_P (TREE_TYPE
(t)) || POINTER_TYPE_P (TREE_TYPE (t))

Richard.

> +
>    switch (TREE_CODE (t))
>      {
>      case INTEGER_CST:
> --
> 2.17.1
>
  

Patch

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 7e1ea58518b..3ccac9b28df 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -16567,6 +16567,9 @@  c_getstr (tree str)
 wide_int
 tree_nonzero_bits (const_tree t)
 {
+  gcc_assert (TREE_CODE (TREE_TYPE (t)) != VECTOR_TYPE
+	      && TREE_CODE (TREE_TYPE (t)) != COMPLEX_TYPE);
+
   switch (TREE_CODE (t))
     {
     case INTEGER_CST: