public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/98956 Optimizing out boolean left shift
@ 2021-11-30 16:34 Navid Rahimi
  2021-11-30 22:03 ` Andrew Pinski
  0 siblings, 1 reply; 6+ messages in thread
From: Navid Rahimi @ 2021-11-30 16:34 UTC (permalink / raw)
  To: Navid Rahimi via Gcc-patches

[-- Attachment #1: Type: text/plain, Size: 694 bytes --]

Hi GCC community,

This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.

Tree-optimization/98956:

Adding new optimization to match.pd:
                * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
                * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
                * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.

1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98956
2) https://compiler-explorer.com/z/nj4PTrecW
3) https://alive2.llvm.org/ce/z/jyJAoS

Best wishes,
Navid.

[-- Attachment #2: 0001-Tree-optimization-98956.patch --]
[-- Type: application/octet-stream, Size: 2973 bytes --]

From cae2ada74487761e6dfd285aa9340308ba659e98 Mon Sep 17 00:00:00 2001
From: Navid Rahimi <navidrahimi@microsoft.com>
Date: Tue, 16 Nov 2021 21:31:21 -0800
Subject: [PATCH] Tree-optimization/98956:

Adding new optimization to match.pd:
            * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
            * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
            * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
---
 gcc/match.pd                              |  9 +++++
 gcc/testsuite/gcc.dg/tree-ssa/pr98956-2.c | 30 +++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr98956.c   | 41 +++++++++++++++++++++++
 3 files changed, 80 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr98956-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr98956.c

diff --git a/gcc/match.pd b/gcc/match.pd
index a319aefa808..e9afc625f32 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4449,6 +4449,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  )
 )
 
+/* cmp : ==, != */
+/* ((B0 << x) cmp 0) -> B0 cmp 0 */
+(for cmp (eq ne)
+ (simplify
+  (cmp (lshift (convert@3 truth_valued_p@0) @1) integer_zerop@2)
+   (if (TREE_CODE (TREE_TYPE (@3)) == INTEGER_TYPE
+       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
+    (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
+
 /* -(type)!A -> (type)A - 1.  */
 (simplify
  (negate (convert?:s (logical_inverted_value:s @0)))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr98956-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr98956-2.c
new file mode 100644
index 00000000000..0fe1d4836a8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr98956-2.c
@@ -0,0 +1,30 @@
+/* PR tree-optimization/98956 */
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times "func_side_effect" 3 "optimized" } } */
+
+#include <stdbool.h>
+
+extern int func_side_effect ();
+
+bool
+k (bool c)
+{
+  bool v = (c << func_side_effect ());
+  return v;
+}
+
+bool
+k_convert (bool c)
+{
+  bool v = (bool)(c << func_side_effect ());
+  return v;
+}
+
+bool
+kk (bool c)
+{
+  bool v = !(c << func_side_effect ());
+  return v;
+}
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr98956.c b/gcc/testsuite/gcc.dg/tree-ssa/pr98956.c
new file mode 100644
index 00000000000..3bd989d3d08
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr98956.c
@@ -0,0 +1,41 @@
+/* PR tree-optimization/98956 */
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-not " << " "optimized" } } */
+
+#include <stdbool.h>
+
+bool g(bool c)
+{
+    return (c << 22);
+}
+
+bool h(bool c)
+{
+    return (c << 23);
+}
+
+bool i(bool c)
+{
+    return !(c << 11);
+}
+
+bool j(bool c)
+{
+    return !(c << 4);
+}
+
+bool k(bool c, int i)
+{
+    return !(c << i);
+}
+
+bool l(bool c, long i)
+{
+    return !(c << i);
+}
+
+bool convert (bool c, int i) {
+  bool v = (bool)(c << i);
+  return v;
+}
\ No newline at end of file
-- 
2.25.1


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

* Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
  2021-11-30 16:34 [PATCH] tree-optimization/98956 Optimizing out boolean left shift Navid Rahimi
@ 2021-11-30 22:03 ` Andrew Pinski
  2021-11-30 23:08   ` [EXTERNAL] " Navid Rahimi
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Pinski @ 2021-11-30 22:03 UTC (permalink / raw)
  To: Navid Rahimi; +Cc: Navid Rahimi via Gcc-patches

On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Hi GCC community,
>
> This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.
>

A better way to optimize this is the following (which I describe in PR 64992):
 take: (t << 1) != 0;

This should be transformed into:
(t & 0x7fffffff) != 0

The rest will just fall out really.  That is there is no reason to
special case bool here.
I have most of the patch except for creating the mask part which
should be simple, I just did not want to look up the wi:: functions at
the time I was writing it into the bug report.

Thanks,
Andrew Pinski



> Tree-optimization/98956:
>
> Adding new optimization to match.pd:
>                 * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
>                 * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
>                 * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
>
> 1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98956
> 2) https://compiler-explorer.com/z/nj4PTrecW
> 3) https://alive2.llvm.org/ce/z/jyJAoS
>
> Best wishes,
> Navid.

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

* Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
  2021-11-30 22:03 ` Andrew Pinski
