Skip to content

Commit 5bd1b93

Browse files
committed
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
1 parent ad4a513 commit 5bd1b93

File tree

8 files changed

+31
-78
lines changed

8 files changed

+31
-78
lines changed

llvm/examples/BrainF/BrainF.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ void BrainF::header(LLVMContext& C) {
125125
//brainf.end:
126126
endbb = BasicBlock::Create(C, label, brainf_func);
127127

128-
//call free(i8 *%arr)
129-
CallInst::CreateFree(ptr_arr, endbb)->insertInto(endbb, endbb->end());
128+
// call free(i8 *%arr)
129+
builder->SetInsertPoint(endbb);
130+
builder->CreateFree(ptr_arr);
130131

131132
//ret void
132133
ReturnInst::Create(C, endbb);

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ class IRBuilderBase {
631631
CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
632632
Value *ArraySize, Function *MallocF = nullptr,
633633
const Twine &Name = "");
634+
/// Generate the IR for a call to the builtin free function.
635+
CallInst *CreateFree(Value *Source,
636+
ArrayRef<OperandBundleDef> Bundles = std::nullopt);
634637

635638
CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
636639
Value *Size, Align Alignment,

llvm/include/llvm/IR/Instructions.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,16 +1603,6 @@ class CallInst : public CallBase {
16031603
static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
16041604
Instruction *InsertPt = nullptr);
16051605

1606-
/// Generate the IR for a call to the builtin free function.
1607-
static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
1608-
static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
1609-
static Instruction *CreateFree(Value *Source,
1610-
ArrayRef<OperandBundleDef> Bundles,
1611-
Instruction *InsertBefore);
1612-
static Instruction *CreateFree(Value *Source,
1613-
ArrayRef<OperandBundleDef> Bundles,
1614-
BasicBlock *InsertAtEnd);
1615-
16161606
// Note that 'musttail' implies 'tail'.
16171607
enum TailCallKind : unsigned {
16181608
TCK_None = 0,

llvm/lib/IR/Core.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,8 +3583,7 @@ LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
35833583
}
35843584

35853585
LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3586-
return wrap(unwrap(B)->Insert(
3587-
CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3586+
return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
35883587
}
35893588

35903589
LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,

llvm/lib/IR/IRBuilder.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ CallInst *IRBuilderBase::CreateMalloc(Type *IntPtrTy, Type *AllocTy,
349349
MallocF, Name);
350350
}
351351

352+
/// CreateFree - Generate the IR for a call to the builtin free function.
353+
CallInst *IRBuilderBase::CreateFree(Value *Source,
354+
ArrayRef<OperandBundleDef> Bundles) {
355+
assert(Source->getType()->isPointerTy() &&
356+
"Can not free something of nonpointer type!");
357+
358+
Module *M = BB->getParent()->getParent();
359+
360+
Type *VoidTy = Type::getVoidTy(M->getContext());
361+
Type *VoidPtrTy = PointerType::getUnqual(M->getContext());
362+
// prototype free as "void free(void*)"
363+
FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, VoidPtrTy);
364+
CallInst *Result = CreateCall(FreeFunc, Source, Bundles, "");
365+
Result->setTailCall();
366+
if (Function *F = dyn_cast<Function>(FreeFunc.getCallee()))
367+
Result->setCallingConv(F->getCallingConv());
368+
369+
return Result;
370+
}
371+
352372
CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove(
353373
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
354374
uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,

llvm/lib/IR/Instructions.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -809,61 +809,6 @@ void CallInst::updateProfWeight(uint64_t S, uint64_t T) {
809809
setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
810810
}
811811

812-
static Instruction *createFree(Value *Source,
813-
ArrayRef<OperandBundleDef> Bundles,
814-
Instruction *InsertBefore,
815-
BasicBlock *InsertAtEnd) {
816-
assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
817-
"createFree needs either InsertBefore or InsertAtEnd");
818-
assert(Source->getType()->isPointerTy() &&
819-
"Can not free something of nonpointer type!");
820-
821-
BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
822-
Module *M = BB->getParent()->getParent();
823-
824-
Type *VoidTy = Type::getVoidTy(M->getContext());
825-
Type *VoidPtrTy = PointerType::getUnqual(M->getContext());
826-
// prototype free as "void free(void*)"
827-
FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, VoidPtrTy);
828-
CallInst *Result = nullptr;
829-
if (InsertBefore)
830-
Result = CallInst::Create(FreeFunc, Source, Bundles, "", InsertBefore);
831-
else
832-
Result = CallInst::Create(FreeFunc, Source, Bundles, "");
833-
Result->setTailCall();
834-
if (Function *F = dyn_cast<Function>(FreeFunc.getCallee()))
835-
Result->setCallingConv(F->getCallingConv());
836-
837-
return Result;
838-
}
839-
840-
/// CreateFree - Generate the IR for a call to the builtin free function.
841-
Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
842-
return createFree(Source, std::nullopt, InsertBefore, nullptr);
843-
}
844-
Instruction *CallInst::CreateFree(Value *Source,
845-
ArrayRef<OperandBundleDef> Bundles,
846-
Instruction *InsertBefore) {
847-
return createFree(Source, Bundles, InsertBefore, nullptr);
848-
}
849-
850-
/// CreateFree - Generate the IR for a call to the builtin free function.
851-
/// Note: This function does not add the call to the basic block, that is the
852-
/// responsibility of the caller.
853-
Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
854-
Instruction *FreeCall =
855-
createFree(Source, std::nullopt, nullptr, InsertAtEnd);
856-
assert(FreeCall && "CreateFree did not create a CallInst");
857-
return FreeCall;
858-
}
859-
Instruction *CallInst::CreateFree(Value *Source,
860-
ArrayRef<OperandBundleDef> Bundles,
861-
BasicBlock *InsertAtEnd) {
862-
Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
863-
assert(FreeCall && "CreateFree did not create a CallInst");
864-
return FreeCall;
865-
}
866-
867812
//===----------------------------------------------------------------------===//
868813
// InvokeInst Implementation
869814
//===----------------------------------------------------------------------===//

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,14 +1401,9 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
14011401
if (auto *CB = dyn_cast<CallBase>(I))
14021402
if (auto Bundle = CB->getOperandBundle(LLVMContext::OB_funclet))
14031403
Bundles.push_back(OperandBundleDef(*Bundle));
1404-
auto *Free = CallInst::CreateFree(SetjmpTable, Bundles, I);
1404+
IRB.SetInsertPoint(I);
1405+
auto *Free = IRB.CreateFree(SetjmpTable, Bundles);
14051406
Free->setDebugLoc(DL);
1406-
// CallInst::CreateFree may create a bitcast instruction if its argument
1407-
// types mismatch. We need to set the debug loc for the bitcast too.
1408-
if (auto *FreeCallI = dyn_cast<CallInst>(Free)) {
1409-
if (auto *BitCastI = dyn_cast<BitCastInst>(FreeCallI->getArgOperand(0)))
1410-
BitCastI->setDebugLoc(DL);
1411-
}
14121407
}
14131408

14141409
// Every call to saveSetjmp can change setjmpTable and setjmpTableSize

polly/lib/CodeGen/IslNodeBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,8 @@ void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) {
13021302
SAI->setBasePtr(CreatedArray);
13031303

13041304
// Insert the free call at polly.exiting
1305-
CallInst::CreateFree(CreatedArray,
1306-
std::get<1>(StartExitBlocks)->getTerminator());
1305+
Builder.SetInsertPoint(std::get<1>(StartExitBlocks)->getTerminator());
1306+
Builder.CreateFree(CreatedArray);
13071307
} else {
13081308
auto InstIt = Builder.GetInsertBlock()
13091309
->getParent()

0 commit comments

Comments
 (0)