This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/68971] -Woverflow false alarm in code unreachable after __builtin_mul_overflow
- From: "msebor at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 18 Dec 2015 03:56:48 +0000
- Subject: [Bug c/68971] -Woverflow false alarm in code unreachable after __builtin_mul_overflow
- Auto-submitted: auto-generated
- References: <bug-68971-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68971
--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
This is a valid constant expression because 1 is and GCC doesn't warn on the
division by zero:
1 ? 0 : 1 / 0
This is not a constant expression because rand() isn't, and so GCC warns about
the division by zero:
rand() ? 0 : 1 / 0
And likewise, this is not a constant expression because
__builtin_mul_overflow(x, y, z) isn't, and so GCC warns on the multiplication
that overflows:
__builtin_mul_overflow (x, y, z) ? 0 : 0x7fffffff * 0x7fffffff
GCC might manage to compute the __builtin_mul_overflow result during
optimization but that doesn't make it a constant expression. (One can test
whether an expression is a constant expression using __builtin_constant_p.)
The warning is working as designed.
That aside, since __builtin_mul_overflow has already computed the product it
seems that the result should be used rather than computed again using plain
multiplication. I don't quite see why it's doing it twice. That doesn't seem
like the intended use of the builtin.