From patchwork Wed May 11 11:19:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 53802 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 51C3838485A6 for ; Wed, 11 May 2022 11:19:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51C3838485A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652267987; bh=yfAEWwzqeDgGsHny8WkkQUKCaqfexppZOeolz5OK3Og=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RJ3BN8i1ZIexN1ouM6AuwbJ9O9tFtDU0GzCWMzPrjR3yzarIKSDI973CSXGIHklCW ANmuzMPl1JmUZnjXaXD/onjEy3cAAvmg4CtreG17ak/Yxz1oqN7FsNpU7DxAvvZ/qm xxaMhClqdO183TpNWpMt+DZFwxGImpzbMJ1159FM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 90DD63857402 for ; Wed, 11 May 2022 11:19:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 90DD63857402 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 5A7AF1F8C6 for ; Wed, 11 May 2022 11:19:16 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3CC0B13A76 for ; Wed, 11 May 2022 11:19:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7cy3DbSbe2KWQwAAMHmgww (envelope-from ) for ; Wed, 11 May 2022 11:19:16 +0000 Date: Wed, 11 May 2022 13:19:15 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Implement some of fold_binary_op_with_conditional_arg in match.pd MIME-Version: 1.0 Message-Id: <20220511111916.3CC0B13A76@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The following allows (c != 0 ? 0 : 100) != 0 to be simplified as c != 0 as fold_binary_op_with_conditional_arg would have done via forwprop and GENERIC folding. Likewise it allows to combine (a != 0) != 0 directly via match.pd instead of only via forwprop and again fold_binary_op_with_conditional_arg. The patterns do not fully implement all cases of fold_binary_op_with_conditional_arg, some aspects like "any of the operands simplify" cannot currently be expressed. Bootstrapped and tested on x86_64-unknown-linux-gnu. 2022-05-11 Richard Biener * generic-match-head.cc: Include tree-eh.h. * match.pd ((cond ...) cmp X): New simplification inspired by fold_binary_op_with_conditional_arg. (eq/ne (cmp ...) true/false): Likewise. * gcc.dg/tree-ssa/pr61839_1.c: Adjust. * gcc.dg/tree-ssa/vrp24.c: Likewise. --- gcc/generic-match-head.cc | 1 + gcc/match.pd | 41 ++++++++++++++++++++++- gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/vrp24.c | 4 +-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc index e11a736b343..cb0fbd32fa6 100644 --- a/gcc/generic-match-head.cc +++ b/gcc/generic-match-head.cc @@ -38,6 +38,7 @@ along with GCC; see the file COPYING3. If not see #include "optabs-tree.h" #include "dbgcnt.h" #include "tm.h" +#include "tree-eh.h" /* Routine to determine if the types T1 and T2 are effectively the same for GENERIC. If T1 or T2 is not a type, the test diff --git a/gcc/match.pd b/gcc/match.pd index 632243ea92e..f5efa77560c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4656,7 +4656,34 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (ic == icmp) (icmp @0 @1) (if (ic == ncmp) - (ncmp @0 @1)))))) + (ncmp @0 @1))))) + /* The following bits are handled by fold_binary_op_with_conditional_arg. */ + (simplify + (ne (cmp@2 @0 @1) integer_zerop) + (if (types_match (type, TREE_TYPE (@2))) + (cmp @0 @1))) + (simplify + (eq (cmp@2 @0 @1) integer_truep) + (if (types_match (type, TREE_TYPE (@2))) + (cmp @0 @1))) + (simplify + (ne (cmp@2 @0 @1) integer_truep) + (if (types_match (type, TREE_TYPE (@2))) + (with { enum tree_code ic = invert_tree_comparison + (cmp, HONOR_NANS (@0)); } + (if (ic == icmp) + (icmp @0 @1) + (if (ic == ncmp) + (ncmp @0 @1)))))) + (simplify + (eq (cmp@2 @0 @1) integer_zerop) + (if (types_match (type, TREE_TYPE (@2))) + (with { enum tree_code ic = invert_tree_comparison + (cmp, HONOR_NANS (@0)); } + (if (ic == icmp) + (icmp @0 @1) + (if (ic == ncmp) + (ncmp @0 @1))))))) /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. ??? The transformation is valid for the other operators if overflow @@ -5486,6 +5513,18 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp (bit_and@2 @0 integer_pow2p@1) @1) (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) +/* From fold_binary_op_with_conditional_arg handle the case of + rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the + compares simplify. */ +(for cmp (simple_comparison) + (simplify + (cmp:c (cond @0 @1 @2) @3) + /* Do not move possibly trapping operations into the conditional as this + pessimizes code and causes gimplification issues when applied late. */ + (if (!FLOAT_TYPE_P (TREE_TYPE (@3)) + || operation_could_trap_p (cmp, true, false, @3)) + (cond @0 (cmp! @1 @3) (cmp! @2 @3))))) + (for cmp (ge lt) /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */ /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c index f5af7a1d6b6..d41256736a2 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c @@ -38,7 +38,7 @@ int main () } /* Scan for c = 972195717) >> [0, 1] in function foo. */ -/* { dg-final { scan-tree-dump-times "486097858 : 972195717" 1 "vrp1" } } */ +/* { dg-final { scan-tree-dump-times "972195717 : 486097858" 1 "vrp1" } } */ /* Previously we were checking for two ?: with constant PHI arguments, but now we collapse them into one. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c index 91015da86ae..c28ca473fc6 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-evrp-details -fdump-tree-optimized -fno-tree-ccp" } */ +/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-evrp-details -fdump-tree-optimized -fno-tree-ccp --param logical-op-non-short-circuit=1" } */ struct rtx_def; @@ -89,5 +89,5 @@ L7: boolean operation. */ /* { dg-final { scan-tree-dump-times "Simplified relational" 2 "evrp" } } */ -/* { dg-final { scan-tree-dump-times "if " 4 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if " 3 "optimized" } } */