I would say that any code that breaks when optimization is turned on is probably already broken. Try compiling it with -Wall, or run over it with a good linter, and you will probably find warnings telling you what's wrong.
Actual compiler bugs (even in optimizing mode) where invalid code is generated are exceedingly rare. In my 25-year career of getting paid for writing software, I've seen two.
I think some of you have experienced things that seem supernatural.
Strange behavior of generated code, for example, that varies depending on small changes. For example, {a=b;c=d;} works but with {c=d;a=b;} the behavior is completely different.
These bugs are extremely difficult to catch and reproduce.
More than 30 years ago, I had such a thing and I ended up sending M$ a bug report via snail mail with a short, super simple code snippet that reproducibly produced very different, obviously incorrect machine code depending on the ordering of some instructions when using -Os. With the next MSC update we got some months later, the produced assembly was okay.
I can only say that in this case, I had several colleagues review the source code and nobody could find anything incorrect, and all considered the output of the -Os option as functionally different from the source code.
The issue was aggravated by the fact that the bug depended on some particular ordering of instructions, so some simple changes that had no effect on the functional outcome could make the incorrect code generation disappear.
So we had to "work around" by sticking with the order of instructions that did not produce incorrect assembly.
My personal guess is, such things might happen more frequently, but stay undiscovered because when the code gets changed during debugging, and the error disappears for some unexplainable reason and cannot be reproduced anymore, people will soon stop investigating and move on.
Especially if one, not considering the possibility of incorrectly generated machine code, does not carefully examine the generated machine code in deep detail to understand what is happening.