https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64511
--- Comment #6 from rguenther at suse dot de <rguenther at suse dot de> --- On Tue, 13 Jan 2015, jakub at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64511 > > Jakub Jelinek <jakub at gcc dot gnu.org> changed: > > What |Removed |Added > ---------------------------------------------------------------------------- > CC| |jakub at gcc dot gnu.org > > --- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> --- > x % x for x == 0 is undefined behavior, so perhaps with the exception of > sanitization we can just assume it is not 0 and thus fold x % x to 0. > Or does e.g. Ada/Java require something different? See the inconsistent handing of % vs. / in fold (we fold 0 % x to 0 but preserve literal 0 % 0 - we don't fold 0 / x to 0). In my view we should just go ahead and fold, though with -fnon-call-exceptions one may do try { x % x abort (); } catch (...) { } and throw from inside a trap handler. Which means that one can catch undefined behavior. IMHO -fnon-call-exceptions and sub-flags like -ftrapv tell GCC to treat the specific undefined behavior as defined - namely trapping. We don't have a special flag for divide-by-zero-traps though (it's not exactly "overflow"). So maybe with making the folding more consistent we should simply add such flag, -fdivide-by-zero-traps which also makes modulo-by-zero trap?