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 @@ -128,7 +128,9 @@ endbb = BasicBlock::Create(C, label, brainf_func); //call free(i8 *%arr) - CallInst::CreateFree(ptr_arr, endbb)->insertInto(endbb, endbb->end()); + builder->SetInsertPoint(endbb->getTerminator()); + 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 @@ -619,6 +619,10 @@ TBAATag, ScopeTag, NoAliasTag); } + /// 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, uint32_t ElementSize, 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 @@ -1635,9 +1635,6 @@ 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 { 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 @@ -3587,8 +3587,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 @@ -291,6 +291,26 @@ return CI; } +/// 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 @@ -946,61 +946,6 @@ ArraySize, OpB, MallocF, Name); } -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 @@ -1402,14 +1402,8 @@ 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); + 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()