make this handle redefinition of malloc function with different prototype correctly

llvm-svn: 86712
This commit is contained in:
Victor Hernandez 2009-11-10 19:53:28 +00:00
parent f89a32ac45
commit bb336a1987
1 changed files with 9 additions and 7 deletions

View File

@ -494,22 +494,21 @@ static Instruction *createMalloc(Instruction *InsertBefore,
BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
Module* M = BB->getParent()->getParent();
const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
if (!MallocF)
Value *MallocFunc = MallocF;
if (!MallocFunc)
// prototype malloc as "void *malloc(size_t)"
MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy,
IntPtrTy, NULL));
if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
CallInst *MCall = NULL;
Instruction *Result = NULL;
if (InsertBefore) {
MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Result = MCall;
if (Result->getType() != AllocPtrType)
// Create a cast instruction to convert to the right type...
Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
} else {
MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Result = MCall;
if (Result->getType() != AllocPtrType) {
InsertAtEnd->getInstList().push_back(MCall);
@ -518,7 +517,10 @@ static Instruction *createMalloc(Instruction *InsertBefore,
}
}
MCall->setTailCall();
MCall->setCallingConv(MallocF->getCallingConv());
if (Function *F = dyn_cast<Function>(MallocFunc)) {
MCall->setCallingConv(F->getCallingConv());
if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
}
assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
"Malloc has void return type");