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