From b99df4f8c242e13e5321c4916eab4bbecf1eadb2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 28 Feb 2004 18:51:45 +0000 Subject: [PATCH] if there is already a prototype for malloc/free, use it, even if it's incorrect. Do not just inject a new prototype. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11951 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LowerAllocations.cpp | 52 ++++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp index b655a2b9e91..23a705b0ec5 100644 --- a/lib/Transforms/Utils/LowerAllocations.cpp +++ b/lib/Transforms/Utils/LowerAllocations.cpp @@ -67,8 +67,13 @@ FunctionPass *llvm::createLowerAllocationsPass() { // bool LowerAllocations::doInitialization(Module &M) { const Type *SBPTy = PointerType::get(Type::SByteTy); - MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0); - FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0); + MallocFunc = M.getNamedFunction("malloc"); + FreeFunc = M.getNamedFunction("free"); + + if (MallocFunc == 0) + MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0); + if (FreeFunc == 0) + FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0); return true; } @@ -101,13 +106,30 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0), MallocArg, "", I); } + + const FunctionType *MallocFTy = MallocFunc->getFunctionType(); + std::vector MallocArgs; + if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) { + if (MallocFTy->getNumParams() > 0 && + MallocFTy->getParamType(0) != Type::UIntTy) + MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I); + MallocArgs.push_back(MallocArg); + } + + // If malloc is prototyped to take extra arguments, pass nulls. + for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i) + MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i))); + // Create the call to Malloc... - CallInst *MCall = new CallInst(MallocFunc, - std::vector(1, MallocArg), "", I); + CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I); // Create a cast instruction to convert to the right type... - CastInst *MCast = new CastInst(MCall, MI->getType(), "", I); + Value *MCast; + if (MCall->getType() != Type::VoidTy) + MCast = new CastInst(MCall, MI->getType(), "", I); + else + MCast = Constant::getNullValue(MI->getType()); // Replace all uses of the old malloc inst with the cast inst MI->replaceAllUsesWith(MCast); @@ -115,13 +137,23 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { Changed = true; ++NumLowered; } else if (FreeInst *FI = dyn_cast(I)) { - // Cast the argument to free into a ubyte*... - CastInst *MCast = new CastInst(FI->getOperand(0), - PointerType::get(Type::SByteTy), "", I); + const FunctionType *FreeFTy = FreeFunc->getFunctionType(); + std::vector FreeArgs; + + if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) { + Value *MCast = FI->getOperand(0); + if (FreeFTy->getNumParams() > 0 && + FreeFTy->getParamType(0) != MCast->getType()) + MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I); + FreeArgs.push_back(MCast); + } + + // If malloc is prototyped to take extra arguments, pass nulls. + for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i) + FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i))); // Insert a call to the free function... - CallInst *FCall = new CallInst(FreeFunc, std::vector(1, MCast), - "", I); + new CallInst(FreeFunc, FreeArgs, "", I); // Delete the old free instruction I = --BBIL.erase(I);