From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6560F385828E; Fri, 6 Oct 2023 12:14:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6560F385828E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1696594460; bh=vMmidBvhk8JN9qLwUW4iEm7tyF0dvx4ks3mdlMVyYCA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=QdgOPoR5LxGj3CpvOxwNg6fdp/fR0FJ78PAPWfFEyi6ETKt489A24G8dBGa6vx6cY jHjJjZDoDndoAPRNcG95SsmP/xBcecj0YWRMZpSkr2KuiiZavx24zmWwkfGL0Rrlvg OdoGvX6yHevqEfU8x7ybIl/OWP+/Tc6MFTQsA+Ew= From: "k.frolov at samsung dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/111708] Calling external global function instead of local static function. Date: Fri, 06 Oct 2023 12:14:19 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: accepts-invalid, wrong-code 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: Message-ID: In-Reply-To: References: 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 --- Comment #4 from Kirill Frolov --- With updated source ICC still gives an error.=20 MSVC works from my point of view correcly, same as Clang. Lets see to a C-standard (https://files.lhmouse.com/standards/ISO%20C%20N2176.pdf), section 6.2.2 states that: | 4) For an identifier declared with the storage-class specifier extern | in a scope in which a prior declaration of that identifier is visible [31] | if the prior declaration specifies internal or external linkage, | the linkage of the identifier at the later declaration is the same as | the linkage specified at the prior declaration. If no prior declaration | _is_ _visible_, or if the prior declaration specifies no linkage, then the | identifier has external linkage. | [31] As specified in 6.2.1, the later declaration might hide the | prior declaration. Lets see section 6.2.1: | 4) ... Within the inner scope, the identifier designates the entity | declared in the inner scope; the entity declared in the outer scope | is hidden (and _not_ _visible_) within the inner scope. As a result, I must conclude that GCC generates code right, but Clang, MSVC and ICC works incorrectly. But this happens with "-Os" options. With "-O0" option GCC generates _wrong_ _code_, as it avoids calling of _external_ function (https://godbolt.org/z/6GbPdc9vT): main: push rbp mov rbp, rsp sub rsp, 16 mov DWORD PTR [rbp-4], edi mov QWORD PTR [rbp-16], rsi mov eax, DWORD PTR [rbp-4] mov edi, eax call f leave ret f: push rbp mov rbp, rsp sub rsp, 32 mov DWORD PTR [rbp-20], edi mov eax, DWORD PTR [rbp-20] mov DWORD PTR [rbp-4], eax cmp DWORD PTR [rbp-4], 0 jg .L4 mov eax, 0 jmp .L5 .L4: mov eax, DWORD PTR [rbp-4] sub eax, 1 mov edi, eax call f .L5: leave ret In any case, GCC generated different code with -O0 and -Os, and such behavi= our is definitely a bug.=