aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-12-06 10:55:12 +0100
committerJakub Jelinek <jakub@redhat.com>2020-12-06 10:55:12 +0100
commit9e12b8b1819342ef7efac58cf7f4ba4294abe551 (patch)
treee85728c3ca27665a12469a0206a08fd0869b6ff3
parentbd0f0243869b3941a256ca0ea9c8aca141412f7e (diff)
downloadgcc-9e12b8b1819342ef7efac58cf7f4ba4294abe551.zip
gcc-9e12b8b1819342ef7efac58cf7f4ba4294abe551.tar.gz
gcc-9e12b8b1819342ef7efac58cf7f4ba4294abe551.tar.bz2
match.pd: Improve conditional_replacement for x ? 0 : -1 [PR796232]
As mentioned in the PR, for boolean x we currently optimize in phiopt x ? 0 : -1 into -(int)!x but it can be optimized as (int) x - 1 which is one less operation both in GIMPLE and in x86 assembly. This patch optimizes it in match.pd, by optimizing -(type)!x when x has boolean range into (type)x - 1. 2020-12-06 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/96232 * match.pd (-(type)!A -> (type)A - 1): New optimization. * gcc.dg/tree-ssa/pr96232-1.c: New test.
-rw-r--r--gcc/match.pd10
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c11
2 files changed, 21 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 68201ff..43bacb4 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3812,6 +3812,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cnd (logical_inverted_value truth_valued_p@0) @1 @2)
(cnd @0 @2 @1)))
+/* -(type)!A -> (type)A - 1. */
+(simplify
+ (negate (convert?:s (logical_inverted_value:s @0)))
+ (if (INTEGRAL_TYPE_P (type)
+ && TREE_CODE (type) != BOOLEAN_TYPE
+ && TYPE_PRECISION (type) > 1
+ && TREE_CODE (@0) == SSA_NAME
+ && ssa_name_has_boolean_range (@0))
+ (plus (convert:type @0) { build_all_ones_cst (type); })))
+
/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
return all -1 or all 0 results. */
/* ??? We could instead convert all instances of the vec_cond to negate,
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c
new file mode 100644
index 0000000..3170ffd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c
@@ -0,0 +1,11 @@
+/* PR tree-optimization/96232 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump " \\+ -1;" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "~x_\[0-9]*\\\(D\\\)" "optimized" } } */
+
+int
+foo (_Bool x)
+{
+ return x ? 0 : -1;
+}