IRBuilder

The IRBuilder Class is a helper providing an API for LLVM IR generation. Instances of the IRBuilder class keep track of the current place to insert instructions and has member functions to create new instructions.

In order to better understand LLVM’s instructions and their uses, we strongly encourage you to checkout LLVM’s Instruction Reference. For in-depth explanations of the GEP instruction, see The Often Misunderstood GEP Instruction.

Insert points:

// Return pointer to the current Basic Block (attribute of IRBuilderBase class).
BasicBlock *GetInsertBlock() const;

//Insert and return the specified instruction.
InstTy *Insert (InstTy *I, const Twine &Name="") const

// Set the current insert point to a previously-saved location.
void restoreIP (InsertPoint IP)

// Return the current insert point.
InsertPoint saveIP () const

// Specify that created instructions should be appended to the end of the specified block.
void SetInsertPoint (BasicBlock *TheBB)

Instruction creation member functions:

Create instructions:
// Create Instructions
AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize=nullptr, const Twine &Name="");

CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None, const Twine &Name = "", MDNode *FPMathTag = nullptr);

BranchInst *CreateBr(BasicBlock *Dest);

BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr);

LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name);

ReturnInst *CreateRet(Value *V);

ReturnInst *CreateRetVoid();

StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile=false);

CreateAlloca

AllocaInst *CreateAlloca(Type *Ty,
                        Value *ArraySize=nullptr,
                        const Twine &Name="");
Parameters:
  • Ty: This parameter specifies the type of the allocated memory.

  • ArraySize: This is an optional parameter that specifies the size of the array to be allocated. If you’re allocating memory for a single variable, you can pass nullptr or omit this parameter.

  • Name: This is an optional parameter that specifies a name for the alloca instruction. If you don’t provide a name, LLVM will automatically generate a name.

CreateCall

CallInst *CreateCall(FunctionCallee Callee,
                    ArrayRef<Value *> Args = None,
                    const Twine &Name = "",
                    MDNode *FPMathTag = nullptr);
Parameters:
  • Callee: This parameter specifies the function callee, which can be either a Function or a Value that represents a function pointer.

  • Args: This parameter specifies the arguments to be passed to the function call as an array of Value pointers.

  • Name: This is an optional parameter that specifies a name for the call instruction.

  • FPMathTag: This is an optional parameter that allows you to specify an optional metadata node for the call instruction, which can be used to attach additional information.

CreateBr

A basic block in LLVM IR is a sequence of instructions that are executed in a sequential order. A basic block ends with the first jump, whether conditional or not. The CreateBr function allows you to create a basic block jump instruction with the provided destination basic block.

BranchInst *CreateBr(BasicBlock *Dest);
Parameters:
  • Dest: This parameter specifies the destination basic block to which the unconditional branch should jump.

CreateCondBr:

BranchInst *CreateCondBr(Value *Cond,
                        BasicBlock *True,
                        BasicBlock *False,
                        MDNode *BranchWeights=nullptr,
                        MDNode *Unpredictable=nullptr);
Parameters:
  • Cond: This parameter specifies the condition value that determines whether the branch should take the “true” or “false” path. You can pass a pointer to a Value object, which represents a value in LLVM IR, as the condition for the branch instruction.

  • True: This parameter specifies the destination basic block to which the branch should jump if the condition is true.

  • False: This parameter specifies the destination basic block to which the branch should jump if the condition is false.

  • BranchWeights: This parameter is an optional metadata node that represents the branch weights for profile-guided optimization (PGO) purposes.

  • Unpredictable: This parameter is an optional metadata node that represents that the branch is considered unpredictable or hard to predict by the optimizer.

LoadInst

LoadInst *CreateLoad(Type *Ty,
                    Value *Ptr,
                    const char *Name);
Parameters:
  • Ty: This parameter specifies the type of the structure whose member’s address you want to calculate.

  • Ptr: This parameter specifies the memory location from which the value should be loaded.

  • Name: This parameter is an optional string that specifies the name of the resulting load instruction.

ReturnInst

ReturnInst *CreateRet(Value *V);
Parameters:
  • V: This parameter specifies the value that should be returned from the function

ReturnVoid

ReturnInst *CreateRetVoid();

StoreInst

StoreInst *CreateStore(Value *Val,
                       Value *Ptr,
                       bool isVolatile=false);
Parameters:
  • Val: This parameter specifies the value that should be stored to the memory location.

  • Ptr: This parameter specifies the memory location to which the value should be stored.

  • isVolatile: This parameter is an optional boolean flag that indicates whether the store operation should be marked as volatile

