Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] if-to-switch: fix matching of negative conditions
@ 2020-12-08 13:22 Martin Liška
  2020-12-08 13:35 ` Martin Liška
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Liška @ 2020-12-08 13:22 UTC (permalink / raw)
  To: gdb-patches

We must be careful which edge we follow for conditions
of a negative form (index != 2).

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/ChangeLog:

	PR tree-optimization/98182
	* gimple-if-to-switch.cc (pass_if_to_switch::execute): Request
	chain linkage through false edges only.

gcc/testsuite/ChangeLog:

	PR tree-optimization/98182
	* gcc.dg/tree-ssa/if-to-switch-10.c: New test.
	* gcc.dg/tree-ssa/pr98182.c: New test.
---
  gcc/gimple-if-to-switch.cc                    |  6 +++
  .../gcc.dg/tree-ssa/if-to-switch-10.c         | 44 +++++++++++++++++++
  gcc/testsuite/gcc.dg/tree-ssa/pr98182.c       | 18 ++++++++
  3 files changed, 68 insertions(+)
  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr98182.c

diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc
index 8e1043ae7c4..311f6f6ac97 100644
--- a/gcc/gimple-if-to-switch.cc
+++ b/gcc/gimple-if-to-switch.cc
@@ -522,6 +522,12 @@ pass_if_to_switch::execute (function *fun)
  	      if (!info2 || info->m_ranges[0].exp != info2->m_ranges[0].exp)
  		break;
  
+	      /* It is important that the blocks are linked through FALSE_EDGE.
+		 For an expression of index != VALUE, true and false edges
+		 are flipped.  */
+	      if (info2->m_false_edge != e)
+		break;
+
  	      chain->m_entries.safe_push (info2);
  	      bitmap_set_bit (seen_bbs, e->src->index);
  	      info = info2;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
new file mode 100644
index 00000000000..7b8da1c9f3c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-iftoswitch-optimized" } */
+
+int global;
+int foo ();
+
+int main(int argc, char **argv)
+{
+  if (argc != 1)
+    {
+      if (argc != 2)
+	{
+	  if (argc == 3)
+	    {
+	      foo ();
+	      foo ();
+	    }
+	  else if (argc == 4)
+	    {
+	      foo ();
+	    }
+	  else if (argc == 5)
+	    {
+	      global = 2;
+	    }
+	  else
+	    global -= 123;
+	}
+      else
+	{
+	  global += 1;
+	}
+    }
+  else
+    foo ();
+
+
+  global -= 12;
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "Canonical GIMPLE case clusters: 1 2 3 4 5" "iftoswitch" } } */
+/* { dg-final { scan-tree-dump "Condition chain with \[^\n\r]\* BBs transformed into a switch statement." "iftoswitch" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c b/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c
new file mode 100644
index 00000000000..29a547e3788
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c
@@ -0,0 +1,18 @@
+/* PR tree-optimization/98182 */
+/* { dg-do compile } */
+/* { dg-options "-O1 --param case-values-threshold=1 -fdump-tree-iftoswitch-optimized" } */
+
+int global;
+int foo ();
+
+int main(int argc, char **argv)
+{
+  if (argc != 1)
+    __builtin_abort ();
+  else if (argc != 2)
+    __builtin_abort ();
+  else
+    return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "Condition chain" "iftoswitch" } } */
-- 
2.29.2


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

* Re: [PATCH] if-to-switch: fix matching of negative conditions
  2020-12-08 13:22 [PATCH] if-to-switch: fix matching of negative conditions Martin Liška
@ 2020-12-08 13:35 ` Martin Liška
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Liška @ 2020-12-08 13:35 UTC (permalink / raw)
  To: gdb-patches

Whoops, sorry, bad mailing list.

On 12/8/20 2:22 PM, Martin Liška wrote:
> We must be careful which edge we follow for conditions
> of a negative form (index != 2).
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin
> 
> gcc/ChangeLog:
> 
>      PR tree-optimization/98182
>      * gimple-if-to-switch.cc (pass_if_to_switch::execute): Request
>      chain linkage through false edges only.
> 
> gcc/testsuite/ChangeLog:
> 
>      PR tree-optimization/98182
>      * gcc.dg/tree-ssa/if-to-switch-10.c: New test.
>      * gcc.dg/tree-ssa/pr98182.c: New test.
> ---
>   gcc/gimple-if-to-switch.cc                    |  6 +++
>   .../gcc.dg/tree-ssa/if-to-switch-10.c         | 44 +++++++++++++++++++
>   gcc/testsuite/gcc.dg/tree-ssa/pr98182.c       | 18 ++++++++
>   3 files changed, 68 insertions(+)
>   create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
>   create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr98182.c
> 
> diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc
> index 8e1043ae7c4..311f6f6ac97 100644
> --- a/gcc/gimple-if-to-switch.cc
> +++ b/gcc/gimple-if-to-switch.cc
> @@ -522,6 +522,12 @@ pass_if_to_switch::execute (function *fun)
>             if (!info2 || info->m_ranges[0].exp != info2->m_ranges[0].exp)
>           break;
> 
> +          /* It is important that the blocks are linked through FALSE_EDGE.
> +         For an expression of index != VALUE, true and false edges
> +         are flipped.  */
> +          if (info2->m_false_edge != e)
> +        break;
> +
>             chain->m_entries.safe_push (info2);
>             bitmap_set_bit (seen_bbs, e->src->index);
>             info = info2;
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
> new file mode 100644
> index 00000000000..7b8da1c9f3c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-10.c
> @@ -0,0 +1,44 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-iftoswitch-optimized" } */
> +
> +int global;
> +int foo ();
> +
> +int main(int argc, char **argv)
> +{
> +  if (argc != 1)
> +    {
> +      if (argc != 2)
> +    {
> +      if (argc == 3)
> +        {
> +          foo ();
> +          foo ();
> +        }
> +      else if (argc == 4)
> +        {
> +          foo ();
> +        }
> +      else if (argc == 5)
> +        {
> +          global = 2;
> +        }
> +      else
> +        global -= 123;
> +    }
> +      else
> +    {
> +      global += 1;
> +    }
> +    }
> +  else
> +    foo ();
> +
> +
> +  global -= 12;
> +  return 0;
> +}
> +
> +/* { dg-final { scan-tree-dump "Canonical GIMPLE case clusters: 1 2 3 4 5" "iftoswitch" } } */
> +/* { dg-final { scan-tree-dump "Condition chain with \[^\n\r]\* BBs transformed into a switch statement." "iftoswitch" } } */
> +
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c b/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c
> new file mode 100644
> index 00000000000..29a547e3788
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr98182.c
> @@ -0,0 +1,18 @@
> +/* PR tree-optimization/98182 */
> +/* { dg-do compile } */
> +/* { dg-options "-O1 --param case-values-threshold=1 -fdump-tree-iftoswitch-optimized" } */
> +
> +int global;
> +int foo ();
> +
> +int main(int argc, char **argv)
> +{
> +  if (argc != 1)
> +    __builtin_abort ();
> +  else if (argc != 2)
> +    __builtin_abort ();
> +  else
> +    return 0;
> +}
> +
> +/* { dg-final { scan-tree-dump-not "Condition chain" "iftoswitch" } } */


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

end of thread, other threads:[~2020-12-08 13:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08 13:22 [PATCH] if-to-switch: fix matching of negative conditions Martin Liška
2020-12-08 13:35 ` Martin Liška

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox