public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111708] New: Calling external global function instead of local static function.
@ 2023-10-05 17:47 k.frolov at samsung dot com
  2023-10-05 18:08 ` [Bug c/111708] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: k.frolov at samsung dot com @ 2023-10-05 17:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111708

            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 static
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 defined
local function with same name.

The problem is reproducible with any available GCC version, starting from GCC
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 = 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: 

-std=c99 -Wall -Wextra -fstrict-aliasing -Wcast-align -Os

With the following compilator options clang and gcc in C++ mode (with command
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 defines
"f" function by itself. Please note, there is neither ".local" nor ".global"
instruction for fuction "f". 

Code generated with "-O0" in C mode is (almost, differencies exist due to
symbol mangling) same as code generated with "-O0" in C++ mode. 

Link to godbolt site demonstrating the effect: https://godbolt.org/z/7d9GG8KvY

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-12-17 20:51 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-05 17:47 [Bug c/111708] New: Calling external global function instead of local static function k.frolov at samsung dot com
2023-10-05 18:08 ` [Bug c/111708] " pinskia at gcc dot gnu.org
2023-10-05 18:12 ` pinskia at gcc dot gnu.org
2023-10-06 11:56 ` k.frolov at samsung dot com
2023-10-06 12:14 ` k.frolov at samsung dot com
2023-10-06 12:32 ` k.frolov at samsung dot com
2023-10-08  8:36 ` muecker at gwdg dot de
2023-10-08 18:19 ` muecker at gwdg dot de
2023-10-14 12:43 ` muecker at gwdg dot de
2023-10-17 18:19 ` cvs-commit at gcc dot gnu.org
2023-11-03 19:14 ` uecker at gcc dot gnu.org
2023-12-17 20:51 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).