From d8483dd1c46f351c157082d919815c0c5e984aeb Mon Sep 17 00:00:00 2001 From: rsandifo Date: Thu, 12 Jul 2018 13:01:17 +0000 Subject: [PATCH] Extend tree code folds to IFN_COND_* This patch adds match.pd support for applying normal folds to their IFN_COND_* forms. E.g. the rule: (plus @0 (negate @1)) -> (minus @0 @1) also allows the fold: (IFN_COND_ADD @0 @1 (negate @2) @3) -> (IFN_COND_SUB @0 @1 @2 @3) Actually doing this by direct matches in gimple-match.c would probably lead to combinatorial explosion, so instead, the patch makes gimple_match_op carry a condition under which the operation happens ("cond"), and the value to use when the condition is false ("else_value"). Thus in the example above we'd do the following (a) convert: cond:NULL_TREE (IFN_COND_ADD @0 @1 @4 @3) else_value:NULL_TREE to: cond:@0 (plus @1 @4) else_value:@3 (b) apply gimple_resimplify to (plus @1 @4) (c) reintroduce cond and else_value when constructing the result. Nested operations inherit the condition of the outer operation (so that we don't introduce extra faults) but have a null else_value. If we try to build such an operation, the target gets to choose what else_value it can handle efficiently: obvious choices include one of the operands or a zero constant. (The alternative would be to have some representation for an undefined value, but that seems a bit invasive, and isn't likely to be useful here.) I've made the condition a mandatory part of the gimple_match_op constructor so that it doesn't accidentally get dropped. 2018-07-12 Richard Sandiford gcc/ * target.def (preferred_else_value): New target hook. * doc/tm.texi.in (TARGET_PREFERRED_ELSE_VALUE): New hook. * doc/tm.texi: Regenerate. * targhooks.h (default_preferred_else_value): Declare. * targhooks.c (default_preferred_else_value): New function. * internal-fn.h (conditional_internal_fn_code): Declare. * internal-fn.c (FOR_EACH_CODE_MAPPING): New macro. (get_conditional_internal_fn): Use it. (conditional_internal_fn_code): New function. * gimple-match.h (gimple_match_cond): New struct. (gimple_match_op): Add a cond member function. (gimple_match_op::gimple_match_op): Update all forms to take a gimple_match_cond. * genmatch.c (expr::gen_transform): Use the same condition as res_op for the suboperation, but don't specify a particular else_value. * tree-ssa-sccvn.c (vn_nary_simplify, vn_reference_lookup_3) (visit_nary_op, visit_reference_op_load): Pass gimple_match_cond::UNCOND to the gimple_match_op constructor. * gimple-match-head.c: Include tree-eh.h (convert_conditional_op): New function. (maybe_resimplify_conditional_op): Likewise. (gimple_resimplify1): Call maybe_resimplify_conditional_op. (gimple_resimplify2): Likewise. (gimple_resimplify3): Likewise. (gimple_resimplify4): Likewise. (maybe_push_res_to_seq): Return null for conditional operations. (try_conditional_simplification): New function. (gimple_simplify): Call it. Pass conditions to the gimple_match_op constructor. * match.pd: Fold VEC_COND_EXPRs of an IFN_COND_* call to a new IFN_COND_* call. * config/aarch64/aarch64.c (aarch64_preferred_else_value): New function. (TARGET_PREFERRED_ELSE_VALUE): Redefine. gcc/testsuite/ * gcc.dg/vect/vect-cond-arith-2.c: New test. * gcc.target/aarch64/sve/loop_add_6.c: Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@262586 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 37 +++++ gcc/config/aarch64/aarch64.c | 13 ++ gcc/doc/tm.texi | 16 +++ gcc/doc/tm.texi.in | 2 + gcc/genmatch.c | 4 +- gcc/gimple-match-head.c | 161 +++++++++++++++++++++- gcc/gimple-match.h | 92 ++++++++++--- gcc/internal-fn.c | 54 +++++--- gcc/internal-fn.h | 1 + gcc/match.pd | 9 ++ gcc/target.def | 19 +++ gcc/targhooks.c | 8 ++ gcc/targhooks.h | 1 + gcc/testsuite/ChangeLog | 5 + gcc/testsuite/gcc.dg/vect/vect-cond-arith-2.c | 45 ++++++ gcc/testsuite/gcc.target/aarch64/sve/loop_add_6.c | 46 +++++++ gcc/tree-ssa-sccvn.c | 19 ++- 17 files changed, 484 insertions(+), 48 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/vect-cond-arith-2.c create mode 100644 gcc/testsuite/gcc.target/aarch64/sve/loop_add_6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 89d8fa87b3e..a8694e2c234 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,40 @@ +2018-07-12 Richard Sandiford + + * target.def (preferred_else_value): New target hook. + * doc/tm.texi.in (TARGET_PREFERRED_ELSE_VALUE): New hook. + * doc/tm.texi: Regenerate. + * targhooks.h (default_preferred_else_value): Declare. + * targhooks.c (default_preferred_else_value): New function. + * internal-fn.h (conditional_internal_fn_code): Declare. + * internal-fn.c (FOR_EACH_CODE_MAPPING): New macro. + (get_conditional_internal_fn): Use it. + (conditional_internal_fn_code): New function. + * gimple-match.h (gimple_match_cond): New struct. + (gimple_match_op): Add a cond member function. + (gimple_match_op::gimple_match_op): Update all forms to take a + gimple_match_cond. + * genmatch.c (expr::gen_transform): Use the same condition as res_op + for the suboperation, but don't specify a particular else_value. + * tree-ssa-sccvn.c (vn_nary_simplify, vn_reference_lookup_3) + (visit_nary_op, visit_reference_op_load): Pass + gimple_match_cond::UNCOND to the gimple_match_op constructor. + * gimple-match-head.c: Include tree-eh.h + (convert_conditional_op): New function. + (maybe_resimplify_conditional_op): Likewise. + (gimple_resimplify1): Call maybe_resimplify_conditional_op. + (gimple_resimplify2): Likewise. + (gimple_resimplify3): Likewise. + (gimple_resimplify4): Likewise. + (maybe_push_res_to_seq): Return null for conditional operations. + (try_conditional_simplification): New function. + (gimple_simplify): Call it. Pass conditions to the gimple_match_op + constructor. + * match.pd: Fold VEC_COND_EXPRs of an IFN_COND_* call to a new + IFN_COND_* call. + * config/aarch64/aarch64.c (aarch64_preferred_else_value): New + function. + (TARGET_PREFERRED_ELSE_VALUE): Redefine. + 2018-07-12 Jan Hubicka * lto-streamer-out.c (DFS::DFS_write_tree_body): Do not stream diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c index 01f35f8e852..6ef0cc75018 100644 --- a/gcc/config/aarch64/aarch64.c +++ b/gcc/config/aarch64/aarch64.c @@ -1320,6 +1320,16 @@ aarch64_get_mask_mode (poly_uint64 nunits, poly_uint64 nbytes) return default_get_mask_mode (nunits, nbytes); } +/* Implement TARGET_PREFERRED_ELSE_VALUE. Prefer to use the first + arithmetic operand as the else value if the else value doesn't matter, + since that exactly matches the SVE destructive merging form. */ + +static tree +aarch64_preferred_else_value (unsigned, tree, unsigned int, tree *ops) +{ + return ops[0]; +} + /* Implement TARGET_HARD_REGNO_NREGS. */ static unsigned int @@ -17990,6 +18000,9 @@ aarch64_libgcc_floating_mode_supported_p #undef TARGET_VECTORIZE_EMPTY_MASK_IS_EXPENSIVE #define TARGET_VECTORIZE_EMPTY_MASK_IS_EXPENSIVE \ aarch64_empty_mask_is_expensive +#undef TARGET_PREFERRED_ELSE_VALUE +#define TARGET_PREFERRED_ELSE_VALUE \ + aarch64_preferred_else_value #undef TARGET_INIT_LIBFUNCS #define TARGET_INIT_LIBFUNCS aarch64_init_libfuncs diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 7e2cdc2f202..ff6d5146010 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -6048,6 +6048,22 @@ expanded sequence has been inserted. This hook is also responsible for allocating any storage for reductions when necessary. @end deftypefn +@deftypefn {Target Hook} tree TARGET_PREFERRED_ELSE_VALUE (unsigned @var{ifn}, tree @var{type}, unsigned @var{nops}, tree *@var{ops}) +This hook returns the target's preferred final argument for a call +to conditional internal function @var{ifn} (really of type +@code{internal_fn}). @var{type} specifies the return type of the +function and @var{ops} are the operands to the conditional operation, +of which there are @var{nops}. + +For example, if @var{ifn} is @code{IFN_COND_ADD}, the hook returns +a value of type @var{type} that should be used when @samp{@var{ops}[0]} +and @samp{@var{ops}[1]} are conditionally added together. + +This hook is only relevant if the target supports conditional patterns +like @code{cond_add@var{m}}. The default implementation returns a zero +constant of type @var{type}. +@end deftypefn + @node Anchored Addresses @section Anchored Addresses @cindex anchored addresses diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index b7b0e8ac8dc..2f97151f341 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -4149,6 +4149,8 @@ address; but often a machine-dependent strategy can generate better code. @hook TARGET_GOACC_REDUCTION +@hook TARGET_PREFERRED_ELSE_VALUE + @node Anchored Addresses @section Anchored Addresses @cindex anchored addresses diff --git a/gcc/genmatch.c b/gcc/genmatch.c index d6bd90d1533..0492e804fc3 100644 --- a/gcc/genmatch.c +++ b/gcc/genmatch.c @@ -2497,8 +2497,8 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple, /* ??? Building a stmt can fail for various reasons here, seq being NULL or the stmt referencing SSA names occuring in abnormal PHIs. So if we fail here we should continue matching other patterns. */ - fprintf_indent (f, indent, "gimple_match_op tem_op (%s, %s", - opr_name, type); + fprintf_indent (f, indent, "gimple_match_op tem_op " + "(res_op->cond.any_else (), %s, %s", opr_name, type); for (unsigned i = 0; i < ops.length (); ++i) fprintf (f, ", ops%d[%u]", depth, i); fprintf (f, ");\n"); diff --git a/gcc/gimple-match-head.c b/gcc/gimple-match-head.c index c5a19239798..e165a77132b 100644 --- a/gcc/gimple-match-head.c +++ b/gcc/gimple-match-head.c @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3. If not see #include "case-cfn-macros.h" #include "gimplify.h" #include "optabs-tree.h" +#include "tree-eh.h" /* Forward declarations of the private auto-generated matchers. @@ -68,6 +69,95 @@ constant_for_folding (tree t) && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)); } +/* Try to convert conditional operation ORIG_OP into an IFN_COND_* + operation. Return true on success, storing the new operation in NEW_OP. */ + +static bool +convert_conditional_op (gimple_match_op *orig_op, + gimple_match_op *new_op) +{ + internal_fn ifn; + if (orig_op->code.is_tree_code ()) + ifn = get_conditional_internal_fn ((tree_code) orig_op->code); + else + return false; + if (ifn == IFN_LAST) + return false; + unsigned int num_ops = orig_op->num_ops; + new_op->set_op (as_combined_fn (ifn), orig_op->type, num_ops + 2); + new_op->ops[0] = orig_op->cond.cond; + for (unsigned int i = 0; i < num_ops; ++i) + new_op->ops[i + 1] = orig_op->ops[i]; + tree else_value = orig_op->cond.else_value; + if (!else_value) + else_value = targetm.preferred_else_value (ifn, orig_op->type, + num_ops, orig_op->ops); + new_op->ops[num_ops + 1] = else_value; + return true; +} + +/* RES_OP is the result of a simplification. If it is conditional, + try to replace it with the equivalent UNCOND form, such as an + IFN_COND_* call or a VEC_COND_EXPR. Also try to resimplify the + result of the replacement if appropriate, adding any new statements to + SEQ and using VALUEIZE as the valueization function. Return true if + this resimplification occurred and resulted in at least one change. */ + +static bool +maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op, + tree (*valueize) (tree)) +{ + if (!res_op->cond.cond) + return false; + + if (!res_op->cond.else_value + && res_op->code.is_tree_code ()) + { + /* The "else" value doesn't matter. If the "then" value is a + gimple value, just use it unconditionally. This isn't a + simplification in itself, since there was no operation to + build in the first place. */ + if (gimple_simplified_result_is_gimple_val (res_op)) + { + res_op->cond.cond = NULL_TREE; + return false; + } + + /* Likewise if the operation would not trap. */ + bool honor_trapv = (INTEGRAL_TYPE_P (res_op->type) + && TYPE_OVERFLOW_TRAPS (res_op->type)); + if (!operation_could_trap_p ((tree_code) res_op->code, + FLOAT_TYPE_P (res_op->type), + honor_trapv, res_op->op_or_null (1))) + { + res_op->cond.cond = NULL_TREE; + return false; + } + } + + /* If the "then" value is a gimple value and the "else" value matters, + create a VEC_COND_EXPR between them, then see if it can be further + simplified. */ + gimple_match_op new_op; + if (res_op->cond.else_value + && VECTOR_TYPE_P (res_op->type) + && gimple_simplified_result_is_gimple_val (res_op)) + { + new_op.set_op (VEC_COND_EXPR, res_op->type, + res_op->cond.cond, res_op->ops[0], + res_op->cond.else_value); + *res_op = new_op; + return gimple_resimplify3 (seq, res_op, valueize); + } + + /* Otherwise try rewriting the operation as an IFN_COND_* call. + Again, this isn't a simplification in itself, since it's what + RES_OP already described. */ + if (convert_conditional_op (res_op, &new_op)) + *res_op = new_op; + + return false; +} /* Helper that matches and simplifies the toplevel result from a gimple_simplify run (where we don't want to build @@ -93,6 +183,7 @@ gimple_resimplify1 (gimple_seq *seq, gimple_match_op *res_op, if (TREE_OVERFLOW_P (tem)) tem = drop_tree_overflow (tem); res_op->set_value (tem); + maybe_resimplify_conditional_op (seq, res_op, valueize); return true; } } @@ -122,6 +213,9 @@ gimple_resimplify1 (gimple_seq *seq, gimple_match_op *res_op, } --depth; + if (maybe_resimplify_conditional_op (seq, res_op, valueize)) + return true; + return false; } @@ -151,6 +245,7 @@ gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op, if (TREE_OVERFLOW_P (tem)) tem = drop_tree_overflow (tem); res_op->set_value (tem); + maybe_resimplify_conditional_op (seq, res_op, valueize); return true; } } @@ -190,6 +285,9 @@ gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op, } --depth; + if (maybe_resimplify_conditional_op (seq, res_op, valueize)) + return true; + return canonicalized; } @@ -221,6 +319,7 @@ gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op, if (TREE_OVERFLOW_P (tem)) tem = drop_tree_overflow (tem); res_op->set_value (tem); + maybe_resimplify_conditional_op (seq, res_op, valueize); return true; } } @@ -257,6 +356,9 @@ gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op, } --depth; + if (maybe_resimplify_conditional_op (seq, res_op, valueize)) + return true; + return canonicalized; } @@ -295,6 +397,9 @@ gimple_resimplify4 (gimple_seq *seq, gimple_match_op *res_op, } --depth; + if (maybe_resimplify_conditional_op (seq, res_op, valueize)) + return true; + return false; } @@ -353,6 +458,12 @@ maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res) tree *ops = res_op->ops; unsigned num_ops = res_op->num_ops; + /* The caller should have converted conditional operations into an UNCOND + form and resimplified as appropriate. The conditional form only + survives this far if that conversion failed. */ + if (res_op->cond.cond) + return NULL_TREE; + if (res_op->code.is_tree_code ()) { if (!res @@ -614,6 +725,50 @@ do_valueize (tree op, tree (*valueize)(tree), bool &valueized) return op; } +/* If RES_OP is a call to a conditional internal function, try simplifying + the associated unconditional operation and using the result to build + a new conditional operation. For example, if RES_OP is: + + IFN_COND_ADD (COND, A, B, ELSE) + + try simplifying (plus A B) and using the result to build a replacement + for the whole IFN_COND_ADD. + + Return true if this approach led to a simplification, otherwise leave + RES_OP unchanged (and so suitable for other simplifications). When + returning true, add any new statements to SEQ and use VALUEIZE as the + valueization function. + + RES_OP is known to be a call to IFN. */ + +static bool +try_conditional_simplification (internal_fn ifn, gimple_match_op *res_op, + gimple_seq *seq, tree (*valueize) (tree)) +{ + tree_code code = conditional_internal_fn_code (ifn); + if (code == ERROR_MARK) + return false; + + unsigned int num_ops = res_op->num_ops; + gimple_match_op cond_op (gimple_match_cond (res_op->ops[0], + res_op->ops[num_ops - 1]), + code, res_op->type, num_ops - 2); + for (unsigned int i = 1; i < num_ops - 1; ++i) + cond_op.ops[i - 1] = res_op->ops[i]; + switch (num_ops - 2) + { + case 2: + if (!gimple_resimplify2 (seq, &cond_op, valueize)) + return false; + break; + default: + gcc_unreachable (); + } + *res_op = cond_op; + maybe_resimplify_conditional_op (seq, res_op, valueize); + return true; +} + /* The main STMT based simplification entry. It is used by the fold_stmt and the fold_stmt_to_constant APIs. */ @@ -699,7 +854,7 @@ gimple_simplify (gimple *stmt, gimple_match_op *res_op, gimple_seq *seq, tree rhs = TREE_OPERAND (rhs1, 1); lhs = do_valueize (lhs, top_valueize, valueized); rhs = do_valueize (rhs, top_valueize, valueized); - gimple_match_op res_op2 (TREE_CODE (rhs1), + gimple_match_op res_op2 (res_op->cond, TREE_CODE (rhs1), TREE_TYPE (rhs1), lhs, rhs); if ((gimple_resimplify2 (seq, &res_op2, valueize) || valueized) @@ -770,6 +925,10 @@ gimple_simplify (gimple *stmt, gimple_match_op *res_op, gimple_seq *seq, tree arg = gimple_call_arg (stmt, i); res_op->ops[i] = do_valueize (arg, top_valueize, valueized); } + if (internal_fn_p (cfn) + && try_conditional_simplification (as_internal_fn (cfn), + res_op, seq, valueize)) + return true; switch (num_args) { case 1: diff --git a/gcc/gimple-match.h b/gcc/gimple-match.h index 69b53f21157..0fe394d169c 100644 --- a/gcc/gimple-match.h +++ b/gcc/gimple-match.h @@ -40,16 +40,57 @@ private: int rep; }; +/* Represents the condition under which an operation should happen, + and the value to use otherwise. The condition applies elementwise + (as for VEC_COND_EXPR) if the values are vectors. */ +struct gimple_match_cond +{ + enum uncond { UNCOND }; + + /* Build an unconditional op. */ + gimple_match_cond (uncond) : cond (NULL_TREE), else_value (NULL_TREE) {} + gimple_match_cond (tree, tree); + + gimple_match_cond any_else () const; + + /* The condition under which the operation occurs, or NULL_TREE + if the operation is unconditional. */ + tree cond; + + /* The value to use when the condition is false. This is NULL_TREE if + the operation is unconditional or if the value doesn't matter. */ + tree else_value; +}; + +inline +gimple_match_cond::gimple_match_cond (tree cond_in, tree else_value_in) + : cond (cond_in), else_value (else_value_in) +{ +} + +/* Return a gimple_match_cond with the same condition but with an + arbitrary ELSE_VALUE. */ + +inline gimple_match_cond +gimple_match_cond::any_else () const +{ + return gimple_match_cond (cond, NULL_TREE); +} + /* Represents an operation to be simplified, or the result of the simplification. */ struct gimple_match_op { - gimple_match_op () : type (NULL_TREE), num_ops (0) {} - gimple_match_op (code_helper, tree, unsigned int); - gimple_match_op (code_helper, tree, tree); - gimple_match_op (code_helper, tree, tree, tree); - gimple_match_op (code_helper, tree, tree, tree, tree); - gimple_match_op (code_helper, tree, tree, tree, tree, tree); + gimple_match_op (); + gimple_match_op (const gimple_match_cond &, code_helper, tree, unsigned int); + gimple_match_op (const gimple_match_cond &, + code_helper, tree, tree); + gimple_match_op (const gimple_match_cond &, + code_helper, tree, tree, tree); + gimple_match_op (const gimple_match_cond &, + code_helper, tree, tree, tree, tree); + gimple_match_op (const gimple_match_cond &, + code_helper, tree, tree, tree, tree, tree); void set_op (code_helper, tree, unsigned int); void set_op (code_helper, tree, tree); @@ -63,6 +104,10 @@ struct gimple_match_op /* The maximum value of NUM_OPS. */ static const unsigned int MAX_NUM_OPS = 4; + /* The conditions under which the operation is performed, and the value to + use as a fallback. */ + gimple_match_cond cond; + /* The operation being performed. */ code_helper code; @@ -76,39 +121,49 @@ struct gimple_match_op tree ops[MAX_NUM_OPS]; }; -/* Constructor that takes the code, type and number of operands, but leaves - the caller to fill in the operands. */ +inline +gimple_match_op::gimple_match_op () + : cond (gimple_match_cond::UNCOND), type (NULL_TREE), num_ops (0) +{ +} + +/* Constructor that takes the condition, code, type and number of + operands, but leaves the caller to fill in the operands. */ inline -gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, +gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in, + code_helper code_in, tree type_in, unsigned int num_ops_in) - : code (code_in), type (type_in), num_ops (num_ops_in) + : cond (cond_in), code (code_in), type (type_in), num_ops (num_ops_in) { } /* Constructors for various numbers of operands. */ inline -gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, +gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in, + code_helper code_in, tree type_in, tree op0) - : code (code_in), type (type_in), num_ops (1) + : cond (cond_in), code (code_in), type (type_in), num_ops (1) { ops[0] = op0; } inline -gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, +gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in, + code_helper code_in, tree type_in, tree op0, tree op1) - : code (code_in), type (type_in), num_ops (2) + : cond (cond_in), code (code_in), type (type_in), num_ops (2) { ops[0] = op0; ops[1] = op1; } inline -gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, +gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in, + code_helper code_in, tree type_in, tree op0, tree op1, tree op2) - : code (code_in), type (type_in), num_ops (3) + : cond (cond_in), code (code_in), type (type_in), num_ops (3) { ops[0] = op0; ops[1] = op1; @@ -116,9 +171,10 @@ gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, } inline -gimple_match_op::gimple_match_op (code_helper code_in, tree type_in, +gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in, + code_helper code_in, tree type_in, tree op0, tree op1, tree op2, tree op3) - : code (code_in), type (type_in), num_ops (4) + : cond (cond_in), code (code_in), type (type_in), num_ops (4) { ops[0] = op0; ops[1] = op1; diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c index a7bb748b66e..eb5493fcd81 100644 --- a/gcc/internal-fn.c +++ b/gcc/internal-fn.c @@ -3219,6 +3219,21 @@ static void (*const internal_fn_expanders[]) (internal_fn, gcall *) = { 0 }; +/* Invoke T(CODE, IFN) for each conditional function IFN that maps to a + tree code CODE. */ +#define FOR_EACH_CODE_MAPPING(T) \ + T (PLUS_EXPR, IFN_COND_ADD) \ + T (MINUS_EXPR, IFN_COND_SUB) \ + T (MULT_EXPR, IFN_COND_MUL) \ + T (TRUNC_DIV_EXPR, IFN_COND_DIV) \ + T (TRUNC_MOD_EXPR, IFN_COND_MOD) \ + T (RDIV_EXPR, IFN_COND_RDIV) \ + T (MIN_EXPR, IFN_COND_MIN) \ + T (MAX_EXPR, IFN_COND_MAX) \ + T (BIT_AND_EXPR, IFN_COND_AND) \ + T (BIT_IOR_EXPR, IFN_COND_IOR) \ + T (BIT_XOR_EXPR, IFN_COND_XOR) + /* Return a function that only performs CODE when a certain condition is met and that uses a given fallback value otherwise. For example, if CODE is a binary operation associated with conditional function FN: @@ -3238,31 +3253,30 @@ get_conditional_internal_fn (tree_code code) { switch (code) { - case PLUS_EXPR: - return IFN_COND_ADD; - case MINUS_EXPR: - return IFN_COND_SUB; - case MIN_EXPR: - return IFN_COND_MIN; - case MAX_EXPR: - return IFN_COND_MAX; - case TRUNC_DIV_EXPR: - return IFN_COND_DIV; - case TRUNC_MOD_EXPR: - return IFN_COND_MOD; - case RDIV_EXPR: - return IFN_COND_RDIV; - case BIT_AND_EXPR: - return IFN_COND_AND; - case BIT_IOR_EXPR: - return IFN_COND_IOR; - case BIT_XOR_EXPR: - return IFN_COND_XOR; +#define CASE(CODE, IFN) case CODE: return IFN; + FOR_EACH_CODE_MAPPING(CASE) +#undef CASE default: return IFN_LAST; } } +/* If IFN implements the conditional form of a tree code, return that + tree code, otherwise return ERROR_MARK. */ + +tree_code +conditional_internal_fn_code (internal_fn ifn) +{ + switch (ifn) + { +#define CASE(CODE, IFN) case IFN: return CODE; + FOR_EACH_CODE_MAPPING(CASE) +#undef CASE + default: + return ERROR_MARK; + } +} + /* Return true if IFN is some form of load from memory. */ bool diff --git a/gcc/internal-fn.h b/gcc/internal-fn.h index 34ea8c67a4e..a1c8b4cbef6 100644 --- a/gcc/internal-fn.h +++ b/gcc/internal-fn.h @@ -193,6 +193,7 @@ direct_internal_fn_supported_p (internal_fn fn, tree type0, tree type1, extern bool set_edom_supported_p (void); extern internal_fn get_conditional_internal_fn (tree_code); +extern tree_code conditional_internal_fn_code (internal_fn); extern bool internal_load_fn_p (internal_fn); extern bool internal_store_fn_p (internal_fn); diff --git a/gcc/match.pd b/gcc/match.pd index 4d0a28dcddf..7e4020eb3ef 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4884,3 +4884,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (with { tree op_type = TREE_TYPE (@4); } (if (element_precision (type) == element_precision (op_type)) (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1))))))) + +/* Detect cases in which a VEC_COND_EXPR effectively replaces the + "else" value of an IFN_COND_*. */ +(for cond_op (COND_BINARY) + (simplify + (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4) + (with { tree op_type = TREE_TYPE (@3); } + (if (element_precision (type) == element_precision (op_type)) + (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))) diff --git a/gcc/target.def b/gcc/target.def index 112c77222a9..ff89e72dd2b 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -2040,6 +2040,25 @@ HOOK_VECTOR_END (vectorize) #define HOOK_PREFIX "TARGET_" DEFHOOK +(preferred_else_value, + "This hook returns the target's preferred final argument for a call\n\ +to conditional internal function @var{ifn} (really of type\n\ +@code{internal_fn}). @var{type} specifies the return type of the\n\ +function and @var{ops} are the operands to the conditional operation,\n\ +of which there are @var{nops}.\n\ +\n\ +For example, if @var{ifn} is @code{IFN_COND_ADD}, the hook returns\n\ +a value of type @var{type} that should be used when @samp{@var{ops}[0]}\n\ +and @samp{@var{ops}[1]} are conditionally added together.\n\ +\n\ +This hook is only relevant if the target supports conditional patterns\n\ +like @code{cond_add@var{m}}. The default implementation returns a zero\n\ +constant of type @var{type}.", + tree, + (unsigned ifn, tree type, unsigned nops, tree *ops), + default_preferred_else_value) + +DEFHOOK (record_offload_symbol, "Used when offloaded functions are seen in the compilation unit and no named\n\ sections are available. It is called once for each symbol that must be\n\ diff --git a/gcc/targhooks.c b/gcc/targhooks.c index 7315f1a9797..9b06d7a8fac 100644 --- a/gcc/targhooks.c +++ b/gcc/targhooks.c @@ -2306,4 +2306,12 @@ default_select_early_remat_modes (sbitmap) { } +/* The default implementation of TARGET_PREFERRED_ELSE_VALUE. */ + +tree +default_preferred_else_value (unsigned, tree type, unsigned, tree *) +{ + return build_zero_cst (type); +} + #include "gt-targhooks.h" diff --git a/gcc/targhooks.h b/gcc/targhooks.h index 4107e22d97c..8d234cf9467 100644 --- a/gcc/targhooks.h +++ b/gcc/targhooks.h @@ -283,5 +283,6 @@ extern enum flt_eval_method default_excess_precision (enum excess_precision_type ATTRIBUTE_UNUSED); extern bool default_stack_clash_protection_final_dynamic_probe (rtx); extern void default_select_early_remat_modes (sbitmap); +extern tree default_preferred_else_value (unsigned, tree, unsigned, tree *); #endif /* GCC_TARGHOOKS_H */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0f21b510d37..8ce8a41a351 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-07-12 Richard Sandiford + + * gcc.dg/vect/vect-cond-arith-2.c: New test. + * gcc.target/aarch64/sve/loop_add_6.c: Likewise. + 2018-07-12 Richard Biener PR target/84829 diff --git a/gcc/testsuite/gcc.dg/vect/vect-cond-arith-2.c b/gcc/testsuite/gcc.dg/vect/vect-cond-arith-2.c new file mode 100644 index 00000000000..15ec005df26 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/vect-cond-arith-2.c @@ -0,0 +1,45 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fgimple -fdump-tree-optimized -ffast-math" } */ + +double __GIMPLE (startwith("loop")) +neg_xi (double *x) +{ + int i; + long unsigned int index; + long unsigned int offset; + double * xi_ptr; + double xi; + double neg_xi; + double res; + unsigned int ivtmp; + + bb_1: + goto bb_2; + + bb_2: + res_1 = __PHI (bb_1: 0.0, bb_3: res_2); + i_4 = __PHI (bb_1: 0, bb_3: i_5); + ivtmp_6 = __PHI (bb_1: 100U, bb_3: ivtmp_7); + index = (long unsigned int) i_4; + offset = index * 8UL; + xi_ptr = x_8(D) + offset; + xi = *xi_ptr; + neg_xi = -xi; + res_2 = neg_xi + res_1; + i_5 = i_4 + 1; + ivtmp_7 = ivtmp_6 - 1U; + if (ivtmp_7 != 0U) + goto bb_3; + else + goto bb_4; + + bb_3: + goto bb_2; + + bb_4: + res_3 = __PHI (bb_2: res_2); + return res_3; +} + +/* { dg-final { scan-tree-dump { = \.COND_ADD} "vect" { target { vect_double_cond_arith && vect_fully_masked } } } } */ +/* { dg-final { scan-tree-dump { = \.COND_SUB} "optimized" { target { vect_double_cond_arith && vect_fully_masked } } } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/loop_add_6.c b/gcc/testsuite/gcc.target/aarch64/sve/loop_add_6.c new file mode 100644 index 00000000000..aab5eddddb0 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/loop_add_6.c @@ -0,0 +1,46 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -ftree-vectorize -fgimple -ffast-math" } */ + +double __GIMPLE (startwith("loop")) +neg_xi (double *x) +{ + int i; + long unsigned int index; + long unsigned int offset; + double * xi_ptr; + double xi; + double neg_xi; + double res; + unsigned int ivtmp; + + bb_1: + goto bb_2; + + bb_2: + res_1 = __PHI (bb_1: 0.0, bb_3: res_2); + i_4 = __PHI (bb_1: 0, bb_3: i_5); + ivtmp_6 = __PHI (bb_1: 100U, bb_3: ivtmp_7); + index = (long unsigned int) i_4; + offset = index * 8UL; + xi_ptr = x_8(D) + offset; + xi = *xi_ptr; + neg_xi = -xi; + res_2 = neg_xi + res_1; + i_5 = i_4 + 1; + ivtmp_7 = ivtmp_6 - 1U; + if (ivtmp_7 != 0U) + goto bb_3; + else + goto bb_4; + + bb_3: + goto bb_2; + + bb_4: + res_3 = __PHI (bb_2: res_2); + return res_3; +} + +/* { dg-final { scan-assembler {\tfsub\tz[0-9]+\.d, p[0-7]/m} } } */ +/* { dg-final { scan-assembler-not {\tsel\t} } } */ +/* { dg-final { scan-assembler-not {\tmovprfx\t} } } */ diff --git a/gcc/tree-ssa-sccvn.c b/gcc/tree-ssa-sccvn.c index d779c66f6e0..4a2f70293b8 100644 --- a/gcc/tree-ssa-sccvn.c +++ b/gcc/tree-ssa-sccvn.c @@ -1786,7 +1786,8 @@ vn_nary_simplify (vn_nary_op_t nary) { if (nary->length > gimple_match_op::MAX_NUM_OPS) return NULL_TREE; - gimple_match_op op (nary->opcode, nary->type, nary->length); + gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode, + nary->type, nary->length); memcpy (op.ops, nary->op, sizeof (tree) * nary->length); return vn_nary_build_or_lookup_1 (&op, false); } @@ -2014,8 +2015,8 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_, else if (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)) { - gimple_match_op res_op (NOP_EXPR, vr->type, - gimple_call_arg (def_stmt, 1)); + gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR, + vr->type, gimple_call_arg (def_stmt, 1)); val = vn_nary_build_or_lookup (&res_op); if (!val || (TREE_CODE (val) == SSA_NAME @@ -2155,7 +2156,8 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_, || known_eq (ref->size, TYPE_PRECISION (vr->type))) && multiple_p (ref->size, BITS_PER_UNIT)) { - gimple_match_op op (BIT_FIELD_REF, vr->type, + gimple_match_op op (gimple_match_cond::UNCOND, + BIT_FIELD_REF, vr->type, SSA_VAL (gimple_assign_rhs1 (def_stmt)), bitsize_int (ref->size), bitsize_int (offset - offset2)); @@ -3686,7 +3688,8 @@ visit_nary_op (tree lhs, gassign *stmt) unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1)); if (lhs_prec == rhs_prec) { - gimple_match_op match_op (NOP_EXPR, type, ops[0]); + gimple_match_op match_op (gimple_match_cond::UNCOND, + NOP_EXPR, type, ops[0]); result = vn_nary_build_or_lookup (&match_op); if (result) { @@ -3699,7 +3702,8 @@ visit_nary_op (tree lhs, gassign *stmt) { tree mask = wide_int_to_tree (type, wi::mask (rhs_prec, false, lhs_prec)); - gimple_match_op match_op (BIT_AND_EXPR, + gimple_match_op match_op (gimple_match_cond::UNCOND, + BIT_AND_EXPR, TREE_TYPE (lhs), ops[0], mask); result = vn_nary_build_or_lookup (&match_op); @@ -3823,7 +3827,8 @@ visit_reference_op_load (tree lhs, tree op, gimple *stmt) of VIEW_CONVERT_EXPR (result). So first simplify and lookup this expression to see if it is already available. */ - gimple_match_op res_op (VIEW_CONVERT_EXPR, TREE_TYPE (op), result); + gimple_match_op res_op (gimple_match_cond::UNCOND, + VIEW_CONVERT_EXPR, TREE_TYPE (op), result); result = vn_nary_build_or_lookup (&res_op); } -- 2.11.4.GIT