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: 159380 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1768521vqb; Sun, 29 Oct 2023 09:41:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFMnpcRvIv7vhZnno5Akt/X0qHSjwbOGvBT2WsW4VyAnj3/sdoAFztT9JDIw4grQbO8G/e/ X-Received: by 2002:a05:620a:4113:b0:76f:1ead:e4f4 with SMTP id j19-20020a05620a411300b0076f1eade4f4mr8574706qko.15.1698597709718; Sun, 29 Oct 2023 09:41:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698597709; cv=pass; d=google.com; s=arc-20160816; b=oRxDhKabq+NPDm4/Rz/nMx7DEatN5zTUEWBZDDIF/ENTt1Zu4ad4uxOEq7UokaXJN6 r4q9LNn2D1y1or+vBASOA758ek4MabX/Z0+nCmdUp6Cm6SXw+mh5U6/L3jEXd8ZwpwIg IxGNzfM9BZNbP1wwTYS5wIxYjfOlDoKxwWV0MkS0uPAtF4nnkxF3GleZLWMXxDy6Dt13 95Noz8AhbDFWOjNk42Qpw1OBhwS5s6RYzNIcS+cfLEJjMTyCSrpmYhrJ1cc0GNpmtg7N ImxqZbIJH3FZyI7IjQM9qz4Ts8E1+itrvROasWg+ddvYhM00cbD6fxFx1dZ3RbNmtQNC Iqtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=UR+I5VJhuYUK9n/m0JqTX8/xOrUGN5FygiFs7Rytjzo=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=glktsowB/OXl9EW179jYLx2w104nHLV+ashiso41/bZKGGNVt+lwb+/fcEyGK/e8IF kEosQ7o60z6nAdmhp5W1Eazsc2dw4g6NLN8cjEuPmTJgSp2Nw2WmFLbWkNyZyf9nIlPD YhdQAb13P9oyIDIjMi2ItA1C0sHqRboKNvYIYO44cHnanXXGD5jn9I8mE+DpEuplh90E jtC057FfjaN2iIIfuAWXExR0hZO1hMHpWu5O9ymYYtSbiEvqQRxEbuDQWwTZFoSPPzpl gY/eru3WTSWcEb/JDl2KBy9d0+xAq1X0a6t1Fd90k5ymFpr9eMi3KCs1JYYltSd3t2SL 2wvA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KFwomFXN; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 18-20020a05620a049200b00777731bf89csi3940906qkr.150.2023.10.29.09.41.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:41:49 -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=@gmail.com header.s=20230601 header.b=KFwomFXN; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B40F3861856 for ; Sun, 29 Oct 2023 16:41:49 +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-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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781108792149797528 X-GMAIL-MSGID: 1781108792149797528 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" } } */