5 ms·
Turn on the "standard" optimization -Os and we get the following, much more reasonable result That's still 250% more instructions than the necessary "xor eax,
by userbinator 12y ago
Turn on the "standard" optimization -Os and we get the following, much more reasonable result
That's still 250% more instructions than the necessary "xor eax, eax; ret". I find it a bit disappointing that the compiler would miss this trivial optimisation opportunity, while at the same time doing subtle and often unwanted things with undefined behaviour. C was conceived as a "portable assembly language", and as much as the language lawyers, theoreticists, and compiler writers love to exercise their "undefined behaviour" rights and try to dissuade others from thinking of it that way, that's what people use it for and that's what they'll expect.
Rather, undefined behaviour should be interpreted as "do the obvious thing, the results may differ on different platforms, and turn off all optimisations exploiting it because it's either intentional or the programmer has made a mistake so the compiler should also generate obviously wrong code." A warning would be nice too. Instead of blind adherence to the standard, consider what behaviour programmers actually expect, and write compilers accordingly. One example of this is casting integers to pointers of various types, and using them to access hardware. Device drivers would be impossible to write if the compiler decided that such undefined behaviour meant it could remove the accesses completely (which is completely legal according to the standard.)
More examples: dereferencing a null pointer should generate code that accesses address 0. Signed integers should wrap around on overflow. Shifts should do whatever the shift instruction of the architecture does. Trying to access beyond the end of the array should try to access memory there. Division by zero will do what the machine instruction would do. Etc.
- foodevl 12y agoThe 250% more instructions are the frame pointer, which is how your debugger figures out the stack trace when you happen to break execution at the "xor eax, eax". Use -fomit-frame-pointer to get rid of that. That has nothing to do with optimization.
- userbinator 12y agoThose extra instructions shouldn't be necessary even when debugging, since that's what the -g option is for; to generate information the debugger can use: http://yosefk.com/blog/getting-the-call-stack-without-a-frame-pointer.html http://yosefk.com/blog/getting-the-call-stack-without-a-fram... When optimisation is enabled, all extraneous instructions should disappear.
- to3m 12y agoThe C standard even describes the probable result of undefined behaviour as ranging from "ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment". But for some reason nobody seems to pay any attention to the second possibility. I don't know why people like the current situation so much. Strikes me it's bad for everybody. If you know how the underlying system works, you'll be confused by this second pile of strange rules that you now have to keep on top of; if you don't know the underlying system works, you're no better served by these new rules than you would have been by simply finding out. And it's not even like most systems these days vary all that much! - assuming you can even find something that isn't ARM/x86/x64 in the first place <-- this statement may be mildly tongue in cheek. Very strange. My favourite link on the subject is http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasing.html http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasin.... "The C compiler writers have no idea who their users are" seems plausible :)