Basically, yes. But, if you do find a particularly ridiculous example of a missed optimization opportunity, then you should report it to the developers!
Braindead source code will always produce braindead machine code though: to a certain extent the compiler still has to do what you say, rather than what you meant, although many common idioms are recognised and "fixed" (the rule is that it has got to be impossible to tell that it's been altered without using a debugger).
And then there are still tricks, new and old, that are useful, at least on some architectures.
For example, if you have a loop that counts from 0 to 100 and does something to an array, some compilers might reverse the counter and make it go from 100 down to zero (because comparing against zero is cheaper than against another constant), but they can't do that if you loop has a side effect. If you don't care that the side-effect happens in reverse order then you can get better code if you reverse the counter yourself.
Another useful trick that GCC has is __builtin_expect(expr, bool)
, with which you can tell the compiler that expr
is likely to be true
or false
, so it can optimize branches accordingly. Similarly, __builtin_unreachable()
can tell GCC that something can't happen, so it doesn't have to allow for the case where it does.
In general though, the compiler is good enough that you really don't need to care unless your program spends 90% of its runtime in that one tiny function. (For example, memcpy
is still typically written in assembler).