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. flipkart emi,
    flipkart debit card emi,
    flipkart emi for debit card,
    flipkart emi on debit card,
    flipkart emi with debit card,
    flipkart emi sbi debit card,
    flipkart no emi cost,
    flipkart emi,
    flipkart with emi,
    flipkart email id,
    flipkart emi mobile,
    flipkart emi without credit card,
    flipkart mobile emi on debit card,
    flipkart emi options,
    flipkart emi card,
    flipkart emi using debit card,
    flipkart emi bajaj finserv
    zestmoney flipkart emi,
    flipkart emi laptop,
    flipkart emi process,
    flipkart emi phone,
    flipkart no cost emi laptops,
    flipkart emi in debit card,
    flipkart emi debit card icici,
    flipkart emi on credit card,
    flipkart emi credit card,
    flipkart emi policy,
    flipkart emi rules,
    flipkart emi cancellation,
    flipkart 0 emi,
    flipkart emi eligibility check,
    flipkart easy emi on debit card,
    flipkart zero emi,
    flipkart emi procedure,
    flipkart emi pay,
    flipkart emi eligibility check sms,
    flipkart emi calculator,
    flipkart emi customer care number,
    flipkart emi process debit card,,
    flipkart give emi on debit card,
    flipkart emi sms,
    flipkart emi icici debit card,,
    flipkart no emi cost ,
    flipkart emi app,
    flipkart emi shopping,
    flipkart no cost emi led tv,
    flipkart easy emi option,
    flipkart emi loan,
    flipkart emi for mobile,
    flipkart emi service,
    flipkart emi with credit card,
    flipkart in emi,
    flipkart debit card emi laptop,
    flipkart cardless emi link,
    flipkart free emi,
    flipkart emi hdfc,
    flipkart emi credit,,
    flipkart emi hdfc debit card,
    flipkart emi message,
    flipkart emi information,
    flipkart emi contact number,
    flipkart emi msg,
    flipkart new emi scheme,
    flipkart emi on debit card hdfc,
    flipkart emi number,
    flipkart debit card emi kaise kare,
    flipkart emi on debit card axis bank,
    flipkart emi registration,
    flipkart emi on icici debit card,
    flipkart emi on debit card quora,
    flipkart emi on axis debit card,
    flipkart debit card emi eligibility icici,
    flipkart debit card emi yes bank,
    flipkart emi on debit card sms,
    flipkart emi bank list,
    flipkart emi process in hindi,
    flipkart emi return policy
    flipkart debit card emi youtube,
    flipkart pay later emi,
    flipkart hdfc emi offer,
    flipkart emi how to pay,
    flipkart hdfc debit card emi eligibility,
    flipkart debit card emi eligibility sbi,
    flipkart debit card emi 2018,
    flipkart emi sbi,
    flipkart emi conditions,
    flipkart emi review,
    flipkart emi payment date,
    flipkart emi helpline number,
    flipkart emi how it works,
    flipkart se emi kaise kare,
    flipkart emi quora,
    flipkart debit card emi quora,
    flipkart emi faq,
    flipkart emi interest rate,
    flipkart emi 60000,,
    flipkart emi without interest,
    flipkart no cost emi quora


  4. كل هذه التعليمات لو طبقت بحرص ستفى بالغرض ولكن فيما يتصل تعدد قطعه الاثاث و مبالغة مقدار الأثاث لا مفر من الاستعانة بمؤسسة نقل اثاث بالعاصمة السعودية محترفة للفيام بهذه المهمه بيسر و يسر ودون تعرض الاثاث المخصص بك الى اى خدوش او كسور نتيجة لقلة الخبره فى اعمال نقل وتحزين الاثاث فيما يتصل الاستهامة بمؤسسة رابط لنقل وتخزين الاثاث
    شركة نقل عفش
    شركة نقل عفش من الرياض الى الامارات

    شركة نقل اثاث من الرياض الى الامارات
    شركة نقل عفش بجازان


Note: Only a member of this blog may post a comment.