Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 1 | //===- LowerAllocations.cpp - Remove Malloc & Free Instructions -------------=// |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements a pass that lowers malloc and free instructions to |
| 4 | // calls to %malloc & %free functions. This transformation is a target |
| 5 | // dependant tranformation because we depend on the size of data types and |
| 6 | // alignment constraints. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Transforms/LowerAllocations.h" |
| 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 | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 17 | using std::vector; |
| 18 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 19 | |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 20 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 21 | // module contains a declaration for a malloc and a free function. |
| 22 | // |
| 23 | // This function is always successful. |
| 24 | // |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 25 | bool LowerAllocations::doInitialization(Module *M) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 26 | bool Changed = false; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 27 | const MethodType *MallocType = |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 28 | MethodType::get(PointerType::get(Type::SByteTy), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 29 | vector<const Type*>(1, Type::UIntTy), false); |
| 30 | |
| 31 | SymbolTable *SymTab = M->getSymbolTableSure(); |
| 32 | |
| 33 | // Check for a definition of malloc |
| 34 | if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) { |
| 35 | MallocMeth = cast<Method>(V); // Yup, got it |
| 36 | } else { // Nope, add one |
Chris Lattner | ed4feac | 2001-12-04 18:01:49 +0000 | [diff] [blame] | 37 | M->getMethodList().push_back(MallocMeth = new Method(MallocType, false, |
| 38 | "malloc")); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 39 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | const MethodType *FreeType = |
| 43 | MethodType::get(Type::VoidTy, |
Chris Lattner | 7ef6559 | 2001-10-31 06:36:23 +0000 | [diff] [blame] | 44 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 45 | false); |
| 46 | |
| 47 | // Check for a definition of free |
| 48 | if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) { |
| 49 | FreeMeth = cast<Method>(V); // Yup, got it |
| 50 | } else { // Nope, add one |
Chris Lattner | ed4feac | 2001-12-04 18:01:49 +0000 | [diff] [blame] | 51 | M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free")); |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 52 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 55 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 58 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 59 | // instructions over, assuming that the pass has already been initialized. |
| 60 | // |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 61 | bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 62 | bool Changed = false; |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 63 | assert(MallocMeth && FreeMeth && BB && "Pass not initialized!"); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 64 | |
| 65 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 66 | for (unsigned i = 0; i < BB->size(); ++i) { |
| 67 | BasicBlock::InstListType &BBIL = BB->getInstList(); |
| 68 | if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) { |
| 69 | BBIL.remove(BBIL.begin()+i); // remove the malloc instr... |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 71 | const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType(); |
| 72 | |
| 73 | // Get the number of bytes to be allocated for one element of the |
| 74 | // requested type... |
| 75 | unsigned Size = DataLayout.getTypeSize(AllocTy); |
| 76 | |
| 77 | // malloc(type) becomes sbyte *malloc(constint) |
| 78 | Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); |
| 79 | if (MI->getNumOperands() && Size == 1) { |
| 80 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 81 | } else if (MI->getNumOperands()) { |
| 82 | // Multiply it by the array size if neccesary... |
| 83 | MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0), |
| 84 | MallocArg); |
| 85 | BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg)); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame^] | 87 | |
| 88 | // Create the call to Malloc... |
| 89 | CallInst *MCall = new CallInst(MallocMeth, |
| 90 | vector<Value*>(1, MallocArg)); |
| 91 | BBIL.insert(BBIL.begin()+i, MCall); |
| 92 | |
| 93 | // Create a cast instruction to convert to the right type... |
| 94 | CastInst *MCast = new CastInst(MCall, MI->getType()); |
| 95 | BBIL.insert(BBIL.begin()+i+1, MCast); |
| 96 | |
| 97 | // Replace all uses of the old malloc inst with the cast inst |
| 98 | MI->replaceAllUsesWith(MCast); |
| 99 | delete MI; // Delete the malloc inst |
| 100 | Changed = true; |
| 101 | } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) { |
| 102 | BBIL.remove(BB->getInstList().begin()+i); |
| 103 | |
| 104 | // Cast the argument to free into a ubyte*... |
| 105 | CastInst *MCast = new CastInst(FI->getOperand(0), |
| 106 | PointerType::get(Type::UByteTy)); |
| 107 | BBIL.insert(BBIL.begin()+i, MCast); |
| 108 | |
| 109 | // Insert a call to the free function... |
| 110 | CallInst *FCall = new CallInst(FreeMeth, |
| 111 | vector<Value*>(1, MCast)); |
| 112 | BBIL.insert(BBIL.begin()+i+1, FCall); |
| 113 | |
| 114 | // Delete the old free instruction |
| 115 | delete FI; |
| 116 | Changed = true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 120 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |