Move CallInst::CreateFree to IRBuilderBase

Similarly to D158861 I'm moving the `CreateFree` method from `CallInst` to `IRBuilderBase`.

Differential Revision: https://reviews.llvm.org/D159418
This commit is contained in:
Konrad Kleine 2023-08-29 11:16:42 +02:00
parent ad4a513027
commit 5bd1b93cb2
8 changed files with 31 additions and 78 deletions

View File

@ -125,8 +125,9 @@ void BrainF::header(LLVMContext& C) {
//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);

View File

@ -631,6 +631,9 @@ public:
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<OperandBundleDef> Bundles = std::nullopt);
CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
Value *Size, Align Alignment,

View File

@ -1603,16 +1603,6 @@ public:
static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> 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<OperandBundleDef> Bundles,
Instruction *InsertBefore);
static Instruction *CreateFree(Value *Source,
ArrayRef<OperandBundleDef> Bundles,
BasicBlock *InsertAtEnd);
// Note that 'musttail' implies 'tail'.
enum TailCallKind : unsigned {
TCK_None = 0,

View File

@ -3583,8 +3583,7 @@ LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
}
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,

View File

@ -349,6 +349,26 @@ CallInst *IRBuilderBase::CreateMalloc(Type *IntPtrTy, Type *AllocTy,
MallocF, Name);
}
/// CreateFree - Generate the IR for a call to the builtin free function.
CallInst *IRBuilderBase::CreateFree(Value *Source,
ArrayRef<OperandBundleDef> 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<Function>(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,

View File

@ -809,61 +809,6 @@ void CallInst::updateProfWeight(uint64_t S, uint64_t T) {
setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
}
static Instruction *createFree(Value *Source,
ArrayRef<OperandBundleDef> 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<Function>(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<OperandBundleDef> 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<OperandBundleDef> Bundles,
BasicBlock *InsertAtEnd) {
Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
assert(FreeCall && "CreateFree did not create a CallInst");
return FreeCall;
}
//===----------------------------------------------------------------------===//
// InvokeInst Implementation
//===----------------------------------------------------------------------===//

View File

@ -1401,14 +1401,9 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
if (auto *CB = dyn_cast<CallBase>(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<CallInst>(Free)) {
if (auto *BitCastI = dyn_cast<BitCastInst>(FreeCallI->getArgOperand(0)))
BitCastI->setDebugLoc(DL);
}
}
// Every call to saveSetjmp can change setjmpTable and setjmpTableSize

View File

@ -1302,8 +1302,8 @@ void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) {
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()