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