From patchwork Mon Oct 23 23:03: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: 157131 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1601779vqx; Mon, 23 Oct 2023 16:04:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIrW/VmALIPUZtCT500Kup68hplTMgBUPTq36VchpbJRYjwmHQbsrCanu78IN0M2Ecqa2u X-Received: by 2002:a05:6214:23c9:b0:66d:61c5:8717 with SMTP id hr9-20020a05621423c900b0066d61c58717mr10673378qvb.1.1698102259324; Mon, 23 Oct 2023 16:04:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698102259; cv=pass; d=google.com; s=arc-20160816; b=Xn55qbCMM7WP+UnOHDqUC8NJR7h6IWHpIJconSewOOSKOOMxioWNUztv5v7fxaIrBa K0UY8sNdtU8GPmFYwLDrdSCeOJQnqktZO9fdy5GKYFsfRmv7KcawBAec5Vea0POy3ZVv cM4Gtq/4zcRgzis5TCPayUAsR1BbfyTAcQg5H0t1vQa1NAfydDFGVkKB53PE/XAExP69 JsAzYqQzWPauIoj1iF90r5E5KHF/8GjkHhw5OxMysfIo72Xe/su55QODFcbz5GIvOLe7 jtgXUpc03lSZh1gs8j64ktTRSlCvH64wF1FHvucRJu0KKTWkchXL6lkIXIUAObhOhiwC VA5Q== 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:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=+PJTwZ1kkz4zXgmFFHgXxX2jym0pSXTHgmQBQlLS0rg=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=CUbp8x7dFTp+aKuxExxCow4rxCMeGzor6FUFhrmDlra/egg9IR9Wt+AYX8PiGt8COS I3QPEECi+ST1vgYaBHaRoRn/PqsCygoy7MXfAM+gvonFGhQlvrJmovxfU26qtcMwVWmD 42pXSJreAeLN1rSXc2910eYmte5d0B/E01eFCxbpLcL/9bhLyUiG9BXB0jyYxGbeuu4P kMKQIsFgE1y5kE2dyBx2j7AA0MXaovBy/2HszWiJzeCG8OXprEZPRs9UcVziF1wygDbG BBbZcBzpPGjP6B1nqucPGf1ScV+wZaQQcAWk7DybJVuJVt0nrUPdI2mwwVy+51SOZrDd ssUw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cc1HwVry; 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 bs36-20020a05620a472400b007726499db83si6303785qkb.229.2023.10.23.16.04.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:04:19 -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=cc1HwVry; 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 E9A4D385840A for ; Mon, 23 Oct 2023 23:04:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 957923858D37 for ; Mon, 23 Oct 2023 23:03:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 957923858D37 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 957923858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698102235; cv=none; b=RhwMUtkkb3RjvnqGIpGGmb0igb6Vo4ZiEC6pJ5PkQUXIi3By8YEa4VmasoD2qcXqOq8cYt064JRjcJAPxl12XOaP35f9bgIuncbbGC3/ZNaGF/810HkWDBBeoPESCXpXhyipvUN6t522rRGMaM/fkSLMiz5Oj7Wze0ihT5vdpIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698102235; c=relaxed/simple; bh=YUhtViUQHfowZTY2xL3d55Ih2KqA8OEBpHODwmo2q7k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=oHaA1A8vJ8RXoWneeU5qbGN/4YGKCcQcZZVerN1mfScMZjrNOMhJrfnKpTOvbaf8Rmv0+y7b+P4P5EKvY84W6r/s12LpjW+y753y2NdIPdi3cSfR8Jwd6mSPhXeGEI7P4FwU+/TG1fXEUq2j31svVZK04ML9rRm4U7Rtm/O8Tn8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1ca3a54d2c4so32104345ad.3 for ; Mon, 23 Oct 2023 16:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698102232; x=1698707032; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+PJTwZ1kkz4zXgmFFHgXxX2jym0pSXTHgmQBQlLS0rg=; b=cc1HwVryy+A6ddDFlbz15ok5gwDHlMeC2jlrgBQEDo3gE/+kklF1taojA3LLaa/s/0 HCrO//BjI9MRhKurpWgHrGYMLkoBpYiDaFjkLMQsI6XnpgS66FI1BU73c/5dMg02XJaJ CeyErHZVolj+7cqHtfW2xWxWQea23Xdbjhx5biJlzHUCUrPOd7Nil00P4ANmLktMXijf rVO3amjVWoU/VPBIIQIMudgNCuBMBhAh53qvEw3lTegxBEhMNy20VBgtnGiyRFggbr99 uX9AAHzqVzCPHroK8bN9qKD4oM7311iRaCOmHvXEDQT6o8GwGKkMHNSCg8851oxfeLeU 1UIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698102232; x=1698707032; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+PJTwZ1kkz4zXgmFFHgXxX2jym0pSXTHgmQBQlLS0rg=; b=YO57i8HKheCHxC8DiU1CPTZTEohNebBMoMdYv9d6L3TyToWwFslN4PYhqjBaLe4xW9 oMhBq1SWqXfU61CHbKlh2THrOI8ebDoXGeRGSJZM8vNKjOqx4YneEKxPq41eXrTFc5pk /RCvdKqObY6Hr+5uPujPh9pKQ5MAevSXxGqrObdUqj0wB+1jYy6/a7dSGVqe/4VAbFqY Hv+SlZu2HyYk/ffgvnRxv1awk7rj/BIwCr0KE/mMll6H6XD/+kShvx9NtTBV+YCGErX8 nY8CnHNAuLb8ZGWbcYmlhA6Sjr+iuJPRmzIrhliJfHkS7ZE+l/6B+mtzAwmIgy2Y8ntj rkKA== X-Gm-Message-State: AOJu0Yz4GiJGLaxOWnTWYXAkUBDMT8L++lyhL+IVnJ9MjhuK2K5mllly 51OOHHU1zpYcq0Tbj6ua9lXgjaZuPHptbg== X-Received: by 2002:a17:902:e0ca:b0:1c9:dfb8:a5a0 with SMTP id e10-20020a170902e0ca00b001c9dfb8a5a0mr8165194pla.10.1698102232276; Mon, 23 Oct 2023 16:03:52 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id b1-20020a170902ed0100b001b392bf9192sm6413828pld.145.2023.10.23.16.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:03:51 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] match: Fix the `popcnt(a&b) + popcnt(a|b)` patthern for types [PR111913] Date: Mon, 23 Oct 2023 16:03:48 -0700 Message-Id: <20231023230348.606997-1-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 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: 1780589274981247525 X-GMAIL-MSGID: 1780589274981247525 So this pattern needs a little help on the gimple side of things to know what the type popcount should be. For most builtins, the type is the same as the input but popcount and others are not. And when using it with another outer expression, genmatch needs some slight help to know that the return type was type rather than the argument type. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111913 gcc/ChangeLog: * match.pd (`popcount(X&Y) + popcount(X|Y)`): Add the resulting type for popcount. gcc/testsuite/ChangeLog: * gcc.c-torture/compile/fold-popcount-1.c: New test. * gcc.dg/fold-popcount-8a.c: New test. --- gcc/match.pd | 2 +- .../gcc.c-torture/compile/fold-popcount-1.c | 13 ++++++++ gcc/testsuite/gcc.dg/fold-popcount-8a.c | 33 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/fold-popcount-1.c create mode 100644 gcc/testsuite/gcc.dg/fold-popcount-8a.c diff --git a/gcc/match.pd b/gcc/match.pd index ce8d159d260..f725a685863 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -8600,7 +8600,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */ (simplify (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1))) - (plus (POPCOUNT @0) (POPCOUNT @1))) + (plus (POPCOUNT:type @0) (POPCOUNT:type @1))) /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */ /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */ diff --git a/gcc/testsuite/gcc.c-torture/compile/fold-popcount-1.c b/gcc/testsuite/gcc.c-torture/compile/fold-popcount-1.c new file mode 100644 index 00000000000..d3d3a2976e0 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/fold-popcount-1.c @@ -0,0 +1,13 @@ +/* PR tree-optimization/111913 */ + +int f(unsigned int x, unsigned int y) +{ + return __builtin_popcount (x&y) + __builtin_popcount (y|x--); +} + +int f2(unsigned int x, unsigned int y) +{ + int t = __builtin_popcount (x&y); + int t1 = __builtin_popcount (x|y); + return t + t1; +} diff --git a/gcc/testsuite/gcc.dg/fold-popcount-8a.c b/gcc/testsuite/gcc.dg/fold-popcount-8a.c new file mode 100644 index 00000000000..3001522f259 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-popcount-8a.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int foo1(unsigned int x, unsigned int y) +{ + int t = __builtin_popcount (x&y); + int t1 = __builtin_popcount (x|y); + return t + t1; +} + +int foo2(unsigned int x, unsigned int y) +{ + int t1 = __builtin_popcount (x|y); + int t = __builtin_popcount (x&y); + return t + t1; +} + +int foo3(unsigned int y, unsigned int x) +{ + int t = __builtin_popcount (x&y); + int t1 = __builtin_popcount (x|y); + return t + t1; +} + +int foo4(unsigned int y, unsigned int x) +{ + int t1 = __builtin_popcount (x|y); + int t = __builtin_popcount (x&y); + return t + t1; +} + +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " \\| " "optimized" } } */