aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/LowerAllocations.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-02-28 18:51:45 +0000
committerChris Lattner <sabre@nondot.org>2004-02-28 18:51:45 +0000
commitb99df4f8c242e13e5321c4916eab4bbecf1eadb2 (patch)
tree81b63635e2b709dadf172479443a90dd668fb02b /lib/Transforms/Utils/LowerAllocations.cpp
parent8a4045e4d7495ac19935caf65cf39b8cc4e987b1 (diff)
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
Diffstat (limited to 'lib/Transforms/Utils/LowerAllocations.cpp')
-rw-r--r--lib/Transforms/Utils/LowerAllocations.cpp52
1 files changed, 42 insertions, 10 deletions
diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp
index b655a2b9e9..23a705b0ec 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<Value*> 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<Value*>(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<FreeInst>(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<Value*> 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<Value*>(1, MCast),
- "", I);
+ new CallInst(FreeFunc, FreeArgs, "", I);
// Delete the old free instruction
I = --BBIL.erase(I);