From ef1e989e4f91bb8a414786e91bef52587953448d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 3 Mar 2005 01:03:43 +0000 Subject: [PATCH] Add an optional argument to lower to a specific constant value instead of to a "sizeof" expression. llvm-svn: 20414 --- .../Transforms/Scalar/LowerAllocations.cpp | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LowerAllocations.cpp b/llvm/lib/Transforms/Scalar/LowerAllocations.cpp index d826a91d8d75..abed41edc2ad 100644 --- a/llvm/lib/Transforms/Scalar/LowerAllocations.cpp +++ b/llvm/lib/Transforms/Scalar/LowerAllocations.cpp @@ -31,8 +31,10 @@ namespace { class LowerAllocations : public BasicBlockPass { Function *MallocFunc; // Functions in the module we are processing Function *FreeFunc; // Initialized by doInitialization + bool LowerMallocArgToInteger; public: - LowerAllocations() : MallocFunc(0), FreeFunc(0) {} + LowerAllocations(bool LowerToInt = false) + : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired(); @@ -59,8 +61,8 @@ namespace { } // createLowerAllocationsPass - Interface to this file... -FunctionPass *llvm::createLowerAllocationsPass() { - return new LowerAllocations(); +FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) { + return new LowerAllocations(LowerMallocArgToInteger); } @@ -95,7 +97,8 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { BasicBlock::InstListType &BBIL = BB.getInstList(); - const Type *IntPtrTy = getAnalysis().getIntPtrType(); + const TargetData &TD = getAnalysis(); + const Type *IntPtrTy = TD.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) { @@ -103,8 +106,13 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { const Type *AllocTy = MI->getType()->getElementType(); // malloc(type) becomes sbyte *malloc(size) - Value *MallocArg = ConstantExpr::getCast(ConstantExpr::getSizeOf(AllocTy), - IntPtrTy); + Value *MallocArg; + if (LowerMallocArgToInteger) + MallocArg = ConstantUInt::get(Type::ULongTy, TD.getTypeSize(AllocTy)); + else + MallocArg = ConstantExpr::getSizeOf(AllocTy); + MallocArg = ConstantExpr::getCast(cast(MallocArg), IntPtrTy); + if (MI->isArrayAllocation()) { if (isa(MallocArg) && cast(MallocArg)->getRawValue() == 1) {