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?


Hi,


On 09/14/2017 09:50 PM, Marc Glisse wrote:
On Thu, 14 Sep 2017, Niels Möller wrote:

This is more of a question than a bug report, so I'm trying to send it
to the list rather than filing a bugzilla issue.

I think it's quite common to write for- and while-loops where the
condition is always initially true. A simple example might be

double average (const double *a, size_t n)
{
 double sum;
 size_t i;

 assert (n > 0);
 for (i = 0, sum = 0; i < n; i++)
   sum += a[i];
 return sum / n;
}

The programmer could do the microptimization to rewrite it as a
do-while-loop instead. It would be nice if gcc could infer that the
condition is initially true, and convert to a do-while loop
automatically.

Converting to a do-while-loop should produce slightly better code,
omitting the typical jump to enter the loop at the end where the
condition is checked. It would also make analysis of where variables are
written more accurate, which is my main concern at the moment.

Hello,

assert is not what you want, since it completely disappears with -DNDEBUG. clang has __builtin_assume, with gcc you want a test and __builtin_unreachable. Replacing your assert with
if(n==0)__builtin_unreachable();
gcc does skip the first test of the loop, as can be seen in the dump
produced with -fdump-tree-optimized.


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".

Thanks,
Geza


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