From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 84CD63858D35; Sun, 2 Aug 2020 18:22:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84CD63858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596392569; bh=0pcXDlKrCGIddYfMxK7M+/FM1OR42EqTOBRirhd4UyQ=; h=From:To:Subject:Date:From; b=u2e/TglqBcjIcwf4IFsd0nyOmHxgh091IopUEYi890adPEcDXF06SXzPNJwPIO+85 vyiq+YHJgdu+gH2+CyHPzh5sruzDCb5bK9yaJ+2ZBwykq+JWEVmXkuX+MxZE3SEHj5 5ShTvEubsGmDuBxcZEZSyMhtJLcN+emWtg0yz+Lg= From: "milasudril at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/96419] New: Constant propoagation works on global variable, but not in a function Date: Sun, 02 Aug 2020 18:22:49 +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: 10.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: milasudril 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Aug 2020 18:22:49 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96419 Bug ID: 96419 Summary: Constant propoagation works on global variable, but not in a function Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: milasudril at gmail dot com Target Milestone: --- I tried to implement a map-like data structure, with compile-time keys: #include #include #include namespace fixed_flatmap_detail { template constexpr auto sort(std::array const& x, Compare const& compare) { auto tmp =3D x; std::ranges::sort(tmp, compare); return tmp; } } template> class FixedFlatmap { public: using key_type =3D std::remove_reference_t; using value_type =3D Value; static constexpr auto size() { return std::size(Keys::items); } static constexpr auto const keys() { return s_keys; } constexpr auto const values() const { return m_vals; } constexpr auto values() { return m_vals; } constexpr auto find(key_type const& key) const { auto i =3D std::ranges::lower_bound(s_keys, key, Compare{}); if(i !=3D std::end(s_keys) && !Compare{}(key, *i)) [[likely]] { return std::begin(m_vals) + (i - std::begin(s_keys)); } return static_cast(nullptr); } constexpr auto find(key_type const& key) { return const_cast(std::as_const(*this).find(key)); } private: static constexpr auto s_keys =3D fixed_flatmap_detail::sort(Keys::items, Compare{}); std::array m_vals; }; struct KeyStruct { static constexpr std::array items{"Foo", "Bar", "Kaka"}; }; FixedFlatmap my_vals{}; auto this_value_is_computed_at_compile_time =3D my_vals.find("Kaka"); int* test_lookup(FixedFlatmap& vals) { return vals.find("Foo"); // =3D=3D static_cast(static_cast(&vals) + sizeof(int)) } Interestingly gcc succeeds to compute `find` on a global variable, but fail= s as soon as the same structure is allocated in a function. I am not an expert in compilers, but realize that it could be trickier to compute it on a non-glo= bal object (base address is not known at compile-time). However, the binary sea= rch does not even use *this. Thus, `std::lower_bound` and outcome validation sh= ould be possible to compute. Godbolt: https://gcc.godbolt.org/z/c7E3P9=