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: 1854051 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=cc1HwVry; 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 4SDrNT2Yptz23jl for ; Tue, 24 Oct 2023 10:04:08 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 734E73858430 for ; Mon, 23 Oct 2023 23:04:06 +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-Google-Smtp-Source: AGHT+IFrPeTVU3+CaXZthnTU3/RlODdPnO78yczsQp93WoBSbLCV4kouD6XVO5NKx04bKXjd7HyY9g== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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" } } */