This is the mail archive of the gcc-bugs@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]

[Bug c/68971] -Woverflow false alarm in code unreachable after __builtin_mul_overflow


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.

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