Re:Defining the Undefinedness of C (2015) [pdf]
by bbatha on Wednesday July 26, 2017 @12:37PM
(#14857751)
> Division by zero is UB. This lets the compiler assume it does not happen and thus it won't have to emit additional instructions to check for it.
Right, but it doesn't then follow that it can then "optimize" out the rest of your program. It could allow it to compile to what's expected and rely on the platform to handle corrupt program state. Which is what happens if you trigger run time UB anyway. It can also be implementation defined, because virtually all modern platforms trap on division by zero which would lead to more predictable optimizer behavior.
To be eliminate the check at compile to it would have to be able to infer the range of the values of the divisor.
> and their compilers don't optimize as aggressively as GCC or Clang.
Some languages use GCC or LLVM as optimizing backends. So they can suffer from just the same UB if the language itself does not prevent it.
https://github.com/rust-lang/rust/issues/28728