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