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]

Re: Optimize based on possible enum class values?


On Thu, May 19, 2016 at 12:03 PM, Mikhail Maltsev <maltsevm@gmail.com> wrote:
> On 05/19/2016 04:37 AM, âJosh Chia (èää) wrote:
>> 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?
>>
> You can use this idiom to tell GCC that 'condition' is true:
>
> if (!condition)
>   __builtin_unreachable();
>
> GCC is able to infer value ranges from conditions like e.g. (x >= a && x <= b).

Or,

#define __assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)

for portability.

-- 
Mathieu


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