diff options
author | Evan Cheng <evan.cheng@apple.com> | 2009-09-10 04:36:43 +0000 |
---|---|---|
committer | Evan Cheng <evan.cheng@apple.com> | 2009-09-10 04:36:43 +0000 |
commit | fabcb9127f278a77b8aae5673be1115390c55050 (patch) | |
tree | b08e3c11348110692e65483dafca45b0489b12d4 /lib/Transforms | |
parent | fe220285b5ef1e4480ddd7f7c8bb182b88a33b16 (diff) | |
download | external_llvm-fabcb9127f278a77b8aae5673be1115390c55050.zip external_llvm-fabcb9127f278a77b8aae5673be1115390c55050.tar.gz external_llvm-fabcb9127f278a77b8aae5673be1115390c55050.tar.bz2 |
Add malloc call utility functions. Patch by Victor Hernandez.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81426 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/Utils/LowerAllocations.cpp | 58 |
1 files changed, 6 insertions, 52 deletions
diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp index 7276d55..cc1f6ea 100644 --- a/lib/Transforms/Utils/LowerAllocations.cpp +++ b/lib/Transforms/Utils/LowerAllocations.cpp @@ -33,13 +33,13 @@ namespace { /// @free calls. /// class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass { - Constant *MallocFunc; // Functions in the module we are processing - Constant *FreeFunc; // Initialized by doInitialization + Constant *FreeFunc; // Functions in the module we are processing + // Initialized by doInitialization bool LowerMallocArgToInteger; public: static char ID; // Pass ID, replacement for typeid explicit LowerAllocations(bool LowerToInt = false) - : BasicBlockPass(&ID), MallocFunc(0), FreeFunc(0), + : BasicBlockPass(&ID), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { @@ -88,10 +88,6 @@ Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) { // bool LowerAllocations::doInitialization(Module &M) { const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext())); - // Prototype malloc as "char* malloc(...)", because we don't know in - // doInitialization whether size_t is int or long. - FunctionType *FT = FunctionType::get(BPTy, true); - MallocFunc = M.getOrInsertFunction("malloc", FT); FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()), BPTy, (Type *)0); return true; @@ -102,7 +98,7 @@ bool LowerAllocations::doInitialization(Module &M) { // bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { bool Changed = false; - assert(MallocFunc && FreeFunc && "Pass not initialized!"); + assert(FreeFunc && "Pass not initialized!"); BasicBlock::InstListType &BBIL = BB.getInstList(); @@ -112,50 +108,8 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { // Loop over all of the instructions, looking for malloc or free instructions for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { if (MallocInst *MI = dyn_cast<MallocInst>(I)) { - const Type *AllocTy = MI->getType()->getElementType(); - - // malloc(type) becomes i8 *malloc(size) - Value *MallocArg; - if (LowerMallocArgToInteger) - MallocArg = ConstantInt::get(Type::getInt64Ty(BB.getContext()), - TD.getTypeAllocSize(AllocTy)); - else - MallocArg = ConstantExpr::getSizeOf(AllocTy); - MallocArg = - ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg), - IntPtrTy); - - if (MI->isArrayAllocation()) { - if (isa<ConstantInt>(MallocArg) && - cast<ConstantInt>(MallocArg)->isOne()) { - MallocArg = MI->getOperand(0); // Operand * 1 = Operand - } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) { - CO = - ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/); - MallocArg = ConstantExpr::getMul(CO, - cast<Constant>(MallocArg)); - } else { - Value *Scale = MI->getOperand(0); - if (Scale->getType() != IntPtrTy) - Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/, - "", I); - - // Multiply it by the array size if necessary... - MallocArg = BinaryOperator::Create(Instruction::Mul, Scale, - MallocArg, "", I); - } - } - - // Create the call to Malloc. - CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I); - MCall->setTailCall(); - - // Create a cast instruction to convert to the right type... - Value *MCast; - if (MCall->getType() != Type::getVoidTy(BB.getContext())) - MCast = new BitCastInst(MCall, MI->getType(), "", I); - else - MCast = Constant::getNullValue(MI->getType()); + Value *MCast = CallInst::CreateMalloc(I, MI->getType(), IntPtrTy, + MI->getOperand(0)); // Replace all uses of the old malloc inst with the cast inst MI->replaceAllUsesWith(MCast); |