public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix tree-optimization/103314 : Limit folding of (type) X op CST where type is a nop convert to gimple
@ 2021-11-19  6:33 apinski
  2021-11-19  9:27 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: apinski @ 2021-11-19  6:33 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

There is some re-association code in fold_binary which conflicts with
this optimization due keeping around some "constants" which are not
INTEGER_CST (1 << -1) so we end up in an infinite loop because of that.
So we need to limit this case to GIMPLE level only.

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

	PR tree-optimization/103314

gcc/ChangeLog:

	* match.pd ((type) X op CST): Restrict the equal
	TYPE_PRECISION case to GIMPLE only.

gcc/testsuite/ChangeLog:

	* gcc.c-torture/compile/pr103314-1.c: New test.
---
 gcc/match.pd                                     | 6 +++++-
 gcc/testsuite/gcc.c-torture/compile/pr103314-1.c | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr103314-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 4dc66fb47f2..24a84e3b504 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1619,7 +1619,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	      after hoisting the conversion the operation will be narrower.
 	      It is also a good if the conversion is a nop as moves the
 	      conversion to one side; allowing for combining of the conversions.  */
-	   TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
+	   TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
+	   /* The conversion check for being a nop can only be done at the gimple
+	      level as fold_binary has some re-association code which can conflict
+	      with this if there is a "constant" which is not a full INTEGER_CST.  */
+	   || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
 	   /* It's also a good idea if the conversion is to a non-integer
 	      mode.  */
 	   || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c b/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c
new file mode 100644
index 00000000000..f4a63130421
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c
@@ -0,0 +1,6 @@
+/* { dg-options "" } */
+int main() {
+  int t = 1;
+  unsigned c = 0, d1 = t ? 1 ^ c ^ 1 >> (-1) : 0; /* { dg-warning "is negative"  } */
+  return d1;
+}
-- 
2.17.1


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

* Re: [PATCH] Fix tree-optimization/103314 : Limit folding of (type) X op CST where type is a nop convert to gimple
  2021-11-19  6:33 [PATCH] Fix tree-optimization/103314 : Limit folding of (type) X op CST where type is a nop convert to gimple apinski
@ 2021-11-19  9:27 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2021-11-19  9:27 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

On Fri, Nov 19, 2021 at 7:34 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> There is some re-association code in fold_binary which conflicts with
> this optimization due keeping around some "constants" which are not
> INTEGER_CST (1 << -1) so we end up in an infinite loop because of that.
> So we need to limit this case to GIMPLE level only.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

Richard.

>         PR tree-optimization/103314
>
> gcc/ChangeLog:
>
>         * match.pd ((type) X op CST): Restrict the equal
>         TYPE_PRECISION case to GIMPLE only.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/compile/pr103314-1.c: New test.
> ---
>  gcc/match.pd                                     | 6 +++++-
>  gcc/testsuite/gcc.c-torture/compile/pr103314-1.c | 6 ++++++
>  2 files changed, 11 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr103314-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 4dc66fb47f2..24a84e3b504 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1619,7 +1619,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>               after hoisting the conversion the operation will be narrower.
>               It is also a good if the conversion is a nop as moves the
>               conversion to one side; allowing for combining of the conversions.  */
> -          TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
> +          TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
> +          /* The conversion check for being a nop can only be done at the gimple
> +             level as fold_binary has some re-association code which can conflict
> +             with this if there is a "constant" which is not a full INTEGER_CST.  */
> +          || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
>            /* It's also a good idea if the conversion is to a non-integer
>               mode.  */
>            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
> diff --git a/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c b/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c
> new file mode 100644
> index 00000000000..f4a63130421
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/compile/pr103314-1.c
> @@ -0,0 +1,6 @@
> +/* { dg-options "" } */
> +int main() {
> +  int t = 1;
> +  unsigned c = 0, d1 = t ? 1 ^ c ^ 1 >> (-1) : 0; /* { dg-warning "is negative"  } */
> +  return d1;
> +}
> --
> 2.17.1
>

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

end of thread, other threads:[~2021-11-19  9:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-19  6:33 [PATCH] Fix tree-optimization/103314 : Limit folding of (type) X op CST where type is a nop convert to gimple apinski
2021-11-19  9:27 ` Richard Biener

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