public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107622] New: Missing optimization of switch-statement
@ 2022-11-10 20:11 klaus.doldinger64 at googlemail dot com
2022-11-10 20:20 ` [Bug c++/107622] " pinskia at gcc dot gnu.org
` (11 more replies)
0 siblings, 12 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2022-11-10 20:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
Bug ID: 107622
Summary: Missing optimization of switch-statement
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: klaus.doldinger64 at googlemail dot com
Target Milestone: ---
In the following example the default-case could not be reached. Therefore
introducing std::unreachable() should be useless. But the compiler produces
slightly better code with std::unreachable() as it removes one (unneccessary)
comparison (tested for x86 and avr targets).
volatile uint8_t o;
enum class State : uint8_t {A, B, C};
void g(const State s) {
switch(s) {
case State::A:
o = 10;
break;
case State::B:
o = 11;
break;
case State::C:
o = 12;
break;
default:
// std::unreachable(); // __builtin_unreachable();
break;
}
}
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
@ 2022-11-10 20:20 ` pinskia at gcc dot gnu.org
2022-11-10 21:31 ` pinskia at gcc dot gnu.org
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-10 20:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> In the following example the default-case
Yes it can. You can pass a 0xf to that function and still have well defined
behavior.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
2022-11-10 20:20 ` [Bug c++/107622] " pinskia at gcc dot gnu.org
@ 2022-11-10 21:31 ` pinskia at gcc dot gnu.org
2022-11-11 4:57 ` klaus.doldinger64 at googlemail dot com
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-10 21:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is one part which GCC could optimize better that is the store.
There might be another bug about that too.
Note as I mentioned __builtin_unreachable is needed if truely the value coming
inside g is outside of [0,3] range.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
2022-11-10 20:20 ` [Bug c++/107622] " pinskia at gcc dot gnu.org
2022-11-10 21:31 ` pinskia at gcc dot gnu.org
@ 2022-11-11 4:57 ` klaus.doldinger64 at googlemail dot com
2022-11-11 5:01 ` klaus.doldinger64 at googlemail dot com
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2022-11-11 4:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #3 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
(In reply to Andrew Pinski from comment #1)
> > In the following example the default-case
>
> Yes it can. You can pass a 0xf to that function and still have well defined
> behavior.
Oh yes, thank you for the hint!
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (2 preceding siblings ...)
2022-11-11 4:57 ` klaus.doldinger64 at googlemail dot com
@ 2022-11-11 5:01 ` klaus.doldinger64 at googlemail dot com
2022-11-11 9:20 ` rguenth at gcc dot gnu.org
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2022-11-11 5:01 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #4 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
In the following class the static data member is accessible via explicit
template instantiation from the outside. So the compiler cannot reason that the
value is in [0,2]. But this does not hold for the function g(): here mState2 is
definitely not accessible from outside. But here the std::unreachable() is
still neccessary. here the optimization is missing.
struct FSM {
enum class State : uint8_t {A, B, C};
static void f() {
switch(mState) {
case State::A:
o = 10;
break;
case State::B:
o = 11;
break;
case State::C:
o = 12;
break;
default:
// std::unreachable();
break;
}
}
static void g() {
static State mState2{State::A}; // not accessible from outside
switch(mState2) {
case State::A:
o = 10;
break;
case State::B:
o = 11;
break;
case State::C:
o = 12;
break;
default:
// std::unreachable();
break;
}
}
private:
inline static State mState{State::A}; // still modifyable via explicit
template instantiation
};
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (3 preceding siblings ...)
2022-11-11 5:01 ` klaus.doldinger64 at googlemail dot com
@ 2022-11-11 9:20 ` rguenth at gcc dot gnu.org
2022-11-11 10:16 ` klaus.doldinger64 at googlemail dot com
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-11-11 9:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |missed-optimization
--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Did you try -fstrict-enums?
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (4 preceding siblings ...)
2022-11-11 9:20 ` rguenth at gcc dot gnu.org
@ 2022-11-11 10:16 ` klaus.doldinger64 at googlemail dot com
2022-11-11 15:07 ` glisse at gcc dot gnu.org
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2022-11-11 10:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #6 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
(In reply to Richard Biener from comment #5)
> Did you try -fstrict-enums?
Yes, that doesn't change anything.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (5 preceding siblings ...)
2022-11-11 10:16 ` klaus.doldinger64 at googlemail dot com
@ 2022-11-11 15:07 ` glisse at gcc dot gnu.org
2022-11-21 10:32 ` marxin at gcc dot gnu.org
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2022-11-11 15:07 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #7 from Marc Glisse <glisse at gcc dot gnu.org> ---
(Wilhelm, when you post testcases, please post the full file including the
#include lines)
(In reply to Richard Biener from comment #5)
> Did you try -fstrict-enums?
IIUC, even if optimizations using -fstrict-enums were implemented, they would
only help with the first testcase if the number of enum values was a power of
2. For {A,B,C}, -fstrict-enums still considers 3 a valid value.
I have long wanted an attribute to specify that a particular enum is only
allowed to take the values explicitly listed, though I cannot find a relevant
issue in bugzilla at the moment.
Comment #4 is an independent issue where gcc fails to notice that since the
static variable does not escape, it can be replaced with a local constant.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (6 preceding siblings ...)
2022-11-11 15:07 ` glisse at gcc dot gnu.org
@ 2022-11-21 10:32 ` marxin at gcc dot gnu.org
2022-12-02 14:17 ` marxin at gcc dot gnu.org
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-11-21 10:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
Martin Liška <marxin at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Assignee|unassigned at gcc dot gnu.org |marxin at gcc dot gnu.org
Ever confirmed|0 |1
CC| |marxin at gcc dot gnu.org
Last reconfirmed| |2022-11-21
Status|UNCONFIRMED |ASSIGNED
--- Comment #8 from Martin Liška <marxin at gcc dot gnu.org> ---
Will take a look.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (7 preceding siblings ...)
2022-11-21 10:32 ` marxin at gcc dot gnu.org
@ 2022-12-02 14:17 ` marxin at gcc dot gnu.org
2022-12-02 14:45 ` jakub at gcc dot gnu.org
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-12-02 14:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
Martin Liška <marxin at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |NEW
Assignee|marxin at gcc dot gnu.org |unassigned at gcc dot gnu.org
--- Comment #9 from Martin Liška <marxin at gcc dot gnu.org> ---
So as was already said, -fstrict-enums is coarse-grained and can catch only a
situation like:
enum State {A, B, C};
...
void g(const State s) {
switch(s) {
...
case 257:
o = 13;
break;
orig.C:18:7: warning: case label value exceeds maximum value for type
[-Wswitch-outside-range]
18 | case 257:
| ^~~~
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (8 preceding siblings ...)
2022-12-02 14:17 ` marxin at gcc dot gnu.org
@ 2022-12-02 14:45 ` jakub at gcc dot gnu.org
2023-04-16 21:55 ` klaus.doldinger64 at googlemail dot com
2023-04-17 6:50 ` klaus.doldinger64 at googlemail dot com
11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-12-02 14:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
If you use enum with fixed underlying type, then all the values of the
underlying type are valid, even with -fstrict-enums. Otherwise
namespace std {
enum class byte : unsigned char {};
}
couldn't work properly. Otherwise, with -fstrict-enums, enum { A, B, C } can
have
values 0, 1, 2, 3 and without -fstrict-enums all the values of the underlying
type.
See https://eel.is/c++draft/dcl.enum#8 for more details.
So, except for the https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622#c4 g
case where
we should have optimized it into constant I don't see any missed optimization
here.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (9 preceding siblings ...)
2022-12-02 14:45 ` jakub at gcc dot gnu.org
@ 2023-04-16 21:55 ` klaus.doldinger64 at googlemail dot com
2023-04-17 6:50 ` klaus.doldinger64 at googlemail dot com
11 siblings, 0 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2023-04-16 21:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #11 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
Without an underlying type but with -fshort-enums the underlying type should be
as small as possible. In this case this should be a uint8_t. But in this case
we get a 16-bit value for mState. This is a clear violation of -fshort-enums.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/107622] Missing optimization of switch-statement
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
` (10 preceding siblings ...)
2023-04-16 21:55 ` klaus.doldinger64 at googlemail dot com
@ 2023-04-17 6:50 ` klaus.doldinger64 at googlemail dot com
11 siblings, 0 replies; 13+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2023-04-17 6:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #12 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
(In reply to Wilhelm M from comment #11)
> Without an underlying type but with -fshort-enums the underlying type should
> be as small as possible. In this case this should be a uint8_t. But in this
> case we get a 16-bit value for mState. This is a clear violation of
> -fshort-enums.
That (my previous comment) was clear nonsense. enum class has always underlying
type int if not specified according to the standard.
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2023-04-17 6:50 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-10 20:11 [Bug c++/107622] New: Missing optimization of switch-statement klaus.doldinger64 at googlemail dot com
2022-11-10 20:20 ` [Bug c++/107622] " pinskia at gcc dot gnu.org
2022-11-10 21:31 ` pinskia at gcc dot gnu.org
2022-11-11 4:57 ` klaus.doldinger64 at googlemail dot com
2022-11-11 5:01 ` klaus.doldinger64 at googlemail dot com
2022-11-11 9:20 ` rguenth at gcc dot gnu.org
2022-11-11 10:16 ` klaus.doldinger64 at googlemail dot com
2022-11-11 15:07 ` glisse at gcc dot gnu.org
2022-11-21 10:32 ` marxin at gcc dot gnu.org
2022-12-02 14:17 ` marxin at gcc dot gnu.org
2022-12-02 14:45 ` jakub at gcc dot gnu.org
2023-04-16 21:55 ` klaus.doldinger64 at googlemail dot com
2023-04-17 6:50 ` klaus.doldinger64 at googlemail dot com
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).