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" } } */ From patchwork Sun Oct 29 16:40:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 159378 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1768416vqb; Sun, 29 Oct 2023 09:41:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJ6Kq6I+SlNagriJujM3RnI4FUqwsvhRC0SzZ+SHk3Fv+Dc7/WHPGqLEHbrnUR+duI1DjJ X-Received: by 2002:a05:6214:5684:b0:66d:7f65:71c9 with SMTP id qm4-20020a056214568400b0066d7f6571c9mr18493855qvb.9.1698597687907; Sun, 29 Oct 2023 09:41:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698597687; cv=pass; d=google.com; s=arc-20160816; b=NyFO+0CoUkB/MXoDBBSEjjyC02vuVl4K+pvbEVyII80/o3eH4ultVq5ADvtNhc4En5 vc35zRXyaKwKDMlw7tZru5WuyQSWoPrfqr5fQaueneDtTDduOP++G/45BkqcK/GYcT8N mvp/rOCQGvmVuzA+APW060s1Cz7WtTQ26iI/HQHxe9GJbtzFw67fHFPC8jOlK6S6GZzi 57ummSeKZnTd19mSx/ZdRPg1G04t1Q0VMl0VziQbwE2jbF64Tixw2Mudt3RjEMPJ/GdU vYragGshxN+i6ogoXb6H6mIBwsw0UR9gXQ1kvDEu/isOl1JlFmdjsg99+a+sbCykhisc umKQ== 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=Nk+xE6Pijlh1XgPuC+am48B2OJrHyniAiqRE2k7eXyc=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=Dc0/OSM03q/fnGleLwOXLezIND59DigSYu6fdZB3MT6K1zNJfx8DHvx2ZK8RXvxqdI 6fXFgm1xe0U9oFJW+0xd4GWYZZUNTuDFRuqcSM3SLdEUOviKfu61gAjekwpH5SsvvtLk v+jZBCbPROTcNWzE6BVtHCdpfGOU9wPieLuc3G/B/Gmr//iaI1WDN9YntbulXmQ/OdTI tb5vBur5+MyNWv/9pA56sprAZ5flDli94dsFnVhSiyabOMhyhQRa+QC/aA6QGwf/eABO wd+TuyyGlinPG+jhhByPaOVQO1oUgJRMc22K5UU420DVieSLPGQMMDwRgK37EooIWvwo 5fGw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=P0OPBqKb; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w10-20020a0cdf8a000000b00656262151b7si4159376qvl.256.2023.10.29.09.41.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:41:27 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=P0OPBqKb; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 B8D4E387546D for ; Sun, 29 Oct 2023 16:41:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 8A97F3857C4F for ; Sun, 29 Oct 2023 16:40:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A97F3857C4F 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 8A97F3857C4F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; cv=none; b=OOK/p4ZFoF+S4lr6zLPz6AK/PMx5AJbLX8gP33L2yJ0vv8w9NJlfchu6j/SQ/sZhQT2b1D1uY5SmRMuhc3jmtPBB5zhLQnwxPO/4p9+OhpePC43kXNJtRtsUI83e7S42e3/ICStLd5EA4vIM8vYLF4XnHetQEgnm3SwNIc8Hbj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; c=relaxed/simple; bh=0IjZA8ECwqysX5BpeogAczwcWFCoFeyQbqcfXKQMwNw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=IYk6fH77Sca40bzrKdJ7v4fwmUxVwTA779kjffZtrKexZO0jjXkgfcjKmbmWUW5An3/IelUzbx8ITWnkb7y3h2aR9tucDhcKWKQuKZstPvnU2bYQamSUZwg23rABnPJlZ4CirZAMmUnItaPsZdEuWh3TXT8pdcCdRuGk06kllXI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6b77ab73c6fso2553994b3a.1 for ; Sun, 29 Oct 2023 09:40:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698597655; x=1699202455; 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=Nk+xE6Pijlh1XgPuC+am48B2OJrHyniAiqRE2k7eXyc=; b=P0OPBqKbUm7PVvQvndizTq69wp/qnr6iLtyNlxFe4GFzGao+Yev+Uhv00ZaUfc02Dz vbXkQ1CQWqq/f98AvDPDJEqS7BixQn1pUM1aFEM+8JnmYLZxCNNGi3GymrGH0vk0e9px wJnRLo7GjyLYoOcgCAqIjfxTfH7EdQcmkgQuM8AG0hkWVpqo+D0TNS7QsQWyO/g27Bjk pEb5z+pQJlYhHJykui9HC73WTsXWcAahzWp4ado6I6CmxDQxCmX54IgRUXQNIAQSGr/u RNCAD0eI8iBihhT7mv7TNe7Z8aOrN1A8/2mnJn5VzB1ymypjwYi3l3y7vCbCiHg+fOwG kL8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698597655; x=1699202455; 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=Nk+xE6Pijlh1XgPuC+am48B2OJrHyniAiqRE2k7eXyc=; b=cibvInzfxBthRwHNyYgh2ezE2cIj52mBoyay75DytIE5YuFQU4Xce7du04ydQ+nIfW tRZ8P/CuwtTfiddhepz7QDThb2vAeg9Tq/HnC/V3X3hmPrVA5jNH0BW5JmENc3RiGTvq GOHDzBoOIiWKUkT+79KoctIlEffBC1a0pBWRrHN1qDLYu12XRVnoOtokLKGcouK7FjbF VBL7V82kEXh7jmlosp4UuhiPVa3RO9ifHI5HMZo5JallV0JZfk69POROkvi9XQX50GGj 52cqGwUTI4ZPVUr2V7iA2D5qatINnsLmJIelQWHJ5Ow24H/rNfpYU0fpjK7uqn5SuF6b yDRg== X-Gm-Message-State: AOJu0YwGhXXzEkzbwNKD5sclfT9UW1TiEcOTrZOUwaQ9Z8iJ/gTdkpvk z7FPAh1yJaDuI/iB1iVKwW1IJGj7qNA= X-Received: by 2002:a05:6a20:4407:b0:17b:62ae:a8bf with SMTP id ce7-20020a056a20440700b0017b62aea8bfmr12697511pzb.30.1698597654968; Sun, 29 Oct 2023 09:40:54 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:40:54 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH 2/3] MATCH: Move jump_function_from_stmt support to match.pd Date: Sun, 29 Oct 2023 09:40:48 -0700 Message-Id: <20231029164049.994454-3-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.4 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: 1781108769266490851 X-GMAIL-MSGID: 1781108769266490851 This moves the value_replacement support for jump_function_from_stmt to match pattern. This allows us to optimize things earlier in phiopt1 rather than waiting to phiopt2. Which means phiopt1 needs to be disable for vrp03.c testcase. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * match.pd (PTR == 0 ? 0 : &PTR->field): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp03.c: Disable phiopt1. * c-c++-common/analyzer/inlining-3-multiline.c: Likewise. * c-c++-common/analyzer/inlining-3.c: Likewise. * gcc.dg/tree-ssa/phi-opt-value-3.c: New testcase. --- gcc/match.pd | 21 ++++++++++++++++++ .../analyzer/inlining-3-multiline.c | 5 ++++- .../c-c++-common/analyzer/inlining-3.c | 3 +++ .../gcc.dg/tree-ssa/phi-opt-value-3.c | 22 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/vrp03.c | 2 +- 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 22899c51a2f..9bc945ccada 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4159,6 +4159,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (eq @0 integer_zerop) @1 (op@2 @1 @0)) @2)) +/* PTR == 0 ? 0 : &PTR->field -> PTR if field offset was 0. */ +(simplify + (cond (eq @0 integer_zerop) integer_zerop ADDR_EXPR@1) + (with { + poly_int64 offset; + tree res = NULL_TREE; + tree tem = @1; + if (TREE_CODE (tem) == SSA_NAME) + if (gassign *def = dyn_cast (SSA_NAME_DEF_STMT (tem))) + if (gimple_assign_rhs_code (def) == ADDR_EXPR) + tem = gimple_assign_rhs1 (def); + + if (TREE_CODE (tem) == ADDR_EXPR) + res = get_addr_base_and_unit_offset (TREE_OPERAND (tem, 0), &offset); + } + (if (res + && TREE_CODE (res) == MEM_REF + && known_eq (mem_ref_offset (res) + offset, 0) + && operand_equal_p (TREE_OPERAND (res, 0), @0)) + (convert @0)))) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/c-c++-common/analyzer/inlining-3-multiline.c b/gcc/testsuite/c-c++-common/analyzer/inlining-3-multiline.c index fbd20e949b6..9741b91abee 100644 --- a/gcc/testsuite/c-c++-common/analyzer/inlining-3-multiline.c +++ b/gcc/testsuite/c-c++-common/analyzer/inlining-3-multiline.c @@ -3,6 +3,9 @@ /* { dg-additional-options "-O2 -fdiagnostics-show-path-depths" } */ /* { dg-additional-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret" } */ +/* Disable phi-opt1 because get_input_file_name gets optimized to just + `return inpf;`. */ +/* { dg-additional-options "-fdisable-tree-phiopt1" } */ #include "../../gcc.dg/analyzer/analyzer-decls.h" typedef __SIZE_TYPE__ size_t; @@ -96,4 +99,4 @@ test (const input_file *inpf) | (4) ...to here | (5) argument 1 ('') NULL where non-null expected | - { dg-end-multiline-output "" { target c++ } } */ \ No newline at end of file + { dg-end-multiline-output "" { target c++ } } */ diff --git a/gcc/testsuite/c-c++-common/analyzer/inlining-3.c b/gcc/testsuite/c-c++-common/analyzer/inlining-3.c index 0345585bed2..2b2b4858d45 100644 --- a/gcc/testsuite/c-c++-common/analyzer/inlining-3.c +++ b/gcc/testsuite/c-c++-common/analyzer/inlining-3.c @@ -2,6 +2,9 @@ after early inlining. */ /* { dg-additional-options "-O2 -fdiagnostics-show-path-depths" } */ +/* Disable phi-opt1 because get_input_file_name gets optimized to just + `return inpf;`. */ +/* { dg-additional-options "-fdisable-tree-phiopt1" } */ #include "../../gcc.dg/analyzer/analyzer-decls.h" typedef __SIZE_TYPE__ size_t; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-3.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-3.c new file mode 100644 index 00000000000..ad55bd288b9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-3.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +struct a +{ + int b[1]; +}; + +int call1(int *a); + +int f(struct a *b) +{ + int *c = b->b; + int t = call1(c); + int *d; + if (b) d = b->b; else d = 0; + int t1 = call1(d); + return t+t1; +} + +/* 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 "call1 " 2 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp03.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp03.c index 4cbaca41332..1adbf33cad3 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp03.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp03.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fdisable-tree-evrp -fdump-tree-vrp1 -fno-thread-jumps" } */ +/* { dg-options "-O2 -fdisable-tree-evrp -fdump-tree-vrp1 -fno-thread-jumps -fdisable-tree-phiopt1" } */ struct A { From patchwork Sun Oct 29 16:40:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 159379 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1768448vqb; Sun, 29 Oct 2023 09:41:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNudHc8I7WoFnqWFnsU7tJvHC8d1BSott6bQj9CrSiBoF4o0qh5lDDBZyggu8eTAPpqkLv X-Received: by 2002:a05:620a:2445:b0:774:2bdc:aa4 with SMTP id h5-20020a05620a244500b007742bdc0aa4mr9313541qkn.54.1698597695641; Sun, 29 Oct 2023 09:41:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698597695; cv=pass; d=google.com; s=arc-20160816; b=leFRTpyTW4reJVpCmHWwNnWrZoUlrMEfq/vIthBkCAbC+NrAIagdHFJOoIlkDrxNZQ UXACfbB1dHi0xAachpcnI8e/ujEFudd9wEL8C8JtFv3Y+W9UdG+W562yRTRKKcxElyqY LPVvUBkdcCfjhRhK4FinaOeY2qIpyIqa32LdcP0BI63p3P3Hyl5Wfyln7Pxus9STodd1 XUT06+lERljdixIN14yc5NG1As8vfE13zR9nxrINkwk3JE1AHz6v640DRUhOJqHFuj3H zGBcGaTG0Y2cdHH1b09QAgHJRFOQLR/VXonmE2fLCr8GTcTBetOVilcYiwsdtpcpPEed FNPA== 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=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=yuqOqtcTRjZCAmv7L9eURseVDXpRDJp5e7eXSAexkGxwzWgu+fos2PgVG2lzqjt2gq z3gcpfMAAx0piB/QFzWLcgoMnpTaSOZ7Vd+WXOOV/8dMhVqCcgQfZZ0AQrWZLd/kFjce REnl1fHGoMcO/Y9hScVorsQlgClaAjUTw/PuTCvtWp9DiPgiqnF80H1ucDO/lnyo4TkP +7Zsif4kWwKHQzT5C+5p2nu9w6U0pOqhhKhZHkaDrCq4fiYTxbUrrI5Dx022hbB9fU9b wbMHQKs545YJoUB8d7i1V+q7J42tMb6TOkqyZ4KPjez7LAGHeFiS5nQUFjQljEXt9tKP k+ew== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WPI7CXau; 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 c2-20020a05620a268200b00776ff8c1216si4221069qkp.772.2023.10.29.09.41.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:41:35 -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=WPI7CXau; 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 02D12387546A for ; Sun, 29 Oct 2023 16:41:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 572F6385276F for ; Sun, 29 Oct 2023 16:40:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 572F6385276F 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 572F6385276F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; cv=none; b=ffgqUqh+Jw+6xBY0BAeIQHjXEmvA8MCt96tJiBTBsEvePW04t/luXCgJAKNLN1DMcOXyJ/wwM9qLAQB/FHcql0m83116VraxwWI1fU3rBeJo2nxsLO1tSnNgB5RSDX+BqMtggOCzIXZsZg9yfdqPyyOV36coyaP/nSuZ9VRtv04= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; c=relaxed/simple; bh=nd1DK2TugqSHUdvWFR/tpqiukW3g5WsCqFpnIViTV04=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Gan5RpsbmnryUkIGZ2PcvbQ0WTOQPSf4lE44F9A4dR9L+5oo3FcJtRzQ2+L5GzRw3S9hVH1h54WSOtMh3y1k0/Jx8uqZRNaNKXfO7eeOgDNKJS5r8koxeeuBh5F+iiPkQ7LcygX7Vdsmq5XHsFZKpVJXsLgeA9BIi4QcOQEsgS8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b2e44c7941so2644323b6e.2 for ; Sun, 29 Oct 2023 09:40:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698597656; x=1699202456; 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=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; b=WPI7CXaufuQlaf01jk4zWyeGh9G4QqO0gvlgV5fC9hkBDFM1zTZGIjVxBTLb1qlHn9 d8CEWjrZAVFzd+Lhcy3qn0977th/Rx67IpxgsVQOM+QmgA4lxGtIzEkDepGYvYQpeX8k fF9UQc1s/61bjo6V3SQpXA+yBb1GHm9afgHjglKcyWNZzM51dH9MyXdu5tYjAf2dZ0YF y9C8qn8YtE++0DfL+edZqPDdT7msRfR7cAfWSH7KR3xCFXhLnCf2wyVj6anqqnbqMKLC /fxfTwz6XrQWaIo2On0vvlp7DJdTRMLJcEE3YaRnFfv3gx3UAGe0lo5grpB9/UkGvtt7 ucDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698597656; x=1699202456; 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=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; b=rvZtnfVSKj3WExjBZwkpV8SUsRRomP4KgL1unczHZ/YLWo2+jW8fKbYqKUecK/0Jq9 5XCFGf87kIUU2xRs9Pf7qw6oJoJToPV/gEADzY3YXw1rktIIB7CPGLRJfZmroCCUWCeZ Y2LCR/AQmkY2oal9QVMBRcO1CU9bJaViHOiPG3jL6N1KleCk6S5HVrwd6r11+PP1zHeq ty9MZcppo09epZSbrhhKJY8CkQRbVmdM4StVGIsRGwrMFTOjV2H2PrfDSeO0hmIR1/pC kvgi49Ah6BZV24cxTFZVa3BrnSjA47qLPbvIX7BGB4RRMDVZLKBKEEMgdbHlxpD6kT7E z08A== X-Gm-Message-State: AOJu0YzKDo27hxvU3GMjvSfNl8FokWYM8QQExsxYzCJO9TMxKiQ4JC7z /ZedjNXvl/FbOjuVy35JEs2QqI6pZcs= X-Received: by 2002:a05:6808:191c:b0:3b2:e219:5518 with SMTP id bf28-20020a056808191c00b003b2e2195518mr9567637oib.11.1698597656263; Sun, 29 Oct 2023 09:40:56 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:40:55 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH 3/3] MATCH: Add some more value_replacement simplifications to match Date: Sun, 29 Oct 2023 09:40:49 -0700 Message-Id: <20231029164049.994454-4-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.5 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: 1781108777616441514 X-GMAIL-MSGID: 1781108777616441514 This moves a few more value_replacements simplifications to match. /* a == 1 ? b : a * b -> a * b */ /* a == 1 ? b : b / a -> b / a */ /* a == -1 ? b : a & b -> a & b */ Also adds a testcase to show can we catch these where value_replacement would not (but other passes would). Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (`a == 1 ? b : a OP b`): New pattern. (`a == -1 ? b : a & b`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-value-4.c: New test. --- gcc/match.pd | 18 ++++++++++ .../gcc.dg/tree-ssa/phi-opt-value-4.c | 36 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c diff --git a/gcc/match.pd b/gcc/match.pd index 9bc945ccada..6efa97cc6ae 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4159,6 +4159,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (eq @0 integer_zerop) @1 (op@2 @1 @0)) @2)) +/* a == 1 ? b : b / a -> b / a */ +(for op (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (cond (eq @0 integer_onep) @1 (op@2 @1 @0)) + @2)) + +/* a == 1 ? b : a * b -> a * b */ +(for op (mult) + (simplify + (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0)) + @2)) + +/* a == -1 ? b : a & b -> a & b */ +(for op (bit_and) + (simplify + (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0)) + @2)) + /* PTR == 0 ? 0 : &PTR->field -> PTR if field offset was 0. */ (simplify (cond (eq @0 integer_zerop) integer_zerop ADDR_EXPR@1) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c new file mode 100644 index 00000000000..380082cb463 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-fre3 -fdump-tree-phiopt1 -fdump-tree-optimized" } */ + +[[gnu::const]] +int constcall(int); + +int fdiv(int a, int b) +{ + int c = b/a; + int t = constcall(c); + int d; + if (a == 1) d = b; else d = c; + return constcall(d) + t; +} +int fmult(int a, int b) +{ + int c = b*a; + int t = constcall(c); + int d; + if (a == 1) d = b; else d = c; + return constcall(d) + t; +} +int fand(int a, int b) +{ + int c = b&a; + int t = constcall(c); + int d; + if (a == -1) d = b; else d = c; + return constcall(d) + t; +} + +/* Should be able to optimize away the if statements in phiopt1. */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ +/* fre3 should be optimize each function to just `return constcall(a OP b) * 2;`. */ +/* { dg-final { scan-tree-dump-times "constcall " 3 "fre3" } } */ +/* { dg-final { scan-tree-dump-times "constcall " 3 "optimized" } } */