From patchwork Wed Aug 2 16:23:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Ross X-Patchwork-Id: 129934 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp574413vqx; Wed, 2 Aug 2023 09:24:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlHudWV+MuAtP2nf06BQdnYhQFAYLpWotzefQW8Bt4Ju7/NGUUvilqSvDJ9MiGlYeLcfK9xv X-Received: by 2002:a17:907:1dee:b0:99b:c1b7:d806 with SMTP id og46-20020a1709071dee00b0099bc1b7d806mr5445962ejc.0.1690993451444; Wed, 02 Aug 2023 09:24:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690993451; cv=none; d=google.com; s=arc-20160816; b=p1m8/3YdaUH+OIrOEDSzQGYqRUZTDrXMGNmH8v9u07hCUoYjDe/n3f8vGB2Pd66FF/ dnZViREWufAEUkAuPv5bteydrNGBuL7vfpK2AKuGi9yrMppN3YUeuB9VcUCHav7D78v+ ZmLobp1jToIRjhFnU6Xof4Ho6LvgbdjK3xatzYywhXJdBJjggj/9+Q87d3oTaVokaNRZ Cn3zOsOxndWst6TpkpxJEh/74q+hUA/hiP6yR8mWqgRGq/TFAuFa6EVsNyvKJ1dq9lFx ulJeicKuJW7I8nciJ4kEEhJIok5A0D8o30dcu2jEV8zu3tqhKLi0H6tAHDyKq0cMkm65 7VAg== 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=l4BFZ6EEGe8weAOaccxja/9yr+C9dlx9Yn0NvFkxeww=; fh=+7oqyIxxdDZHhy0RvojIG59KP1WcqOWasYDHPRLsigU=; b=n9LGO5tAl+o57br0OnmbwUtm9P2uTG8IRqi+DPiAT3xiQSUJXvUcI1UcfpRobxBHUw 9MsmmIR4OlldAnl3jADU6sd6AZ54TsXpUtn+pcQZny7QonkXcmz9Cqm+NIB5FTJfAqg4 1b2IBg+k8YF0t109yGKYAOh7P9+6w070AU9jMsDNPHi/0McMkuL2//8refjM5HZ56aio tF9AtQQ0NpWh5diH33cDDLglE5VpSw6vg5Loqnjw+4UVr0JEYfDj42nnq/sRzafaRTka a52UuPzg4ADi1j5HlBGOXRIcKT40Vsl+1PMFTTj0tlAiBlYq88+fU5QZP/KMyLroB8xu PfNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Gk46p41m; 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 m15-20020a17090607cf00b00993150ec3c7si9921334ejc.966.2023.08.02.09.24.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 09:24:11 -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=Gk46p41m; 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 5ED6B3858D37 for ; Wed, 2 Aug 2023 16:24:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5ED6B3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690993450; bh=l4BFZ6EEGe8weAOaccxja/9yr+C9dlx9Yn0NvFkxeww=; 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=Gk46p41mS1TTET27H4KNXFMmqFjfK/JUJESnfYiuhQfsQFsIPxIfDy4perJxq1YJb Z3yctPyBtlOBP3aA4iH6vojptrxYwSAJQ2wJMuGQsiu2U68OE0HqYuYWRLVu1OS4PG UxtOtAKmg62XlXizH6ArMIEbbahgzP6CdMEtvHhY= 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 642C43858D1E for ; Wed, 2 Aug 2023 16:23:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 642C43858D1E Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-V8OtHvXRPRGPGBITOGEAsA-1; Wed, 02 Aug 2023 12:23:25 -0400 X-MC-Unique: V8OtHvXRPRGPGBITOGEAsA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5569E3C100BB for ; Wed, 2 Aug 2023 16:23:25 +0000 (UTC) Received: from drross.com (unknown [10.2.16.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3334F2166B25; Wed, 2 Aug 2023 16:23:25 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: Drew Ross Subject: [PATCH] match.pd: Canonicalize (signed x << c) >> c [PR101955] Date: Wed, 2 Aug 2023 12:23:22 -0400 Message-Id: <20230802162322.447469-1-drross@redhat.com> In-Reply-To: <20230802162143.447292-1-drross@redhat.com> References: <20230802162143.447292-1-drross@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 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: 1773135149318691023 X-GMAIL-MSGID: 1773135149318691023 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..62de97f0186 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 (convert @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..6a04288511f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101955.c @@ -0,0 +1,63 @@ +/* { dg-do compile { target int32 } } */ +/* { 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" } } */