Skip to content

Commit

Permalink
middle-end: refactory vect_recog_absolute_difference to simplify flow…
Browse files Browse the repository at this point in the history
… [PR114769]

Hi All,

As the reporter in PR114769 points out the control flow for the abd detection
is hard to follow.  This is because vect_recog_absolute_difference has two
different ways it can return true.

1. It can return true when the widening operation is matched, in which case
   unprom is set, half_type is not NULL and diff_stmt is not set.

2. It can return true when the widening operation is not matched, but the stmt
   being checked is a minus.  In this case unprom is not set, half_type is set
   to NULL and diff_stmt is set.  This because to get to diff_stmt you have to
   dig through the abs statement and any possible promotions.

This however leads to complicated uses of the function at the call sites as the
exact semantic needs to be known to use it safely.

vect_recog_absolute_difference has two callers:

1. vect_recog_sad_pattern where if you return true with unprom not set, then
   *half_type will be NULL.  The call to vect_supportable_direct_optab_p will
   always reject it since there's no vector mode for NULL.  Note that if looking
   at the dump files, the convention in the dump files have always been that we
   first indicate that a pattern could possibly be recognize and then check that
   it's supported.

   This change somewhat incorrectly makes the diagnostic message get printed for
   "invalid" patterns.

2. vect_recog_abd_pattern, where if half_type is NULL, it then uses diff_stmt to
   set them.

This refactors the code, it now only has 1 success condition, and diff_stmt is
always set to the minus statement in the abs if there is one.

The function now only returns success if the widening minus is found, in which
case unprom and half_type set.

This then leaves it up to the caller to decide if they want to do anything with
diff_stmt.

Thanks,
Tamar

gcc/ChangeLog:

	PR tree-optimization/114769
	* tree-vect-patterns.cc:
	(vect_recog_absolute_difference): Have only one success condition.
	(vect_recog_abd_pattern): Handle further checks if
	vect_recog_absolute_difference fails.
  • Loading branch information
TamarChristinaArm committed Apr 19, 2024
1 parent 9451b6c commit 1216460
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions gcc/tree-vect-patterns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,7 @@ vect_split_statement (vec_info *vinfo, stmt_vec_info stmt2_info, tree new_rhs,
HALF_TYPE and UNPROM will be set should the statement be found to
be a widened operation.
DIFF_STMT will be set to the MINUS_EXPR
statement that precedes the ABS_STMT unless vect_widened_op_tree
succeeds.
statement that precedes the ABS_STMT if it is a MINUS_EXPR..
*/
static bool
vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt,
Expand Down Expand Up @@ -843,24 +842,19 @@ vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt,
if (!diff_stmt_vinfo)
return false;

gassign *diff = dyn_cast <gassign *> (STMT_VINFO_STMT (diff_stmt_vinfo));
if (diff_stmt && diff
&& gimple_assign_rhs_code (diff) == MINUS_EXPR
&& TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd)))
*diff_stmt = diff;

/* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
inside the loop (in case we are analyzing an outer-loop). */
if (vect_widened_op_tree (vinfo, diff_stmt_vinfo,
MINUS_EXPR, IFN_VEC_WIDEN_MINUS,
false, 2, unprom, half_type))
return true;

/* Failed to find a widen operation so we check for a regular MINUS_EXPR. */
gassign *diff = dyn_cast <gassign *> (STMT_VINFO_STMT (diff_stmt_vinfo));
if (diff_stmt && diff
&& gimple_assign_rhs_code (diff) == MINUS_EXPR
&& TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd)))
{
*diff_stmt = diff;
*half_type = NULL_TREE;
return true;
}

return false;
}

Expand Down Expand Up @@ -1499,27 +1493,22 @@ vect_recog_abd_pattern (vec_info *vinfo,
tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt));

vect_unpromoted_value unprom[2];
gassign *diff_stmt;
tree half_type;
if (!vect_recog_absolute_difference (vinfo, last_stmt, &half_type,
gassign *diff_stmt = NULL;
tree abd_in_type;
if (!vect_recog_absolute_difference (vinfo, last_stmt, &abd_in_type,
unprom, &diff_stmt))
return NULL;

tree abd_in_type, abd_out_type;

if (half_type)
{
abd_in_type = half_type;
abd_out_type = abd_in_type;
}
else
{
/* We cannot try further without having a non-widening MINUS. */
if (!diff_stmt)
return NULL;

unprom[0].op = gimple_assign_rhs1 (diff_stmt);
unprom[1].op = gimple_assign_rhs2 (diff_stmt);
abd_in_type = signed_type_for (out_type);
abd_out_type = abd_in_type;
}

tree abd_out_type = abd_in_type;

tree vectype_in = get_vectype_for_scalar_type (vinfo, abd_in_type);
if (!vectype_in)
return NULL;
Expand Down

0 comments on commit 1216460

Please sign in to comment.