Skip to content

Commit 45bb45f

Browse files
committed
[llvm] Move CallInst::CreateMalloc to IRBuilderBase::CreateMalloc
This removes `CreateMalloc` from `CallInst` and adds it to the `IRBuilderBase` class. We no longer needed the `Instruction *InsertBefore` and `BasicBlock *InsertAtEnd` arguments of the `createMalloc` helper function because we're using `IRBuilder` now. That's why I we also don't need 4 `CreateMalloc` functions, but only two. Differential Revision: https://reviews.llvm.org/D158861
1 parent 1d7b59c commit 45bb45f

File tree

9 files changed

+84
-170
lines changed

9 files changed

+84
-170
lines changed

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ val build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue
20342034

20352035
(** [build_malloc ty name b] creates an [malloc]
20362036
instruction at the position specified by the instruction builder [b].
2037-
See the method [llvm::CallInst::CreateMalloc]. *)
2037+
See the method [llvm::IRBuilderBase::CreateMalloc]. *)
20382038
val build_malloc : lltype -> string -> llbuilder -> llvalue
20392039

20402040
(** [build_array_malloc ty val name b] creates an [array malloc]

llvm/examples/BrainF/BrainF.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ void BrainF::header(LLVMContext& C) {
9494
Type* Int8Ty = IntegerType::getInt8Ty(C);
9595
Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty);
9696
allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy);
97-
ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem,
98-
nullptr, "arr");
99-
cast<Instruction>(ptr_arr)->insertInto(BB, BB->end());
97+
ptr_arr = builder->CreateMalloc(IntPtrTy, Int8Ty, allocsize, val_mem, nullptr,
98+
"arr");
10099

101100
//call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i1 0)
102101
{

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ class IRBuilderBase {
619619
TBAATag, ScopeTag, NoAliasTag);
620620
}
621621

622+
CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
623+
Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
624+
Function *MallocF = nullptr, const Twine &Name = "");
625+
626+
/// CreateMalloc - Generate the IR for a call to malloc:
627+
/// 1. Compute the malloc call's argument as the specified type's size,
628+
/// possibly multiplied by the array size if the array size is not
629+
/// constant 1.
630+
/// 2. Call malloc with that argument.
631+
CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
632+
Value *ArraySize, Function *MallocF = nullptr,
633+
const Twine &Name = "");
634+
622635
CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
623636
Value *Size, Align Alignment,
624637
uint32_t ElementSize,

llvm/include/llvm/IR/Instructions.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,32 +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 malloc:
1607-
/// 1. Compute the malloc call's argument as the specified type's size,
1608-
/// possibly multiplied by the array size if the array size is not
1609-
/// constant 1.
1610-
/// 2. Call malloc with that argument.
1611-
/// 3. Bitcast the result of the malloc call to the specified type.
1612-
static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1613-
Type *AllocTy, Value *AllocSize,
1614-
Value *ArraySize = nullptr,
1615-
Function *MallocF = nullptr,
1616-
const Twine &Name = "");
1617-
static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1618-
Type *AllocTy, Value *AllocSize,
1619-
Value *ArraySize = nullptr,
1620-
Function *MallocF = nullptr,
1621-
const Twine &Name = "");
1622-
static Instruction *
1623-
CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, Type *AllocTy,
1624-
Value *AllocSize, Value *ArraySize = nullptr,
1625-
ArrayRef<OperandBundleDef> Bundles = std::nullopt,
1626-
Function *MallocF = nullptr, const Twine &Name = "");
1627-
static Instruction *
1628-
CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, Type *AllocTy,
1629-
Value *AllocSize, Value *ArraySize = nullptr,
1630-
ArrayRef<OperandBundleDef> Bundles = std::nullopt,
1631-
Function *MallocF = nullptr, const Twine &Name = "");
16321606
/// Generate the IR for a call to the builtin free function.
16331607
static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
16341608
static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);

llvm/lib/IR/Core.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,21 +3534,17 @@ LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
35343534
Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
35353535
Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
35363536
AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3537-
Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3538-
ITy, unwrap(Ty), AllocSize,
3539-
nullptr, nullptr, "");
3540-
return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3537+
return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3538+
nullptr, Name));
35413539
}
35423540

35433541
LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
35443542
LLVMValueRef Val, const char *Name) {
35453543
Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
35463544
Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
35473545
AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3548-
Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3549-
ITy, unwrap(Ty), AllocSize,
3550-
unwrap(Val), nullptr, "");
3551-
return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3546+
return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3547+
nullptr, Name));
35523548
}
35533549

35543550
LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,

llvm/lib/IR/IRBuilder.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,64 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
291291
return CI;
292292
}
293293

