| Message ID | 20261001162056.68977-1-ktkachov@nvidia.com |
|---|---|
| State | New |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD5174BA903B for <patchwork@sourceware.org>; Thu, 1 Oct 2026 16:22:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD5174BA903B Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=i6HObtry X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012004.outbound.protection.outlook.com [40.93.195.4]) by sourceware.org (Postfix) with ESMTPS id 4CA124BA2E07 for <gcc-patches@gcc.gnu.org>; Thu, 1 Oct 2026 16:21:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CA124BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nvidia.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4CA124BA2E07 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=40.93.195.4 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1790871713; cv=pass; b=xhYn7E0hvVJ/cWy66graIM94VLujarumRoVjzioqZSafgP+pk3X8ptfEbf0dGCSTYbHyudJpdXk5wQIrnDWlBoiWvQjtXPgzeJKl6xXCX4FPS+SB96OKw4w6Pm/Gif1Lc24s6nV959h+3yZzFMFfpuD0RV8om5uhvCMHXrfgQCY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1790871713; c=relaxed/simple; bh=/xhirmgTEr/i/ZJ3fInmDgtQsyoEQhwh4BTkpcG2Rdk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ASHZtk0la5b/YmIy0HljjVhZVu/x+e5eNvnNv3X+O6zEWI+Ft/t/Y0MW6GV/bPHIEYF8CjdnPUMySX1qDzJz3kyQJrHjn7mqrRIXp/OUZLfWasBWg5TK0s6TEMgRMLy5DNG6dQdcXDVOx0beEIRX+pCtOFN21icRJD3SIPGO4kc= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=i6HObtry DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CA124BA2E07 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=a7UFE0IcCnctA1B3aEil2wlYiA86xAJPY2xjhagcKkuqd+YeFtSiA1k3OPTrRDHZgItNNEK/TYbkLOh1ZjMHS209i/Ii0BazDAQ4i1CYYc+uUBeLFNIUrdxihNCd+kQpoIQO7lalmVYg2es668s7iOGzsvswJwDQv+U3WlN6oA/H6H2wVbANQVsmrwF049VrEwpKGb+fwAEcML35oBtGQmt/cjmiFmQGhRz7TRu35BW9SW2WP+VC9wGk7IBxxNlK1s2RGts71LRcHGyuayAerT4w903rOsRbnRyyqnqGU1wCdJCeElomRdDKx7Sa7pQ5Nw2IwxQ3L+ddexVkN3I7Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QHlmFjb4QXuMvaZ6C5SirRNOp4MGffyD9upmQDxnQsg=; b=d6ZujXgtmm5tTYqO3fCAcNOeQ9tR1EhM/XIvkPeR8+TpinfO6/zwXU2SwGWangvrjTZfxC+ECWeBNk7Tt8nTZKSAyobCK99sv+NGTD1EAkU3FSO7a3rt9albRqgnRQDF8x/7EtpmK6C4cBdQc7ZgmC8/VCmrTjAz5mci77dExkCpdb6iqNJ1C/hkAjQajTn1PlgO9Qhxq/FpFgV6Mq8fXpZqeU8ClbT6Ej5h1svrnEmy57M60VBqsALAHeLJQZckVDDIYxlpdbsajgLbb17cB9//UrYCK5qPwXPtUT+4TEel1utGd84ri8XWru2NG4Hsi5Lt77BthTahi9oclZWw+Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QHlmFjb4QXuMvaZ6C5SirRNOp4MGffyD9upmQDxnQsg=; b=i6HObtry0LwiviGG9CHX2TYIJd1rRfNNjmqLFOtgRVMguU/YeJBgdLx/Je8X4/HCecN5TQ5rVdRrJiacXuaMaNdkJ72P+h6lKUjSU1T3+qnNqvy/XZdNMX+OqkkQs84TmwCVwrqKRfxYRlDsjLUaY6EBj0ZDxsS9Uas9GmDQezsfTLYCys1TZa9+jt5NiBBdSR+VSdBucUXucGrLKCl79ZpetToqMzArzgBJsk8sYrX5/9q1+x4vSlrUOpW8hZqecrkZNdgwb+9aiSqW5xNrFveIxnAvCseaucn9i1xOPTuOuCvLjlQBAaRSkbuv+s20NJynvvDw/KZHRJt6jenWsg== Received: from MN2PR01CA0045.prod.exchangelabs.com (2603:10b6:208:23f::14) by DS7PR12MB5960.namprd12.prod.outlook.com (2603:10b6:8:7f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.472.18; Thu, 1 Oct 2026 16:21:46 +0000 Received: from BN1PEPF00005FFC.namprd05.prod.outlook.com (2603:10b6:208:23f:cafe::5c) by MN2PR01CA0045.outlook.office365.com (2603:10b6:208:23f::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.23 via Frontend Transport; Thu, 1 Oct 2026 16:21:45 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BN1PEPF00005FFC.mail.protection.outlook.com (10.167.243.228) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.472.14 via Frontend Transport; Thu, 1 Oct 2026 16:21:45 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 1 Oct 2026 09:21:08 -0700 Received: from ktkachov-mlt.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 1 Oct 2026 09:21:07 -0700 From: <ktkachov@nvidia.com> To: <gcc-patches@gcc.gnu.org> CC: Kyrylo Tkachov <ktkachov@nvidia.com> Subject: [PATCH 1/4] cfgcleanup: keep tail merging out of an unfactored computed goto [PR127246] Date: Thu, 1 Oct 2026 18:20:53 +0200 Message-ID: <20261001162056.68977-1-ktkachov@nvidia.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.126.231.37] X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN1PEPF00005FFC:EE_|DS7PR12MB5960:EE_ X-MS-Office365-Filtering-Correlation-Id: 96694d11-039c-495f-3dca-08df1fd8165c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700016|376014|23010399003|1800799024|56012099006|6133799003|11063799006|10067099003|18002099003; X-Microsoft-Antispam-Message-Info: jvteAGJhI1DxtF3xQxShXILSi83v+gY2UzP746u3f0hTEJ06zpU4CQbrFEJak8wLTUvmhWDXx5PcpDfQjd0FTCI8ZAPKcAsnvtGb3bPEq8kcTB6fW3L7gXlz41GvP8bkkLk1VwRw8+dUkEj4Y2KskWpxhI8WjYfpj+9dAxRvqkQmDLgyjOPkAqn/lotRuuZtFGobkqvvWi8AsZ4sDmjLk73JVxxhKSts5rPmbjWKJi8YeoB4W90GDkSxegK6QLDg6seR3BURO8Ct0XCLFXcO/3kzth+fW42KlBX8rtoX/bfAHQnn2QnHC9iA6GqIvcfcdQSx5otmqyZC5BbbUOfzZQnto+mTHn53Uo8MpEDi/yyBms16HaLOessQ41ELZd0jdIo/f4OXCco9N/bpqWw422PDaXddWeLyVNlNlvqQDYBgpdyuPw3Yp+Vjmt38HmagkTMvsFxzHRt3JDyKeu5S/V0PgUKTb++JiAkDv5jxn4c0F8dhqVMKRdGK+aEZ6F4n9q8QUjGp+0wZ5ec1wbYBCJrow+PCthXIMDlV2vgx6uIdaQQsjSWOe6V+sbbdQCUvqgD1r07QtBPAQ/XcTjS6fmwvc/+bavNrNIiDPEqCNj6qBBadmzBiWbwXsQmAuZzMYVJISOp909BmCbk067qt8kv8nCl1S+TnHCzjGcZYEJRd2zseNs+oJvS2aNc96y98DtWOxf1XaZhltO3mS6Z3FA== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(36860700016)(376014)(23010399003)(1800799024)(56012099006)(6133799003)(11063799006)(10067099003)(18002099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: daYaaKGsN0p+NNbINoP9udrwMTT3zN/MOPfxZPA4fqNgIuXL4fgCSy5M/p1zOJ4LQh6gcoG/HgwHdNEW1TYX7fB5aTJKh3IE7GGRG6oqomKsL+WQZ8TBmS+4n1ki7pgnN8YMeE1u4iRekAQdh+XCL4iy9QbMGaCUt/g08fHshY2F5TYMsqg+DmHEllF7wBc8qxPJ7AwbmfFAS9cvNy1+/cGihsjKoi7zPXyPGwxXbPhseap+vzzJEI9TM9J2WHnbnhMak3F2x2V95K8bBdsT3zo8id+G2HL8ZDCYLs73S3Y54pjVOBUZj84vYDYeXTHi7V/2p60QypYo/FKfKnCbdsct7VCrKpsIwQRRUcI/3vD+NleOCiQS6+R7+QDJps7jqva6ufV4BAxeUnlb9AEqzsQMyFHNdh870wg25TDz+Z0mVCJWVNcliRElL5rnxB8D X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2026 16:21:45.6398 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 96694d11-039c-495f-3dca-08df1fd8165c X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN1PEPF00005FFC.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5960 X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GB_MSAUTHSOURCE_NORTHAM, GIT_PATCH_0, LOCAL_AUTHENTICATION_FAIL_SPF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
[1/4] cfgcleanup: keep tail merging out of an unfactored computed goto [PR127246]
|
|
Commit Message
Kyrylo Tkachov
Oct. 1, 2026, 4:20 p.m. UTC
From: Kyrylo Tkachov <ktkachov@nvidia.com> bbcb0c056be (PR15242) stopped try_crossjump_bb from sharing the tails of the predecessors of a computed jump unless optimizing for size. pass_duplicate_computed_gotos copies the jump back into each predecessor and a shared tail defeats that. efd8f7507b3 replaced !optimize_size with optimize_bb_for_size_p (bb), which inverted the test: tails are now shared in code optimized for speed and kept apart in code optimized for size. Test optimize_function_for_speed_p (cfun) instead. PR42621 fixed the bb-reorder half of the same change in the same way, in the gate of pass_duplicate_computed_gotos. As in bbcb0c056be, the guard then applies to the whole function, also to a block that is too large to be unfactored. try_head_merge_bb has a copy of the inverted test that cannot change code, because every successor edge of a computed jump is abnormal. It stays as it is. On the PR127246 testcase with identical handler tails, jump2 no longer crossjumps the handlers at -O2 only for compgotos to copy the tails back. At -Os the handler tails are shared again, which makes the aarch64 code of the PR testcase 54% smaller. Bootstrapped and regression tested on aarch64-unknown-linux-gnu and x86_64-pc-linux-gnu. Ok for trunk? Thanks, Kyrill gcc/ChangeLog: PR rtl-optimization/127246 * cfgcleanup.cc (try_crossjump_bb): Keep predecessors of a computed jump apart when the function, not the block, is optimized for speed. gcc/testsuite/ChangeLog: PR rtl-optimization/127246 * gcc.dg/pr127246-1.c: New test. * gcc.dg/pr127246-2.c: New test. Signed-off-by: Kyrylo Tkachov <ktkachov@nvidia.com> --- gcc/cfgcleanup.cc | 2 +- gcc/testsuite/gcc.dg/pr127246-1.c | 48 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr127246-2.c | 12 ++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr127246-1.c create mode 100644 gcc/testsuite/gcc.dg/pr127246-2.c
Comments
On Thu, Oct 1, 2026 at 6:22 PM <ktkachov@nvidia.com> wrote: > > From: Kyrylo Tkachov <ktkachov@nvidia.com> > > bbcb0c056be (PR15242) stopped try_crossjump_bb from sharing the tails of > the predecessors of a computed jump unless optimizing for size. > pass_duplicate_computed_gotos copies the jump back into each predecessor > and a shared tail defeats that. efd8f7507b3 replaced !optimize_size with > optimize_bb_for_size_p (bb), which inverted the test: tails are now shared > in code optimized for speed and kept apart in code optimized for size. > > Test optimize_function_for_speed_p (cfun) instead. PR42621 fixed the > bb-reorder half of the same change in the same way, in the gate of > pass_duplicate_computed_gotos. As in bbcb0c056be, the guard then applies > to the whole function, also to a block that is too large to be unfactored. > try_head_merge_bb has a copy of the inverted test that cannot change code, > because every successor edge of a computed jump is abnormal. It stays as > it is. > > On the PR127246 testcase with identical handler tails, jump2 no longer > crossjumps the handlers at -O2 only for compgotos to copy the tails back. > At -Os the handler tails are shared again, which makes the aarch64 code of > the PR testcase 54% smaller. > > Bootstrapped and regression tested on aarch64-unknown-linux-gnu and > x86_64-pc-linux-gnu. > > Ok for trunk? > Thanks, > Kyrill > > gcc/ChangeLog: > > PR rtl-optimization/127246 > * cfgcleanup.cc (try_crossjump_bb): Keep predecessors of a computed > jump apart when the function, not the block, is optimized for speed. > > gcc/testsuite/ChangeLog: > > PR rtl-optimization/127246 > * gcc.dg/pr127246-1.c: New test. > * gcc.dg/pr127246-2.c: New test. > > Signed-off-by: Kyrylo Tkachov <ktkachov@nvidia.com> > --- > gcc/cfgcleanup.cc | 2 +- > gcc/testsuite/gcc.dg/pr127246-1.c | 48 +++++++++++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr127246-2.c | 12 ++++++++ > 3 files changed, 61 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.dg/pr127246-1.c > create mode 100644 gcc/testsuite/gcc.dg/pr127246-2.c > > diff --git a/gcc/cfgcleanup.cc b/gcc/cfgcleanup.cc > index 1d9ec908dab..7e5a0775e22 100644 > --- a/gcc/cfgcleanup.cc > +++ b/gcc/cfgcleanup.cc > @@ -2182,7 +2182,7 @@ try_crossjump_bb (int mode, basic_block bb) > > /* Don't crossjump if this block ends in a computed jump, > unless we are optimizing for size. */ > - if (optimize_bb_for_size_p (bb) > + if (optimize_function_for_speed_p (cfun) pass_duplicate_computed_gotos::gate has return (optimize > 0 && flag_expensive_optimizations && ! optimize_function_for_size_p (fun)); so please use ! optimize_function_for_size_p (cfun) && flag_expensive_optimizations and adjust the comment to mention the reason, duplicating computed gotos. OK with that change. Richard. > && bb != EXIT_BLOCK_PTR_FOR_FN (cfun) > && computed_jump_p (BB_END (bb))) > return false; > diff --git a/gcc/testsuite/gcc.dg/pr127246-1.c b/gcc/testsuite/gcc.dg/pr127246-1.c > new file mode 100644 > index 00000000000..cfea56d631d > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr127246-1.c > @@ -0,0 +1,48 @@ > +/* PR rtl-optimization/127246 */ > +/* { dg-do compile } */ > +/* { dg-require-effective-target label_values } */ > +/* { dg-require-effective-target indirect_jumps } */ > +/* { dg-options "-O2 -fno-tree-slp-vectorize -fdump-rtl-jump2-details" } */ > + > +/* pass_duplicate_computed_gotos copies the dispatch back into every handler, > + so tail merging must not share the identical handler tails in front of > + it. */ > + > +struct frame > +{ > + const unsigned short *instr_ptr; > + long *stackpointer; > +}; > + > +#define OPS(X) \ > + X (0) X (1) X (2) X (3) X (4) X (5) X (6) X (7) X (8) X (9) X (10) \ > + X (11) X (12) X (13) X (14) X (15) X (16) X (17) X (18) X (19) > + > +extern long op (long); > + > +#define DISPATCH() goto *targets[*next_instr & 0xff] > + > +#define HANDLER(N) \ > + L_##N: \ > + frame->instr_ptr = next_instr; \ > + next_instr += 1; \ > + frame->stackpointer = stack_pointer; \ > + stack_pointer[-1] = op (N); \ > + stack_pointer -= 1; \ > + DISPATCH (); > + > +#define ENTRY(N) &&L_##N, > + > +void > +interp (struct frame *frame) > +{ > + static const void *const targets[] = { OPS (ENTRY) }; > + const unsigned short *next_instr = frame->instr_ptr; > + long *stack_pointer = frame->stackpointer; > + > + DISPATCH (); > + > + OPS (HANDLER) > +} > + > +/* { dg-final { scan-rtl-dump-not "Cross jumping" "jump2" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr127246-2.c b/gcc/testsuite/gcc.dg/pr127246-2.c > new file mode 100644 > index 00000000000..02328f04eab > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr127246-2.c > @@ -0,0 +1,12 @@ > +/* PR rtl-optimization/127246 */ > +/* { dg-do compile } */ > +/* { dg-require-effective-target label_values } */ > +/* { dg-require-effective-target indirect_jumps } */ > +/* { dg-options "-Os -fdump-rtl-jump2-details" } */ > + > +/* When optimizing for size the computed goto is not unfactored, so tail > + merging the handler tails is what we want. */ > + > +#include "pr127246-1.c" > + > +/* { dg-final { scan-rtl-dump "Cross jumping" "jump2" } } */ > -- > 2.50.1 (Apple Git-155) >
> On 2 Oct 2026, at 08:47, Richard Biener <richard.guenther@gmail.com> wrote: > > On Thu, Oct 1, 2026 at 6:22 PM <ktkachov@nvidia.com> wrote: >> >> From: Kyrylo Tkachov <ktkachov@nvidia.com> >> >> bbcb0c056be (PR15242) stopped try_crossjump_bb from sharing the tails of >> the predecessors of a computed jump unless optimizing for size. >> pass_duplicate_computed_gotos copies the jump back into each predecessor >> and a shared tail defeats that. efd8f7507b3 replaced !optimize_size with >> optimize_bb_for_size_p (bb), which inverted the test: tails are now shared >> in code optimized for speed and kept apart in code optimized for size. >> >> Test optimize_function_for_speed_p (cfun) instead. PR42621 fixed the >> bb-reorder half of the same change in the same way, in the gate of >> pass_duplicate_computed_gotos. As in bbcb0c056be, the guard then applies >> to the whole function, also to a block that is too large to be unfactored. >> try_head_merge_bb has a copy of the inverted test that cannot change code, >> because every successor edge of a computed jump is abnormal. It stays as >> it is. >> >> On the PR127246 testcase with identical handler tails, jump2 no longer >> crossjumps the handlers at -O2 only for compgotos to copy the tails back. >> At -Os the handler tails are shared again, which makes the aarch64 code of >> the PR testcase 54% smaller. >> >> Bootstrapped and regression tested on aarch64-unknown-linux-gnu and >> x86_64-pc-linux-gnu. >> >> Ok for trunk? >> Thanks, >> Kyrill >> >> gcc/ChangeLog: >> >> PR rtl-optimization/127246 >> * cfgcleanup.cc (try_crossjump_bb): Keep predecessors of a computed >> jump apart when the function, not the block, is optimized for speed. >> >> gcc/testsuite/ChangeLog: >> >> PR rtl-optimization/127246 >> * gcc.dg/pr127246-1.c: New test. >> * gcc.dg/pr127246-2.c: New test. >> >> Signed-off-by: Kyrylo Tkachov <ktkachov@nvidia.com> >> --- >> gcc/cfgcleanup.cc | 2 +- >> gcc/testsuite/gcc.dg/pr127246-1.c | 48 +++++++++++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr127246-2.c | 12 ++++++++ >> 3 files changed, 61 insertions(+), 1 deletion(-) >> create mode 100644 gcc/testsuite/gcc.dg/pr127246-1.c >> create mode 100644 gcc/testsuite/gcc.dg/pr127246-2.c >> >> diff --git a/gcc/cfgcleanup.cc b/gcc/cfgcleanup.cc >> index 1d9ec908dab..7e5a0775e22 100644 >> --- a/gcc/cfgcleanup.cc >> +++ b/gcc/cfgcleanup.cc >> @@ -2182,7 +2182,7 @@ try_crossjump_bb (int mode, basic_block bb) >> >> /* Don't crossjump if this block ends in a computed jump, >> unless we are optimizing for size. */ >> - if (optimize_bb_for_size_p (bb) >> + if (optimize_function_for_speed_p (cfun) > > pass_duplicate_computed_gotos::gate has > > return (optimize > 0 > && flag_expensive_optimizations > && ! optimize_function_for_size_p (fun)); > > so please use ! optimize_function_for_size_p (cfun) && > flag_expensive_optimizations > and adjust the comment to mention the reason, duplicating computed gotos. > > OK with that change. Thanks, here’s what I’m pushing. Kyrill > Richard. > >> && bb != EXIT_BLOCK_PTR_FOR_FN (cfun) >> && computed_jump_p (BB_END (bb))) >> return false; >> diff --git a/gcc/testsuite/gcc.dg/pr127246-1.c b/gcc/testsuite/gcc.dg/pr127246-1.c >> new file mode 100644 >> index 00000000000..cfea56d631d >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr127246-1.c >> @@ -0,0 +1,48 @@ >> +/* PR rtl-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-require-effective-target label_values } */ >> +/* { dg-require-effective-target indirect_jumps } */ >> +/* { dg-options "-O2 -fno-tree-slp-vectorize -fdump-rtl-jump2-details" } */ >> + >> +/* pass_duplicate_computed_gotos copies the dispatch back into every handler, >> + so tail merging must not share the identical handler tails in front of >> + it. */ >> + >> +struct frame >> +{ >> + const unsigned short *instr_ptr; >> + long *stackpointer; >> +}; >> + >> +#define OPS(X) \ >> + X (0) X (1) X (2) X (3) X (4) X (5) X (6) X (7) X (8) X (9) X (10) \ >> + X (11) X (12) X (13) X (14) X (15) X (16) X (17) X (18) X (19) >> + >> +extern long op (long); >> + >> +#define DISPATCH() goto *targets[*next_instr & 0xff] >> + >> +#define HANDLER(N) \ >> + L_##N: \ >> + frame->instr_ptr = next_instr; \ >> + next_instr += 1; \ >> + frame->stackpointer = stack_pointer; \ >> + stack_pointer[-1] = op (N); \ >> + stack_pointer -= 1; \ >> + DISPATCH (); >> + >> +#define ENTRY(N) &&L_##N, >> + >> +void >> +interp (struct frame *frame) >> +{ >> + static const void *const targets[] = { OPS (ENTRY) }; >> + const unsigned short *next_instr = frame->instr_ptr; >> + long *stack_pointer = frame->stackpointer; >> + >> + DISPATCH (); >> + >> + OPS (HANDLER) >> +} >> + >> +/* { dg-final { scan-rtl-dump-not "Cross jumping" "jump2" } } */ >> diff --git a/gcc/testsuite/gcc.dg/pr127246-2.c b/gcc/testsuite/gcc.dg/pr127246-2.c >> new file mode 100644 >> index 00000000000..02328f04eab >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr127246-2.c >> @@ -0,0 +1,12 @@ >> +/* PR rtl-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-require-effective-target label_values } */ >> +/* { dg-require-effective-target indirect_jumps } */ >> +/* { dg-options "-Os -fdump-rtl-jump2-details" } */ >> + >> +/* When optimizing for size the computed goto is not unfactored, so tail >> + merging the handler tails is what we want. */ >> + >> +#include "pr127246-1.c" >> + >> +/* { dg-final { scan-rtl-dump "Cross jumping" "jump2" } } */ >> -- >> 2.50.1 (Apple Git-155)
diff --git a/gcc/cfgcleanup.cc b/gcc/cfgcleanup.cc index 1d9ec908dab..7e5a0775e22 100644 --- a/gcc/cfgcleanup.cc +++ b/gcc/cfgcleanup.cc @@ -2182,7 +2182,7 @@ try_crossjump_bb (int mode, basic_block bb) /* Don't crossjump if this block ends in a computed jump, unless we are optimizing for size. */ - if (optimize_bb_for_size_p (bb) + if (optimize_function_for_speed_p (cfun) && bb != EXIT_BLOCK_PTR_FOR_FN (cfun) && computed_jump_p (BB_END (bb))) return false; diff --git a/gcc/testsuite/gcc.dg/pr127246-1.c b/gcc/testsuite/gcc.dg/pr127246-1.c new file mode 100644 index 00000000000..cfea56d631d --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr127246-1.c @@ -0,0 +1,48 @@ +/* PR rtl-optimization/127246 */ +/* { dg-do compile } */ +/* { dg-require-effective-target label_values } */ +/* { dg-require-effective-target indirect_jumps } */ +/* { dg-options "-O2 -fno-tree-slp-vectorize -fdump-rtl-jump2-details" } */ + +/* pass_duplicate_computed_gotos copies the dispatch back into every handler, + so tail merging must not share the identical handler tails in front of + it. */ + +struct frame +{ + const unsigned short *instr_ptr; + long *stackpointer; +}; + +#define OPS(X) \ + X (0) X (1) X (2) X (3) X (4) X (5) X (6) X (7) X (8) X (9) X (10) \ + X (11) X (12) X (13) X (14) X (15) X (16) X (17) X (18) X (19) + +extern long op (long); + +#define DISPATCH() goto *targets[*next_instr & 0xff] + +#define HANDLER(N) \ + L_##N: \ + frame->instr_ptr = next_instr; \ + next_instr += 1; \ + frame->stackpointer = stack_pointer; \ + stack_pointer[-1] = op (N); \ + stack_pointer -= 1; \ + DISPATCH (); + +#define ENTRY(N) &&L_##N, + +void +interp (struct frame *frame) +{ + static const void *const targets[] = { OPS (ENTRY) }; + const unsigned short *next_instr = frame->instr_ptr; + long *stack_pointer = frame->stackpointer; + + DISPATCH (); + + OPS (HANDLER) +} + +/* { dg-final { scan-rtl-dump-not "Cross jumping" "jump2" } } */ diff --git a/gcc/testsuite/gcc.dg/pr127246-2.c b/gcc/testsuite/gcc.dg/pr127246-2.c new file mode 100644 index 00000000000..02328f04eab --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr127246-2.c @@ -0,0 +1,12 @@ +/* PR rtl-optimization/127246 */ +/* { dg-do compile } */ +/* { dg-require-effective-target label_values } */ +/* { dg-require-effective-target indirect_jumps } */ +/* { dg-options "-Os -fdump-rtl-jump2-details" } */ + +/* When optimizing for size the computed goto is not unfactored, so tail + merging the handler tails is what we want. */ + +#include "pr127246-1.c" + +/* { dg-final { scan-rtl-dump "Cross jumping" "jump2" } } */