From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id EE9513858D33; Tue, 31 Oct 2023 11:52:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE9513858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1698753169; bh=jPAUK30evBNYlPWM3ZqF810WbKsxeNYlCaEnboo0t00=; h=From:To:Subject:Date:From; b=JdvEy58cVIWzszRm7t5L5yRNIesDtwjZHviegY+pJVR2xnvO1R24O53QfISX1XrwM 9nknfsA56NAN2ck6Fph6gDa8ozuVxyAC3CIlqvy06e+d6C2V0kTCxzVfYDqax7L7Gl 7XhlEIt4M/Ez9SVH/YRBK/UACyL7VYhfecQCOzE8= From: "ville.voutilainen at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/112312] New: GCC fails to optimize a C++ algorithm with a function passed in as well as with a lambda Date: Tue, 31 Oct 2023 11:52:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: ville.voutilainen 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=3D112312 Bug ID: 112312 Summary: GCC fails to optimize a C++ algorithm with a function passed in as well as with a lambda Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: ville.voutilainen at gmail dot com Target Milestone: --- See=20 https://godbolt.org/z/s8dWGzb9r When passing a (pointer to) function as a predicate argument for a C++ stdl= ib algorithm, various people hope that it optimizes as well as passing a lambda that does exactly the same thing. And with clang, it does. With gcc, the generated code is much worse for the function case, even though the function definiti= on is nearby and visible. The test code used on godbolt pasted for convenience: #include #include #include static bool pred1(const std::string& a, const std::string& b) { return false; } auto pred2 =3D [](const std::string& a, const std::string& b) { return false; }; bool func1(const std::vector& vec, const std::string& needle) { return std::ranges::lower_bound(vec, needle, pred1) !=3D vec.end(); } bool func2(const std::vector& vec, const std::string& needle) { return std::ranges::lower_bound(vec, needle, pred2) !=3D vec.end(); }=