Pure/const function not getting executed as the first operand to logical OR ( || ) (C++)

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

 



When compiling the following C++ snippet with -O0, I'm getting a Floating Point Exception on my Ubuntu 21.10, g++ 11.2.0, x86-64.

#include <iostream>

int func(int n, int r)
{
    return n % r;
}
int main()
{
    const int n = 15, r = 0;
    if (r == 0 || func(n, r))
        std::cout << "YES" << std::endl;
    else
        std::cout << "NO" << std::endl;
    if (func(n, r) || r == 0)
        std::cout << "YES" << std::endl;
    else
        std::cout << "NO" << std::endl;
    return 0;
}

With -O0, I'm getting a Floating Point error in the second if condition since it involves executing the "func" function where division by zero occurs. 

YES
Floating point exception (core dumped)

Now, with -O1 optimization, this is the output.

YES
YES

The "func" call has been optimized away. I checked the assembly code output with -S and verified the same. Now, I would like to know what was the optimization (the optimization option) that led to this happening. When I tried compiling the same code with -O0 but with an additional "-fipa-pure-const", I'm getting the same output as the O1 program with two YESes. But when I try compiling with both the -O1 and -fno-ipa-pure-const, I am not getting the Floating Point Exception. If -fipa-pure-const is responsible for the optimization that removes the function call, shouldn't the function call occur with -fno-ipa-pure-const. I have tried various combinations of options related to dead code elimination and branch probabilities, but can't figure out what's happening?

Thanks,
Vishal



[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