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?


Andreas Schwab <schwab@suse.de> writes:

> The problem is that assert is not allowed to evaluate the expression
> with -DNDEBUG, and side effects must not be carried out.

I'm suggesting that with -DNDEBUG, assert(x) should let the compiler
assume that x is true, but without producing any code to evaluate it at
runtime.

E.g, given

  void foo (size_t n)
  {
    size_t i;
    assert (n < 17);
    for (i = n; i < 17; i++)
      { ... }
  }

the compiler could infer that for the loop body is executed at least
once for any valid input to the function. (Where assert is used to
declare what valid inputs are like).

Then I guess one can't implement that as simply as

#define assert(x) do {if (!(x)) __builtin_unreachable() } while(0)

if that would require the compiler to generate code to evaluate x at
runtime (I'm not familiar with how __builtin_unreachable works). So one
might need something like __builtin_assume which never produces any code
to evaluate its argument at runtime.

Regarsd,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.


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