From patchwork Wed Aug 10 17:10:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Feifer X-Patchwork-Id: 467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:20da:b0:2d3:3019:e567 with SMTP id n26csp3307654pxc; Wed, 10 Aug 2022 10:11:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR6tCS9pffzFPYW2JfrtiOnbRb4179mH0B5KUl4sLCAwJQ6rZEkhjSXOOmIyz4/EdL/Mkamc X-Received: by 2002:a05:6402:1f87:b0:43b:b88d:1d93 with SMTP id c7-20020a0564021f8700b0043bb88d1d93mr26966959edc.314.1660151481268; Wed, 10 Aug 2022 10:11:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660151481; cv=none; d=google.com; s=arc-20160816; b=srUk2Kb+FckhsuCZ6pFhGL48MWW3Fmvlkp9MOo4INdxUWaWSBdU7zm7yauStN1iDO+ 5iS/GiWiG+OeYZhufOKzouUZjGTwKMTXBe51s8eIYNSTwRfpwuymacZLTtZLNBUF8rro dDN/EAXctSrYWd2ePvZc7R4eXcyqZIuUxukLT0ykNBuHceuVfcJG3wseYDLkOfOTMcYI GwpydN7riLH26DYiyxbKYrt2evL2loA+FTFSMKDRdgoomy+0cQgyEefAXkXvDg3TUXSx oT68xZbbeIDcFxvl50cYlzHMJiiXw5ZqcWSk5ypqSWvXvy0WfZLuE0TfQvJh7DD7ovIP zkOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=STV8jejhvhyk0H+ZWWPDev/qSJgUANK4XPxfIMAkrAk=; b=xbjlXRHBsL6QlqlhfaGNw6OlO7pfHenOZ0OJc3nrBP+UgFW28rGJAsMVmc6tql+QS3 K2td2/svoUDz3Z78ighxZLsyZwr1BZyVPB+6s7DWVpbpdq9awWF63QXcP/bRHY9DsGzW DZvhe58uLkHFpvNArPVm3lwHcqwVmwXVeQVx6zbCZhYSIaQHspUK2hldwMBxHOeZ+OPn 8YunyqM6XzJ42EE2P6J8MIHwaeiJzNF1xraxh4PjR3on3GFSwxPhIce5OnZztHoHLCJ1 MtPx+adgufZtZli8RmRoMKIxmQP+Xy+RQ7L70SV05jkn0pwRcNh+QH15uLHzN+QtUNPK qSrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fop1buLg; 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=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ht15-20020a170907608f00b007306d818fbcsi4816938ejc.563.2022.08.10.10.11.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 10:11:21 -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=@gcc.gnu.org header.s=default header.b=fop1buLg; 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=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29BDC385801A for ; Wed, 10 Aug 2022 17:11:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29BDC385801A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660151480; bh=STV8jejhvhyk0H+ZWWPDev/qSJgUANK4XPxfIMAkrAk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fop1buLgoxnpCxdYOVTGjWGj0iVgaD7SDLvk2g2zV/iloDWmd7xLEuDOtRmZEc/Ws KhubVnbuujDY/WPvodQwZLse2tMsiGju3wMexPfcuTH9PdzPv0ZkmvPf26BmpHKM+r qLuxiNjuUYLpgRHOCpK/TLoS3uAdomphFa4+Umxs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 971053858406 for ; Wed, 10 Aug 2022 17:10:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 971053858406 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-36-L48g5HIVP4mI05c1RAV3qg-1; Wed, 10 Aug 2022 13:10:34 -0400 X-MC-Unique: L48g5HIVP4mI05c1RAV3qg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A720A805B72 for ; Wed, 10 Aug 2022 17:10:33 +0000 (UTC) Received: from sfeifer.remote.csb (unknown [10.22.9.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8901018EAA for ; Wed, 10 Aug 2022 17:10:33 +0000 (UTC) To: GCC Patches Subject: [PATCH] match.pd: Add abs with bitwise and pattern [PR106243] Date: Wed, 10 Aug 2022 13:10:00 -0400 Message-Id: <20220810171000.2855425-1-sfeifer@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sam Feifer via Gcc-patches From: Sam Feifer Reply-To: Sam Feifer Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740794999612412526?= X-GMAIL-MSGID: =?utf-8?q?1740794999612412526?= This patch adds a simplification to match.pd that was discussed on the thread for pr106243. It simplifies the pattern, abs(x) & 1, to x & 1. There are also tests for the simplification in this patch. I couldn't figure out how to get abs to work with vectors. If a test for that is necessary, could I get some guidance on using abs with vector types? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR tree-optimization/106243 gcc/ChangeLog: * match.pd (abs(x) & 1): New simplification. gcc/testsuite/ChangeLog: * gcc.dg/pr106243-2.c: New test. * gcc.dg/pr106243-3.c: New test. --- gcc/match.pd | 5 +++++ gcc/testsuite/gcc.dg/pr106243-2.c | 31 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr106243-3.c | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr106243-2.c create mode 100644 gcc/testsuite/gcc.dg/pr106243-3.c base-commit: be58bf98e98bb431ed26ca8be84586075fe8be82 diff --git a/gcc/match.pd b/gcc/match.pd index f82f94ad1fe..c04e70f34c1 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -8071,3 +8071,8 @@ and, (simplify (bit_and (negate @0) integer_onep@1) (bit_and @0 @1)) + +/* abs(x) & 1 -> x & 1. */ +(simplify + (bit_and (abs @0) integer_onep@1) + (bit_and @0 @1)) diff --git a/gcc/testsuite/gcc.dg/pr106243-2.c b/gcc/testsuite/gcc.dg/pr106243-2.c new file mode 100644 index 00000000000..27e66f59160 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106243-2.c @@ -0,0 +1,31 @@ +/* PR tree-optimization/106243 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +__attribute__((noipa)) int foo (int x) { + return abs(x) & 1; +} + +__attribute__((noipa)) int bar (int x) { + return (0 - abs(x)) & 1; +} + +/* Commutative property. */ +__attribute__((noipa)) int baz (int x) { + return 1 & abs(x); +} + +/* Forward propogation. */ +__attribute__((noipa)) int qux (int x) { + int y = abs(x); + return y & 1; +} + +/* Should not simplify. */ +__attribute__((noipa)) int thud (int x) { + return abs(x) & -1; +} + +/* { dg-final {scan-tree-dump-times " ABS_EXPR " 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr106243-3.c b/gcc/testsuite/gcc.dg/pr106243-3.c new file mode 100644 index 00000000000..68800868751 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106243-3.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/106243 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "pr106243-2.c" + +int main () { + + if (foo(6) != 0 + || bar(-3) != 1 + || baz(32) != 0 + || qux(-128) != 0 + || foo (127) != 1) { + __builtin_abort(); + } + + return 0; +}