public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCHv2] Add a couple of A?CST1:CST2 match and simplify optimizations
@ 2021-05-23  9:41 apinski
  2021-05-25 14:22 ` Richard Biener
  0 siblings, 1 reply; 16+ messages in thread
From: apinski @ 2021-05-23  9:41 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

Instead of some of the more manual optimizations inside phi-opt,
it would be good idea to do a lot of the heavy lifting inside match
and simplify instead. In the process, this moves the three simple
A?CST1:CST2 (where CST1 or CST2 is zero) simplifications.

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

Differences from V1:
* Use bit_xor 1 instead of bit_not to fix the problem with boolean types
which are not 1 bit precision.

Thanks,
Andrew Pinski

gcc:
	* match.pd (A?CST1:CST2): Add simplifcations for A?0:+-1, A?+-1:0,
	A?POW2:0 and A?0:POW2.
---
 gcc/match.pd | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 1fc6b7b1557..ad6b057c56d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3711,6 +3711,47 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (if (integer_all_onesp (@1) && integer_zerop (@2))
     @0))))
 
+/* A few simplifications of "a ? CST1 : CST2". */
+/* NOTE: Only do this on gimple as the if-chain-to-switch
+   optimization depends on the gimple to have if statements in it. */
+#if GIMPLE
+(simplify
+ (cond @0 INTEGER_CST@1 INTEGER_CST@2)
+ (switch
+  (if (integer_zerop (@2))
+   (switch
+    /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
+    (if (integer_onep (@1))
+     (convert (convert:boolean_type_node @0)))
+    /* a ? -1 : 0 -> -a. */
+    (if (integer_all_onesp (@1))
+     (negate (convert (convert:boolean_type_node @0))))
+    /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
+    (if (!POINTER_TYPE_P (type) && integer_pow2p (@1))
+     (with {
+       tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
+      }
+      (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
+  (if (integer_zerop (@1))
+   (with {
+      tree booltrue = constant_boolean_node (true, boolean_type_node);
+    }
+    (switch
+     /* a ? 0 : 1 -> !a. */
+     (if (integer_onep (@2))
+      (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
+     /* a ? -1 : 0 -> -(!a). */
+     (if (integer_all_onesp (@2))
+      (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
+     /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
+     (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
+      (with {
+	tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
+       }
+       (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
+        { shift; }))))))))
+#endif
+
 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
    be extended.  */
 /* This pattern implements two kinds simplification:
-- 
2.17.1


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

end of thread, other threads:[~2021-05-28  7:16 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-23  9:41 [PATCHv2] Add a couple of A?CST1:CST2 match and simplify optimizations apinski
2021-05-25 14:22 ` Richard Biener
2021-05-26  8:28   ` Bernd Edlinger
2021-05-26  9:01     ` Andrew Pinski
2021-05-26 11:07       ` Andrew Pinski
2021-05-26 11:27         ` Richard Biener
2021-05-26 11:37           ` Andrew Pinski
2021-05-26 12:05             ` Richard Biener
2021-05-26 16:59               ` Andrew MacLeod
2021-05-26 17:03               ` Bernd Edlinger
2021-05-26 17:07                 ` Bernd Edlinger
2021-05-26 17:15                   ` Andrew MacLeod
2021-05-26 16:54         ` Aldy Hernandez
2021-05-26 17:29         ` Andrew MacLeod
2021-05-28  4:53           ` Jeff Law
2021-05-28  7:16             ` 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).