Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recompile a x86 code with LLVM to some faster one x86

Is it possible to run LLVM compiler with input of x86 32bit code? There is a huge algorithm which I have no source code and I want to make it run faster on the same hardware. Can I translate it from x86 back to x86 with optimizations.

This Code runs a long time, so I want to do static recompilation of it. Also, I can do a runtime profile of it and give to LLVM hints, which branches are more probable.

The original Code is written for x86 + x87, and uses no SSE/MMX/SSE2. After recompilation It has chances to use x86_64 and/or SSE3. Also, the code will be regenerated in more optimal way to hardware decoder.

Thanks.

like image 501
osgx Avatar asked Jan 08 '11 22:01

osgx


1 Answers

LLVM can't do this out of the box. You'd have to write an x86 binary to LLVM intermediate representation (IR) converter. That would be a very non-trivial task. If the x86 code was simple enough it might map pretty closely to IR, but some x86 instructions won't map directly, e.g. stack pointer manipulations.

Edit: You could also consider trying an approach similar to what QEMU does. QEMU translates the binaries on the fly, that it when I run PowerPC code, each basic block is translated into X86 code before it is executed. You could figure out how to break your object file into the basic blocks and generate LLVM IR for each block, discarding stuff (like parameter passing, etc.) and replacing that with straight LLVM IR.

Still a BIG job, though. Probably easier to rewrite the algorithm from scratch.

This exact process is described in "Dynamically Translating x86 to LLVM using QEMU"

like image 153
Richard Pennington Avatar answered Sep 25 '22 06:09

Richard Pennington