Re: Optimization of signed integer overflow since GCC 8

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 13/05/2024 14:42, Rademacher Lars via Gcc-help wrote:
> Dear all,
> 
> I am having the problem, that since GCC 8 in the following code the if-section is optimized away for any optimization levels above 0.
> But actually for a negative value of 'a', the value of 'ret' will overflow and thus 'ret' will be < 0, so the if-section cannot be optimized away.
> 
> #include <stdint.h>
> int32_t foo(int32_t a)
> {
>     int32_t ret = INT32_MAX - a;
> 
>     if (ret < 0) {
>         ret -= 50;
>     }
> 
>     return ret;
> }
> 
> I am guessing, this is due to the fact, that signed integer overflow is undefined behavior in C/C++ and thus the compiler assumes this is prevented. Can you confirm this?

Correct.

> Can you give a hint, if this is a correct assumption and why this was introduced?

> 
> Which part of GCC 8 release notes represents this optimization?

https://gcc.gnu.org/gcc-8/changes.html

Just look for 'signed integer overflow'.

R.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux