diff --git a/llvm/examples/BrainF/BrainF.cpp b/llvm/examples/BrainF/BrainF.cpp --- a/llvm/examples/BrainF/BrainF.cpp +++ b/llvm/examples/BrainF/BrainF.cpp @@ -125,8 +125,9 @@ //brainf.end: endbb = BasicBlock::Create(C, label, brainf_func); - //call free(i8 *%arr) - CallInst::CreateFree(ptr_arr, endbb)->insertInto(endbb, endbb->end()); + // call free(i8 *%arr) + builder->SetInsertPoint(endbb); + builder->CreateFree(ptr_arr); //ret void ReturnInst::Create(C, endbb); diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -631,6 +631,9 @@ CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize, Value *ArraySize, Function *MallocF = nullptr, const Twine &Name = ""); + /// Generate the IR for a call to the builtin free function. + CallInst *CreateFree(Value *Source, + ArrayRef Bundles = std::nullopt); CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, Value *Size, Align Alignment, diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -1603,16 +1603,6 @@ static CallInst *Create(CallInst *CI, ArrayRef Bundles, Instruction *InsertPt = nullptr); - /// Generate the IR for a call to the builtin free function. - static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); - static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); - static Instruction *CreateFree(Value *Source, - ArrayRef Bundles, - Instruction *InsertBefore); - static Instruction *CreateFree(Value *Source, - ArrayRef Bundles, - BasicBlock *InsertAtEnd); - // Note that 'musttail' implies 'tail'. enum TailCallKind : unsigned { TCK_None = 0, diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3583,8 +3583,7 @@ } LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { - return wrap(unwrap(B)->Insert( - CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); + return wrap(unwrap(B)->CreateFree(unwrap(PointerVal))); } LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty, diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp --- a/llvm/lib/IR/IRBuilder.cpp +++ b/llvm/lib/IR/IRBuilder.cpp @@ -349,6 +349,26 @@ MallocF, Name); } +/// CreateFree - Generate the IR for a call to the builtin free function. +CallInst *IRBuilderBase::CreateFree(Value *Source, + ArrayRef Bundles) { + assert(Source->getType()->isPointerTy() && + "Can not free something of nonpointer type!"); + + Module *M = BB->getParent()->getParent(); + + Type *VoidTy = Type::getVoidTy(M->getContext()); + Type *VoidPtrTy = PointerType::getUnqual(M->getContext()); + // prototype free as "void free(void*)" + FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, VoidPtrTy); + CallInst *Result = CreateCall(FreeFunc, Source, Bundles, ""); + Result->setTailCall(); + if (Function *F = dyn_cast(FreeFunc.getCallee())) + Result->setCallingConv(F->getCallingConv()); + + return Result; +} + CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove( Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag, diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -809,61 +809,6 @@ setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); } -static Instruction *createFree(Value *Source, - ArrayRef Bundles, - Instruction *InsertBefore, - BasicBlock *InsertAtEnd) { - assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && - "createFree needs either InsertBefore or InsertAtEnd"); - assert(Source->getType()->isPointerTy() && - "Can not free something of nonpointer type!"); - - BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; - Module *M = BB->getParent()->getParent(); - - Type *VoidTy = Type::getVoidTy(M->getContext()); - Type *VoidPtrTy = PointerType::getUnqual(M->getContext()); - // prototype free as "void free(void*)" - FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, VoidPtrTy); - CallInst *Result = nullptr; - if (InsertBefore) - Result = CallInst::Create(FreeFunc, Source, Bundles, "", InsertBefore); - else - Result = CallInst::Create(FreeFunc, Source, Bundles, ""); - Result->setTailCall(); - if (Function *F = dyn_cast(FreeFunc.getCallee())) - Result->setCallingConv(F->getCallingConv()); - - return Result; -} - -/// CreateFree - Generate the IR for a call to the builtin free function. -Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { - return createFree(Source, std::nullopt, InsertBefore, nullptr); -} -Instruction *CallInst::CreateFree(Value *Source, - ArrayRef Bundles, - Instruction *InsertBefore) { - return createFree(Source, Bundles, InsertBefore, nullptr); -} - -/// CreateFree - Generate the IR for a call to the builtin free function. -/// Note: This function does not add the call to the basic block, that is the -/// responsibility of the caller. -Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { - Instruction *FreeCall = - createFree(Source, std::nullopt, nullptr, InsertAtEnd); - assert(FreeCall && "CreateFree did not create a CallInst"); - return FreeCall; -} -Instruction *CallInst::CreateFree(Value *Source, - ArrayRef Bundles, - BasicBlock *InsertAtEnd) { - Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); - assert(FreeCall && "CreateFree did not create a CallInst"); - return FreeCall; -} - //===----------------------------------------------------------------------===// // InvokeInst Implementation //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp @@ -1401,14 +1401,9 @@ if (auto *CB = dyn_cast(I)) if (auto Bundle = CB->getOperandBundle(LLVMContext::OB_funclet)) Bundles.push_back(OperandBundleDef(*Bundle)); - auto *Free = CallInst::CreateFree(SetjmpTable, Bundles, I); + IRB.SetInsertPoint(I); + auto *Free = IRB.CreateFree(SetjmpTable, Bundles); Free->setDebugLoc(DL); - // CallInst::CreateFree may create a bitcast instruction if its argument - // types mismatch. We need to set the debug loc for the bitcast too. - if (auto *FreeCallI = dyn_cast(Free)) { - if (auto *BitCastI = dyn_cast(FreeCallI->getArgOperand(0))) - BitCastI->setDebugLoc(DL); - } } // Every call to saveSetjmp can change setjmpTable and setjmpTableSize diff --git a/polly/lib/CodeGen/IslNodeBuilder.cpp b/polly/lib/CodeGen/IslNodeBuilder.cpp --- a/polly/lib/CodeGen/IslNodeBuilder.cpp +++ b/polly/lib/CodeGen/IslNodeBuilder.cpp @@ -1302,8 +1302,8 @@ SAI->setBasePtr(CreatedArray); // Insert the free call at polly.exiting - CallInst::CreateFree(CreatedArray, - std::get<1>(StartExitBlocks)->getTerminator()); + Builder.SetInsertPoint(std::get<1>(StartExitBlocks)->getTerminator()); + Builder.CreateFree(CreatedArray); } else { auto InstIt = Builder.GetInsertBlock() ->getParent()