From patchwork Wed Dec 20 10:22:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 181597 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2540207dyi; Wed, 20 Dec 2023 02:23:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IEI2Hqb2YJbXO685B43lY6Vc/n5TUS2dZVlHl9sVOYgndxJ1/G289/aVKWD30Zc4x4SPdif X-Received: by 2002:a05:6214:b6c:b0:67f:9eb:f1ec with SMTP id ey12-20020a0562140b6c00b0067f09ebf1ecmr15462117qvb.56.1703067784740; Wed, 20 Dec 2023 02:23:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1703067784; cv=pass; d=google.com; s=arc-20160816; b=XFCZJ5MeAdCctIInO7Eaqlka7EeFVYA5dIUIDmOOiZJCtJRixvBz08KH5yPPxdCrpx lfbPAPxddVfEtcL5m1BQh62/1uEeQiZbHrdeJPnagHytnNzyLWsJHaNLknQ4fHWwyFLG r4tMyYS6wq/JSckxU5NV+AK6kVz4cG0PHajy8fKKXt941M4FSAfD8Gf7HtzqDJvwtMh4 mgsKHPkM23DvxDCSWdHQXkEIJjGO0Z0SEzD35wokkLISlsgT9CrKHJubo21qIT6d93SZ Y943QyNK/YCt2zkyXYskk7YtXQ9xU/lSq9nqFGjMezLptu9lDwZf14Xyr5lfRGXhOYld oQDQ== 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=tdk81/GioRulLN+/OGlW9hA6ZcyTlGjfDG2Yi1D+B1w=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=oBIhdwoPr+TnwW8Ji9vt2uBxKI+5uaKGQhh/XXPd5WdqYt/ICe0N6y+A84LI0744wN ML8d7dZ0CVCzrLcwypfCjUKEJRkE1IDBadhIyIsYyWQ1NLZ0wxHygsPOoTa3Ys5+asby bZ1DObFAKiAqyDqVcSyeSC1iUAA20xefp1VVxR4V4TtDQDuo0J0/WAFklZv0V7K5O65C X+C7gWyVTtkL9P0cak/dRaipkt7yxMoLsHH68vA94GB+f6OnE/ahzHilL73PrE2RlJVH gJD/zANqCLyOC0o1K1277WvB4XiD4fccIlTkAobtbJRmem9FLCruAH1ZjI8LaRkifZuG 5TvA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AzvKUmjj; 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 y14-20020a0cf14e000000b0067f6204a180si2955687qvl.19.2023.12.20.02.23.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 02:23:04 -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=AzvKUmjj; 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 73BD43861030 for ; Wed, 20 Dec 2023 10:23:04 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id A0B283858D20 for ; Wed, 20 Dec 2023 10:22:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0B283858D20 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 A0B283858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703067761; cv=none; b=hJxsp3VXc9Lej5DH3QnjCOm8lM16pii8N+9qJrOZ2sMtJoW5YwYpNU9+5TCCHqjhiK9ciO1zGO/ahgVIJEzecwTlWLEQ2BQMbgU5FyNIx+WcFOpJvHZ8BtauyC7rXtAYY02LshFBklJ16oGzf25hG8kWHhMQm6fXTpnnyfex88Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703067761; c=relaxed/simple; bh=b3wBtNY8pkw12flSyFRpMQzNLcNtw+ZjWU+4xIv7B9I=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=uil5b7rU+m5hkDa7tfAewT/XREkJMXPcrOTVMUtAKUZmHecsRV0VeaaiMDy4YK6IGLR8ZBRtsJEQRMEAoTrThJKHsBKsQZ3Jh4e+Bz0bVl60NsjZOsN/Lye65291ZeHSjasJFBkqaq/FOFfv8eO8P6xFSu2AXHH0MwD7gR/VdQI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703067758; 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=tdk81/GioRulLN+/OGlW9hA6ZcyTlGjfDG2Yi1D+B1w=; b=AzvKUmjjy775RbQBb6b+qjl5oNL3UJ0swSl/n27Tp3AmeaW7U0GmLuU8PuGA8qEaTj7i0R yJk3M+jMBO0G6uZk0M4Wu2XRhEpD79wIAN/dEYcYDsGzYGDAXULY/39v1Dbq4TdDFur3c9 DTXu2qQK+Ddsf/tL8RtOGqOe5spVP4s= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-658-Cp2p8gz7O6WHEjdkLtthTA-1; Wed, 20 Dec 2023 05:22:36 -0500 X-MC-Unique: Cp2p8gz7O6WHEjdkLtthTA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 49A6E2823F62; Wed, 20 Dec 2023 10:22:36 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0D808C15A0C; Wed, 20 Dec 2023 10:22:35 +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 3BKAMXpM2729185 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 20 Dec 2023 11:22:33 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BKAMWrp2729184; Wed, 20 Dec 2023 11:22:32 +0100 Date: Wed, 20 Dec 2023 11:22:32 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Fix up handling of nested casts in mergeable stmt handling [PR112941] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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_H3, 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: 1785796005568126387 X-GMAIL-MSGID: 1785796005568126387 Hi! The following patch fixes 2 issues in handling of casts for mergeable stmts. The first hunk fixes the case when we have two nested casts (typically after optimization that is zero-extension of a sign-extension because everything else should have been folded into a single cast). If the lowering of the outer cast needs to make the code conditional (e.g. for (...) { if (idx <= 32) { if (idx < 32) { ... handle_operand (idx); ... } else { ... handle_operand (32); ... } } ... } ) and the lowering of the inner one as well, right now it creates invalid SSA form, because even for the inner cast we need a PHI on the loop and the PHI argument from the latch edge is a SSA_NAME initialized in the conditionally executed bb. The hunk fixes that by detecting such a case and adding further PHI nodes at the end of the ifs such that the right value propagates to the next loop iteration. We can use 0 arguments for the other edges because the inner operand handling is only done for the first set of iterations and then the other ifs take over. The rest fixes a case of again invalid SSA form, when for a sign extension we need to use the 0 or -1 value initialized by earlier iteration in a constant idx case, the code was using the value of the loop PHI argument from latch edge rather than result; that is correct for cases expanded in straight line code after the loop, but not inside of the loop for the cases of handle_cast conditionals, there we should use PHI result. This is done in the second hunk and supported by the remaining hunks, where it clears m_bb to tell the code we aren't in the loop anymore. Note, this patch doesn't deal with similar problems during multiplication, division, floating casts etc. where we just emit a library call. I'll need to make sure in that case we don't merge more than one cast per operand. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-20 Jakub Jelinek PR tree-optimization/112941 * gimple-lower-bitint.cc (bitint_large_huge::handle_cast): If save_cast_conditional, instead of adding assignment of t4 to m_data[save_data_cnt + 1] before m_gsi, add phi nodes such that t4 propagates to m_bb loop. For constant idx, use m_data[save_data_cnt] rather than m_data[save_data_cnt + 1] if inside of the m_bb loop. (bitint_large_huge::lower_mergeable_stmt): Clear m_bb when no longer expanding inside of that loop. (bitint_large_huge::lower_comparison_stmt): Likewise. (bitint_large_huge::lower_addsub_overflow): Likewise. (bitint_large_huge::lower_mul_overflow): Likewise. (bitint_large_huge::lower_bit_query): Likewise. * gcc.dg/bitint-55.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-12-15 10:10:51.000000000 +0100 +++ gcc/gimple-lower-bitint.cc 2023-12-19 18:32:52.937388747 +0100 @@ -1401,8 +1401,29 @@ bitint_large_huge::handle_cast (tree lhs add_phi_arg (phi, m_data[save_data_cnt], edge_false, UNKNOWN_LOCATION); add_phi_arg (phi, ext, edge_true_true, UNKNOWN_LOCATION); - g = gimple_build_assign (m_data[save_data_cnt + 1], t4); - insert_before (g); + if (!save_cast_conditional) + { + g = gimple_build_assign (m_data[save_data_cnt + 1], t4); + insert_before (g); + } + else + for (basic_block bb = gsi_bb (m_gsi);;) + { + edge e1 = single_succ_edge (bb); + edge e2 = find_edge (e1->dest, m_bb), e3; + tree t5 = (e2 ? m_data[save_data_cnt + 1] + : make_ssa_name (m_limb_type)); + phi = create_phi_node (t5, e1->dest); + edge_iterator ei; + FOR_EACH_EDGE (e3, ei, e1->dest->preds) + add_phi_arg (phi, (e3 == e1 ? t4 + : build_zero_cst (m_limb_type)), + e3, UNKNOWN_LOCATION); + if (e2) + break; + t4 = t5; + bb = e1->dest; + } } if (m_bitfld_load) { @@ -1470,6 +1491,8 @@ bitint_large_huge::handle_cast (tree lhs m_data_cnt = tree_to_uhwi (m_data[save_data_cnt + 2]); if (TYPE_UNSIGNED (rhs_type)) t = build_zero_cst (m_limb_type); + else if (m_bb) + t = m_data[save_data_cnt]; else t = m_data[save_data_cnt + 1]; } @@ -2586,6 +2609,7 @@ bitint_large_huge::lower_mergeable_stmt m_gsi = gsi_after_labels (edge_bb); else m_gsi = gsi_for_stmt (stmt); + m_bb = NULL; } } } @@ -2712,6 +2736,7 @@ bitint_large_huge::lower_mergeable_stmt NULL_TREE, NULL_TREE); insert_before (g); m_gsi = gsi_for_stmt (stmt); + m_bb = NULL; } } } @@ -2890,6 +2915,7 @@ bitint_large_huge::lower_comparison_stmt extract_true_false_edges_from_block (gsi_bb (m_gsi), &true_edge, &false_edge); m_gsi = gsi_after_labels (false_edge->dest); + m_bb = NULL; } } @@ -4208,6 +4234,7 @@ bitint_large_huge::lower_addsub_overflow NULL_TREE, NULL_TREE); insert_before (g); m_gsi = gsi_for_stmt (final_stmt); + m_bb = NULL; } } } @@ -4405,6 +4432,7 @@ bitint_large_huge::lower_mul_overflow (t &true_edge, &false_edge); m_gsi = gsi_after_labels (false_edge->dest); + m_bb = NULL; } } @@ -4744,6 +4772,7 @@ bitint_large_huge::lower_bit_query (gimp m_gsi = gsi_after_labels (edge_bb); else m_gsi = gsi_for_stmt (stmt); + m_bb = NULL; } } } @@ -4905,6 +4934,7 @@ bitint_large_huge::lower_bit_query (gimp extract_true_false_edges_from_block (gsi_bb (m_gsi), &true_edge, &false_edge); m_gsi = gsi_after_labels (false_edge->dest); + m_bb = NULL; } } } --- gcc/testsuite/gcc.dg/bitint-55.c.jj 2023-12-19 23:37:41.161537400 +0100 +++ gcc/testsuite/gcc.dg/bitint-55.c 2023-12-19 23:37:08.886986817 +0100 @@ -0,0 +1,129 @@ +/* PR tree-optimization/112941 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +#if __BITINT_MAXWIDTH__ >= 4096 +void +f1 (_BitInt(4096) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] += (unsigned _BitInt(2048)) r; + p[1] += (unsigned _BitInt(2048)) s; + p[2] += (unsigned _BitInt(2048)) t; + p[3] += (unsigned _BitInt(2048)) u; +} + +void +f2 (_BitInt(4094) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] -= (unsigned _BitInt(2048)) r; + p[1] -= (unsigned _BitInt(2048)) s; + p[2] -= (unsigned _BitInt(2048)) t; + p[3] -= (unsigned _BitInt(2048)) u; +} + +void +f3 (_BitInt(4096) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] += (unsigned _BitInt(2110)) r; + p[1] += (unsigned _BitInt(2110)) s; + p[2] += (unsigned _BitInt(2110)) t; + p[3] += (unsigned _BitInt(2110)) u; +} + +void +f4 (_BitInt(4094) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] -= (unsigned _BitInt(2110)) r; + p[1] -= (unsigned _BitInt(2110)) s; + p[2] -= (unsigned _BitInt(2110)) t; + p[3] -= (unsigned _BitInt(2110)) u; +} + +void +f5 (unsigned _BitInt(4096) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] += (unsigned _BitInt(2048)) r; + p[1] += (unsigned _BitInt(2048)) s; + p[2] += (unsigned _BitInt(2048)) t; + p[3] += (unsigned _BitInt(2048)) u; +} + +void +f6 (unsigned _BitInt(4094) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] -= (unsigned _BitInt(2048)) r; + p[1] -= (unsigned _BitInt(2048)) s; + p[2] -= (unsigned _BitInt(2048)) t; + p[3] -= (unsigned _BitInt(2048)) u; +} + +void +f7 (unsigned _BitInt(4096) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] += (unsigned _BitInt(2110)) r; + p[1] += (unsigned _BitInt(2110)) s; + p[2] += (unsigned _BitInt(2110)) t; + p[3] += (unsigned _BitInt(2110)) u; +} + +void +f8 (unsigned _BitInt(4094) *p, int r, _BitInt(115) s, _BitInt(128) t, _BitInt(231) u) +{ + p[0] -= (unsigned _BitInt(2110)) r; + p[1] -= (unsigned _BitInt(2110)) s; + p[2] -= (unsigned _BitInt(2110)) t; + p[3] -= (unsigned _BitInt(2110)) u; +} + +#if __SIZEOF_INT128__ +void +f9 (_BitInt(4096) *p, __int128 r) +{ + p[0] += (unsigned _BitInt(2048)) r; +} + +void +f10 (_BitInt(4094) *p, __int128 r) +{ + p[0] -= (unsigned _BitInt(2048)) r; +} + +void +f11 (_BitInt(4096) *p, __int128 r) +{ + p[0] += (unsigned _BitInt(2110)) r; +} + +void +f12 (_BitInt(4094) *p, __int128 r) +{ + p[0] -= (unsigned _BitInt(2110)) r; +} + +void +f13 (unsigned _BitInt(4096) *p, __int128 r) +{ + p[0] += (unsigned _BitInt(2048)) r; +} + +void +f14 (unsigned _BitInt(4094) *p, __int128 r) +{ + p[0] -= (unsigned _BitInt(2048)) r; +} + +void +f15 (unsigned _BitInt(4096) *p, __int128 r) +{ + p[0] += (unsigned _BitInt(2110)) r; +} + +void +f16 (unsigned _BitInt(4094) *p, __int128 r) +{ + p[0] -= (unsigned _BitInt(2110)) r; +} +#endif +#else +int i; +#endif