From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4DFBB3858CDB; Thu, 5 Oct 2023 17:47:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DFBB3858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1696528037; bh=CWQEqZFI7+bNPhULtbT4kT5m0HFKA+CpCbZysIHEP0I=; h=From:To:Subject:Date:From; b=fxQ7f9ad0j3Ox0iLXItA1YBG88RcSnsOHHs6IUiFpfPVGDod6uzAfdsOTeUIcywrD vrGoAtO8A6HgMvhs5HTkIP1XzG/PtehHFdGhDLnkH10MLzARrBmZ9Pbppa8PWuDoiR PnaibEEsbZoA9MPIRqklPstG65E1XtllwwINg8Js= From: "k.frolov at samsung dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/111708] New: Calling external global function instead of local static function. Date: Thu, 05 Oct 2023 17:47:16 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: k.frolov at samsung dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111708 Bug ID: 111708 Summary: Calling external global function instead of local static function. Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: k.frolov at samsung dot com Target Milestone: --- I have a test case, in which as I believe GCC compiler must call local stat= ic function but not external function instead. This test case work as I expect with clang compiler, with gcc in C++ mode, but in C mode I have strange behaviour when gcc tries to call external function instead of already defin= ed local function with same name. The problem is reproducible with any available GCC version, starting from G= CC 4.1 and ending with current trunk. The problem is independend from the target platform and reproducible when generating code for any platform. In following examples aarch64 is used as target platform. The problem depends on optimization options: with option "-O0" compiler behaviour changes (see below). ///// Test code (.c source): static int f(int); int main(int argc, const char *argv[]) { (void)argv; return f(argc); } static int f(int f) { int x =3D f; { int f(int); if (x < 1) return 0; else return f(x - 1); } } //// end of test source. Code compiled with the following compiler options:=20 -std=3Dc99 -Wall -Wextra -fstrict-aliasing -Wcast-align -Os With the following compilator options clang and gcc in C++ mode (with comma= nd line option "-x c") produces same output: main: mov w0, 0 ret GCC in C mode produces following output: main: cmp w0, 0 ble .L2 sub w0, w0, #1 b f .L2: mov w0, 0 ret Where "f" is some unknown and global symbol. With optimization turned off, following code is generated by GCC in C-mode: .global main .type main, %function main: stp x29, x30, [sp, -32]! mov x29, sp str w0, [sp, 28] str x1, [sp, 16] ldr w0, [sp, 28] bl f ldp x29, x30, [sp], 32 ret .type f, %function f: stp x29, x30, [sp, -48]! mov x29, sp str w0, [sp, 28] ldr w0, [sp, 28] str w0, [sp, 44] ldr w0, [sp, 44] cmp w0, 0 bgt .L4 mov w0, 0 b .L5 .L4: ldr w0, [sp, 44] sub w0, w0, #1 bl f .L5: ldp x29, x30, [sp], 48 ret As you see, external symbol "f" is not referenced anymore, as compiler defi= nes "f" function by itself. Please note, there is neither ".local" nor ".global" instruction for fuction "f".=20 Code generated with "-O0" in C mode is (almost, differencies exist due to symbol mangling) same as code generated with "-O0" in C++ mode.=20 Link to godbolt site demonstrating the effect: https://godbolt.org/z/7d9GG8= KvY=