Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 1 | //===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=// |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 3 | // This file defines two passes that convert malloc and free instructions to |
| 4 | // calls to and from %malloc & %free function calls. The LowerAllocations |
| 5 | // transformation is a target dependant tranformation because it depends on the |
| 6 | // size of data types and alignment constraints. |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | d7db863 | 2002-01-22 01:04:08 +0000 | [diff] [blame^] | 10 | #include "llvm/Transforms/ChangeAllocations.h" |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 11 | #include "llvm/Target/TargetData.h" |
| 12 | #include "llvm/DerivedTypes.h" |
| 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/SymbolTable.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 16 | #include "llvm/ConstantVals.h" |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 17 | #include "TransformInternals.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | using std::vector; |
| 19 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 20 | |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 21 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 22 | // module contains a declaration for a malloc and a free function. |
| 23 | // |
| 24 | // This function is always successful. |
| 25 | // |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 26 | bool LowerAllocations::doInitialization(Module *M) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 27 | bool Changed = false; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 28 | const MethodType *MallocType = |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 29 | MethodType::get(PointerType::get(Type::SByteTy), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 30 | vector<const Type*>(1, Type::UIntTy), false); |
| 31 | |
| 32 | SymbolTable *SymTab = M->getSymbolTableSure(); |
| 33 | |
| 34 | // Check for a definition of malloc |
| 35 | if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) { |
| 36 | MallocMeth = cast<Method>(V); // Yup, got it |
| 37 | } else { // Nope, add one |
Chris Lattner | ed4feac | 2001-12-04 18:01:49 +0000 | [diff] [blame] | 38 | M->getMethodList().push_back(MallocMeth = new Method(MallocType, false, |
| 39 | "malloc")); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 40 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | const MethodType *FreeType = |
| 44 | MethodType::get(Type::VoidTy, |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 45 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 46 | false); |
| 47 | |
| 48 | // Check for a definition of free |
| 49 | if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) { |
| 50 | FreeMeth = cast<Method>(V); // Yup, got it |
| 51 | } else { // Nope, add one |
Chris Lattner | ed4feac | 2001-12-04 18:01:49 +0000 | [diff] [blame] | 52 | M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free")); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 53 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 56 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 59 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 60 | // instructions over, assuming that the pass has already been initialized. |
| 61 | // |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 62 | bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 63 | bool Changed = false; |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 64 | assert(MallocMeth && FreeMeth && BB && "Pass not initialized!"); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 65 | |
| 66 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 67 | for (unsigned i = 0; i < BB->size(); ++i) { |
| 68 | BasicBlock::InstListType &BBIL = BB->getInstList(); |
| 69 | if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) { |
| 70 | BBIL.remove(BBIL.begin()+i); // remove the malloc instr... |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 72 | const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType(); |
| 73 | |
| 74 | // Get the number of bytes to be allocated for one element of the |
| 75 | // requested type... |
| 76 | unsigned Size = DataLayout.getTypeSize(AllocTy); |
| 77 | |
| 78 | // malloc(type) becomes sbyte *malloc(constint) |
| 79 | Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); |
| 80 | if (MI->getNumOperands() && Size == 1) { |
| 81 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 82 | } else if (MI->getNumOperands()) { |
| 83 | // Multiply it by the array size if neccesary... |
| 84 | MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0), |
| 85 | MallocArg); |
| 86 | BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg)); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 88 | |
| 89 | // Create the call to Malloc... |
| 90 | CallInst *MCall = new CallInst(MallocMeth, |
| 91 | vector<Value*>(1, MallocArg)); |
| 92 | BBIL.insert(BBIL.begin()+i, MCall); |
| 93 | |
| 94 | // Create a cast instruction to convert to the right type... |
| 95 | CastInst *MCast = new CastInst(MCall, MI->getType()); |
| 96 | BBIL.insert(BBIL.begin()+i+1, MCast); |
| 97 | |
| 98 | // Replace all uses of the old malloc inst with the cast inst |
| 99 | MI->replaceAllUsesWith(MCast); |
| 100 | delete MI; // Delete the malloc inst |
| 101 | Changed = true; |
| 102 | } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) { |
| 103 | BBIL.remove(BB->getInstList().begin()+i); |
| 104 | |
| 105 | // Cast the argument to free into a ubyte*... |
| 106 | CastInst *MCast = new CastInst(FI->getOperand(0), |
| 107 | PointerType::get(Type::UByteTy)); |
| 108 | BBIL.insert(BBIL.begin()+i, MCast); |
| 109 | |
| 110 | // Insert a call to the free function... |
| 111 | CallInst *FCall = new CallInst(FreeMeth, |
| 112 | vector<Value*>(1, MCast)); |
| 113 | BBIL.insert(BBIL.begin()+i+1, FCall); |
| 114 | |
| 115 | // Delete the old free instruction |
| 116 | delete FI; |
| 117 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 121 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 124 | bool RaiseAllocations::doInitialization(Module *M) { |
| 125 | SymbolTable *ST = M->getSymbolTable(); |
| 126 | if (!ST) return false; |
| 127 | |
| 128 | // If the module has a symbol table, they might be referring to the malloc |
| 129 | // and free functions. If this is the case, grab the method pointers that |
| 130 | // the module is using. |
| 131 | // |
| 132 | // Lookup %malloc and %free in the symbol table, for later use. If they |
| 133 | // don't exist, or are not external, we do not worry about converting calls |
| 134 | // to that function into the appropriate instruction. |
| 135 | // |
| 136 | const PointerType *MallocType = // Get the type for malloc |
| 137 | PointerType::get(MethodType::get(PointerType::get(Type::SByteTy), |
| 138 | vector<const Type*>(1, Type::UIntTy), false)); |
| 139 | MallocMeth = cast_or_null<Method>(ST->lookup(MallocType, "malloc")); |
| 140 | if (MallocMeth && !MallocMeth->isExternal()) |
| 141 | MallocMeth = 0; // Don't mess with locally defined versions of the fn |
| 142 | |
| 143 | const PointerType *FreeType = // Get the type for free |
| 144 | PointerType::get(MethodType::get(Type::VoidTy, |
| 145 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), false)); |
| 146 | FreeMeth = cast_or_null<Method>(ST->lookup(FreeType, "free")); |
| 147 | if (FreeMeth && !FreeMeth->isExternal()) |
| 148 | FreeMeth = 0; // Don't mess with locally defined versions of the fn |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // doOneCleanupPass - Do one pass over the input method, fixing stuff up. |
| 154 | // |
| 155 | bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) { |
| 156 | bool Changed = false; |
| 157 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 158 | |
| 159 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
| 160 | Instruction *I = *BI; |
| 161 | |
| 162 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 163 | if (CI->getCalledValue() == MallocMeth) { // Replace call to malloc? |
| 164 | const Type *PtrSByte = PointerType::get(Type::SByteTy); |
| 165 | MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1), |
| 166 | CI->getName()); |
| 167 | CI->setName(""); |
| 168 | BI = BIL.insert(BI, MallocI)+1; |
| 169 | ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte)); |
| 170 | Changed = true; |
| 171 | continue; // Skip the ++BI |
| 172 | } else if (CI->getCalledValue() == FreeMeth) { // Replace call to free? |
| 173 | ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1))); |
| 174 | Changed = true; |
| 175 | continue; // Skip the ++BI |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ++BI; |
| 180 | } |
| 181 | |
| 182 | return Changed; |
| 183 | } |