Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 1 | //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | bf43787 | 2004-01-28 20:43:01 +0000 | [diff] [blame] | 10 | // The LowerAllocations transformation is a target-dependent tranformation |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 11 | // because it depends on the size of data types and alignment constraints. |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 42706e4 | 2002-07-23 22:04:17 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/iMemory.h" |
| 19 | #include "llvm/iOther.h" |
Chris Lattner | 9b55e5a | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 22 | #include "Support/Statistic.h" |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 25 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 26 | Statistic<> NumLowered("lowerallocs", "Number of allocations lowered"); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 3cab9f0 | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 28 | /// LowerAllocations - Turn malloc and free instructions into %malloc and |
| 29 | /// %free calls. |
| 30 | /// |
| 31 | class LowerAllocations : public BasicBlockPass { |
| 32 | Function *MallocFunc; // Functions in the module we are processing |
| 33 | Function *FreeFunc; // Initialized by doInitialization |
| 34 | public: |
| 35 | LowerAllocations() : MallocFunc(0), FreeFunc(0) {} |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 3cab9f0 | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 37 | /// doPassInitialization - For the lower allocations pass, this ensures that |
| 38 | /// a module contains a declaration for a malloc and a free function. |
| 39 | /// |
| 40 | bool doInitialization(Module &M); |
| 41 | |
| 42 | /// runOnBasicBlock - This method does the actual work of converting |
| 43 | /// instructions over, assuming that the pass has already been initialized. |
| 44 | /// |
| 45 | bool runOnBasicBlock(BasicBlock &BB); |
| 46 | }; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 3cab9f0 | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 48 | RegisterOpt<LowerAllocations> |
| 49 | X("lowerallocs", "Lower allocations from instructions to calls"); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 50 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 52 | // createLowerAllocationsPass - Interface to this file... |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 53 | FunctionPass *llvm::createLowerAllocationsPass() { |
Chris Lattner | 3cab9f0 | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 54 | return new LowerAllocations(); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 0686e43 | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 58 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 59 | // module contains a declaration for a malloc and a free function. |
| 60 | // |
| 61 | // This function is always successful. |
| 62 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 63 | bool LowerAllocations::doInitialization(Module &M) { |
Chris Lattner | 2887328 | 2003-08-31 00:22:27 +0000 | [diff] [blame] | 64 | const Type *SBPTy = PointerType::get(Type::SByteTy); |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 65 | MallocFunc = M.getNamedFunction("malloc"); |
| 66 | FreeFunc = M.getNamedFunction("free"); |
| 67 | |
| 68 | if (MallocFunc == 0) |
| 69 | MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0); |
| 70 | if (FreeFunc == 0) |
| 71 | FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 73 | return true; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | ba7aef39 | 2004-07-15 01:08:08 +0000 | [diff] [blame] | 76 | static Constant *getSizeof(const Type *Ty) { |
| 77 | Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty)); |
| 78 | std::vector<Constant*> Idx; |
| 79 | Idx.push_back(ConstantUInt::get(Type::UIntTy, 1)); |
| 80 | Ret = ConstantExpr::getGetElementPtr(Ret, Idx); |
| 81 | return ConstantExpr::getCast(Ret, Type::UIntTy); |
| 82 | } |
| 83 | |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 84 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 85 | // instructions over, assuming that the pass has already been initialized. |
| 86 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 87 | bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { |
Chris Lattner | 6fea032 | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 88 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | assert(MallocFunc && FreeFunc && "Pass not initialized!"); |
| 90 | |
| 91 | BasicBlock::InstListType &BBIL = BB.getInstList(); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 92 | |
| 93 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 95 | if (MallocInst *MI = dyn_cast<MallocInst>(I)) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 96 | const Type *AllocTy = MI->getType()->getElementType(); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 97 | |
Chris Lattner | ba7aef39 | 2004-07-15 01:08:08 +0000 | [diff] [blame] | 98 | // malloc(type) becomes sbyte *malloc(size) |
| 99 | Value *MallocArg = getSizeof(AllocTy); |
| 100 | if (MI->isArrayAllocation()) { |
| 101 | if (isa<ConstantUInt>(MallocArg) && |
| 102 | cast<ConstantUInt>(MallocArg)->getValue() == 1) { |
| 103 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 104 | } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) { |
| 105 | MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg)); |
| 106 | } else { |
| 107 | // Multiply it by the array size if necessary... |
| 108 | MallocArg = BinaryOperator::create(Instruction::Mul, |
| 109 | MI->getOperand(0), |
| 110 | MallocArg, "", I); |
| 111 | } |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 113 | |
| 114 | const FunctionType *MallocFTy = MallocFunc->getFunctionType(); |
| 115 | std::vector<Value*> MallocArgs; |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 117 | if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) { |
| 118 | if (MallocFTy->getNumParams() > 0 && |
| 119 | MallocFTy->getParamType(0) != Type::UIntTy) |
| 120 | MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I); |
| 121 | MallocArgs.push_back(MallocArg); |
| 122 | } |
| 123 | |
| 124 | // If malloc is prototyped to take extra arguments, pass nulls. |
| 125 | for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i) |
| 126 | MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i))); |
| 127 | |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 128 | // Create the call to Malloc... |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 129 | CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 130 | |
| 131 | // Create a cast instruction to convert to the right type... |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 132 | Value *MCast; |
| 133 | if (MCall->getType() != Type::VoidTy) |
| 134 | MCast = new CastInst(MCall, MI->getType(), "", I); |
| 135 | else |
| 136 | MCast = Constant::getNullValue(MI->getType()); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 137 | |
| 138 | // Replace all uses of the old malloc inst with the cast inst |
| 139 | MI->replaceAllUsesWith(MCast); |
Chris Lattner | a239e68 | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 140 | I = --BBIL.erase(I); // remove and delete the malloc instr... |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 141 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 142 | ++NumLowered; |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 143 | } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) { |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 144 | const FunctionType *FreeFTy = FreeFunc->getFunctionType(); |
| 145 | std::vector<Value*> FreeArgs; |
| 146 | |
| 147 | if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) { |
| 148 | Value *MCast = FI->getOperand(0); |
| 149 | if (FreeFTy->getNumParams() > 0 && |
| 150 | FreeFTy->getParamType(0) != MCast->getType()) |
| 151 | MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I); |
| 152 | FreeArgs.push_back(MCast); |
| 153 | } |
| 154 | |
| 155 | // If malloc is prototyped to take extra arguments, pass nulls. |
| 156 | for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i) |
| 157 | FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i))); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 158 | |
| 159 | // Insert a call to the free function... |
Chris Lattner | 772eafa | 2004-02-28 18:51:45 +0000 | [diff] [blame] | 160 | new CallInst(FreeFunc, FreeArgs, "", I); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 161 | |
| 162 | // Delete the old free instruction |
Chris Lattner | a239e68 | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 163 | I = --BBIL.erase(I); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 164 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 165 | ++NumLowered; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Chris Lattner | 6fea032 | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 169 | return Changed; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 170 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 171 | |