top | item 33772842

(no title)

asgeir | 3 years ago

The compiler may make optimization decisions based on the presence of UB so the code only needs to be present.

The compiler can't know that an arbitrary part of a function is unreachable if, for instance, that code path is controlled by a parameter or global state since that equates to solving the halting problem.

https://blog.llvm.org/2011/05/what-every-c-programmer-should...

discuss

order

haberman|3 years ago

I don't think this is correct. If UB is never invoked, the program is conforming and must be translated/executed correctly.

DR #109 specifically states that a compiler may not fail to translate a strictly conforming program just because some possible executions could trigger UB: https://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_109.html

Edit: downvoters, please explain your disagreement. I cited a primary document in support of my position.

asgeir|3 years ago

The undefined behavior can still affect code generation even though the program gets translated.

It's just that the generated code may not do exactly what you expect it to do because the presence of undefined behavior allowed the compiler to make assumptions which may surprise you.

edit: It seems that I am incorrect