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 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(Value *Ptr, const char *Name); ReturnInst *CreateRet(Value *V); ReturnInst *CreateRetVoid(); StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile=false); // Create Value Value *CreateBitCast(Value *V, Type *DestTy, const Twine &Name = ""); Value *CreateGEP(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); // Create a PHI node PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name=""); // 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 HasNUW=false, bool HasNSW=false); Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false); 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="");