This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] teach phi-opt to produce -(a COND b)


On Fri, Apr 27, 2012 at 3:01 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
> This patch teaches phiopt to look at phis whose arguments are -1 and 0,
> and produce negated setcc statements.
>
> Bootstrapped/regtested x86_64-pc-linux-gnu, together with the patch
> for pr53138. ÂOk for mainline?

I came up with this same patch independently.  I was going to submit
it but I never got around to it.
Thanks for submitting this.

-- Andrew Pinski

>
> Paolo
>
> 2012-04-27 ÂPaolo Bonzini Â<bonzini@gnu.org>
>
> Â Â Â Â* tree-ssa-phiopt.c (conditional_replacement): Replace PHIs
> Â Â Â Âwhose arguments are -1 and 0, by negating the result of the
> Â Â Â Âconditional.
>
> 2012-04-27 ÂPaolo Bonzini Â<bonzini@gnu.org>
>
> Â Â Â Â* gcc.c-torture/execute/20120427-2.c: New testcase.
> Â Â Â Â* gcc.dg/tree-ssa/phi-opt-10.c: New testcase.
>
>
> Index: tree-ssa-phiopt.c
> ===================================================================
> --- tree-ssa-phiopt.c  (revisione 186859)
> +++ tree-ssa-phiopt.c  (copia locale)
> @@ -536,17 +536,21 @@
> Â gimple_stmt_iterator gsi;
> Â edge true_edge, false_edge;
> Â tree new_var, new_var2;
> + Âbool neg;
>
> Â /* FIXME: Gimplification of complex type is too hard for now. Â*/
> Â if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
> Â Â Â || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE)
> Â Â return false;
>
> - Â/* The PHI arguments have the constants 0 and 1, then convert
> - Â Â it to the conditional. Â*/
> + Â/* The PHI arguments have the constants 0 and 1, or 0 and -1, then
> + Â Â convert it to the conditional. Â*/
> Â if ((integer_zerop (arg0) && integer_onep (arg1))
> Â Â Â || (integer_zerop (arg1) && integer_onep (arg0)))
> - Â Â;
> + Â Âneg = false;
> + Âelse if ((integer_zerop (arg0) && integer_all_onesp (arg1))
> + Â Â Â Â Â|| (integer_zerop (arg1) && integer_all_onesp (arg0)))
> + Â Âneg = true;
> Â else
> Â Â return false;
>
> @@ -558,7 +562,7 @@
> Â Â Âfalls through into BB.
>
> Â Â ÂThere is a single PHI node at the join point (BB) and its arguments
> - Â Â are constants (0, 1).
> + Â Â are constants (0, 1) or (0, -1).
>
> Â Â ÂSo, given the condition COND, and the two PHI arguments, we can
> Â Â Ârewrite this PHI into non-branching code:
> @@ -585,12 +589,20 @@
> Â Â Âedge so that we know when to invert the condition below. Â*/
> Â extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
> Â if ((e0 == true_edge && integer_zerop (arg0))
> - Â Â Â|| (e0 == false_edge && integer_onep (arg0))
> + Â Â Â|| (e0 == false_edge && !integer_zerop (arg0))
> Â Â Â || (e1 == true_edge && integer_zerop (arg1))
> - Â Â Â|| (e1 == false_edge && integer_onep (arg1)))
> + Â Â Â|| (e1 == false_edge && !integer_zerop (arg1)))
> Â Â cond = fold_build1_loc (gimple_location (stmt),
> - Â Â Â Â Â Â Â Â Â Â Â Â Â TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â ÂTRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
>
> + Âif (neg)
> + Â Â{
> + Â Â Âcond = fold_convert_loc (gimple_location (stmt),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â TREE_TYPE (result), cond);
> + Â Â Âcond = fold_build1_loc (gimple_location (stmt),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂNEGATE_EXPR, TREE_TYPE (cond), cond);
> + Â Â}
> +
> Â /* Insert our new statements at the end of conditional block before the
> Â Â ÂCOND_STMT. Â*/
> Â gsi = gsi_for_stmt (stmt);
> Index: testsuite/gcc.c-torture/execute/20120427-2.c
> ===================================================================
> --- testsuite/gcc.c-torture/execute/20120427-2.c    Â(revisione 0)
> +++ testsuite/gcc.c-torture/execute/20120427-2.c    Â(revisione 0)
> @@ -0,0 +1,38 @@
> +typedef struct sreal
> +{
> + Âunsigned sig; Â Â Â Â Â Â Â Â/* Significant. Â*/
> + Âint exp; Â Â Â Â Â Â /* Exponent. Â*/
> +} sreal;
> +
> +sreal_compare (sreal *a, sreal *b)
> +{
> + Âif (a->exp > b->exp)
> + Â Âreturn 1;
> + Âif (a->exp < b->exp)
> + Â Âreturn -1;
> + Âif (a->sig > b->sig)
> + Â Âreturn 1;
> + Âif (a->sig < b->sig)
> + Â Âreturn -1;
> + Âreturn 0;
> +}
> +
> +sreal a[] = {
> + Â { 0, 0 },
> + Â { 1, 0 },
> + Â { 0, 1 },
> + Â { 1, 1 }
> +};
> +
> +int main()
> +{
> + Âint i, j;
> + Âfor (i = 0; i <= 3; i++) {
> + Â Âfor (j = 0; j < 3; j++) {
> + Â Â Âif (i < j && sreal_compare(&a[i], &a[j]) != -1) abort();
> + Â Â Âif (i == j && sreal_compare(&a[i], &a[j]) != 0) abort();
> + Â Â Âif (i > j && sreal_compare(&a[i], &a[j]) != 1) abort();
> + Â Â}
> + Â}
> + Âreturn 0;
> +}
> Index: testsuite/gcc.dg/tree-ssa/phi-opt-10.c
> ===================================================================
> --- testsuite/gcc.dg/tree-ssa/phi-opt-10.c   Â(revisione 0)
> +++ testsuite/gcc.dg/tree-ssa/phi-opt-10.c   Â(revisione 0)
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-optimized" } */
> +
> +int nem1_phi (unsigned long a) { return a ? -1 : 0; }
> +int eqm1_phi (unsigned long a) { return a ? 0 : -1; }
> +
> +int spaceship1 (long a) { return a > 0 ? 1 : a < 0 ? -1 : 0; }
> +int spaceship2 (long a) { return a > 0 ? 1 : a == 0 ? 0 : -1; }
> +
> +/* { dg-final { scan-tree-dump-times " = -D" 4 "optimized"} } */
> +/* { dg-final { cleanup-tree-dump "optimized" } } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]