[LLVMdev] New LLVMBuilder api

Chris Lattner sabre at nondot.org
Sun May 27 11:01:28 PDT 2007

I just checked in a new LLVMBuilder class into llvm/Support/LLVMBuilder.h, 
and switched llvm-gcc over to use it. This class is based on feedback Tom 
Tromey gave on LLVM way back here: 

Basically, when creating a frontend, you end up creating a lot of 
instructions.  This has three suboptimal aspects to it:

1. The constructors for the instructions all must be passed a place to
    insert the instructions into.  Because the instructions often take a
    long list of arguments, this is nontrivial can can cause errors (Tom
    hit a problem with Alloca, for example).

2. The instruction ctors all default construct an std::string.  In
    practice, this is a very modest overhead, but if it isn't needed, it
    shouldn't happen.

3. Some of the construction APIs are very verbose, my "favorite" being
    BinaryOperator::createAdd and friends.

The new API separates the idea of what to construct from where to insert 
it.  In particular, when you create an LLVMBuilder object, you can tell it 
where to insert all subsequently created instructions.  This means you end 
up writing code like this:

LLVMBuilder B;

Value *V1 = B.CreateAdd(a, b, "tmp");
Value *V2 = B.CreateMul(V1, c, "whatever");
Value *V3 = B.CreateCall(FnPtr, V1, V2);

The name strings are c strings, are always at the end of the argument 
list, and are always optional.

This API is simple and consistent, which makes it easy to understand.  It 
would also be really easy to build a C api for it, if someone is 
interested (hint hint :),



More information about the llvm-dev mailing list