Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The LowerAllocations transformation is a target-dependent tranformation |
| 11 | // because it depends on the size of data types and alignment constraints. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "lowerallocs" |
| 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumLowered, "Number of allocations lowered"); |
| 29 | |
| 30 | namespace { |
| 31 | /// LowerAllocations - Turn malloc and free instructions into %malloc and |
| 32 | /// %free calls. |
| 33 | /// |
| 34 | class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass { |
| 35 | Constant *MallocFunc; // Functions in the module we are processing |
| 36 | Constant *FreeFunc; // Initialized by doInitialization |
| 37 | bool LowerMallocArgToInteger; |
| 38 | public: |
| 39 | static char ID; // Pass ID, replacement for typeid |
Dan Gohman | 34c280e | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 40 | explicit LowerAllocations(bool LowerToInt = false) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | : BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0), |
| 42 | LowerMallocArgToInteger(LowerToInt) {} |
| 43 | |
| 44 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | AU.addRequired<TargetData>(); |
| 46 | AU.setPreservesCFG(); |
| 47 | |
| 48 | // This is a cluster of orthogonal Transforms: |
| 49 | AU.addPreserved<UnifyFunctionExitNodes>(); |
| 50 | AU.addPreservedID(PromoteMemoryToRegisterID); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | AU.addPreservedID(LowerSwitchID); |
| 52 | AU.addPreservedID(LowerInvokePassID); |
| 53 | } |
| 54 | |
| 55 | /// doPassInitialization - For the lower allocations pass, this ensures that |
| 56 | /// a module contains a declaration for a malloc and a free function. |
| 57 | /// |
| 58 | bool doInitialization(Module &M); |
| 59 | |
| 60 | virtual bool doInitialization(Function &F) { |
| 61 | return BasicBlockPass::doInitialization(F); |
| 62 | } |
| 63 | |
| 64 | /// runOnBasicBlock - This method does the actual work of converting |
| 65 | /// instructions over, assuming that the pass has already been initialized. |
| 66 | /// |
| 67 | bool runOnBasicBlock(BasicBlock &BB); |
| 68 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 71 | char LowerAllocations::ID = 0; |
| 72 | static RegisterPass<LowerAllocations> |
| 73 | X("lowerallocs", "Lower allocations from instructions to calls"); |
| 74 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | // Publically exposed interface to pass... |
Dan Gohman | 66a636e | 2008-05-13 02:05:11 +0000 | [diff] [blame^] | 76 | const PassInfo *const llvm::LowerAllocationsID = &X; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | // createLowerAllocationsPass - Interface to this file... |
| 78 | Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) { |
| 79 | return new LowerAllocations(LowerMallocArgToInteger); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | // doInitialization - For the lower allocations pass, this ensures that a |
| 84 | // module contains a declaration for a malloc and a free function. |
| 85 | // |
| 86 | // This function is always successful. |
| 87 | // |
| 88 | bool LowerAllocations::doInitialization(Module &M) { |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 89 | const Type *BPTy = PointerType::getUnqual(Type::Int8Ty); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | // Prototype malloc as "char* malloc(...)", because we don't know in |
| 91 | // doInitialization whether size_t is int or long. |
| 92 | FunctionType *FT = FunctionType::get(BPTy, std::vector<const Type*>(), true); |
| 93 | MallocFunc = M.getOrInsertFunction("malloc", FT); |
| 94 | FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | // runOnBasicBlock - This method does the actual work of converting |
| 99 | // instructions over, assuming that the pass has already been initialized. |
| 100 | // |
| 101 | bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { |
| 102 | bool Changed = false; |
| 103 | assert(MallocFunc && FreeFunc && "Pass not initialized!"); |
| 104 | |
| 105 | BasicBlock::InstListType &BBIL = BB.getInstList(); |
| 106 | |
| 107 | const TargetData &TD = getAnalysis<TargetData>(); |
| 108 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 109 | |
| 110 | // Loop over all of the instructions, looking for malloc or free instructions |
| 111 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
| 112 | if (MallocInst *MI = dyn_cast<MallocInst>(I)) { |
| 113 | const Type *AllocTy = MI->getType()->getElementType(); |
| 114 | |
| 115 | // malloc(type) becomes sbyte *malloc(size) |
| 116 | Value *MallocArg; |
| 117 | if (LowerMallocArgToInteger) |
Duncan Sands | f99fdc6 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 118 | MallocArg = ConstantInt::get(Type::Int64Ty, TD.getABITypeSize(AllocTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 119 | else |
| 120 | MallocArg = ConstantExpr::getSizeOf(AllocTy); |
| 121 | MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg), |
| 122 | IntPtrTy); |
| 123 | |
| 124 | if (MI->isArrayAllocation()) { |
| 125 | if (isa<ConstantInt>(MallocArg) && |
| 126 | cast<ConstantInt>(MallocArg)->isOne()) { |
| 127 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 128 | } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) { |
| 129 | CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/); |
| 130 | MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg)); |
| 131 | } else { |
| 132 | Value *Scale = MI->getOperand(0); |
| 133 | if (Scale->getType() != IntPtrTy) |
| 134 | Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/, |
| 135 | "", I); |
| 136 | |
| 137 | // Multiply it by the array size if necessary... |
| 138 | MallocArg = BinaryOperator::create(Instruction::Mul, Scale, |
| 139 | MallocArg, "", I); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Create the call to Malloc. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 144 | CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | MCall->setTailCall(); |
| 146 | |
| 147 | // Create a cast instruction to convert to the right type... |
| 148 | Value *MCast; |
| 149 | if (MCall->getType() != Type::VoidTy) |
| 150 | MCast = new BitCastInst(MCall, MI->getType(), "", I); |
| 151 | else |
| 152 | MCast = Constant::getNullValue(MI->getType()); |
| 153 | |
| 154 | // Replace all uses of the old malloc inst with the cast inst |
| 155 | MI->replaceAllUsesWith(MCast); |
| 156 | I = --BBIL.erase(I); // remove and delete the malloc instr... |
| 157 | Changed = true; |
| 158 | ++NumLowered; |
| 159 | } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) { |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 160 | Value *PtrCast = |
| 161 | new BitCastInst(FI->getOperand(0), |
| 162 | PointerType::getUnqual(Type::Int8Ty), "", I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 163 | |
| 164 | // Insert a call to the free function... |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 165 | CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | |
| 167 | // Delete the old free instruction |
| 168 | I = --BBIL.erase(I); |
| 169 | Changed = true; |
| 170 | ++NumLowered; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return Changed; |
| 175 | } |
| 176 | |