@ 2021-11-30 23:08   ` Navid Rahimi
  2021-11-30 23:18     ` Andrew Pinski
  0 siblings, 1 reply; 6+ messages in thread
From: Navid Rahimi @ 2021-11-30 23:08 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Navid Rahimi via Gcc-patches

Hi Andrew,

Thanks for your detailed comment. There are two problem I wanted to discuss with you about:

a) The optimization I have sent patch, does optimize variable length "<<" too(for example B0 << x, where x is variable). This [1] link shows the actual optimization and a link for the proof is included in the editor.

b) I am unable to prove the optimization you are describing for non-constant length shift. You can take a look at the code example [2] and proof [3]. I am getting "Transformation doesn't verify!" when I do implement the optimization you mentioned for non-constant shift.

The optimization you are describing only works for "(take: (t << 1) != 0) -> ((t & 0x7fffffff) != 0)" which only is provable and works for INTEGER_CST.

My understanding might be incorrect here, please don't hesitate to correct me.

1) https://compiler-explorer.com/z/r46znh4Tj
2) https://compiler-explorer.com/z/K1so39dbK
3) https://alive2.llvm.org/ce/z/-54zZv

Best wishes,
Navid.

________________________________________
From: Andrew Pinski <pinskia@gmail.com>
Sent: Tuesday, November 30, 2021 14:03
To: Navid Rahimi
Cc: Navid Rahimi via Gcc-patches
Subject: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift

On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Hi GCC community,
>
> This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.
>

A better way to optimize this is the following (which I describe in PR 64992):
 take: (t << 1) != 0;

This should be transformed into:
(t & 0x7fffffff) != 0

The rest will just fall out really.  That is there is no reason to
special case bool here.
I have most of the patch except for creating the mask part which
should be simple, I just did not want to look up the wi:: functions at
the time I was writing it into the bug report.

Thanks,
Andrew Pinski



> Tree-optimization/98956:
>
> Adding new optimization to match.pd:
>                 * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
>                 * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
>                 * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
>
> 1) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D98956&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=EO7zAIa9sux4JklTDeALImoX3Kcjqeug%2BssU0E%2Fp6mY%3D&amp;reserved=0
> 2) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fnj4PTrecW&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=GyivNuda31%2FPXJQQ4Z9tK2cFtj3N9YcvRdtM7rVkhHg%3D&amp;reserved=0
> 3) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2FjyJAoS&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=esqOKjKS5JZDbNBmAi0Bwwk0JTTHzInQ2Lgeq%2BPHJ9w%3D&amp;reserved=0
>
> Best wishes,
> Navid.

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

* Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
  2021-11-30 23:08   ` [EXTERNAL] " Navid Rahimi
@ 2021-11-30 23:18     ` Andrew Pinski
  2021-11-30 23:38       ` Andrew Pinski
  2021-11-30 23:41       ` Navid Rahimi
  0 siblings, 2 replies; 6+ messages in thread
