From patchwork Tue Aug 1 19:18:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Ross X-Patchwork-Id: 129445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp2881735vqg; Tue, 1 Aug 2023 12:19:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlEOUbRouK9SG3NdhJi+3GtaxX6L/0b74s6eUkblSClKV9WQesCaKz+SyGZzU0nQeK0WT6yC X-Received: by 2002:a17:906:1d6:b0:997:865a:77e5 with SMTP id 22-20020a17090601d600b00997865a77e5mr3497080ejj.45.1690917562430; Tue, 01 Aug 2023 12:19:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690917562; cv=none; d=google.com; s=arc-20160816; b=UH+kn5Rrm5IZh8xH6hrRoSxDddCtOvO9NEtMSSbypoQFUNf1RqcHcU6MuPrls93SI4 bhAhf9C7T8af4FF4P+gkMed/PC99HNEOI49UUcB9nbdDFgK1O+VLxCLtFnB80Fusj9U7 vg7yLIHZX72cxCrzRe3QacSW8dPWnNyEalc/pcqOHU9Sam8MNncKz9mteqzrEFGeqjxg b1q4k86PiZIgCWP4sA49YYhZ5JaIfuuhRHpyuDAwIg3ZuRHN2xCZYHzILmPHhh53SFiQ bN2YoDavtX4cJa811g9QpUFT8mexORd8xTSc6PAxQEKeAZhnJsTDNEG2qVPCMENCZZQ9 5eDA== 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=ngg/oS4+Pnz/xRvY0PFAA8wP1CwL++CYSxw+eqDg2xauDCcneHmnnktl+HLcQY6NIf A2et6EZzOVZ+6sSfw3MfMbQMu52QCW3te31/eZYgZococSuxJKaBGi2ek62ucSPtkSj6 T4dtwHALePruvSB5YEEZMSOaSi/f3j5gGYlXayNDWRy7deCWfXbaZIeyDXO8I8zdpSQZ JFjQrGNE8EbcqW+MMKxch1AUdDfaxpKJFx7iK82YRRKRpaXB05bIpkmNd/4lxUveCkfQ 06SNG6ahgt+vBX4dGtJaVV0GvzXstE30KnPiZnelbP/3zjMkd3Jkhz9p+j9g++uW5h3g PXnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vkJtCjKr; 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 i9-20020a170906a28900b0098d52a2bc16si686265ejz.438.2023.08.01.12.19.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 12:19: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=vkJtCjKr; 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 4C6413858422 for ; Tue, 1 Aug 2023 19:19:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C6413858422 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690917561; 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=vkJtCjKrTxbIum8RJh4bdZlK2vdSlR3qTzdcTMZRe+B+fjNRrJBqe/pikhH5gWXsj D/CIYSnqNgwI7HlFtPioGYfj1TjOoox3k9wO2CqNeRyq6/NOm29yJI8o4UpkzJoMgw T1fokmt+zdyxreDtpnF86lI0ayAFhbJ4gL5Ga4Ro= 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 BFBF13858D28 for ; Tue, 1 Aug 2023 19:18:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BFBF13858D28 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-489-vTywZ_c4M7yL9MnbkjT9pQ-1; Tue, 01 Aug 2023 15:18:36 -0400 X-MC-Unique: vTywZ_c4M7yL9MnbkjT9pQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB1D58A6949 for ; Tue, 1 Aug 2023 19:18:35 +0000 (UTC) Received: from drross.com (unknown [10.2.16.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id 503CD40C2063; Tue, 1 Aug 2023 19:18:35 +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:18:31 -0400 Message-Id: <20230801191831.432511-1-drross@redhat.com> In-Reply-To: <20230801191657.432375-1-drross@redhat.com> References: <20230801191657.432375-1-drross@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 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: 1773055574056926008 X-GMAIL-MSGID: 1773055574056926008 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" } } */