From patchwork Thu Mar 14 10:42:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1912064 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cLyHRtVG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TwP9J3prFz1yWt for ; Thu, 14 Mar 2024 21:43:00 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A79313858C31 for ; Thu, 14 Mar 2024 10:42:57 +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 5F89C3858C31 for ; Thu, 14 Mar 2024 10:42:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F89C3858C31 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 5F89C3858C31 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=1710412955; cv=none; b=iuqo8T2mZfHTdANqXxC2YQ4jx+qgo11LzvOW2JtxuFldDRmAwBcvHszG9Df2viy6C8z13ZyYkw6CRAWKqdaRQ0idClwlEcZ6gLWG9IAOyUeKidI68OBcz2ehVqrT7gBdQ6N86iUqr+cfg6BobC/KCjkSsL2q8HnGuBdPmXauq7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710412955; c=relaxed/simple; bh=mw2uDRR6Ok8kn6Ph8iWmIY/venkTSkU7azL4LL/Xaww=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=YZja9278/q/D5rmpUtovg/PB2wyuackWamtdGq0tNHcTo4eoLy+Y7/nPHHbyDQ7kQ441DQUAyiVCwYGZ0L1BrFHmHtgX1NmzhZBI2DVkqgghSmnUN3LVtlhXXKFIBhaKlG0ZT9x6TKVvVg4zDLshsywV/ba7zKUd2d3fWyAkxHY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710412953; 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:in-reply-to:in-reply-to: references:references; bh=EuHE7+NuggCAYQ0Vgc+rkhj88/zEy6xp3Rnw67/YI/k=; b=cLyHRtVGI58GVw999obSLGHoWb2tCEQI9xJACGTynALm9R/JxjV/f0tOf1pgxWHLRNII9/ +jPLzcY/p68A7qwvTQ169V2m9Q3VeHELZIMzXSiyKQ20qUGdRs3NKbxLqrlKFnEycRadJF ibUabvi8AMXhsegTXYnOg8QBC9NK5YI= 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-623-egyQAXmGO82vuQ8cYTV2sw-1; Thu, 14 Mar 2024 06:42:31 -0400 X-MC-Unique: egyQAXmGO82vuQ8cYTV2sw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 F077484B06B; Thu, 14 Mar 2024 10:42:30 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B54FA2166AE4; Thu, 14 Mar 2024 10:42:30 +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 42EAgSgR3703562 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 14 Mar 2024 11:42:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42EAgSFD3703561; Thu, 14 Mar 2024 11:42:28 +0100 Date: Thu, 14 Mar 2024 11:42:28 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bitint, v2: Fix up adjustment of large/huge _BitInt arguments of returns_twice calls [PR113466] Message-ID: References: <74930234-p163-pqn4-4r73-7s578313238q@fhfr.qr> MIME-Version: 1.0 In-Reply-To: <74930234-p163-pqn4-4r73-7s578313238q@fhfr.qr> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org On Thu, Mar 14, 2024 at 09:59:12AM +0100, Richard Biener wrote: > On Thu, 14 Mar 2024, Jakub Jelinek wrote: > > > On Thu, Mar 14, 2024 at 09:48:45AM +0100, Richard Biener wrote: > > > Ugh. OK, but I wonder whether we might want to simply delay > > > fixing the CFG for inserts before returns-twice? Would that make > > > things less ugly? > > > > You mean in lower_call just remember if we added anything before > > ECF_RETURNS_TWICE call (or say add such stmt into a vector) and > > then fix it up before the end of the pass? > > Yeah, or just walk BBs with abnormal preds, whatever tracking is > easier. Walking all the bbs with abnormal preds would mean I'd have to walk their whole bodies, because the ECF_RETURNS_TWICE call is no longer at the start. The following patch seems to work, ok for trunk if it passes full bootstrap/regtest? 2024-03-14 Jakub Jelinek PR tree-optimization/113466 * gimple-lower-bitint.cc (bitint_large_huge): Add m_returns_twice_calls member. (bitint_large_huge::bitint_large_huge): Initialize it. (bitint_large_huge::~bitint_large_huge): Release it. (bitint_large_huge::lower_call): Remember ECF_RETURNS_TWICE call stmts before which at least one statement has been inserted. (gimple_lower_bitint): Move argument loads before ECF_RETURNS_TWICE calls to a different block and add corresponding PHIs. * gcc.dg/bitint-100.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-03-13 15:34:29.969725763 +0100 +++ gcc/gimple-lower-bitint.cc 2024-03-14 11:25:07.763169074 +0100 @@ -419,7 +419,8 @@ struct bitint_large_huge bitint_large_huge () : m_names (NULL), m_loads (NULL), m_preserved (NULL), m_single_use_names (NULL), m_map (NULL), m_vars (NULL), - m_limb_type (NULL_TREE), m_data (vNULL) {} + m_limb_type (NULL_TREE), m_data (vNULL), + m_returns_twice_calls (vNULL) {} ~bitint_large_huge (); @@ -553,6 +554,7 @@ struct bitint_large_huge unsigned m_bitfld_load; vec m_data; unsigned int m_data_cnt; + vec m_returns_twice_calls; }; bitint_large_huge::~bitint_large_huge () @@ -565,6 +567,7 @@ bitint_large_huge::~bitint_large_huge () delete_var_map (m_map); XDELETEVEC (m_vars); m_data.release (); + m_returns_twice_calls.release (); } /* Insert gimple statement G before current location @@ -5248,6 +5251,7 @@ bitint_large_huge::lower_call (tree obj, default: break; } + bool returns_twice = (gimple_call_flags (stmt) & ECF_RETURNS_TWICE) != 0; for (unsigned int i = 0; i < nargs; ++i) { tree arg = gimple_call_arg (stmt, i); @@ -5271,6 +5275,11 @@ bitint_large_huge::lower_call (tree obj, arg = make_ssa_name (TREE_TYPE (arg)); gimple *g = gimple_build_assign (arg, v); gsi_insert_before (&gsi, g, GSI_SAME_STMT); + if (returns_twice) + { + m_returns_twice_calls.safe_push (stmt); + returns_twice = false; + } } gimple_call_set_arg (stmt, i, arg); if (m_preserved == NULL) @@ -7091,6 +7100,66 @@ gimple_lower_bitint (void) if (edge_insertions) gsi_commit_edge_inserts (); + /* Fix up arguments of ECF_RETURNS_TWICE calls. Those were temporarily + inserted before the call, but that is invalid IL, so move them to the + right place and add corresponding PHIs. */ + if (!large_huge.m_returns_twice_calls.is_empty ()) + { + auto_vec arg_stmts; + while (!large_huge.m_returns_twice_calls.is_empty ()) + { + gimple *stmt = large_huge.m_returns_twice_calls.pop (); + gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (stmt)); + while (gsi_stmt (gsi) != stmt) + { + arg_stmts.safe_push (gsi_stmt (gsi)); + gsi_remove (&gsi, false); + } + gimple *g; + basic_block bb = NULL; + edge e = NULL, ead = NULL; + FOR_EACH_VEC_ELT (arg_stmts, i, g) + { + gsi_safe_insert_before (&gsi, g); + if (i == 0) + { + bb = gimple_bb (stmt); + gcc_checking_assert (EDGE_COUNT (bb->preds) == 2); + e = EDGE_PRED (bb, 0); + ead = EDGE_PRED (bb, 1); + if ((ead->flags & EDGE_ABNORMAL) == 0) + std::swap (e, ead); + gcc_checking_assert ((e->flags & EDGE_ABNORMAL) == 0 + && (ead->flags & EDGE_ABNORMAL)); + } + tree lhs = gimple_assign_lhs (g); + tree arg = lhs; + gphi *phi = create_phi_node (copy_ssa_name (arg), bb); + add_phi_arg (phi, arg, e, UNKNOWN_LOCATION); + tree var = create_tmp_reg (TREE_TYPE (arg)); + suppress_warning (var, OPT_Wuninitialized); + arg = get_or_create_ssa_default_def (cfun, var); + SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg) = 1; + add_phi_arg (phi, arg, ead, UNKNOWN_LOCATION); + arg = gimple_phi_result (phi); + SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg) = 1; + imm_use_iterator iter; + gimple *use_stmt; + FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) + { + if (use_stmt == phi) + continue; + gcc_checking_assert (use_stmt == stmt); + use_operand_p use_p; + FOR_EACH_IMM_USE_ON_STMT (use_p, iter) + SET_USE (use_p, arg); + } + } + update_stmt (stmt); + arg_stmts.truncate (0); + } + } + return ret; } --- gcc/testsuite/gcc.dg/bitint-100.c.jj 2024-03-14 10:32:02.432644685 +0100 +++ gcc/testsuite/gcc.dg/bitint-100.c 2024-03-14 11:21:16.444398599 +0100 @@ -0,0 +1,108 @@ +/* PR tree-optimization/113466 */ +/* { dg-do compile { target bitint575 } } */ +/* { dg-options "-O2" } */ + +int foo (int); + +__attribute__((returns_twice, noipa)) _BitInt(325) +bar (_BitInt(575) x) +{ + (void) x; + return 0wb; +} + +__attribute__((returns_twice, noipa)) _BitInt(325) +garply (_BitInt(575) x, _BitInt(575) y, _BitInt(575) z, int u, int v, _BitInt(575) w) +{ + (void) x; + (void) y; + (void) z; + (void) u; + (void) v; + (void) w; + return 0wb; +} + +_BitInt(325) +baz (_BitInt(575) y) +{ + foo (1); + return bar (y); +} + +_BitInt(325) +qux (int x, _BitInt(575) y) +{ + if (x == 25) + x = foo (2); + else if (x == 42) + x = foo (foo (3)); + return bar (y); +} + +void +corge (int x, _BitInt(575) y, _BitInt(325) *z) +{ + void *q[] = { &&l1, &&l2, &&l3, &&l3 }; + if (x == 25) + { + l1: + x = foo (2); + } + else if (x == 42) + { + l2: + x = foo (foo (3)); + } +l3: + *z = bar (y); + if (x < 4) + goto *q[x & 3]; +} + +_BitInt(325) +freddy (int x, _BitInt(575) y) +{ + bar (y); + ++y; + if (x == 25) + x = foo (2); + else if (x == 42) + x = foo (foo (3)); + return bar (y); +} + +_BitInt(325) +quux (_BitInt(575) x, _BitInt(575) y, _BitInt(575) z) +{ + _BitInt(575) w = x + y; + foo (1); + return garply (x, y, z, 42, 42, w); +} + +_BitInt(325) +grault (int x, _BitInt(575) y, _BitInt(575) z) +{ + _BitInt(575) v = x + y; + _BitInt(575) w = x - y; + if (x == 25) + x = foo (2); + else if (x == 42) + x = foo (foo (3)); + return garply (y, z, v, 0, 0, w); +} + +_BitInt(325) +plugh (int x, _BitInt(575) y, _BitInt(575) z, _BitInt(575) v, _BitInt(575) w) +{ + garply (y, z, v, 1, 2, w); + ++y; + z += 2wb; + v <<= 3; + w *= 3wb; + if (x == 25) + x = foo (2); + else if (x == 42) + x = foo (foo (3)); + return garply (y, z, v, 1, 2, w); +}