This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Optimize based on possible enum class values?
- From: âJosh Chia (èää) <joshchia at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 19 May 2016 09:37:52 +0800
- Subject: Optimize based on possible enum class values?
- Authentication-results: sourceware.org; auth=none
I have an enum class like this that I generated and examined assembly
output for under gcc 5.3.1 x86_64 -O3:
enum class E : uint8_t {
E0,
E1,
E2
};
I have an 'if' condition "if (e == E::E2) ...". I see that the
generated assembly involves comparing the underlying integer value
against 2, and then "sete %al". If we know that the possible values
are only 0, 1, 2, we can simply do a right-shift of 2 on the
underlying integer value. Although I haven't done any benchmarks, this
seems slightly more optimal to me. At least we save one instruction.
Is there a way to tell gcc to assume that the underlying integer value
is for a valid enum value and optimize accordingly? Even more
generally, is there a way to give optimization hints to the compiler
that a variable can only have certain values at a certain point in the
code?