From patchwork Thu Dec 14 22:27:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 178942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8891687dys; Thu, 14 Dec 2023 14:28:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IGpaqFYky1H+ZMQHdrW9crXznHF89O4sQ6Cta00CrDf7PDTFyYBEzjqNMT6uhy4uLfT5sdG X-Received: by 2002:a05:620a:14b8:b0:77f:319b:a100 with SMTP id x24-20020a05620a14b800b0077f319ba100mr12124852qkj.59.1702592902753; Thu, 14 Dec 2023 14:28:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702592902; cv=pass; d=google.com; s=arc-20160816; b=NqfJAtpyC4o85DiJB35LopYK3faKV0imqBFkmZkkaa8/PmBhEacKHMZPnBR/qpuMQy dQo5EZG3eqAmFkrpwg4Oaw7u4rN3W4oyE2y3WCfaizI2O1gZsEHYht2Bq7ojUkIXdccK aTy0JyybBusSOT6bvqvgLgiLW2S0Zrn6fzK6SB0uG4w52e42k8Ij2ivvgVqTU+3F01Dh Be2kX7WcBqVgMpa3bGrdxKl8ZSkMRwY1I5qwk3iyamrpEB2TWInzF1/05Xkyk7Pf5x2z Mk/Wc/YXcYtkPRUlEiio8Pc9xT2fNWe/dl3v+gFyTHAKlkow/ea4IhMGeyJ5+6OboMek zA8A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Rzt55nHckKmurX17uIDEn8k+Er9JsdkP0o0T8/YP/Hs=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=pD4OTlGEsnWlv8jRaE/OeaTamj39M2LCEBJGLwqsvhrREr/1WZWrDt2BdkipC6AEqw Jsvru9SKlrj04IhotYs1f3CYBiARpALc0Ixf+vgDdphmWaUa30Vukr5yKEYWYzRcFsFn eL7h1mCA0ih8+hhB3XA7l3vcXqd9Vsq9RtM8HgmE5xQAXVo2Qdv4IGH/JS6Y0ikpPHhU FDliLIgU1F/UMw0avof5YkHiJy+Cg/1I8HpcfVCtTckAAYro9nb683l/kpVOuV+bpIyk m7ibMavqj2QPKq/DyGm0jj0HtB8nqpS29z1Z8j4CeEgJ8k0kRveYafiPCM5TY1iNWEvn 2WOw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zb4OiyBo; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bp44-20020a05620a45ac00b0076d2a4328c6si17215716qkb.298.2023.12.14.14.28.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:28:22 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=Zb4OiyBo; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7028E384CB99 for ; Thu, 14 Dec 2023 22:28:22 +0000 (GMT) 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 BC6923861858 for ; Thu, 14 Dec 2023 22:27:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC6923861858 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC6923861858 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702592880; cv=none; b=DgNfMx69+PsJzZavPPqrQw6s7hdsBY4VryZDKeijEyHpelMoGqlk1dSoDh1CLSDILMkEG3+aIlZESSUlO9zfrncbmZQEMMS0dm/CW5HTRZB+EiYBVT12ZirACk1yckZocQK3+MvBP0GFnJmOoEpweqvOb+jYBEjsPbcj4WSIs3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702592880; c=relaxed/simple; bh=f2ZdvW+hnCtgN6XglMB+IZV4RRXJ96pamzpi2G5edow=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=folOnNNe50hffSWbrJSXmqKMc2VB13HuXXGlXx7aeqVmAlgF4wp6IPAzOw3uvaQdxIzssUzuc38mjoYgHJ3t7TX/Mh4W6SGKyym2bvmsVXsvztIg6BdamqLnZQn1D1HqoC3OmY0Ylrc7dNyovr/alyDOfpDmhf810fnIoUlBjpA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702592878; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=Rzt55nHckKmurX17uIDEn8k+Er9JsdkP0o0T8/YP/Hs=; b=Zb4OiyBosmMmBhY9pxlGufsCl6voISLISyUQhHSrU6BxADqpGBVgpY/IUGQ7NzK0fidFvZ RPoFPZgNgfu/x45GQE/Gu1goUySMR8PJbVEHEfB5sncENMhO23pto7T4tiFJRB2EZ3ntNp Ti08sFZ0aJKE1mtWFu6kCZWlsRiVTps= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-7xYIf0nOMVy0oY6tk_ymIA-1; Thu, 14 Dec 2023 17:27:56 -0500 X-MC-Unique: 7xYIf0nOMVy0oY6tk_ymIA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A34088350E1; Thu, 14 Dec 2023 22:27:56 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 67ADF51E3; Thu, 14 Dec 2023 22:27:56 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3BEMRrH13914800 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 14 Dec 2023 23:27:54 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BEMRrSb3914799; Thu, 14 Dec 2023 23:27:53 +0100 Date: Thu, 14 Dec 2023 23:27:52 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Optimize sign-extension followed by truncation [PR113024] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785298055852286649 X-GMAIL-MSGID: 1785298055852286649 Hi! While looking at a bitint ICE, I've noticed we don't optimize in f1 and f5 functions below the 2 casts into just one at GIMPLE, even when optimize it in convert_to_integer if it appears in the same stmt. The large match.pd simplification of two conversions in a row has many complex rules and as the testcase shows, everything else from the narrowest -> widest -> prec_in_between all integer conversions is already handled, either because the inside_unsignedp == inter_unsignedp rule kicks in, or the && ((inter_unsignedp && inter_prec > inside_prec) == (final_unsignedp && final_prec > inter_prec)) one, but there is no reason why sign extension to from narrowest to widest type followed by truncation to something in between can't be done just as sign extension from narrowest to the final type. After all, if the widest type is signed rather than unsigned, regardless of the final type signedness we already handle it that way. And since PR93044 we also handle it if the final precision is not wider than the inside precision. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-14 Jakub Jelinek PR tree-optimization/113024 * match.pd (two conversions in a row): Simplify scalar integer sign-extension followed by truncation. * gcc.dg/tree-ssa/pr113024.c: New test. Jakub --- gcc/match.pd.jj 2023-12-14 11:59:28.000000000 +0100 +++ gcc/match.pd 2023-12-14 18:25:00.457961975 +0100 @@ -4754,11 +4754,14 @@ (define_operator_list SYNC_FETCH_AND_AND /* If we have a sign-extension of a zero-extended value, we can replace that by a single zero-extension. Likewise if the final conversion does not change precision we can drop the - intermediate conversion. */ + intermediate conversion. Similarly truncation of a sign-extension + can be replaced by a single sign-extension. */ (if (inside_int && inter_int && final_int && ((inside_prec < inter_prec && inter_prec < final_prec && inside_unsignedp && !inter_unsignedp) - || final_prec == inter_prec)) + || final_prec == inter_prec + || (inside_prec < inter_prec && inter_prec > final_prec + && !inside_unsignedp && inter_unsignedp))) (ocvt @0)) /* Two conversions in a row are not needed unless: --- gcc/testsuite/gcc.dg/tree-ssa/pr113024.c.jj 2023-12-14 18:35:30.652225327 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr113024.c 2023-12-14 18:37:42.056403418 +0100 @@ -0,0 +1,22 @@ +/* PR tree-optimization/113024 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-forwprop1" } */ +/* Make sure we have just a single cast per function rather than 2 casts in some cases. */ +/* { dg-final { scan-tree-dump-times " = \\\(\[a-z \]*\\\) \[xy_\]" 16 "forwprop1" { target { ilp32 || lp64 } } } } */ + +unsigned int f1 (signed char x) { unsigned long long y = x; return y; } +unsigned int f2 (unsigned char x) { unsigned long long y = x; return y; } +unsigned int f3 (signed char x) { long long y = x; return y; } +unsigned int f4 (unsigned char x) { long long y = x; return y; } +int f5 (signed char x) { unsigned long long y = x; return y; } +int f6 (unsigned char x) { unsigned long long y = x; return y; } +int f7 (signed char x) { long long y = x; return y; } +int f8 (unsigned char x) { long long y = x; return y; } +unsigned int f9 (signed char x) { return (unsigned long long) x; } +unsigned int f10 (unsigned char x) { return (unsigned long long) x; } +unsigned int f11 (signed char x) { return (long long) x; } +unsigned int f12 (unsigned char x) { return (long long) x; } +int f13 (signed char x) { return (unsigned long long) x; } +int f14 (unsigned char x) { return (unsigned long long) x; } +int f15 (signed char x) { return (long long) x; } +int f16 (unsigned char x) { return (long long) x; }