From patchwork Wed Aug 2 23:52:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 130218 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp800918vqx; Wed, 2 Aug 2023 16:53:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlEN0sRVUlRkoYAFI6TOP1hLilCzGITRTn9Z4KRYVb8uHio6haoNU34Z2947tldNW+kMJXSo X-Received: by 2002:a17:907:a04b:b0:994:1eb4:6896 with SMTP id gz11-20020a170907a04b00b009941eb46896mr9691528ejc.25.1691020422367; Wed, 02 Aug 2023 16:53:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691020422; cv=none; d=google.com; s=arc-20160816; b=ujcJOtvUZ6t4lO/P16vnYhO1X/lVe9yodNzXUWzmvSyuRMS+/yrCSnBLQEQbP+bMWQ 16zoBDBlqJhAFxAU/Qq77GVKWKbTS0ajOHuI7JL3I88ivvuzm2EhARgCv5KRTGX465fZ rv/7t+VDVbZA7MaLfce9BF5Z7yQM7V0SzpCwwGq/DCz6WAZTPGmFOBNLrt++q0GQV6zh DFyWqb7VGWyAcRz9zzZgJPJ9hk+z4cszLoo9hERipTjMU3Xs9To95TlviunFebOmxSYv p5abVC/htfLrAPmim0Qj7wQVbw5g81Do2TyUPxuNkpYllt3AWw3Oq+qj0/nRiczazg3Z 8oyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=yhsFeklCaD11Yw7K9U6HqMlPrHUwpEfIPNFmc7hyrqU=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=WlSuLFO5qmJFcsI40LOL4ykcvmGt4IXJVsaZxAzQRnvOEChgSR7L0ApQ0WpOiORHmZ eaTvKjHhTTsxZXSgg3IcK7l9JwVQ9sKNV0viqXXNgxPyIEAi2udT1DEXL+gemTKeWmFJ rd/JcV/8Wd+7uJJgzgJzqi4Im0IUc5awr/mPuM2HWI0tW+tyojCweYR1oNTLz/5HTzdt HbocZHa58k02LMRP2dDvx9XIETwL61+LGYl5s6shff87TnIKxJqHwZiBRYdC8koJNfCp HRLaqeszuaA1Pd024gaDlfkk7r93gZqzkMLYFeasWdoTPvODtscMeBZl7wEB9KmE/HGg nfRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K4riAW3o; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u7-20020a170906408700b009936735754dsi314261ejj.897.2023.08.02.16.53.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:53:42 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K4riAW3o; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4BE593858C2A for ; Wed, 2 Aug 2023 23:53:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BE593858C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691020421; bh=yhsFeklCaD11Yw7K9U6HqMlPrHUwpEfIPNFmc7hyrqU=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=K4riAW3oJpbrcP72+QkeZn2jjLaRufhtlHorY3i+2oF4kgDX8YbuuZcsEz92CkdwF 9d9jluWI5Sn9Db+ATX4EFhxX6ud30iD0yMWt1WtDu1GZyI4NG66mlOTaZRYYV0km0p WhJY/Kz5H2dB/ivs/KDJPVZDlBEiCTy11lJa0qik= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id D19FA3858C50 for ; Wed, 2 Aug 2023 23:52:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D19FA3858C50 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 372Kc0Ao012045 for ; Wed, 2 Aug 2023 16:52:55 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3s7xbc8fpr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 02 Aug 2023 16:52:55 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 2 Aug 2023 16:52:53 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 2 Aug 2023 16:52:53 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 71B6C5C68E2; Wed, 2 Aug 2023 16:52:53 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: first of the value replacement moving from phiopt Date: Wed, 2 Aug 2023 16:52:32 -0700 Message-ID: <20230802235232.2265424-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: ObyF0GpCgHpQ6t-y-xpuQCm-ji52rlsr X-Proofpoint-ORIG-GUID: ObyF0GpCgHpQ6t-y-xpuQCm-ji52rlsr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-02_19,2023-08-01_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773163430481337711 X-GMAIL-MSGID: 1773163430481337711 This moves a few simple patterns that are done in value replacement in phiopt over to match.pd. Just the simple ones which might show up in other code. This allows some optimizations to happen even without depending on sinking from happening and in some cases where phiopt is not invoked (cond-1.c is an example there). OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (`a == 0 ? b : b + a`, `a == 0 ? b : b - a`): New patterns. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cond-1.c: New test. * gcc.dg/tree-ssa/phi-opt-33.c: New test. * gcc.dg/tree-ssa/phi-opt-34.c: New test. --- gcc/match.pd | 14 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cond-1.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c | 17 +++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cond-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c diff --git a/gcc/match.pd b/gcc/match.pd index c62f205c13c..1ceff9691a0 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3832,6 +3832,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) (op (mult (convert:type @0) @2) @1)))) +/* ?: Value replacement. */ +/* a == 0 ? b : b + a -> b + a */ +(for op (plus bit_ior bit_xor) + (simplify + (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0)) + @2)) +/* a == 0 ? b : b - a -> b - a */ +/* a == 0 ? b : b ptr+ a -> b ptr+ a */ +/* a == 0 ? b : b shift/rotate a -> b shift/rotate a */ +(for op (lrotate rrotate lshift rshift minus pointer_plus) + (simplify + (cond (eq @0 integer_zerop) @1 (op@2 @1 @0)) + @2)) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cond-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cond-1.c new file mode 100644 index 00000000000..478a818b206 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cond-1.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +int sub(int a, int b, int c, int d) { + int e = (a == 0); + int f = !e; + c = b; + d = b - a ; + return ((-e & c) | (-f & d)); +} + +/* In the end we end up with `(a == 0) ? (b - a) : b` + which then can be optimized to just `(b - a)`. */ + +/* { dg-final { scan-tree-dump-not "cond_expr," "optimized" } } */ +/* { dg-final { scan-tree-dump-not "eq_expr," "optimized" } } */ +/* { dg-final { scan-tree-dump-times "minus_expr," 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c new file mode 100644 index 00000000000..809ccfe1479 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* Phi-OPT should be able to optimize this without sinking being invoked. */ +/* { dg-options "-O -fdump-tree-phiopt2 -fdump-tree-optimized -fno-tree-sink" } */ + +int f(int a, int b, int c) { + int d = a + b; + if (c > 5) return c; + if (a == 0) return b; + return d; +} + +unsigned rot(unsigned x, int n) { + const int bits = __CHAR_BIT__ * __SIZEOF_INT__; + int t = ((x << n) | (x >> (bits - n))); + return (n == 0) ? x : t; +} + +/* { dg-final { scan-tree-dump-times "goto" 2 "phiopt2" } } */ +/* { dg-final { scan-tree-dump-times "goto" 2 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c new file mode 100644 index 00000000000..a90de8926c6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* Phi-OPT should be able to optimize this without sinking being invoked. */ +/* { dg-options "-O -fdump-tree-phiopt2 -fdump-tree-optimized -fno-tree-sink" } */ + +char *f(char *a, __SIZE_TYPE__ b) { + char *d = a + b; + if (b == 0) return a; + return d; +} +int sub(int a, int b, int c) { + int d = a - b; + if (b == 0) return a; + return d; +} + +/* { dg-final { scan-tree-dump-not "goto" "phiopt2" } } */ +/* { dg-final { scan-tree-dump-not "goto" "optimized" } } */