https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108481
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> --- I'd say the implementation intends to warn about UB that still remains in the code after optimizations, if a result of some UB invoking operation isn't used (the DCE case) or as in this case the UB happens only when the result of the UB isn't used but it is used in some other path (sinking) etc., then the UB isn't really invoked in the generated code. While in -O0 code it will be encountered and so it is diagnosed.