News for hackers. Stuff that matters.
Re:An Intro to Compilers by userbinator on Sunday August 13, 2017 @09:26PM (#15006090)
I know it might be a contrived case, but it's rather disappointing to see that even with the -O2 optimisation level, the final generated code contains 43% (3/7) useless instructions to mess around with RBP. I mean, it should be bleeding obvious to the compiler that this function doesn't require any stack allocation, much less an RBP-based one (the tradeoff between using RBP/RSP-based allocation is far more subtle, and I can forgive a compiler for choosing nonoptimally in that case), so why did it emit those useless instructions?

There's plenty of articles about how intelligent compiler optimisers can be, and I don't doubt that they can do some pretty advanced transformations, having read a lot of compiler output; and then, I see things like this, which just make you go why!?!? It's puzzling how compilers behave, applying very sophisticated optimisation techniques but then missing the easy cases. The effect might not be as great as more advanced optimisations, but so is the effort required to do this type of optimisation. This is not even low-hanging fruit, it's sitting on the ground.

However, the compiler will use as few registers as possible.

Compilers usually try to use all the registers they can (although sometimes they do exhibit odd non-optimal behaviour, as described above); eschewing register use and using memory instead will increase code size and time.

Re:An Intro to Compilers by brigade on Sunday August 13, 2017 @10:06PM (#15006204)
The "useless" instructions aren't for potential stack allocation, they're there so backtraces work. If you don't care about that then you can add -fomit-frame-pointer and they disappear.

Compilers don't default to this because having backtraces on crashes and profiling are more useful than the like 1% speed gain from another available register on x86-64.

Re:An Intro to Compilers by userbinator on Sunday August 13, 2017 @11:43PM (#15006465)
Re:An Intro to Compilers by brigade on Monday August 14, 2017 @02:29AM (#15006911)
Are you arguing that they do work on ELF+DWARF with sufficient extra debug info, when running under a debugger, despite documentation that explicitly says it doesn't work? Because I certainly don't expect my users to have a debugger installed (let alone regularly run programs under one) but I'd still like to get useful crashlogs automatically.

Or are you saying that you could potentially reconstruct a backtrace, if someone wrote code to do so that is smart enough to locate and account for all the stack adjustments in reconstructing where each function stored its return address, including runtime alloca() / VLA adjustments and multiple adjustments in the same function (potentially needing to reconstruct the code flow), just from the disassembly? Because the linked MIPS code is nowhere near that smart, and to my and that author's knowledge, such code has not been written for x86.

Either way, nice in theory but not good enough in practice.

Re:An Intro to Compilers by userbinator on Monday August 14, 2017 @03:38AM (#15007133)
The debugging info is enough to find, for each code address, the offset to the return address on the stack, from which you can find that offset, etc. You don't need a debugger but saving the stack is enough to get a backtrace for a crashed process.
Re:An Intro to Compilers by lultimouomo on Monday August 14, 2017 @07:40AM (#15007959)
You can have a minified memory dump sent automatically, then symbolicate it on your servers using the stripped debugging symbols. That's what Breakpad does. I'm not sure how well it deals with alloca(), but I've used in on several shipped pieces of C++ software and it worked mostly fine.
Re:An Intro to Compilers by DSrcl on Sunday August 13, 2017 @10:12PM (#15006228)
Compile with -fno-frame-pointer and you won't see those code. It's for debugger.