Create Value:
// Create Value.
Value *CreateBitCast(Value *V, Type *DestTy, const Twine &Name = "");

Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, const Twine &Name = "");
Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, const Twine &Name = "");

// Create an 'ptrtoint' instruction to preserve type safety when handling
// addresses as integer and vectors of adresses as vectors of integers.
Value *CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name = "");

Value *CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="");


Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", unsigned AddressSpace = 0, Module *M = nullptr);

CreateBitCast

Value *CreateBitCast(Value *V,
                    Type *DestTy,
                    const Twine &Name = "");
Parameters:
  • V: This parameter specifies the value to convert.

  • DestTy: This parameter specifies the target type of the conversion.

  • Name: This parameter is an optional string parameter that specifies the name of the created bitcast instruction.

CreateGEP

Value *CreateGEP(Type *Ty,
                Value *Ptr,
                ArrayRef<Value *> IdxList,
                const Twine &Name = "");
Parameters:
  • Ty: This parameter specifies the type of the structure whose member’s address you want to calculate.

  • Ptr: This parameter specifies the base pointer whose address you want to calculate.

  • IdxList: This parameter specifies the indices for the pointer arithmetic operation.

  • Name: This parameter is an optional string parameter that specifies the name of the created GEP instruction.

CreateStructGEP

Value *CreateStructGEP(Type *Ty,
                       Value *Ptr,
                       unsigned Idx,
                       const Twine &Name = "");
Parameters:
  • Ty: This parameter specifies the type of the structure whose member’s address you want to calculate.

  • Ptr: This parameter specifies the base pointer whose address you want to calculate.

  • Idx: This parameter specifies the index of the member whose address you want to calculate.

  • Name: This parameter is an optional string parameter that specifies the name of the created GEP instruction.

CreatePtrToInt

Create an ‘ptrtoint’ instruction to preserve type safety when handling addresses as integers and vectors of adresses as vectors of integers.

Value *CreatePtrToInt(Value *V,
                      Type *DestTy,
                      const Twine &Name = "");
Parameters:
  • V: This parameter specifies the value that you want to convert from a pointer to an integer.

  • DestTy: This parameter specifies the target type for the integer value that will be produced as the result of the conversion.

  • Name: This parameter is an optional string parameter that specifies the name.

CreateZExtOrTrunc

Value *CreateZExtOrTrunc(Value *V,
                         Type *DestTy,
                         const Twine &Name="");
Parameters:
  • V: This parameter specifies the value that you want to extend or truncate.

  • DestTy: This parameter specifies the target type for the result of the operation.

  • Name: This parameter is an optional string parameter that specifies the name.

CreateGlobalStringPtr

Constant *CreateGlobalStringPtr(StringRef Str,
                                const Twine &Name = "",
                                unsigned AddressSpace = 0,
                                Module *M = nullptr);
Parameters:
  • Str: This parameter specifies the string value to be created.

  • Name: This parameter is an optional string parameter that specifies the name.

  • AddressSpace: This parameter specifies the address space to be used for the global string.

  • M: This parameter is an optional pointer to a Module object that represents the LLVM module in which the global string should be created.

Create PHINode

A phi node is used to represent a value that depends on the incoming values from multiple predecessor basic blocks in a control flow graph (CFG). It is often used in the context of representing variables that have different values depending on which path of execution was taken in a program with branches or loops.

CreatePHI

PHINode *CreatePHI(Type *Ty,
                   unsigned NumReservedValues,
                   const Twine &Name="");
Parameters:
  • Ty: This parameter specifies the type of the value that the phi node will represent.

  • NumReservedValues: This parameter specifies the number of incoming values that the phi node will have.

  • Name: This parameter is an optional string parameter that specifies the name.

Comparation operators:
Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="");
Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="");
Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="");
Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name="");
Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="");
Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="");
Parameters:
  • LHS: A pointer to a Value object that represents the left-hand side of the comparison.

  • RHS: A pointer to a Value object that represents the right-hand side of the comparison.

  • Name: An optional Twine object that specifies the name.

Arithmetics operators
// Binary operations
Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false);
Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false);
Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool H This parameter is optional and defaults to falseasNUW=false, bool HasNSW=false);
Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false);
Parameters:
  • LHS: A pointer to a Value object that represents the left-hand side of the comparison.

  • RHS: A pointer to a Value object that represents the right-hand side of the comparison.

  • Name: An optional Twine object that specifies the name.

  • HasNUW: An optional boolean value that indicates whether the addition operation has no unsigned wrap-around.

  • HasNSW: An optional boolean value that indicates whether the addition operation has no signed wrap-around.

  • isExact: An optional boolean value that indicates whether the signed division operation should be performed with exact semantic