* [PATCH 1/2] PHIOPT: Allow moving of some builtin calls
2023-04-28 17:02 [PATCH 0/2] Porting of builtin_zero_pattern to match Andrew Pinski
@ 2023-04-28 17:02 ` Andrew Pinski
2023-04-30 19:11 ` Jeff Law
2023-04-28 17:02 ` [PATCH 2/2] MATCH: add some of what phiopt's builtin_zero_pattern did Andrew Pinski
1 sibling, 1 reply; 5+ messages in thread
From: Andrew Pinski @ 2023-04-28 17:02 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
While moving working on moving
cond_removal_in_builtin_zero_pattern to match, I noticed
that functions were not allowed to move as we reject all
non-assignments.
This changes to allowing a few calls which are known not
to throw/trap. Right now it is restricted to ones
which cond_removal_in_builtin_zero_pattern handles but
adding more is just adding it to the switch statement.
gcc/ChangeLog:
* tree-ssa-phiopt.cc (empty_bb_or_one_feeding_into_p):
Allow some builtin/internal function calls which
are known not to trap/throw.
(phiopt_worker::match_simplify_replacement):
Use name instead of getting the lhs again.
---
gcc/tree-ssa-phiopt.cc | 35 +++++++++++++++++++++++++++++++----
1 file changed, 31 insertions(+), 4 deletions(-)
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 4b43f1abdbc..024a4362093 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -548,6 +548,7 @@ empty_bb_or_one_feeding_into_p (basic_block bb,
{
stmt = nullptr;
gimple *stmt_to_move = nullptr;
+ tree lhs;
if (empty_block_p (bb))
return true;
@@ -592,17 +593,43 @@ empty_bb_or_one_feeding_into_p (basic_block bb,
if (gimple_uses_undefined_value_p (stmt_to_move))
return false;
- /* Allow assignments and not no calls.
+ /* Allow assignments but allow some builtin/internal calls.
As const calls don't match any of the above, yet they could
still have some side-effects - they could contain
gimple_could_trap_p statements, like floating point
exceptions or integer division by zero. See PR70586.
FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
- should handle this. */
+ should handle this.
+ Allow some known builtin/internal calls that are known not to
+ trap: logical functions (e.g. bswap and bit counting). */
if (!is_gimple_assign (stmt_to_move))
- return false;
+ {
+ if (!is_gimple_call (stmt_to_move))
+ return false;
+ combined_fn cfn = gimple_call_combined_fn (stmt_to_move);
+ switch (cfn)
+ {
+ default:
+ return false;
+ case CFN_BUILT_IN_BSWAP16:
+ case CFN_BUILT_IN_BSWAP32:
+ case CFN_BUILT_IN_BSWAP64:
+ case CFN_BUILT_IN_BSWAP128:
+ CASE_CFN_FFS:
+ CASE_CFN_PARITY:
+ CASE_CFN_POPCOUNT:
+ CASE_CFN_CLZ:
+ CASE_CFN_CTZ:
+ case CFN_BUILT_IN_CLRSB:
+ case CFN_BUILT_IN_CLRSBL:
+ case CFN_BUILT_IN_CLRSBLL:
+ lhs = gimple_call_lhs (stmt_to_move);
+ break;
+ }
+ }
+ else
+ lhs = gimple_assign_lhs (stmt_to_move);
- tree lhs = gimple_assign_lhs (stmt_to_move);
gimple *use_stmt;
use_operand_p use_p;
--
2.39.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] MATCH: add some of what phiopt's builtin_zero_pattern did
2023-04-28 17:02 [PATCH 0/2] Porting of builtin_zero_pattern to match Andrew Pinski
2023-04-28 17:02 ` [PATCH 1/2] PHIOPT: Allow moving of some builtin calls Andrew Pinski
@ 2023-04-28 17:02 ` Andrew Pinski
2023-04-30 19:13 ` Jeff Law
1 sibling, 1 reply; 5+ messages in thread
From: Andrew Pinski @ 2023-04-28 17:02 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
This adds the patterns for
POPCOUNT BSWAP FFS PARITY CLZ and CTZ.
For "a != 0 ? FUNC(a) : CST".
CLRSB, CLRSBL, and CLRSBLL will be moved next.
Note this is not enough to remove
cond_removal_in_builtin_zero_pattern as we need to handle
the case where there is an NOP_CONVERT inside the conditional
to move out of the condition inside match_simplify_replacement.
OK? Bootstrapped and tested on x86_64-linux-gnu.
gcc/ChangeLog:
* match.pd: Add patterns for "a != 0 ? FUNC(a) : CST"
for FUNC of POPCOUNT BSWAP FFS PARITY CLZ and CTZ.
---
gcc/match.pd | 41 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 39 insertions(+), 2 deletions(-)
diff --git a/gcc/match.pd b/gcc/match.pd
index e17597ead26..0e782cde71d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -52,6 +52,8 @@ along with GCC; see the file COPYING3. If not see
gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
(define_operator_list simple_comparison lt le eq ne ge gt)
(define_operator_list swapped_simple_comparison gt ge eq ne le lt)
+(define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
+ BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
#include "cfn-operators.pd"
@@ -4313,8 +4315,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(complex (convert:itype @0) (negate (convert:itype @1)))))
/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
-(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
- BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
+(for bswap (BSWAP)
(simplify
(bswap (bswap @0))
@0)
@@ -7780,6 +7781,42 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(bit_xor (PARITY:s @0) (PARITY:s @1))
(PARITY (bit_xor @0 @1)))
+/* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
+(for func (POPCOUNT BSWAP FFS PARITY)
+ (simplify
+ (cond (ne @0 integer_zerop@1) (func@4 (convert? @2)) integer_zerop@3)
+ @4))
+
+#if GIMPLE
+/* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
+(for func (CLZ)
+ (simplify
+ (cond (ne @0 integer_zerop@1) (func (convert?@4 @2)) INTEGER_CST@3)
+ (with { int val;
+ internal_fn ifn = IFN_LAST;
+ if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
+ && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
+ val) == 2)
+ ifn = IFN_CLZ;
+ }
+ (if (ifn == IFN_CLZ && wi::to_widest (@3) == val)
+ (IFN_CLZ @4)))))
+
+/* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
+(for func (CTZ)
+ (simplify
+ (cond (ne @0 integer_zerop@1) (func (convert?@4 @2)) INTEGER_CST@3)
+ (with { int val;
+ internal_fn ifn = IFN_LAST;
+ if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
+ && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
+ val) == 2)
+ ifn = IFN_CTZ;
+ }
+ (if (ifn == IFN_CTZ && wi::to_widest (@3) == val)
+ (IFN_CTZ @4)))))
+#endif
+
/* Common POPCOUNT/PARITY simplifications. */
/* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
(for pfun (POPCOUNT PARITY)
--
2.39.1
^ permalink raw reply [flat|nested] 5+ messages in thread