This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Optimize based on possible enum class values?


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?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]