Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JIT optimizer for C/C++

I was reading about the advantages of JIT over precompiled and one of those mentioned was that a JIT could adjust branch predictions based on actual runtime data. Now it's been a long time since I wrote a compiler in college, but it seems to me that something similar can be achieved for precompiled code also in most cases (where there are no explicit gotos).

Consider the following code:

   test x
   jne L2:
L1: ...
   jmp L3:
L2: ...
L3:

If we have some runtime instrumentation that sees how many times the 'jne L2' is true, it could physically swap all the instructions in the L1: block and the L2: block. Of course, it would have to know that no thread is within either block during the swap, but those are details...

   test x
   jeq L1:
L2: ...
   jmp L3:
L1: ...
L3:

I understand there are also issues when the program code is loaded in readonly memory, etc. but it's an idea.

So my question is, is such a JIT optimization feasible for C/C++ or am I missing some fundamental reason why this cannot be done? Are there any JIT optimizers for C/C++ out there?

like image 836
Jay Avatar asked Sep 11 '11 06:09

Jay


1 Answers

There is no JIT compiler for C++ that I am aware of; however, GCC does support feedback directed optimization (FDO), which can use runtime profiling to optimize branch prediction and the like.

See the GCC options starting with "-fprofile" (HINT: "-fprofile-use" uses the generated runtime profile to perform the optimization, while "-fprofile-generate" is used to generate the runtime profile).

like image 121
Michael Aaron Safyan Avatar answered Sep 28 '22 05:09

Michael Aaron Safyan