From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 688AA385840F for ; Thu, 20 Jul 2023 14:47:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 688AA385840F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689864443; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z8VKFIaIUunoDZpWIdwQISKrA/zoqZEEYP3P18F7Byg=; b=Tiv6i+AYQA6qn46Y9k4E8PFehpT1RQgtW1jDs31RFuHkbfXnnW/i543c/suYT+lSA3fo7T Zu3bpOLf0Fy+usKeOMIy/d7Y+hRrbaj/tZ9msVTYbM9LFgwMwO13o+3fkCwu8+JQGesELF ZX/N9mhD7ZZpTtyyUAm+PznLfEllldo= 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-600-y4LZC7K9ML69T1i5uxf2hA-1; Thu, 20 Jul 2023 10:47:18 -0400 X-MC-Unique: y4LZC7K9ML69T1i5uxf2hA-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 B0CED3C11C6D for ; Thu, 20 Jul 2023 14:47:17 +0000 (UTC) Received: from drross.com (unknown [10.2.17.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6AF5A40C2070; Thu, 20 Jul 2023 14:47:17 +0000 (UTC) From: Drew Ross To: gcc-bugs@gcc.gnu.org Cc: Drew Ross Subject: [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955] Date: Thu, 20 Jul 2023 10:47:11 -0400 Message-Id: <20230720144711.60975-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: PR middle-end/101955 gcc/ChangeLog: * match.pd (x << c) >> c -> -(x & 1): New simplification. gcc/testsuite/ChangeLog: * gcc.dg/pr101955.c: New test. --- gcc/match.pd | 9 +++++ gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr101955.c diff --git a/gcc/match.pd b/gcc/match.pd index 8543f777a28..bf63652e80f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) +/* Optimize (X << C) >> C where C = precision(type) - 1 and X is signed + into -(X & 1). */ +(simplify + (rshift (lshift @0 INTEGER_CST@1) @@1) + (if (ANY_INTEGRAL_TYPE_P (type) + && !TYPE_UNSIGNED (type) + && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1)) + (negate (bit_and @0 { build_one_cst (type); })))) + /* Optimize x >> x into 0 */ (simplify (rshift @0 @0) diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101955.c new file mode 100644 index 00000000000..0e233269e21 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101955.c @@ -0,0 +1,62 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */ + +typedef int v4si __attribute__((vector_size(16))); + +__attribute__((noipa)) int +t1 (int x) +{ + return (x << 31) >> 31; +} + +__attribute__((noipa)) int +t2 (int x) +{ + int y = x << 31; + int z = y >> 31; + return z; +} + +__attribute__((noipa)) int +t3 (int x) +{ + int w = 31; + int y = x << w; + int z = y >> w; + return z; +} + +__attribute__((noipa)) long long +t4 (long long x) +{ + return (x << 63) >> 63; +} + +__attribute__((noipa)) long long +t5 (long long x) +{ + long long y = x << 63; + long long z = y >> 63; + return z; +} + +__attribute__((noipa)) long long +t6 (long long x) +{ + int w = 63; + long long y = x << w; + long long z = y >> w; + return z; +} + +__attribute__((noipa)) v4si +t7 (v4si x) +{ + return (x << 31) >> 31; +} + +/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */ +/* { dg-final { scan-tree-dump-not " << " "dse1" } } */ +/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */ +/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */ + -- 2.39.3