------- Comment #8 from jsm28 at gcc dot gnu dot org 2008-09-12 20:39 ------- I suggest an option such as -fdivide-checks, off by default. -std=c99 and other conformance options, for languages where INT_MIN % -1 is defined, would enable this option unless -fno-divide-checks is specified by the user. -fwrapv would enable this option unless -fno-divide-checks is specified by the user.
The option would cause checks to be inserted at gimplification time or earlier: A % B would evaluate A and B for their side effects, then check whether B is -1 and if so evaluate to 0 instead of carrying out the modulo operation. If flag_wrapv is set as well, similar checks would be applied to division to catch INT_MIN / -1. If a target macro is defined that says that the implementations of the relevant RTL insn patterns will generate the desired results (0 for modulo, INT_MIN for division) without trapping, then the option would have no effect. I don't know what processors this might apply to. libgcc functions for long long division and modulo need checking. I'd guess they can be arranged to get this right unconditionally rather than needing to call different functions in the two modes. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30484