From 8bfc2f11a481419a5b5d5e63dc432c09c54c8a3a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 15 Jul 2004 01:08:08 +0000 Subject: [PATCH] Now that we codegen the portable "sizeof" efficiently, we can use it for malloc lowering. This means that lowerallocations doesn't need targetdata anymore. yaay. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14835 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LowerAllocations.cpp | 40 +++++++++++++---------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp index 53b4c3a4246..27e0f70315d 100644 --- a/lib/Transforms/Utils/LowerAllocations.cpp +++ b/lib/Transforms/Utils/LowerAllocations.cpp @@ -19,7 +19,6 @@ #include "llvm/iOther.h" #include "llvm/Constants.h" #include "llvm/Pass.h" -#include "llvm/Target/TargetData.h" #include "Support/Statistic.h" using namespace llvm; @@ -35,10 +34,6 @@ namespace { public: LowerAllocations() : MallocFunc(0), FreeFunc(0) {} - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - } - /// doPassInitialization - For the lower allocations pass, this ensures that /// a module contains a declaration for a malloc and a free function. /// @@ -78,6 +73,14 @@ bool LowerAllocations::doInitialization(Module &M) { 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. // @@ -86,25 +89,26 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { assert(MallocFunc && FreeFunc && "Pass not initialized!"); BasicBlock::InstListType &BBIL = BB.getInstList(); - TargetData &DataLayout = getAnalysis(); // 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(); - // Get the number of bytes to be allocated for one element of the - // requested type... - unsigned Size = DataLayout.getTypeSize(AllocTy); - - // malloc(type) becomes sbyte *malloc(constint) - Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); - if (MI->getNumOperands() && Size == 1) { - MallocArg = MI->getOperand(0); // Operand * 1 = Operand - } else if (MI->isArrayAllocation()) { - // Multiply it by the array size if necessary... - MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0), - MallocArg, "", I); + // malloc(type) becomes sbyte *malloc(size) + Value *MallocArg = getSizeof(AllocTy); + if (MI->isArrayAllocation()) { + if (isa(MallocArg) && + cast(MallocArg)->getValue() == 1) { + MallocArg = MI->getOperand(0); // Operand * 1 = Operand + } else if (Constant *CO = dyn_cast(MI->getOperand(0))) { + MallocArg = ConstantExpr::getMul(CO, cast(MallocArg)); + } else { + // Multiply it by the array size if necessary... + MallocArg = BinaryOperator::create(Instruction::Mul, + MI->getOperand(0), + MallocArg, "", I); + } } const FunctionType *MallocFTy = MallocFunc->getFunctionType();