This is similar to Mark Gilsse's patch in the OP, except that it ensures that
the expression will fold back to a single condition. I did include Richi's
patch from #c6 to make it more likely to trigger asap.
I'd appreciate feedback on the match.pd changes; it's my first time looking
into this new(ish) mini-language.
r~
* gimplify.c (gimplify_expr) [VEC_COND_EXPR]: Allow the first
argument to be is_gimple_condexpr.
* match.pd: Simplify and + ior of vec_cond.
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 7be6bd7..c434e55 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -10773,8 +10773,21 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p,
gimple_seq *post_p,
goto expr_2;
}
- case FMA_EXPR:
case VEC_COND_EXPR:
+ {
+ gimplify_status r0, r1, r2;
+ r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
+ post_p, is_gimple_condexpr, fb_rvalue);
+ r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
+ post_p, is_gimple_val, fb_rvalue);
+ r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
+ post_p, is_gimple_val, fb_rvalue);
+ ret = MIN (MIN (r0, r1), r2);
+ recalculate_side_effects (*expr_p);
+ break;
+ }
+
+ case FMA_EXPR:
case VEC_PERM_EXPR:
/* Classified as tcc_expression. */
goto expr_3;
diff --git a/gcc/match.pd b/gcc/match.pd
index 5903782..4192d29 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see
real_zerop real_onep real_minus_onep
zerop
CONSTANT_CLASS_P
+ COMPARISON_CLASS_P
tree_expr_nonnegative_p
integer_valued_real_p
integer_pow2p
@@ -2452,6 +2453,35 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
replace if (x == 0) with tem = ~x; if (tem != 0) which is
clearly less optimal and which we'll transform again in forwprop. */
+/* Simplification of vec_cond. */
+
+(for bcode (bit_and bit_ior)
+ (simplify
+ (bcode (vec_cond COMPARISON_CLASS_P@0 integer_all_onesp@2 integer_zerop@3)
+ (vec_cond COMPARISON_CLASS_P@1 @2 @3))
+ (with
+ {
+ tree al = TREE_OPERAND (@0, 0);
+ tree ar = TREE_OPERAND (@0, 1);
+ tree bl = TREE_OPERAND (@1, 0);
+ tree br = TREE_OPERAND (@1, 1);
+ }
+ (if (operand_equal_p (al, bl, 0) && operand_equal_p (ar, br, 0))
+ (with
+ {
+ tree_code tcode
+ = bcode == BIT_AND_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
+ tree folded
+ = combine_comparisons (UNKNOWN_LOCATION, tcode, TREE_CODE (@0),
+ TREE_CODE (@1), TREE_TYPE (@0), al, ar);
+ }
+ (if (folded)
+ (switch
+ (if (integer_truep (folded)) @2)
+ (if (integer_zerop (folded)) @3)
+ (if (COMPARISON_CLASS_P (folded))
+ { build3 (VEC_COND_EXPR, TREE_TYPE (@2), folded, @2, @3); })))
+ )))))
/* Simplification of math builtins. These rules must all be optimizations
as well as IL simplifications. If there is a possibility that the new