294+
/// isConstantOne - Return true only if val is constant int 1
295+
static bool isConstantOne(const Value *Val) {
296+
assert(Val && "isConstantOne does not work with nullptr Val");
297+
const ConstantInt *CVal = dyn_cast<ConstantInt>(Val);
298+
return CVal && CVal->isOne();
299+
}
300+
301+
CallInst *IRBuilderBase::CreateMalloc(Type *IntPtrTy, Type *AllocTy,
302+
Value *AllocSize, Value *ArraySize,
303+
ArrayRef<OperandBundleDef> OpB,
304+
Function *MallocF, const Twine &Name) {
305+
// malloc(type) becomes:
306+
// i8* malloc(typeSize)
307+
// malloc(type, arraySize) becomes:
308+
// i8* malloc(typeSize*arraySize)
309+
if (!ArraySize)
310+
ArraySize = ConstantInt::get(IntPtrTy, 1);
311+
else if (ArraySize->getType() != IntPtrTy)
312+
ArraySize = CreateIntCast(ArraySize, IntPtrTy, false);
313+
314+
if (!isConstantOne(ArraySize)) {
315+
if (isConstantOne(AllocSize)) {
316+
AllocSize = ArraySize; // Operand * 1 = Operand
317+
} else {
318+
// Multiply type size by the array size...
319+
AllocSize = CreateMul(ArraySize, AllocSize, "mallocsize");
320+
}
321+
}
322+
323+
assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
324+
// Create the call to Malloc.
325+
Module *M = BB->getParent()->getParent();
326+
Type *BPTy = PointerType::getUnqual(Context);
327+
FunctionCallee MallocFunc = MallocF;
328+
if (!MallocFunc)
329+
// prototype malloc as "void *malloc(size_t)"
330+
MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
331+
CallInst *MCall = CreateCall(MallocFunc, AllocSize, OpB, Name);
332+
333+
MCall->setTailCall();
334+
if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
335+
MCall->setCallingConv(F->getCallingConv());
336+
F->setReturnDoesNotAlias();
337+
}
338+
339+
assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
340+
341+
return MCall;
342+
}
343+
344+
CallInst *IRBuilderBase::CreateMalloc(Type *IntPtrTy, Type *AllocTy,
345+
Value *AllocSize, Value *ArraySize,
346+
Function *MallocF, const Twine &Name) {
347+
348+
return CreateMalloc(IntPtrTy, AllocTy, AllocSize, ArraySize, std::nullopt,
349+
MallocF, Name);
350+
}
351+
294352
CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove(
295353
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
296354
uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,

llvm/lib/IR/Instructions.cpp

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

812-
/// IsConstantOne - Return true only if val is constant int 1
813-
static bool IsConstantOne(Value *val) {
814-
assert(val && "IsConstantOne does not work with nullptr val");
815-
const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
816-
return CVal && CVal->isOne();
817-
}
818-
819-
static Instruction *createMalloc(Instruction *InsertBefore,
820-
BasicBlock *InsertAtEnd, Type *IntPtrTy,
821-
Type *AllocTy, Value *AllocSize,
822-
Value *ArraySize,
823-
ArrayRef<OperandBundleDef> OpB,
824-
Function *MallocF, const Twine &Name) {
825-
assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
826-
"createMalloc needs either InsertBefore or InsertAtEnd");
827-
828-
// malloc(type) becomes:
829-
// bitcast (i8* malloc(typeSize)) to type*
830-
// malloc(type, arraySize) becomes:
831-
// bitcast (i8* malloc(typeSize*arraySize)) to type*
832-
if (!ArraySize)
833-
ArraySize = ConstantInt::get(IntPtrTy, 1);
834-
else if (ArraySize->getType() != IntPtrTy) {
835-
if (InsertBefore)
836-
ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
837-
"", InsertBefore);
838-
else
839-
ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
840-
"", InsertAtEnd);
841-
}
842-
843-
if (!IsConstantOne(ArraySize)) {
844-
if (IsConstantOne(AllocSize)) {
845-
AllocSize = ArraySize; // Operand * 1 = Operand
846-
} else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
847-
Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
848-
false /*ZExt*/);
849-
// Malloc arg is constant product of type size and array size
850-
AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
851-
} else {
852-
// Multiply type size by the array size...
853-
if (InsertBefore)
854-
AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
855-
"mallocsize", InsertBefore);
856-
else
857-
AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
858-
"mallocsize", InsertAtEnd);
859-
}
860-
}
861-
862-
assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
863-
// Create the call to Malloc.
864-
BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
865-
Module *M = BB->getParent()->getParent();
866-
Type *BPTy = PointerType::getUnqual(BB->getContext());
867-
FunctionCallee MallocFunc = MallocF;
868-
if (!MallocFunc)
869-
// prototype malloc as "void *malloc(size_t)"
870-
MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
871-
CallInst *MCall = nullptr;
872-
if (InsertBefore) {
873-
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, Name,
874-
InsertBefore);
875-
} else {
876-
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, Name);
877-
}
878-
MCall->setTailCall();
879-
if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
880-
MCall->setCallingConv(F->getCallingConv());
881-
if (!F->returnDoesNotAlias())
882-
F->setReturnDoesNotAlias();
883-
}
884-
assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
885-
886-
return MCall;
887-
}
888-
889-
/// CreateMalloc - Generate the IR for a call to malloc:
890-
/// 1. Compute the malloc call's argument as the specified type's size,
891-
/// possibly multiplied by the array size if the array size is not
892-
/// constant 1.
893-
/// 2. Call malloc with that argument.
894-
/// 3. Bitcast the result of the malloc call to the specified type.
895-
Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
896-
Type *IntPtrTy, Type *AllocTy,
897-
Value *AllocSize, Value *ArraySize,
898-
Function *MallocF,
899-
const Twine &Name) {
900-
return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
901-
ArraySize, std::nullopt, MallocF, Name);
902-
}
903-
Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
904-
Type *IntPtrTy, Type *AllocTy,
905-
Value *AllocSize, Value *ArraySize,
906-
ArrayRef<OperandBundleDef> OpB,
907-
Function *MallocF,
908-
const Twine &Name) {
909-
return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
910-
ArraySize, OpB, MallocF, Name);
911-
}
912-
913-
/// CreateMalloc - Generate the IR for a call to malloc:
914-
/// 1. Compute the malloc call's argument as the specified type's size,
915-
/// possibly multiplied by the array size if the array size is not
916-
/// constant 1.
917-
/// 2. Call malloc with that argument.
918-
/// 3. Bitcast the result of the malloc call to the specified type.
919-
/// Note: This function does not add the bitcast to the basic block, that is the
920-
/// responsibility of the caller.
921-
Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
922-
Type *IntPtrTy, Type *AllocTy,
923-
Value *AllocSize, Value *ArraySize,
924-
Function *MallocF, const Twine &Name) {
925-
return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
926-
ArraySize, std::nullopt, MallocF, Name);
927-
}
928-
Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
929-
Type *IntPtrTy, Type *AllocTy,
930-
Value *AllocSize, Value *ArraySize,
931-
ArrayRef<OperandBundleDef> OpB,
932-
Function *MallocF, const Twine &Name) {
933-
return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
934-
ArraySize, OpB, MallocF, Name);
935-
}
936-
937812
static Instruction *createFree(Value *Source,
938813
ArrayRef<OperandBundleDef> Bundles,
939814
Instruction *InsertBefore,

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,9 +1290,9 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
12901290
// setjmpTable = (int *) malloc(40);
12911291
Type *IntPtrTy = getAddrIntType(&M);
12921292
Constant *size = ConstantInt::get(IntPtrTy, 40);
1293-
Instruction *SetjmpTable =
1294-
CallInst::CreateMalloc(SetjmpTableSize, IntPtrTy, IRB.getInt32Ty(), size,
1295-
nullptr, nullptr, "setjmpTable");
1293+
IRB.SetInsertPoint(SetjmpTableSize);
1294+
auto *SetjmpTable = IRB.CreateMalloc(IntPtrTy, IRB.getInt32Ty(), size,
1295+
nullptr, nullptr, "setjmpTable");
12961296
SetjmpTable->setDebugLoc(FirstDL);
12971297
// CallInst::CreateMalloc may return a bitcast instruction if the result types
12981298
// mismatch. We need to set the debug loc for the original call too.
@@ -1301,7 +1301,6 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
13011301
MallocCallI->setDebugLoc(FirstDL);
13021302
}
13031303
// setjmpTable[0] = 0;
1304-
IRB.SetInsertPoint(SetjmpTableSize);
13051304
IRB.CreateStore(IRB.getInt32(0), SetjmpTable);
13061305
SetjmpTableInsts.push_back(SetjmpTable);
13071306
SetjmpTableSizeInsts.push_back(SetjmpTableSize);

polly/lib/CodeGen/IslNodeBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,9 @@ void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) {
12921292
unsigned Size = SAI->getElemSizeInBytes();
12931293

12941294
// Insert the malloc call at polly.start
1295-
auto InstIt = std::get<0>(StartExitBlocks)->getTerminator();
1296-
auto *CreatedArray = CallInst::CreateMalloc(
1297-
&*InstIt, IntPtrTy, SAI->getElementType(),
1295+
Builder.SetInsertPoint(std::get<0>(StartExitBlocks)->getTerminator());
1296+
auto *CreatedArray = Builder.CreateMalloc(
1297+
IntPtrTy, SAI->getElementType(),
12981298
ConstantInt::get(Type::getInt64Ty(Ctx), Size),
12991299
ConstantInt::get(Type::getInt64Ty(Ctx), ArraySizeInt), nullptr,
13001300
SAI->getName());

0 commit comments

Comments
 (0)