From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 68C83385AC09; Tue, 13 Sep 2022 06:55:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68C83385AC09 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663052131; bh=YinwH4msuJqIE97RrkM7x+9pr2APMSFA6pd1rqXiGbU=; h=From:To:Subject:Date:From; b=JSkCT1l2Apjq5t4xgRR0YuR1qPcoW6cXraWIBOvXbX3H1tyKltr0/4fXkFB/B6wGG QB8Tk04t5UQTtj8343VB6RJqB7u2hdODvI29Bvi6z1KJl8rtqrHd+IgT0TBmW5KDZJ rw/dbswNZqv3tm0A8B+zY0N/34rEntewskJDo7is= From: "lutztonineubert at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/106921] New: [11/12.1] -O1 and -fipa-icf -fpartial-inlining causes wrong code Date: Tue, 13 Sep 2022 06:55:29 +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: 11.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lutztonineubert 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=3D106921 Bug ID: 106921 Summary: [11/12.1] -O1 and -fipa-icf -fpartial-inlining causes wrong code Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lutztonineubert at gmail dot com Target Milestone: --- Short summary: The following code returns 1 if compiled with -O2 (which is wrong) and does return 0 if compiled without optimization. ``` #include #include #include #define GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) static_assert(GCC_VERSION =3D=3D 110300); template class bitset { private: using word_t =3D size_t; static constexpr size_t bits_per_word =3D sizeof(word_t) * 8; static constexpr size_t number_of_words =3D (Bits / bits_per_word) + (((B= its % bits_per_word) =3D=3D 0) ? 0 : 1); public: bool all_first(size_t n) const { { if (n > Bits) { #ifdef RETURN_INSTEAD_TERMINATE return false; #else std::terminate(); #endif } size_t i =3D 0; for (; n > bits_per_word; n -=3D bits_per_word, i++) { if (words_[i] !=3D ~word_t{0}) { return false; } } word_t last_word =3D words_[i]; for (; n !=3D 0; n--) { if ((last_word & 1) !=3D 1) { return false; } last_word >>=3D 1; } return true; } } void fill() noexcept { for (auto& word : words_) { word =3D ~word_t{0}; } } private: std::array words_{}; }; volatile int X =3D 0; int main() { if (X =3D=3D 1) { bitset<123> bitset; static_cast(bitset.all_first(123)); } else { bitset<256> bitset; bitset.fill(); if (!bitset.all_first(255)) { return 1; } } return 0; } ``` See: https://gcc.godbolt.org/z/bEexjrKP4 This issue does not exist in GCC 10 or GCC > 12.1. I couldn't test if it do= es work in GCC 11.3.1 (or the trunk of it). Additional: * I could also trigger the issue with -O1 -fipa-icf -fpartial-inlining=20 * If we do a return false instead of a std::terminate, no wrong code is generated. I am sorry, but I couldn't reduced the code any further - this already took= so much time to figure out it is a compiler bug.=