From: Andrew Pinski @ 2021-11-30 23:18 UTC (permalink / raw)
  To: Navid Rahimi; +Cc: Navid Rahimi via Gcc-patches

On Tue, Nov 30, 2021 at 3:08 PM Navid Rahimi <navidrahimi@microsoft.com> wrote:
>
> Hi Andrew,
>
> Thanks for your detailed comment. There are two problem I wanted to discuss with you about:
>
> a) The optimization I have sent patch, does optimize variable length "<<" too(for example B0 << x, where x is variable). This [1] link shows the actual optimization and a link for the proof is included in the editor.
>
> b) I am unable to prove the optimization you are describing for non-constant length shift. You can take a look at the code example [2] and proof [3]. I am getting "Transformation doesn't verify!" when I do implement the optimization you mentioned for non-constant shift.
>
> The optimization you are describing only works for "(take: (t << 1) != 0) -> ((t & 0x7fffffff) != 0)" which only is provable and works for INTEGER_CST.

No it works with non constants too:
t << y != 0 -> t & (-1u>>y) != 0

When y == 0, you have t != 0.
Which is exactly what you think it should be.
Which can be further reduced to t != 0 as y >= sizeof(t)*BITS_PER_UNIT
is undefined.

Thanks,
Andrew Pinski


>
> My understanding might be incorrect here, please don't hesitate to correct me.
>
> 1) https://compiler-explorer.com/z/r46znh4Tj
> 2) https://compiler-explorer.com/z/K1so39dbK
> 3) https://alive2.llvm.org/ce/z/-54zZv
>
> Best wishes,
> Navid.
>
> ________________________________________
> From: Andrew Pinski <pinskia@gmail.com>
> Sent: Tuesday, November 30, 2021 14:03
> To: Navid Rahimi
> Cc: Navid Rahimi via Gcc-patches
> Subject: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
>
> On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > Hi GCC community,
> >
> > This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.
> >
>
> A better way to optimize this is the following (which I describe in PR 64992):
>  take: (t << 1) != 0;
>
> This should be transformed into:
> (t & 0x7fffffff) != 0
>
> The rest will just fall out really.  That is there is no reason to
> special case bool here.
> I have most of the patch except for creating the mask part which
> should be simple, I just did not want to look up the wi:: functions at
> the time I was writing it into the bug report.
>
> Thanks,
> Andrew Pinski
>
>
>
> > Tree-optimization/98956:
> >
> > Adding new optimization to match.pd:
> >                 * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
> >                 * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
> >                 * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
> >
> > 1) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D98956&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=EO7zAIa9sux4JklTDeALImoX3Kcjqeug%2BssU0E%2Fp6mY%3D&amp;reserved=0
> > 2) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fnj4PTrecW&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=GyivNuda31%2FPXJQQ4Z9tK2cFtj3N9YcvRdtM7rVkhHg%3D&amp;reserved=0
> > 3) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2FjyJAoS&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=esqOKjKS5JZDbNBmAi0Bwwk0JTTHzInQ2Lgeq%2BPHJ9w%3D&amp;reserved=0
> >
> > Best wishes,
> > Navid.

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

* Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
  2021-11-30 23:18     ` Andrew Pinski
@ 2021-11-30 23:38       ` Andrew Pinski
  2021-11-30 23:41       ` Navid Rahimi
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew Pinski @ 2021-11-30 23:38 UTC (permalink / raw)
  To: Navid Rahimi; +Cc: Navid Rahimi via Gcc-patches

On Tue, Nov 30, 2021 at 3:18 PM Andrew Pinski <pinskia@gmail.com> wrote:
>
> On Tue, Nov 30, 2021 at 3:08 PM Navid Rahimi <navidrahimi@microsoft.com> wrote:
> >
> > Hi Andrew,
> >
> > Thanks for your detailed comment. There are two problem I wanted to discuss with you about:
> >
> > a) The optimization I have sent patch, does optimize variable length "<<" too(for example B0 << x, where x is variable). This [1] link shows the actual optimization and a link for the proof is included in the editor.
> >
> > b) I am unable to prove the optimization you are describing for non-constant length shift. You can take a look at the code example [2] and proof [3]. I am getting "Transformation doesn't verify!" when I do implement the optimization you mentioned for non-constant shift.
> >
> > The optimization you are describing only works for "(take: (t << 1) != 0) -> ((t & 0x7fffffff) != 0)" which only is provable and works for INTEGER_CST.
>
> No it works with non constants too:
> t << y != 0 -> t & (-1u>>y) != 0
>
> When y == 0, you have t != 0.
> Which is exactly what you think it should be.
> Which can be further reduced to t != 0 as y >= sizeof(t)*BITS_PER_UNIT
> is undefined.

I filed PR 103509 for the above issue.  Note it only works when
comparing against 0. I also notice that LLVM, ICC and MSVC do not do
the optimization either. But they (except for MSVC) do handle (-1u>>y)
!= 0 to be always true.

Thanks,
Andrew Pinski

>
> Thanks,
> Andrew Pinski
>
>
> >
> > My understanding might be incorrect here, please don't hesitate to correct me.
> >
> > 1) https://compiler-explorer.com/z/r46znh4Tj
> > 2) https://compiler-explorer.com/z/K1so39dbK
> > 3) https://alive2.llvm.org/ce/z/-54zZv
> >
> > Best wishes,
> > Navid.
> >
> > ________________________________________
> > From: Andrew Pinski <pinskia@gmail.com>
> > Sent: Tuesday, November 30, 2021 14:03
> > To: Navid Rahimi
> > Cc: Navid Rahimi via Gcc-patches
> > Subject: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
> >
> > On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > >
> > > Hi GCC community,
> > >
> > > This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.
> > >
> >
> > A better way to optimize this is the following (which I describe in PR 64992):
> >  take: (t << 1) != 0;
> >
> > This should be transformed into:
> > (t & 0x7fffffff) != 0
> >
> > The rest will just fall out really.  That is there is no reason to
> > special case bool here.
> > I have most of the patch except for creating the mask part which
> > should be simple, I just did not want to look up the wi:: functions at
> > the time I was writing it into the bug report.
> >
> > Thanks,
> > Andrew Pinski
> >
> >
> >
> > > Tree-optimization/98956:
> > >
> > > Adding new optimization to match.pd:
> > >                 * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
> > >                 * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
> > >                 * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
> > >
> > > 1) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D98956&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=EO7zAIa9sux4JklTDeALImoX3Kcjqeug%2BssU0E%2Fp6mY%3D&amp;reserved=0
> > > 2) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fnj4PTrecW&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=GyivNuda31%2FPXJQQ4Z9tK2cFtj3N9YcvRdtM7rVkhHg%3D&amp;reserved=0
> > > 3) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2FjyJAoS&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=esqOKjKS5JZDbNBmAi0Bwwk0JTTHzInQ2Lgeq%2BPHJ9w%3D&amp;reserved=0
> > >
> > > Best wishes,
> > > Navid.

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

* Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
  2021-11-30 23:18     ` Andrew Pinski
  2021-11-30 23:38       ` Andrew Pinski
@ 2021-11-30 23:41       ` Navid Rahimi
  1 sibling, 0 replies; 6+ messages in thread
From: Navid Rahimi @ 2021-11-30 23:41 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Navid Rahimi via Gcc-patches

I see. That makes sense. Thanks for the explanation. I was looking at the 64992 and it seems all the implementation right now are only using INTEGER_CST at the moment.

But now it makes sense.

Best wishes,
Navid.

________________________________________
From: Andrew Pinski <pinskia@gmail.com>
Sent: Tuesday, November 30, 2021 15:18
To: Navid Rahimi
Cc: Navid Rahimi via Gcc-patches
Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift

