https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30484

--- Comment #17 from rguenther at suse dot de <rguenther at suse dot de> ---
On Tue, 24 Aug 2021, vincent-gcc at vinc17 dot net wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30484
> 
> --- Comment #16 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
> The issue is that the source code assuming -fno-wrapv may be more complex, 
> thus
> giving slower generated code. Here's an example, which consists in adding 3
> signed integers, for which the user knows that the sum is representable, so
> that the only issue is a potential integer overflow in the first addition. 
> I've
> used GCC 11.2.0 on x86_64.
> 
> With -fwrapv, the integer overflow is well-defined as wrapping, so that the
> user can write:
> 
> int f (int a, int b, int c)
> {
>   return a + b + c;
> }
> 
> The generated code with -O3 -fwrapv has 2 instructions (the 2 additions):
> 
>         addl    %edx, %esi
>         leal    (%rsi,%rdi), %eax
> 
> But without -fwrapv, one needs to make sure that one doesn't get any integer
> overflow. Assume that the user knows that there is a single negative number
> among the 3 integers, so that using this negative number in the first addition
> will avoid an integer overflow. So the user can write:
> 
> int f (int a, int b, int c)
> {
>   if (b < 0)
>     return a + b + c;
>   else
>     return a + c + b;
> }
> 
> The generated code with -O3 has 6 instructions:
> 
>         leal    (%rdi,%rdx), %eax
>         addl    %esi, %edi
>         addl    %edx, %edi
>         addl    %esi, %eax
>         testl   %esi, %esi
>         cmovs   %edi, %eax

True.  The user could have written the following though:

int f (int a, int b, int c)
{
  return (unsigned)a + b + c;
}

or alternatively

int f (int a, int b, int c)
{ 
  return (long)a + b + c;
} 

both of which produce optimal code.

> In theory, the compiler could normally optimize to produce the same code as
> with the source that assumes -fwrapv (here, a + b + c and a + c + b are
> obviously equivalent on a typical processor), but in practice, this is often
> not the case as shown above.

Reply via email to