Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to optimize a compiled binary?

This is more of a curiosity I suppose, but I was wondering whether it is possible to apply compiler optimizations post-compilation. Are most optimization techniques highly-dependent on the IR, or can assembly be translated back and forth fairly easily?

like image 618
Alex Reinking Avatar asked Mar 23 '14 19:03

Alex Reinking


Video Answer


3 Answers

This has been done, though I don't know of many standard tools that do it.

This paper describes an optimizer for Compaq Alpha processors that works after linking has already been done and some of the challenges they faced in writing it.

If you strain the definition a bit, you can use profile-guided optimization to instrument a binary and then rewrite it based on its observable behaviors with regards to cache misses, page faults, etc.

There's also been some work in dynamic translation, in which you run an existing binary in an interpreter and use standard dynamic compilation techniques to try to speed this up. Here's one paper that details this.

Hope this helps!

like image 107
templatetypedef Avatar answered Nov 15 '22 12:11

templatetypedef


There's been some recent research interest in this space. Alex Aiken's STOKE project is doing exactly this with some pretty impressive results. In one example, their optimizer found a function that is twice as fast as gcc -O3 for the Montgomery Multiplication step in OpenSSL's RSA library. It applies these optimizations to already-compiled ELF binaries.

Here is a link to the paper.

like image 38
Alex Reinking Avatar answered Nov 15 '22 11:11

Alex Reinking


Some compiler backends have a peephole optimizer which basically does just that, before it commits to the assembly that represents the IR, it has a little opportunity to optimize.

Basically you would want to do the same thing, from the binary, machine code to machine code. Not the same tool but the same kind of process, examine some size block of code and optimize.

Now the problem you will end up with though is for example you may have had some variables that were marked volatile in C so they are being very inefficiently used in the binary, the optimizer wont know the programmers desire there and could end up optimizing that out.

You could certainly take this back to IR and forward again, nothing to stop you from that.

like image 31
old_timer Avatar answered Nov 15 '22 12:11

old_timer