On Tue, Nov 30, 2021 at 3:08 PM Navid Rahimi <navidrahimi@microsoft.com> wrote:
>
> Hi Andrew,
>
> Thanks for your detailed comment. There are two problem I wanted to discuss with you about:
>
> a) The optimization I have sent patch, does optimize variable length "<<" too(for example B0 << x, where x is variable). This [1] link shows the actual optimization and a link for the proof is included in the editor.
>
> b) I am unable to prove the optimization you are describing for non-constant length shift. You can take a look at the code example [2] and proof [3]. I am getting "Transformation doesn't verify!" when I do implement the optimization you mentioned for non-constant shift.
>
> The optimization you are describing only works for "(take: (t << 1) != 0) -> ((t & 0x7fffffff) != 0)" which only is provable and works for INTEGER_CST.

No it works with non constants too:
t << y != 0 -> t & (-1u>>y) != 0

When y == 0, you have t != 0.
Which is exactly what you think it should be.
Which can be further reduced to t != 0 as y >= sizeof(t)*BITS_PER_UNIT
is undefined.

Thanks,
Andrew Pinski


>
> My understanding might be incorrect here, please don't hesitate to correct me.
>
> 1) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fr46znh4Tj&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521114457%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=hGkyUkh4Srjb5%2BhvYdT30VLaDLGlkM6jBt3TmfcHFUw%3D&amp;reserved=0
> 2) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2FK1so39dbK&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=EiVD3aIDzds%2BIX5EY3onWVuc%2FdMjoeDSyc5I1B2Xr%2F4%3D&amp;reserved=0
> 3) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2F-54zZv&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=vFiA3eWi5Ry3rFwp6iUc61JaVtzoWS6careE4I5rZvk%3D&amp;reserved=0
>
> Best wishes,
> Navid.
>
> ________________________________________
> From: Andrew Pinski <pinskia@gmail.com>
> Sent: Tuesday, November 30, 2021 14:03
> To: Navid Rahimi
> Cc: Navid Rahimi via Gcc-patches
> Subject: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift
>
> On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > Hi GCC community,
> >
> > This patch will add the missed pattern described in bug 98956 [1] to the match.pd. The codegen and correctness proof for this pattern is here [2,3] in case anyone is curious. Tested on x86_64 Linux.
> >
>
> A better way to optimize this is the following (which I describe in PR 64992):
>  take: (t << 1) != 0;
>
> This should be transformed into:
> (t & 0x7fffffff) != 0
>
> The rest will just fall out really.  That is there is no reason to
> special case bool here.
> I have most of the patch except for creating the mask part which
> should be simple, I just did not want to look up the wi:: functions at
> the time I was writing it into the bug report.
>
> Thanks,
> Andrew Pinski
>
>
>
> > Tree-optimization/98956:
> >
> > Adding new optimization to match.pd:
> >                 * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization.
> >                 * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization.
> >                 * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with side-effect.
> >
> > 1) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D98956&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=D2%2Fsh1qyHezbiH8xfcwONjnob00Pvu5ktj2kcFlQSxE%3D&amp;reserved=0
> > 2) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fnj4PTrecW&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=cWJRaWXZBuR6Wt42YkUT5bFpvvWr3n81Kml5KF7gq38%3D&amp;reserved=0
> > 3) https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2FjyJAoS&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=c1%2FRY7g4iSCwo5QwJQ3j%2FrpNvM2wlu2nr%2BtaDOOB7v8%3D&amp;reserved=0
> >
> > Best wishes,
> > Navid.

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

end of thread, other threads:[~2021-11-30 23:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 16:34 [PATCH] tree-optimization/98956 Optimizing out boolean left shift Navid Rahimi
2021-11-30 22:03 ` Andrew Pinski
2021-11-30 23:08   ` [EXTERNAL] " Navid Rahimi
2021-11-30 23:18     ` Andrew Pinski
2021-11-30 23:38       ` Andrew Pinski
2021-11-30 23:41       ` Navid Rahimi

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