From patchwork Sun Oct 29 16:40:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1856760 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=KFwomFXN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SJMcR3tNDz1yQ4 for ; Mon, 30 Oct 2023 03:41:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D3643875DCF for ; Sun, 29 Oct 2023 16:41:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x32.google.com (mail-oa1-x32.google.com [IPv6:2001:4860:4864:20::32]) by sourceware.org (Postfix) with ESMTPS id 0C73F385828D for ; Sun, 29 Oct 2023 16:40:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C73F385828D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0C73F385828D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597656; cv=none; b=ia41vDIbVDW6AeaUSBZqk96aIgr6lQxA5yyiSBp+qv8l+EDMNZnbe/FlPUMrk5csKCW/OHpT5DzgvN2Uow30xvJF2pYD+WyFLJl8kRISYwvE/puitWOsiaJzQiroae65cnVGIPmFCuLmdmJAZ5vJHw1JQEZgblFE89Ov/SLFgXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597656; c=relaxed/simple; bh=hyDABztYkimnI9bCoIsASPtzLkZ2bmtQby5zReeISns=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=a0VwSq/vEk8UwezQTL5LILGDzBhI6qsyKZB2OWJxcexg+f9JP89efGt4X6ex1WdN8Q5NBLlCgzSe6EBuRKD/qUR1K/q9RmCrgtWvu7zgdmYOXUqyQPliy05bmiKzMQ00m8ZptkQqFdmiSO2B7lVitXCMP1Of0CVinG/fNLWGxrU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-1efa01323b4so906218fac.3 for ; Sun, 29 Oct 2023 09:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698597654; x=1699202454; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UR+I5VJhuYUK9n/m0JqTX8/xOrUGN5FygiFs7Rytjzo=; b=KFwomFXNXoXnUtMTgBMo9eC46Am1lKEwuRqUpJ3hntG/MhGGGZ7GpTDUzOa7OjDYS9 ijeRwB3C2u77aJepHoaMpI0/UvNm4UtfIVPHb/MIos4a7SkKtRWKun4ok0aXsYksy1Xg gdmHhSwzvuIsZhHGczGZdpc6pzuFkkLckU9qJ52Tzt+AGnjWBF9dV128GfHeM0NZoYNT uj42AMglGBjD0Tn+XEuhmQgumYDRkV/R9pl0rYO/664WPW2bRZF6sA1gBgddb1IANgE1 HUasuqJbv2xTaHyV/5h9rVyfw/Vy64Zhc4Xh52AvLZdmdeTxENT2nzVw7wtSTEuuG+an +8CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698597654; x=1699202454; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UR+I5VJhuYUK9n/m0JqTX8/xOrUGN5FygiFs7Rytjzo=; b=SiuODECKf3YL2FsUKAUNXMBr36mxWPJpkm7q3lmjdO5RW+SbE197Yx3lpT86SYWQvv wyTFhVj9smzG9Et8qun7TEfE6hieH3taX6FAN4afgpyDwATxSg/QYUdO1UoAFM4RJIT4 T652S9/BdtJZBXnfpWwzTdeTFaE6laNddqkPL2vSIH3R/bFZyQ3VdeuxBNbABZr/+eD6 QK5cvEeANANmKywWwtnu5b3w+h5galuFXoLjfuJ5DK4scsSLwTpb++MmzRBMf12BnkVi /z7gr4lcA/sijrQg3OoL3OV1xcTQFkleMnOvRf1xNQzVdNThyLxhpQKT4U7w0vTEOQFM /K3w== X-Gm-Message-State: AOJu0Yxj5eshKv/+2tV7eTF+T+iAq60QTvxFACghL6RJ83Iyk4e0Jqxg z3iDlK+WDZUgcsmfQZwtS0tY9V4SxS0= X-Google-Smtp-Source: AGHT+IG9vHwC1EXDyHLqh6GyOYe54WT8iYQa9i8uY/UXBSGtmGhRF373KuV5wIskWHk87UCqnmY/xA== X-Received: by 2002:a05:6870:6188:b0:1e9:c10e:401f with SMTP id a8-20020a056870618800b001e9c10e401fmr10094000oah.12.1698597653865; Sun, 29 Oct 2023 09:40:53 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.118.94]) by smtp.gmail.com with ESMTPSA id w27-20020a63af1b000000b005acd5d7e11bsm3591005pge.35.2023.10.29.09.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:40:53 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH 1/3] MATCH: first of the value replacement moving from phiopt Date: Sun, 29 Oct 2023 09:40:47 -0700 Message-Id: <20231029164049.994454-2-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029164049.994454-1-pinskia@gmail.com> References: <20231029164049.994454-1-pinskia@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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). Changes since v1: * v2: Add an extra testcase to showcase improvements at -O1. 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-value-1.c: New test. * gcc.dg/tree-ssa/phi-opt-value-1a.c: New test. * gcc.dg/tree-ssa/phi-opt-value-2.c: New test. --- gcc/match.pd | 14 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cond-1.c | 17 +++++++++++++++++ .../gcc.dg/tree-ssa/phi-opt-value-1.c | 17 +++++++++++++++++ .../gcc.dg/tree-ssa/phi-opt-value-1a.c | 19 +++++++++++++++++++ .../gcc.dg/tree-ssa/phi-opt-value-2.c | 19 +++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cond-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1a.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 7d651a6582d..22899c51a2f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4145,6 +4145,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-value-1.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1.c new file mode 100644 index 00000000000..a90de8926c6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1.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" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1a.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1a.c new file mode 100644 index 00000000000..b884f94ddd2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-1a.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +[[gnu::const]] +int constcall(int); + +int f(int a, int b) +{ + int c = b+a; + int t = constcall(c); + int d; + if (a == 0) d= b; else d= c; + return constcall(d) + t; +} + +/* There should be no if statement and 2 calls to call1. */ +/* { dg-final { scan-tree-dump-not "if " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "constcall " 1 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-2.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-2.c new file mode 100644 index 00000000000..809ccfe1479 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-2.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" } } */