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" |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
| 14 | #include "llvm/iMemory.h" |
| 15 | #include "llvm/iOther.h" |
| 16 | #include "llvm/SymbolTable.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 17 | #include "llvm/ConstantVals.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 19 | #include "TransformInternals.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 20 | using std::vector; |
| 21 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | // LowerAllocations - Turn malloc and free instructions into %malloc and %free |
| 25 | // calls. |
| 26 | // |
| 27 | class LowerAllocations : public BasicBlockPass { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 28 | Function *MallocFunc; // Functions in the module we are processing |
| 29 | Function *FreeFunc; // Initialized by doInitialization |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 30 | |
| 31 | const TargetData &DataLayout; |
| 32 | public: |
| 33 | inline LowerAllocations(const TargetData &TD) : DataLayout(TD) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 34 | MallocFunc = FreeFunc = 0; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // doPassInitialization - For the lower allocations pass, this ensures that a |
| 38 | // 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 | }; |
| 47 | |
| 48 | // RaiseAllocations - Turn %malloc and %free calls into the appropriate |
| 49 | // instruction. |
| 50 | // |
| 51 | class RaiseAllocations : public BasicBlockPass { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 52 | Function *MallocFunc; // Functions in the module we are processing |
| 53 | Function *FreeFunc; // Initialized by doPassInitializationVirt |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 54 | public: |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 55 | inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {} |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 56 | |
| 57 | // doPassInitialization - For the raise allocations pass, this finds a |
| 58 | // declaration for malloc and free if they exist. |
| 59 | // |
| 60 | bool doInitialization(Module *M); |
| 61 | |
| 62 | // runOnBasicBlock - This method does the actual work of converting |
| 63 | // instructions over, assuming that the pass has already been initialized. |
| 64 | // |
| 65 | bool runOnBasicBlock(BasicBlock *BB); |
| 66 | }; |
| 67 | |
| 68 | } // end anonymous namespace |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 69 | |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 70 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 71 | // module contains a declaration for a malloc and a free function. |
| 72 | // |
| 73 | // This function is always successful. |
| 74 | // |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 75 | bool LowerAllocations::doInitialization(Module *M) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 76 | bool Changed = false; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 77 | const MethodType *MallocType = |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 78 | MethodType::get(PointerType::get(Type::SByteTy), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 79 | vector<const Type*>(1, Type::UIntTy), false); |
| 80 | |
| 81 | SymbolTable *SymTab = M->getSymbolTableSure(); |
| 82 | |
| 83 | // Check for a definition of malloc |
| 84 | if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 85 | MallocFunc = cast<Function>(V); // Yup, got it |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 86 | } else { // Nope, add one |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 87 | M->getFunctionList().push_back(MallocFunc = new Function(MallocType, false, |
| 88 | "malloc")); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 89 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const MethodType *FreeType = |
| 93 | MethodType::get(Type::VoidTy, |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 94 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 95 | false); |
| 96 | |
| 97 | // Check for a definition of free |
| 98 | if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 99 | FreeFunc = cast<Function>(V); // Yup, got it |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 100 | } else { // Nope, add one |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 101 | FreeFunc = new Function(FreeType, false,"free"); |
| 102 | M->getFunctionList().push_back(FreeFunc); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 103 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 106 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 109 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 110 | // instructions over, assuming that the pass has already been initialized. |
| 111 | // |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 112 | bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 113 | bool Changed = false; |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 114 | assert(MallocFunc && FreeFunc && BB && "Pass not initialized!"); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 115 | |
| 116 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 117 | for (unsigned i = 0; i < BB->size(); ++i) { |
| 118 | BasicBlock::InstListType &BBIL = BB->getInstList(); |
| 119 | if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) { |
| 120 | BBIL.remove(BBIL.begin()+i); // remove the malloc instr... |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 122 | const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType(); |
| 123 | |
| 124 | // Get the number of bytes to be allocated for one element of the |
| 125 | // requested type... |
| 126 | unsigned Size = DataLayout.getTypeSize(AllocTy); |
| 127 | |
| 128 | // malloc(type) becomes sbyte *malloc(constint) |
| 129 | Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); |
| 130 | if (MI->getNumOperands() && Size == 1) { |
| 131 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 132 | } else if (MI->getNumOperands()) { |
| 133 | // Multiply it by the array size if neccesary... |
| 134 | MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0), |
| 135 | MallocArg); |
| 136 | BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg)); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 138 | |
| 139 | // Create the call to Malloc... |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 140 | CallInst *MCall = new CallInst(MallocFunc, |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 141 | vector<Value*>(1, MallocArg)); |
| 142 | BBIL.insert(BBIL.begin()+i, MCall); |
| 143 | |
| 144 | // Create a cast instruction to convert to the right type... |
| 145 | CastInst *MCast = new CastInst(MCall, MI->getType()); |
| 146 | BBIL.insert(BBIL.begin()+i+1, MCast); |
| 147 | |
| 148 | // Replace all uses of the old malloc inst with the cast inst |
| 149 | MI->replaceAllUsesWith(MCast); |
| 150 | delete MI; // Delete the malloc inst |
| 151 | Changed = true; |
| 152 | } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) { |
| 153 | BBIL.remove(BB->getInstList().begin()+i); |
| 154 | |
| 155 | // Cast the argument to free into a ubyte*... |
| 156 | CastInst *MCast = new CastInst(FI->getOperand(0), |
| 157 | PointerType::get(Type::UByteTy)); |
| 158 | BBIL.insert(BBIL.begin()+i, MCast); |
| 159 | |
| 160 | // Insert a call to the free function... |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 161 | CallInst *FCall = new CallInst(FreeFunc, |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 162 | vector<Value*>(1, MCast)); |
| 163 | BBIL.insert(BBIL.begin()+i+1, FCall); |
| 164 | |
| 165 | // Delete the old free instruction |
| 166 | delete FI; |
| 167 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 171 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 174 | bool RaiseAllocations::doInitialization(Module *M) { |
| 175 | SymbolTable *ST = M->getSymbolTable(); |
| 176 | if (!ST) return false; |
| 177 | |
| 178 | // If the module has a symbol table, they might be referring to the malloc |
| 179 | // and free functions. If this is the case, grab the method pointers that |
| 180 | // the module is using. |
| 181 | // |
| 182 | // Lookup %malloc and %free in the symbol table, for later use. If they |
| 183 | // don't exist, or are not external, we do not worry about converting calls |
| 184 | // to that function into the appropriate instruction. |
| 185 | // |
| 186 | const PointerType *MallocType = // Get the type for malloc |
| 187 | PointerType::get(MethodType::get(PointerType::get(Type::SByteTy), |
| 188 | vector<const Type*>(1, Type::UIntTy), false)); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 189 | MallocFunc = cast_or_null<Function>(ST->lookup(MallocType, "malloc")); |
| 190 | if (MallocFunc && !MallocFunc->isExternal()) |
| 191 | MallocFunc = 0; // Don't mess with locally defined versions of the fn |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 192 | |
| 193 | const PointerType *FreeType = // Get the type for free |
| 194 | PointerType::get(MethodType::get(Type::VoidTy, |
| 195 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), false)); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 196 | FreeFunc = cast_or_null<Function>(ST->lookup(FreeType, "free")); |
| 197 | if (FreeFunc && !FreeFunc->isExternal()) |
| 198 | FreeFunc = 0; // Don't mess with locally defined versions of the fn |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 199 | |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | // doOneCleanupPass - Do one pass over the input method, fixing stuff up. |
| 204 | // |
| 205 | bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) { |
| 206 | bool Changed = false; |
| 207 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 208 | |
| 209 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
| 210 | Instruction *I = *BI; |
| 211 | |
| 212 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 213 | if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc? |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 214 | const Type *PtrSByte = PointerType::get(Type::SByteTy); |
| 215 | MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1), |
| 216 | CI->getName()); |
| 217 | CI->setName(""); |
Chris Lattner | 31b80d9 | 2002-01-22 03:30:06 +0000 | [diff] [blame] | 218 | ReplaceInstWithInst(BIL, BI, MallocI); |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 219 | Changed = true; |
| 220 | continue; // Skip the ++BI |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame^] | 221 | } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free? |
Chris Lattner | 5048c3b | 2002-01-22 00:13:51 +0000 | [diff] [blame] | 222 | ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1))); |
| 223 | Changed = true; |
| 224 | continue; // Skip the ++BI |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | ++BI; |
| 229 | } |
| 230 | |
| 231 | return Changed; |
| 232 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 233 | |
| 234 | Pass *createLowerAllocationsPass(const TargetData &TD) { |
| 235 | return new LowerAllocations(TD); |
| 236 | } |
| 237 | Pass *createRaiseAllocationsPass() { |
| 238 | return new RaiseAllocations(); |
| 239 | } |
| 240 | |
| 241 | |