Wednesday, January 30, 2013

Writing fast, processor-independent code using C++ and LLVM bitcode

Using inline assembly along with C/C++ code has long been the best choice for developers writing optimized code. However the approach had its shortcomings:

- the need to write assembly for each architecture
- the use of inline assembly hinders certain compiler optimizations (such as register allocation)

But thanks to LLVM it is now possible to write processor-independent assembly functions using its bitcode, and use its link-time optimizer to get the function expanded in-line into C/C++ code.

I have created an example that shows how to implement fast integer arithmetics (with overflow detection) in C++, which is available at

It uses a bitcode-level intrinsic called "llvm.sadd.with.overflow.i32" (that gets inlined) to implement integer addition with overflow check.

With the example, the source code
if (! add_with_overflow(&ret, x, y))
gets compiled into
addl    8(%rsp), %esi
jno     LBB1_4

As can be seen, the generated code is highly optimized. Not only does it use the JNO instruction, the source operand of ADDL is placed on stack (which would be faster than on register since the value is never again being referred to). Such kind of an optimization has been impossible with inline assembly of GCC (that requires the arguments to be loaded on registers).

Since the output (after inline expansion) is a .s file (processor dependent assembly), it is possible to link the optimized code using other linkers as well.

Note: the work is based on Fast integer overflow detection - Xi Wang, and I would like to thank the author for his excellent work.

Note 2: Since the bitcode instructions might change in the future, it might be a good idea to limit the length of the functions written in bitcode as short as possible.


  1. This is great do you have a catologue if so I would love one to share with friends and family.
    ICC T20 World Cup 2016 Venues & Starting


    Australia vs India Prediction

  2. This is the time doing a reversal and forward between banks while you apply, and could end up being very expensive by the day's end. On the off chance that you need to minimize time seeking and costs, then the most ideal approach to scan for this sort of advance is to look through the Internet. Payday Loans


  3. tgjobsnews is the best job website for govt jobsBank Jobs

  4. is a protection software solution that communicates with the cloud avoiding the hassle to manage the signature updates to deploy. for office setup visit

  5. Sample Assignment is a renowned for assignment help in Australia and has assisted thousands of international students with their academics. Our dedicated team of experts has been providing full-fledged assignments to students pursuing their courses at various colleges and universities, and found to be avidly googling "assignment help Sydney" across the continent. While Australia is among the most preferred destinations for individuals from around the world, Assignment Experts has also won the trust of a vast pool of students here. The Australian Assignment help, such as Sample Assignment, can be easily contacted via WhatsApp and Messenger too. With our 24-hour online academic assistance, any student can reach out to us whenever he or she is in the need of help of a subject expert.