if-to-switch: Support chain with 2 BBs.
authorMartin Liska <mliska@suse.cz>
Tue, 1 Dec 2020 11:18:46 +0000 (12:18 +0100)
committerMartin Liska <mliska@suse.cz>
Wed, 2 Dec 2020 07:18:18 +0000 (08:18 +0100)
As seen in the test-case, even 2 BBs can handle interesting
cases covered by a jump table or a bit-test.

gcc/ChangeLog:

PR tree-optimization/88702
* gimple-if-to-switch.cc (pass_if_to_switch::execute):
Require at least 2 BBs.
* gimple-if-to-switch.cc (find_conditions): Require
equal precision for low and high of a range.

gcc/testsuite/ChangeLog:

PR tree-optimization/88702
* gcc.dg/tree-ssa/if-to-switch-9.c: New test.

gcc/gimple-if-to-switch.cc
gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-9.c [new file with mode: 0644]

index 0aa1b0e4e7e8cccbe7ab4c946559091c3120caae..8e1043ae7c4ee9537a81c9fa98bb2ecad379bca8 100644 (file)
@@ -431,7 +431,9 @@ find_conditions (basic_block bb,
        if (info.m_ranges[i].exp == NULL_TREE
            || !INTEGRAL_TYPE_P (TREE_TYPE (info.m_ranges[i].exp))
            || info.m_ranges[i].low == NULL_TREE
-           || info.m_ranges[i].high == NULL_TREE)
+           || info.m_ranges[i].high == NULL_TREE
+           || (TYPE_PRECISION (TREE_TYPE (info.m_ranges[i].low))
+               != TYPE_PRECISION (TREE_TYPE (info.m_ranges[i].high))))
          return;
 
       for (unsigned i = 1; i < info.m_ranges.length (); ++i)
@@ -526,7 +528,7 @@ pass_if_to_switch::execute (function *fun)
            }
 
          chain->m_entries.reverse ();
-         if (chain->m_entries.length () >= 3
+         if (chain->m_entries.length () >= 2
              && chain->check_non_overlapping_cases ()
              && chain->is_beneficial ())
            {
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-9.c b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-9.c
new file mode 100644 (file)
index 0000000..e67198b
--- /dev/null
@@ -0,0 +1,11 @@
+/* PR tree-optimization/88702 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-iftoswitch-optimized" } */
+
+int IsHTMLWhitespace(int aChar) {                         
+  return aChar == 0x0009 || aChar == 0x000A ||              
+         aChar == 0x000C || aChar == 0x000D ||              
+         aChar == 0x0020;                                             
+}
+
+/* { dg-final { scan-tree-dump "Condition chain with \[^\n\r]\* BBs transformed into a switch statement." "iftoswitch" } } */