This is the mail archive of the gcc@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: Infering that the condition of a for loop is initially true?


On 2017-09-14 22:36:34 +0200, Geza Herman wrote:
> Do you plan adding something like __builtin_assume to GCC? It is useful,
> because of this idiom to help the compiler to optimize better:
> 
> #ifdef MY_DEBUG
> #define MY_ASSERT(assertion) do { if (!(assertion)) ... } while (0)
> #else
> #define MY_ASSERT(assertion) __builtin_assume(assertion)
> #endif
> 
> It is not the same as "if (!(assertion)) __builtin_unreachable();" because
> __builtin_assume discards any side effects of "assertion".

Yes, that would be better. Because of possible side effects with
condition + __builtin_unreachable(), MPFR currently has:

#if defined(MPFR_HAVE_BUILTIN_UNREACHABLE) && __MPFR_GNUC(4, 8)
# define MPFR_ASSUME(x)                                 \
    (! __builtin_constant_p (!!(x) || !(x)) || (x) ?    \
     (void) 0 : __builtin_unreachable())
#elif defined(_MSC_VER)
# define MPFR_ASSUME(x) __assume(x)
#else
# define MPFR_ASSUME(x) ((void) 0)
#endif

(and the MPFR_ASSERTD() macro does either an assertion checking
or does MPFR_ASSUME(), depending on whether or not one wants
full assertions).

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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