3 ms·
I agree with you. I just wanted to list the one complaint I do have about C: missed optimization opportunities due to lax aliasing rules. Consider the followi
by haberman 16y ago
I agree with you. I just wanted to list the one complaint I do have about C: missed optimization opportunities due to lax aliasing rules.
Consider the following C translation unit:
void foo(const int *i);
void bar();
int baz() {
int i = 1;
foo(&i);
return i + 1;
}
int quux() {
int i;
foo(&i);
i = 1;
bar();
return i + 1;
}
You'd like to think that both baz() and quux() could compile the return statements to a constant "return 2." After all, foo() is taking a pointer to a CONST int. But alas, this is not the case, because foo() could cast away the const. So in truth, both functions are forced to reload the integer from the stack, add 1 to it, and then return that! You can't use any values you had loaded in registers (or in this case, you can't evaluate the expression at compile time).
My example is contrived, but you can easily construct examples that fit the same pattern and are real.
I've heard that Fortran still beats C in optimization in some cases; I would expect that the above is one major reason why. C99's "restrict" addresses some of the difference but cannot help you with the above.