From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 635E13858D35; Mon, 26 Dec 2022 00:05:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 635E13858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672013129; bh=JGovwMks0PACQkohkFB/WbMVLqGsBWfgpsIMGA1gAFc=; h=From:To:Subject:Date:From; b=SASK8+V7WrUjw3Y7f/xL9tgNUbZPnXaVlEEj2QkZ/H4MJqxa4i48tre6NjLzuJhch wvupgixc1EXYwAFc/7rCW7rukp+SdL5OmAAMDGWmWb12vNpNe0yLZuDOaGG6TvX61s N802isGTXxuLk1t142Huqc2JRyOjgZq1tju7FBeI= From: "jhaberman at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/108226] New: __restrict on inlined function parameters does not function as expected Date: Mon, 26 Dec 2022 00:05:28 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jhaberman at gmail 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=3D108226 Bug ID: 108226 Summary: __restrict on inlined function parameters does not function as expected Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jhaberman at gmail dot com Target Milestone: --- In bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D58526 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D60712#c3 it is said that restrict/__restrict on inlined function parameters was fixed in GCC 5. But= I ran into a case where __restrict does not work as expected: // Godbolt link for this example: https://godbolt.org/z/e5j93Ex3v long g; static void Func1(void* p1, int* p2) { switch (*p2) { case 2: __builtin_memcpy(p1, &g, 1); return; case 1: __builtin_memcpy(p1, &g, 8); return; case 0: { __builtin_memcpy(p1, &g, 16); return; } } } static void Func2(char* __restrict p1, int* __restrict p2) { *p2 =3D 1; *p1 =3D 123; Func1(p1, p2); } void Func3(char* p1, int* p2) { *p2 =3D 1; Func2(p1, p2); } The __restrict qualifiers on Func2() should allow the switch() should be optimized away. Clang optimizes it, GCC does not. It appears that __restrict on function parameters can even make the code wo= rse. Consider a slight variation on this example: // Godbolt link for this example: https://godbolt.org/z/Y61qajETd long g; static void Func1(void* p1, int* p2) { switch (*p2) { case 2: __builtin_memcpy(p1, &g, 1); return; case 1: __builtin_memcpy(p1, &g, 8); return; case 0: { __builtin_memcpy(p1, &g, 16); return; } } } // If we remove __restrict here, GCC succeeds in optimizing away the switch= (). static void Func2(char* __restrict p1, int* __restrict p2) { *p1 =3D 123; *p2 =3D 1; Func1(p1, p2); } void Func3(char* p1, int* p2) { *p2 =3D 1; Func2(p1, p2); } In this case, it should be straightforward to optimize away the switch(), e= ven without __restrict. But GCC does not optimize this correctly unless we *remove* __restrict.=