From patchwork Mon Aug 21 18:07:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jose E. Marchesi" X-Patchwork-Id: 136419 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3176472vqi; Mon, 21 Aug 2023 11:08:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7+7jRnPb2cHnLgVSBXKI0D3QzhN3GGfc2IZVtnLigFl+IueGFHCHThUaaZfy5ICzCAaui X-Received: by 2002:ac2:5b4d:0:b0:4fe:5680:db1f with SMTP id i13-20020ac25b4d000000b004fe5680db1fmr4349812lfp.53.1692641332830; Mon, 21 Aug 2023 11:08:52 -0700 (PDT) Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x24-20020aa7d398000000b005231fbd6936si6397563edq.396.2023.08.21.11.08.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:08:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RT+YgYBM; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6DB7B38313A6 for ; Mon, 21 Aug 2023 18:08:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DB7B38313A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692641296; bh=/KFXJCeWcYYFr+n2394wOqJAepePoCEIOBOpvjS95BQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=RT+YgYBMD+WUQC0C22JaiEwNZqYn4SvQKIkzkHKN5g1969t6KrVGVh8OCEwHoYg15 srJy9jDq5JS2WWI63pl4XoNtZYsVFheB6QEZhHGu8KX6FeLmUGJ7zRIFbp8lxyk1Is P8yJrq5W4nUDi6tU12kGHLixSDckkbgpro4nazjQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id AB8C13858CD1 for ; Mon, 21 Aug 2023 18:07:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB8C13858CD1 Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37LG03ja004292; Mon, 21 Aug 2023 18:07:28 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3sjmnc3h78-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 21 Aug 2023 18:07:27 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 37LGXAdC029854; Mon, 21 Aug 2023 18:07:26 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2168.outbound.protection.outlook.com [104.47.58.168]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3sjm6a9k41-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 21 Aug 2023 18:07:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DZORL+4jUAB2mvd9grsRlTDlxQn8xfhdpRbLwd+0rV01KYkIltmGJUZ0b1Tbebrj+RnOOD3PRjmAyHecQijzNEBaBlPxgiBT7mt4XAi1+53hnQX3yeX7rZbOoFGpZtLg3NChXCBqB7DRVztCoMDj5s6TT67jTKlzf4fUUZUkNkOcrVDlHJLFzH0oAkVAmBZ38bs6pnHZaGZnrmIKv3DaNCKtSbhvStVhfbnDnC1aH6J94D+BbKKiBh7JSCDweZav5rs29DySwX3yonUOmprrq9fmZUR1CxaCe3P/klsgz+z08TmsvltKQqhNMBf5XwWhTdfz+6FNybC2lKJDmNruwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=/KFXJCeWcYYFr+n2394wOqJAepePoCEIOBOpvjS95BQ=; b=anItYZWTNOvaUMzvTG8hzX+2IOalk3goCjMYmlNjnk0vUbNWeKeqMbPMAWrZLVMQ0v3mOxeOZnF2TXXRvCTh3LTA2ZOavN+goSgyNNY7UZAdv9H2VeFfKpUmmErqGfOhWp0rTAhmQyQhLT/y+EPWbKqNP0sRd6rqDdsv04nvvZkR4C5h9sS7gCcjFCXOx3EjdwM5466+JPYEuNs/W1c/aJETl6/SRaja0ZYZ6ehSodSNan/jBhiZ9/tjY7+sL0UT8FH9QybhTxf/6V/bp+oYYxR7N4s63dA2I1IbVWysXagH8R+JBEM+ubJOujSJznqm8qv2BXbpAKGRZhunIeY5yA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) by DS7PR10MB5245.namprd10.prod.outlook.com (2603:10b6:5:3a2::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.24; Mon, 21 Aug 2023 18:07:24 +0000 Received: from BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::fd36:b9b2:19f5:6f74]) by BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::fd36:b9b2:19f5:6f74%5]) with mapi id 15.20.6699.022; Mon, 21 Aug 2023 18:07:24 +0000 To: gcc-patches@gcc.gnu.org Cc: Jakub Jelinek Subject: [PATCH V2] Emit funcall external declarations only if actually used. Date: Mon, 21 Aug 2023 20:07:18 +0200 Message-Id: <20230821180718.20489-1-jose.marchesi@oracle.com> X-Mailer: git-send-email 2.30.2 X-ClientProxiedBy: LNXP265CA0053.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:5d::17) To BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB2888:EE_|DS7PR10MB5245:EE_ X-MS-Office365-Filtering-Correlation-Id: e3676898-8af0-4687-8741-08dba2717874 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: w+dSjsFm9rIxgau4+ooJh/LBOKWQ4lN8hapwEWjs+HKDNavU/kRu7gyZWTaXVXoiZ8RAe+Ln90yHJWak9PZw4SimSMukiC0KavwLuCB+8JKQYqVjUFXpJwbdIJqASW8jgRfAza0Pt1KcCJRHt3V7Lna0PFm4j2DCbqO/EvCm/vD8E8lkCw+1+72v9cWbCpUcN6GdQN1IUapzbYDOBX244zmX1P5p5Zo6nbds+MBf6D5b3Ga5g/MNuLCpsyvN4nWBun5E4QqetY/boqjBCblBuNaxXoXZUcDzZw5OXQAj3+qTT2XbXPsxCf8zUuERxyjOojL0HeyB7wTz5RgEzNcBA6zd/FubGB83XzzW3JGt1Nlj9uffSNXNVjek6iC1F1JMJfSOzCPJJTuYX0N1UTSO76ebvu8Wig4dhoyKvbPd6P81Fzo4Eez81+jwmSK/2GYchV+2b8cc5jFH2iz8eIKonZY+PH0oe4Tcg3mbvTuNpt2Ebwg94vvZsI0CNcInMbt9w0I/hlhHuDLJExc9RfApYOFx88Z0acYKxIrX0V3QR0Lb01JQoodTPErgWz2Oth/KIEVqe9mIbVyxgKhSnMni2Q== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB2888.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(136003)(396003)(376002)(366004)(346002)(39860400002)(186009)(1800799009)(451199024)(66556008)(6916009)(66476007)(66946007)(6512007)(316002)(2616005)(8676002)(8936002)(4326008)(1076003)(41300700001)(36756003)(478600001)(6666004)(38100700002)(6486002)(6506007)(83380400001)(2906002)(84970400001)(86362001)(5660300002)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WceYfn+Zh7P4FEPA2hB7J5LFrZCD/uqJvn3LEoP/hfsDNlm09UXaJSMIu4FZcxM51RJ1d2VgjCqB+61GmRAPgZZ4AX+nxP5j6Y07iC9ZzSTeC/4PL7aet3KsNVV//UPlNkTqDJ1nJCNiIiGtsSCwyDWiy0IwuWFZFWvdhsLT1I3xC/y2c7CmnhIAfy6u5X6DbZBcicQimFqM2CqIJwEuCKUzq3EeY4mS+j2bIZzwJLlKMmSZ5QTtKp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cZSB/1rizGyyewuPop9ufPbzgqU1einWAybtfHjEThWu/c/076UvsY6xIkmDJ0QKwPAkIVZhS41TR0AVn0GpM9w== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e3676898-8af0-4687-8741-08dba2717874 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB2888.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2023 18:07:24.1749 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: XnBWzh/v2YZ4ce4HMoYaoNtOffF0fpkxy7XV/cnaDvlaWGeM3U0/HGcghX1OkVo3ReNayFNoai5+GLVpv66eScAP68BkBTesT8yz4pZfioA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR10MB5245 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-21_07,2023-08-18_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 phishscore=0 suspectscore=0 bulkscore=0 malwarescore=0 mlxscore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308210168 X-Proofpoint-GUID: phxqnPO6PIqBs6fCvsjAgZD30kFaW3io X-Proofpoint-ORIG-GUID: phxqnPO6PIqBs6fCvsjAgZD30kFaW3io X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLACK 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Jose E. Marchesi via Gcc-patches" From: "Jose E. Marchesi" Reply-To: "Jose E. Marchesi" Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774863078502783369 X-GMAIL-MSGID: 1774863078502783369 [Differences from V1: - Prototype for call_from_call_insn moved before comment block. - Reuse the `call' flag for SYMBOL_REF_LIBCALL. - Fallback to check REG_CALL_DECL in non-direct calls. - New test to check correct behavior for non-direct calls.] There are many places in GCC where alternative local sequences are tried in order to determine what is the cheapest or best alternative to use in the current target. When any of these sequences involve a libcall, the current implementation of emit_library_call_value_1 introduce a side-effect consisting on emitting an external declaration for the funcall (such as __divdi3) which is thus emitted even if the sequence that does the libcall is not retained. This is problematic in targets such as BPF, because the kernel loader chokes on the spurious symbol __divdi3 and makes the resulting BPF object unloadable. Note that BPF objects are not linked before being loaded. This patch changes emit_library_call_value_1 to mark the target SYMBOL_REF as a libcall. Then, the emission of the external declaration is done in the first loop of final.cc:shorten_branches. This happens only if the corresponding sequence has been kept. Regtested in x86_64-linux-gnu. Tested with host x86_64-linux-gnu with target bpf-unknown-none. gcc/ChangeLog * rtl.h (SYMBOL_REF_LIBCALL): Define. * calls.cc (emit_library_call_value_1): Do not emit external libcall declaration here. * final.cc (shorten_branches): Do it here. gcc/testsuite/ChangeLog * gcc.target/bpf/divmod-libcall-1.c: New test. * gcc.target/bpf/divmod-libcall-2.c: Likewise. * gcc.c-torture/compile/libcall-2.c: Likewise. --- gcc/calls.cc | 9 +++--- gcc/final.cc | 30 +++++++++++++++++++ gcc/rtl.h | 5 ++++ .../gcc.c-torture/compile/libcall-2.c | 8 +++++ .../gcc.target/bpf/divmod-libcall-1.c | 19 ++++++++++++ .../gcc.target/bpf/divmod-libcall-2.c | 16 ++++++++++ 6 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/libcall-2.c create mode 100644 gcc/testsuite/gcc.target/bpf/divmod-libcall-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/divmod-libcall-2.c diff --git a/gcc/calls.cc b/gcc/calls.cc index 1f3a6d5c450..219ea599b16 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -4388,9 +4388,10 @@ emit_library_call_value_1 (int retval, rtx orgfun, rtx value, || argvec[i].partial != 0) update_stack_alignment_for_call (&argvec[i].locate); - /* If this machine requires an external definition for library - functions, write one out. */ - assemble_external_libcall (fun); + /* Mark the emitted target as a libcall. This will be used by final + in order to emit an external symbol declaration if the libcall is + ever used. */ + SYMBOL_REF_LIBCALL (fun) = 1; original_args_size = args_size; args_size.constant = (aligned_upper_bound (args_size.constant @@ -4735,7 +4736,7 @@ emit_library_call_value_1 (int retval, rtx orgfun, rtx value, valreg, old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far); - if (flag_ipa_ra) + if (flag_ipa_ra || SYMBOL_REF_LIBCALL (orgfun)) { rtx datum = orgfun; gcc_assert (GET_CODE (datum) == SYMBOL_REF); diff --git a/gcc/final.cc b/gcc/final.cc index dd3e22547ac..2041e43fdd1 100644 --- a/gcc/final.cc +++ b/gcc/final.cc @@ -804,6 +804,8 @@ make_pass_compute_alignments (gcc::context *ctxt) } +static rtx call_from_call_insn (rtx_call_insn *insn); + /* Make a pass over all insns and compute their actual lengths by shortening any branches of variable length if possible. */ @@ -850,6 +852,34 @@ shorten_branches (rtx_insn *first) for (insn = get_insns (), i = 1; insn; insn = NEXT_INSN (insn)) { INSN_SHUID (insn) = i++; + + /* If this is a `call' instruction implementing a libcall, and + this machine requires an external definition for library + functions, write one out. */ + if (CALL_P (insn)) + { + rtx x; + + if ((x = call_from_call_insn (dyn_cast (insn))) + && (x = XEXP (x, 0)) + && MEM_P (x) + && (x = XEXP (x, 0)) + && SYMBOL_REF_P (x) + && SYMBOL_REF_LIBCALL (x)) + { + /* Direct call. */ + assemble_external_libcall (x); + } + else if ((x = find_reg_note (insn, REG_CALL_DECL, NULL_RTX)) + && (x = XEXP (x, 0))) + { + /* Indirect call with REG_CALL_DECL note. */ + gcc_assert (SYMBOL_REF_P (x)); + if (SYMBOL_REF_LIBCALL (x)) + assemble_external_libcall (x); + } + } + if (INSN_P (insn)) continue; diff --git a/gcc/rtl.h b/gcc/rtl.h index e1c51156f90..28be708a55f 100644 --- a/gcc/rtl.h +++ b/gcc/rtl.h @@ -334,6 +334,7 @@ struct GTY((desc("0"), tag("0"), 1 in a CALL_INSN logically equivalent to ECF_LOOPING_CONST_OR_PURE and DECL_LOOPING_CONST_OR_PURE_P. 1 in a VALUE is SP_DERIVED_VALUE_P in cselib.cc. + 1 in a SYMBOL_REF if it is the target of a libcall. Dumped as "/c" in RTL dumps. */ unsigned int call : 1; /* 1 in a REG, MEM, or CONCAT if the value is set at most once, anywhere. @@ -2734,6 +2735,10 @@ do { \ #define SYMBOL_REF_USED(RTX) \ (RTL_FLAG_CHECK1 ("SYMBOL_REF_USED", (RTX), SYMBOL_REF)->used) +/* 1 if RTX is a symbol_ref that represents a libcall target. */ +#define SYMBOL_REF_LIBCALL(RTX) \ + (RTL_FLAG_CHECK1 ("SYMBOL_REF_LIBCALL", (RTX), SYMBOL_REF)->call) + /* 1 if RTX is a symbol_ref for a weak symbol. */ #define SYMBOL_REF_WEAK(RTX) \ (RTL_FLAG_CHECK1 ("SYMBOL_REF_WEAK", (RTX), SYMBOL_REF)->return_val) diff --git a/gcc/testsuite/gcc.c-torture/compile/libcall-2.c b/gcc/testsuite/gcc.c-torture/compile/libcall-2.c new file mode 100644 index 00000000000..b33944c83ff --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/libcall-2.c @@ -0,0 +1,8 @@ +/* Make sure that external refences for libcalls are generated even for + indirect calls. */ + +/* { dg-do compile } */ +/* { dg-options "-O2 -mcmodel=large" { target x86_64-*-* } } */ +/* { dg-final { scan-assembler "globl\t__divti3" } } */ + +__int128 a, b; void foo () { a = a / b; } diff --git a/gcc/testsuite/gcc.target/bpf/divmod-libcall-1.c b/gcc/testsuite/gcc.target/bpf/divmod-libcall-1.c new file mode 100644 index 00000000000..7481076602a --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/divmod-libcall-1.c @@ -0,0 +1,19 @@ +/* This test makes sure that no spurious external symbol declarations are + emitted for libcalls in tried but eventually not used code sequences. */ + +/* { dg-do compile } */ +/* { dg-options "-O2 -mcpu=v3" } */ +/* { dg-final { scan-assembler-not "global\t__divdi3" } } */ +/* { dg-final { scan-assembler-not "global\t__moddi3" } } */ + +int +foo (unsigned int len) +{ + return ((unsigned long)len) * 234 / 5; +} + +int +bar (unsigned int len) +{ + return ((unsigned long)len) * 234 % 5; +} diff --git a/gcc/testsuite/gcc.target/bpf/divmod-libcall-2.c b/gcc/testsuite/gcc.target/bpf/divmod-libcall-2.c new file mode 100644 index 00000000000..792d689395a --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/divmod-libcall-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mcpu=v3" } */ +/* { dg-final { scan-assembler "global\t__divdi3" } } */ +/* { dg-final { scan-assembler "global\t__moddi3" } } */ + +int +foo (unsigned int len) +{ + return ((long)len) * 234 / 5; +} + +int +bar (unsigned int len) +{ + return ((long)len) * 234 % 5; +}