From patchwork Mon Nov 15 00:08:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 47643 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E490385842D for ; Mon, 15 Nov 2021 00:09:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E490385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636934965; bh=JHiQSu+XVCCnpqq2gbR6CvaVdFREJmsez75tfHtHNbs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=lWgE+2EmhZVjwA13Rslt0szmtE5+1sAgM5xIF34jXnuixn7NhCGyxWq2Z2m7lhS+J eYSUMG/n8Osk44CcF8fGcHf1kXDtK7JMQvkJHJ9TwH59rpesAy0S7qj8x8zA1rntIN Ps8o0cBX9h9F1tP9KdXfLO4IgPLWXdzsw8pKkh0w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 8B5233858D39 for ; Mon, 15 Nov 2021 00:08:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8B5233858D39 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 1AEGqNhh009420 for ; Sun, 14 Nov 2021 16:08:55 -0800 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3cadrsvb6u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 14 Nov 2021 16:08:54 -0800 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Sun, 14 Nov 2021 16:08:52 -0800 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Sun, 14 Nov 2021 16:08:52 -0800 Received: from linux.wrightpinski.org.com (unknown [10.69.242.198]) by maili.marvell.com (Postfix) with ESMTP id 77D2B3F7045; Sun, 14 Nov 2021 16:08:52 -0800 (PST) To: Subject: [PATCH] PR tree-optimization/103216: optimize some A ? (b op CST) : b into b op (A?CST:CST2) Date: Sun, 14 Nov 2021 16:08:49 -0800 Message-ID: <1636934929-842-1-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: mdobCBBo5bP2k9AtWylUUG39oDoYxFkI X-Proofpoint-GUID: mdobCBBo5bP2k9AtWylUUG39oDoYxFkI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.0.607.475 definitions=2021-11-14_10,2021-11-12_01,2020-04-07_01 X-Spam-Status: No, score=-14.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: apinski--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: apinski@marvell.com Cc: Andrew Pinski Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Andrew Pinski For this PR, we have: if (d_5 < 0) goto ; [INV] else goto ; [INV] : v_7 = c_4 | -128; : # v_1 = PHI Which PHI-OPT will try to simplify "(d_5 < 0) ? (c_4 | -128) : c_4" which is not handled currently. This adds a few patterns which allows to try to see if (a ? CST : CST1) where CST1 is either 0, 1 or -1 depending on the operator. Note to optimize this case always, we should check to make sure that the a?CST:CST1 gets simplified to not include the conditional expression. The ! flag does not work as we want to have more simplifcations than just when we simplify it to a leaf node (SSA_NAME or CONSTANT). This adds a new flag ^ to genmatch which says the simplification should happen but not down to the same kind of node. We could allow this for !GIMPLE and use fold_* rather than fold_buildN but I didn't see any use of it for now. Also all of these patterns need to be done late as other optimizations can be done without them. OK? Bootstrapped and tested on x86_64 with no regressions. gcc/ChangeLog: * doc/match-and-simplify.texi: Document ^ flag. * genmatch.c (expr::expr): Add Setting of force_simplify. (expr): Add force_simplify field. (expr::gen_transform): Add support for force_simplify field. (parser::parse_expr): Add parsing of ^ flag for the expr. * match.pd: New patterns to optimize "a ? (b op CST) : b". --- gcc/doc/match-and-simplify.texi | 16 +++++++++++++ gcc/genmatch.c | 35 ++++++++++++++++++++++++++-- gcc/match.pd | 41 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/gcc/doc/match-and-simplify.texi b/gcc/doc/match-and-simplify.texi index e7e5a4f7299..4e3407c0263 100644 --- a/gcc/doc/match-and-simplify.texi +++ b/gcc/doc/match-and-simplify.texi @@ -377,6 +377,22 @@ of the @code{vec_cond} expression but only if the actual plus operations both simplify. Note this is currently only supported for code generation targeting @code{GIMPLE}. +Another modifier for generated expressions is @code{^} which +tells the machinery to only consider the simplification in case +the marked expression simplified away from the original code. +Consider for example + +@smallexample +(simplify + (cond @@0 (plus:s @@1 INTEGER_CST@@2) @@1) + (plus @@1 (cond^ @@0 @@2 @{ build_zero_cst (type); @}))) +@end smallexample + +which moves the inner @code{plus} operation to the outside of the +@code{cond} expression but only if the actual cond operation simplify +wayaway from cond. Note this is currently only supported for code +generation targeting @code{GIMPLE}. + As intermediate conversions are often optional there is a way to avoid the need to repeat patterns both with and without such conversions. Namely you can mark a conversion as being optional diff --git a/gcc/genmatch.c b/gcc/genmatch.c index 95248455ec5..2dca1141df6 100644 --- a/gcc/genmatch.c +++ b/gcc/genmatch.c @@ -698,12 +698,13 @@ public: : operand (OP_EXPR, loc), operation (operation_), ops (vNULL), expr_type (NULL), is_commutative (is_commutative_), is_generic (false), force_single_use (false), force_leaf (false), - opt_grp (0) {} + force_simplify(false), opt_grp (0) {} expr (expr *e) : operand (OP_EXPR, e->location), operation (e->operation), ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative), is_generic (e->is_generic), force_single_use (e->force_single_use), - force_leaf (e->force_leaf), opt_grp (e->opt_grp) {} + force_leaf (e->force_leaf), force_simplify(e->force_simplify), + opt_grp (e->opt_grp) {} void append_op (operand *op) { ops.safe_push (op); } /* The operator and its operands. */ id_base *operation; @@ -721,6 +722,9 @@ public: /* Whether in the result expression this should be a leaf node with any children simplified down to simple operands. */ bool force_leaf; + /* Whether in the result expression this should be a node + with any children simplified down not to use the original operator. */ + bool force_simplify; /* If non-zero, the group for optional handling. */ unsigned char opt_grp; virtual void gen_transform (FILE *f, int, const char *, bool, int, @@ -2527,6 +2531,17 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple, fprintf (f, ", _o%d[%u]", depth, i); fprintf (f, ");\n"); fprintf_indent (f, indent, "tem_op.resimplify (lseq, valueize);\n"); + if (force_simplify) + { + fprintf_indent (f, indent, "if (tem_op.code.is_tree_code ())\n"); + fprintf_indent (f, indent, " {\n"); + indent+=4; + fprintf_indent (f, indent, "if (((tree_code)tem_op.code) == %s)\n", + opr_name); + fprintf_indent (f, indent, " goto %s;\n", fail_label); + indent-=4; + fprintf_indent (f, indent, " }\n"); + } fprintf_indent (f, indent, "_r%d = maybe_push_res_to_seq (&tem_op, %s);\n", depth, !force_leaf ? "lseq" : "NULL"); @@ -4304,6 +4319,22 @@ parser::parse_expr () e->force_leaf = true; } + if (!parsing_match_operand + && token->type == CPP_XOR + && !(token->flags & PREV_WHITE)) + { + if (!gimple) + fatal_at (token, "forcing simplification is not supported for GENERIC"); + if (e->force_leaf) + fatal_at (token, "forcing simplification and forcing to a leaf is not " + "supported"); + if (e->operation->kind != id_base::CODE) + fatal_at (token, "forcing simplification is not supported on non-CODE " + "operations"); + eat_token (CPP_XOR); + e->force_simplify = true; + } + if (token->type == CPP_COLON && !(token->flags & PREV_WHITE)) { diff --git a/gcc/match.pd b/gcc/match.pd index df31964e02f..c66e918bb65 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4186,6 +4186,47 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) #endif +#if GIMPLE +(if (canonicalize_math_p ()) +/* These patterns are mostly used by PHIOPT to move some operations outside of + the if statements. They should be done late because it gives jump threading + and few other passes to reduce what is going on. */ +/* a ? x op CST : x -> x op (a ? CST : 0) if (a ? CST : 0) can be simplified. */ + (for op (plus minus bit_ior bit_xor lshift rshift lrotate rrotate) + (simplify + (cond @0 (op:s @1 INTEGER_CST@2) @1) + (op @1 (cond^ @0 @2 { build_zero_cst (type); })) + ) + (simplify + (cond @0 @1 (op:s @1 INTEGER_CST@2)) + (op @1 (cond^ @0 { build_zero_cst (type); } @2)) + ) + ) +/* a ? x op CST : x -> x op (a ? CST : 1) if (a ? CST : 1) can be simplified. */ + (for op (mult trunc_div ceil_div floor_div) + (simplify + (cond @0 (op:s @1 INTEGER_CST@2) @1) + (op @1 (cond^ @0 @2 { build_one_cst (type); })) + ) + (simplify + (cond @0 @1 (op:s @1 INTEGER_CST@2)) + (op @1 (cond^ @0 { build_one_cst (type); } @2)) + ) + ) +/* a ? x op CST : x -> x op (a ? CST : -1) if (a ? CST : -1) can be simplified. */ + (for op (bit_and) + (simplify + (cond @0 (op @1 INTEGER_CST@2) @1) + (op @1 (cond^ @0 @2 { build_all_ones_cst (type); })) + ) + (simplify + (cond @0 @1 (op @1 INTEGER_CST@2)) + (op @1 (cond^ @0 { build_all_ones_cst (type); } @2)) + ) + ) +) +#endif + /* Simplification moved from fold_cond_expr_with_comparison. It may also be extended. */ /* This pattern implements two kinds simplification: