From de97b5788c7843cadab04fb884d5c532b17a093b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 13 Dec 2004 20:00:02 +0000 Subject: [PATCH] Get rid of getSizeOf, using ConstantExpr::getSizeOf instead. do not insert a prototype for malloc of: void* malloc(uint): on 64-bit u targets this is not correct. Instead of prototype it as void *malloc(...), and pass the correct intptr_t through the "...". Finally, fix Regression/CodeGen/SparcV9/2004-12-13-MallocCrash.ll, by not forming constantexpr casts from pointer to uint. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18908 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LowerAllocations.cpp | 53 ++++++++++++++--------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp index 3bbec3dfd96..d826a91d8d7 100644 --- a/lib/Transforms/Utils/LowerAllocations.cpp +++ b/lib/Transforms/Utils/LowerAllocations.cpp @@ -19,6 +19,7 @@ #include "llvm/Constants.h" #include "llvm/Pass.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Target/TargetData.h" using namespace llvm; namespace { @@ -33,13 +34,19 @@ namespace { public: LowerAllocations() : MallocFunc(0), FreeFunc(0) {} + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + AU.setPreservesCFG(); + } + /// doPassInitialization - For the lower allocations pass, this ensures that /// a module contains a declaration for a malloc and a free function. /// bool doInitialization(Module &M); - virtual bool doInitialization(Function&f) - { return BasicBlockPass::doInitialization(f); } + virtual bool doInitialization(Function &F) { + return BasicBlockPass::doInitialization(F); + } /// runOnBasicBlock - This method does the actual work of converting /// instructions over, assuming that the pass has already been initialized. @@ -67,22 +74,18 @@ bool LowerAllocations::doInitialization(Module &M) { MallocFunc = M.getNamedFunction("malloc"); FreeFunc = M.getNamedFunction("free"); - if (MallocFunc == 0) - MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0); + if (MallocFunc == 0) { + // Prototype malloc as "void* malloc(...)", because we don't know in + // doInitialization whether size_t is int or long. + FunctionType *FT = FunctionType::get(SBPTy,std::vector(),true); + MallocFunc = M.getOrInsertFunction("malloc", FT); + } if (FreeFunc == 0) - FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0); + FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0); return true; } -static Constant *getSizeof(const Type *Ty) { - Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty)); - std::vector Idx; - Idx.push_back(ConstantUInt::get(Type::UIntTy, 1)); - Ret = ConstantExpr::getGetElementPtr(Ret, Idx); - return ConstantExpr::getCast(Ret, Type::UIntTy); -} - // runOnBasicBlock - This method does the actual work of converting // instructions over, assuming that the pass has already been initialized. // @@ -92,23 +95,30 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { BasicBlock::InstListType &BBIL = BB.getInstList(); + const Type *IntPtrTy = getAnalysis().getIntPtrType(); + // 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(I)) { const Type *AllocTy = MI->getType()->getElementType(); // malloc(type) becomes sbyte *malloc(size) - Value *MallocArg = getSizeof(AllocTy); + Value *MallocArg = ConstantExpr::getCast(ConstantExpr::getSizeOf(AllocTy), + IntPtrTy); if (MI->isArrayAllocation()) { - if (isa(MallocArg) && - cast(MallocArg)->getValue() == 1) { + if (isa(MallocArg) && + cast(MallocArg)->getRawValue() == 1) { MallocArg = MI->getOperand(0); // Operand * 1 = Operand } else if (Constant *CO = dyn_cast(MI->getOperand(0))) { + CO = ConstantExpr::getCast(CO, IntPtrTy); MallocArg = ConstantExpr::getMul(CO, cast(MallocArg)); } else { + Value *Scale = MI->getOperand(0); + if (Scale->getType() != IntPtrTy) + Scale = new CastInst(Scale, IntPtrTy, "", I); + // Multiply it by the array size if necessary... - MallocArg = BinaryOperator::create(Instruction::Mul, - MI->getOperand(0), + MallocArg = BinaryOperator::create(Instruction::Mul, Scale, MallocArg, "", I); } } @@ -117,8 +127,11 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { std::vector MallocArgs; if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) { - if (MallocFTy->getNumParams() > 0 && - MallocFTy->getParamType(0) != Type::UIntTy) + if (MallocFTy->isVarArg()) { + if (MallocArg->getType() != IntPtrTy) + MallocArg = new CastInst(MallocArg, IntPtrTy, "", I); + } else if (MallocFTy->getNumParams() > 0 && + MallocFTy->getParamType(0) != Type::UIntTy) MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I); MallocArgs.push_back(MallocArg); } -- 2.34.1