public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108181] New: [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset
@ 2022-12-19 17:43 m.cencora at gmail dot com
2022-12-19 17:49 ` [Bug middle-end/108181] " pinskia at gcc dot gnu.org
2022-12-20 12:42 ` rguenth at gcc dot gnu.org
0 siblings, 2 replies; 3+ messages in thread
From: m.cencora at gmail dot com @ 2022-12-19 17:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108181
Bug ID: 108181
Summary: [missed optimization] Call to virtual function under
runtime index should be optimized into jump with an
offset
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: m.cencora at gmail dot com
Target Milestone: ---
Given code below compiled with g++ 11 or newer, compiler should be able to
optimize 'get' into similar code as manually-optimized 'get_opt'.
g++ -std=c++20 -O2
#include <bit>
#include <cstdint>
struct foo
{
virtual constexpr int& get0() noexcept = 0;
virtual constexpr int& get1() noexcept = 0;
virtual constexpr int& get2() noexcept = 0;
virtual constexpr int& get3() noexcept = 0;
virtual constexpr int& get4() noexcept = 0;
virtual constexpr int& get5() noexcept = 0;
virtual constexpr int& get6() noexcept = 0;
virtual constexpr int& get7() noexcept = 0;
virtual constexpr int& get8() noexcept = 0;
virtual constexpr int& get9() noexcept = 0;
};
template <typename T, unsigned idx>
constexpr auto memPtr = nullptr;
template <typename T>
constexpr auto memPtr<T, 0> = &T::get0;
template <typename T>
constexpr auto memPtr<T, 1> = &T::get1;
template <typename T>
constexpr auto memPtr<T, 2> = &T::get2;
template <typename T>
constexpr auto memPtr<T, 3> = &T::get3;
template <typename T>
constexpr auto memPtr<T, 4> = &T::get4;
template <typename T>
constexpr auto memPtr<T, 5> = &T::get5;
template <typename T>
constexpr auto memPtr<T, 6> = &T::get6;
template <typename T>
constexpr auto memPtr<T, 7> = &T::get7;
template <typename T>
constexpr auto memPtr<T, 8> = &T::get8;
template <typename T>
constexpr auto memPtr<T, 9> = &T::get9;
int& get(unsigned idx, foo* f) noexcept
{
switch (idx)
{
case 0:
return (f->*memPtr<foo, 0>)();
case 1:
return (f->*memPtr<foo, 1>)();
case 2:
return (f->*memPtr<foo, 2>)();
case 3:
return (f->*memPtr<foo, 3>)();
case 4:
return (f->*memPtr<foo, 4>)();
case 5:
return (f->*memPtr<foo, 5>)();
case 6:
return (f->*memPtr<foo, 6>)();
case 7:
return (f->*memPtr<foo, 7>)();
case 8:
return (f->*memPtr<foo, 8>)();
case 9:
return (f->*memPtr<foo, 9>)();
default:
__builtin_unreachable();
}
}
int& get_opt(unsigned idx, foo* f) noexcept
{
// assuming System V x64 ABI
struct RawMemPtr
{
std::uintptr_t v[2];
};
using PtrType = int& (foo::*)() noexcept;
const RawMemPtr rawPtr{{sizeof(void*) * idx + 1, 0}};
const auto ptr = std::bit_cast<PtrType>(rawPtr);
return (f->*ptr)();
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug middle-end/108181] [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset
2022-12-19 17:43 [Bug c++/108181] New: [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset m.cencora at gmail dot com
@ 2022-12-19 17:49 ` pinskia at gcc dot gnu.org
2022-12-20 12:42 ` rguenth at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-19 17:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108181
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
Last reconfirmed| |2022-12-19
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
<bb 2> [local count: 1073741809]:
switch (idx_13(D)) <default: <L40> [0.00%], case 0: <L42> [10.00%], case 1:
<L43> [10.00%], case 2: <L44> [10.00%], case 3: <L45> [10.00%], case 4: <L46>
[10.00%], case 5: <L47> [10.00%], case 6: <L48> [10.00%], case 7: <L49>
[10.00%], case 8: <L50> [10.00%], case 9: <L51> [10.00%]>
<bb 3> [local count: 107374182]:
<L42>:
_1 = MEM[(int (*) () * *)f_14(D)];
iftmp.0_43 = *_1;
_45 = iftmp.0_43 (f_14(D)); [tail call]
goto <bb 14>; [100.00%]
<bb 4> [local count: 107374182]:
<L43>:
_2 = MEM[(int (*) () * *)f_14(D)];
iftmp.2_40 = MEM[(int (*) () *)_2 + 8B];
_42 = iftmp.2_40 (f_14(D)); [tail call]
goto <bb 14>; [100.00%]
So 3 things need to happen. First pulling out the load from f_14 (getting the
vtable) out of the switch.
And then pushing the call out of the switch.
And then seeing the load that is left is idx_13(D) * 4.
There are a few bugs for these things but I am not going to look for them right
now.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug middle-end/108181] [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset
2022-12-19 17:43 [Bug c++/108181] New: [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset m.cencora at gmail dot com
2022-12-19 17:49 ` [Bug middle-end/108181] " pinskia at gcc dot gnu.org
@ 2022-12-20 12:42 ` rguenth at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-12-20 12:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108181
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
There's a related one with puts (".."); in the switch arms where we could sink
the puts call and have the switch compute the address of the string.
tree-ssa-sink.cc has code to sink common stores, that could be enhanced to sink
common calls reasonably easily.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-12-20 12:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-19 17:43 [Bug c++/108181] New: [missed optimization] Call to virtual function under runtime index should be optimized into jump with an offset m.cencora at gmail dot com
2022-12-19 17:49 ` [Bug middle-end/108181] " pinskia at gcc dot gnu.org
2022-12-20 12:42 ` rguenth at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).