logoalt Hacker News

mkarrmann • today at 3:57 PM • 1 reply • view on HN

The "special" thing of note is that GCC considers it acceptable to change the semantics of this program depending on the optimization level.


Replies

wat10000 • today at 4:28 PM

That depends entirely on how you define “the semantics.” Any optimizing compiler has to draw a line between what the program is meant to do and how the emitted code actually makes it happen. The proper location of that line is not obvious. Can it turn x++,x++ into x+=2? That’s normally fine, potentially disastrous if x is a memory mapped register on an external device.

C and C++ happen to have a particularly restrictive view on what “the semantics” of a program really are, such that there are a lot of things you can write in syntactically-legal C that officially erase the semantic meaning of the code. Within that definition of the program’s semantics, gcc’s optimizer is not supposed to change them, and any such change would be a bug. You’re taking a more expansive view of program semantics than the actual language specifies. If you do that, then any optimizing compiler is going to consider it to be acceptable to violate your special bespoke definition of them.

➕ show 2 replies