Skip to content

Instantly share code, notes, and snippets.

@hzhou
Created March 7, 2017 18:06
Show Gist options
  • Save hzhou/118fd499bd1e0e503af26c17ac243a65 to your computer and use it in GitHub Desktop.
Save hzhou/118fd499bd1e0e503af26c17ac243a65 to your computer and use it in GitHub Desktop.
Compiler are not supposed to do dead-code ellimination

C has undefined behavior and this used to be a non-issue. For example, divide by zero, integer overflow, etc. which you are supposed to avoid by common sense. By common sense, we never intended to divide by zero or overflow an integer, so whether they are defined behavior or not, we program the same. In the cases that we do end up dividing by zero or overflowing integer, they are simply bugs in the program. Bugs are part of programmer's life, no big deal. Note here that even these behaviors are defined, they are still bugs or errors.

Now there is another class of undefined behavior called dead code ellimination. Certain code do not have effects running them, so logic applies that why not elliminate them?

The problem here is that dead code often do have an intention. For example, certain dead code is there as a simple test and intende to trigger crash when unexpected condition occurs. Certain dead code are there as a redudancy and intend to trigger error when parts of code become inconsistent. Yes, there are some of the code is simply due to our own inconsistency but they are part of our intention when we put them there anyway. If it is not our intention, we would elliminate that dead code on our own.

By elliminating the dead code, the compiler maybe able to prodeuce the code with the same behavior but faster performance, except, that behavior may not be our intended behavior. It is part of our intention for any conscious code, and if it is slow or produce error results, so be it, getting feedback is a (major) intention.

The bottom line, compilers are not supposed to second guess our intentions. When it is not clear, do not pretent to know better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment