From patchwork Tue Aug 1 19:20:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Ross X-Patchwork-Id: 129447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp2882785vqg; Tue, 1 Aug 2023 12:21:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlGVeoEnHqrNgAp/6C/S6xNJ9B/1THux3wt2MsCmNb4J7d+4fI5C9vLL5fpb70iwE0CAk682 X-Received: by 2002:a17:906:8a75:b0:99b:4afc:7714 with SMTP id hy21-20020a1709068a7500b0099b4afc7714mr3032465ejc.44.1690917682598; Tue, 01 Aug 2023 12:21:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690917682; cv=none; d=google.com; s=arc-20160816; b=K5J9vWZgmVtudKO4sw9id5eSD4kdynBuJ1PS7a98Pt83RwmCpjPVkUGZsolgR6I4A7 MM5DIO1XKj06VO6JrkcWrS+mwa9HlPq98yfMc9DMhRhCSaQoaTdTrXxxYHAV+yVEWcIQ Drgg1TlSseYrl82C3fMCl9tcYJRWEzfdcXCOlqrPrOEgGGqHTQph/5B0fWM07MsmS+9W 6XGsKK1EcGq0wvEry6C44mr6/vqAH/JqYG8/69FJ8JZFCOvDPg0ucISnEINM+61s6R3W /PS1R9eMWkxbFxsswOPc1XzNskwSlcuh/vC6XRIomXoBT8u9WYyglAHltpwDG0Eqk/qO vfRw== 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:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=Q/lJlYW4qC7Q56/c9jEIHOz4RZwm/vphrCfixr6ZSY0=; fh=+7oqyIxxdDZHhy0RvojIG59KP1WcqOWasYDHPRLsigU=; b=ea489uJtoEBVyJKRYmomotgnVXl0q5eeE6NblDojrW693Xn7kQEdIT5IzBzMoHdKoY rOc54qYutQCI3BzjUL6s8bpYxNxeR3e2VUD+i/x6lWxq1DAL/IE1z568d9FhuUFIZ4oT MdDGyyE0FqX2B8S3/nxnE4LgM+Pdgu92LlRyE1NXu4/niPFIUHEOopm34bxX7VW5nzLZ WzGiJY5SyvT1fWen4QY8ZRuemH5oArWKxEO7Qt235Ku6DoY7Wo30HPlsDub1hpvl5i3c XwDsxFCt2M4iIQB+k0+8fsoPNQ0tHPw8miGrGAw1KsQe009lweOKcK6iBtFI70K2TXGw wB6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RisKBgQf; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hh23-20020a170906a95700b0098d2f716c75si1994132ejb.597.2023.08.01.12.21.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 12:21:22 -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=RisKBgQf; 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 880A83858C2A for ; Tue, 1 Aug 2023 19:21:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 880A83858C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690917681; bh=Q/lJlYW4qC7Q56/c9jEIHOz4RZwm/vphrCfixr6ZSY0=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=RisKBgQf53wAt+yyy9Uo4W9lp5wQR59OruedrKcUPBM7e1vW6Udc5T0KOUMvR05M4 rqh/MegyxXT+4zH2wraJZu882o3z4zAO38XEPsLZkHgbgdo+IHnzUuU+SnsJ+9SjRa YoXLqZVEuaBHZq8HU9d635kXHewlftZlEG/gt+SI= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id F052B3858D39 for ; Tue, 1 Aug 2023 19:20:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F052B3858D39 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-199-X1f6lzFhPwa73jnDJQewGg-1; Tue, 01 Aug 2023 15:20:36 -0400 X-MC-Unique: X1f6lzFhPwa73jnDJQewGg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9A4DC8A6947 for ; Tue, 1 Aug 2023 19:20:36 +0000 (UTC) Received: from drross.com (unknown [10.2.16.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8087F40C6CCC; Tue, 1 Aug 2023 19:20:36 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: Drew Ross Subject: [PATCH] match.pd: Canonicalize (signed x << c) >> c [PR101955] Date: Tue, 1 Aug 2023 15:20:33 -0400 Message-Id: <20230801192033.432742-1-drross@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.1 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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: Drew Ross via Gcc-patches From: Drew Ross Reply-To: Drew Ross Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773055699764078826 X-GMAIL-MSGID: 1773055699764078826 Canonicalizes (signed x << c) >> c into the lowest precision(type) - c bits of x IF those bits have a mode precision or a precision of 1. Also combines this rule with (unsigned x << c) >> c -> x & ((unsigned)-1 >> c) to prevent duplicate pattern. Tested successfully on x86_64 and x86 targets. PR middle-end/101955 gcc/ChangeLog: * match.pd ((signed x << c) >> c): New canonicalization. gcc/testsuite/ChangeLog: * gcc.dg/pr101955.c: New test. --- gcc/match.pd | 20 +++++++---- gcc/testsuite/gcc.dg/pr101955.c | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr101955.c diff --git a/gcc/match.pd b/gcc/match.pd index 8543f777a28..62f7c84f565 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3758,13 +3758,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) - TYPE_PRECISION (TREE_TYPE (@2))))) (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1)))) -/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned - types. */ +/* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for + unsigned x OR truncate into the precision(type) - c lowest bits + of signed x (if they have mode precision or a precision of 1) */ (simplify - (rshift (lshift @0 INTEGER_CST@1) @1) - (if (TYPE_UNSIGNED (type) - && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) - (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) + (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1) + (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) + (if (TYPE_UNSIGNED (type)) + (bit_and @0 (rshift { build_minus_one_cst (type); } @1)) + (if (INTEGRAL_TYPE_P (type)) + (with { + int width = element_precision (type) - tree_to_uhwi (@1); + tree stype = build_nonstandard_integer_type (width, 0); + } + (if (width == 1 || type_has_mode_precision_p (stype)) + (convert (convert:stype @0)))))))) /* Optimize x >> x into 0 */ (simplify diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101955.c new file mode 100644 index 00000000000..8619661b291 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101955.c @@ -0,0 +1,63 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +__attribute__((noipa)) int +t1 (int x) +{ + int y = x << 31; + int z = y >> 31; + return z; +} + +__attribute__((noipa)) int +t2 (unsigned int x) +{ + int y = x << 31; + int z = y >> 31; + return z; +} + +__attribute__((noipa)) int +t3 (int x) +{ + return (x << 31) >> 31; +} + +__attribute__((noipa)) int +t4 (int x) +{ + return (x << 24) >> 24; +} + +__attribute__((noipa)) int +t5 (int x) +{ + return (x << 16) >> 16; +} + +__attribute__((noipa)) long long +t6 (long long x) +{ + return (x << 63) >> 63; +} + +__attribute__((noipa)) long long +t7 (long long x) +{ + return (x << 56) >> 56; +} + +__attribute__((noipa)) long long +t8 (long long x) +{ + return (x << 48) >> 48; +} + +__attribute__((noipa)) long long +t9 (long long x) +{ + return (x << 32) >> 32; +} + +/* { dg-final { scan-tree-dump-not " >> " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " << " "optimized" } } */