Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 1 | //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===// |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 3 | // The LowerAllocations transformation is a target dependant tranformation |
| 4 | // because it depends on the size of data types and alignment constraints. |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Chris Lattner | 3787ee6 | 2002-01-22 01:04:08 +0000 | [diff] [blame] | 8 | #include "llvm/Transforms/ChangeAllocations.h" |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 9 | #include "llvm/Module.h" |
Chris Lattner | 06be180 | 2002-04-09 19:08:28 +0000 | [diff] [blame] | 10 | #include "llvm/Function.h" |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 11 | #include "llvm/BasicBlock.h" |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 12 | #include "llvm/DerivedTypes.h" |
| 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iOther.h" |
Chris Lattner | 9b55e5a | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chris Lattner | 9b55e5a | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | using std::vector; |
| 19 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // LowerAllocations - Turn malloc and free instructions into %malloc and %free |
| 23 | // calls. |
| 24 | // |
| 25 | class LowerAllocations : public BasicBlockPass { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 26 | Function *MallocFunc; // Functions in the module we are processing |
| 27 | Function *FreeFunc; // Initialized by doInitialization |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 28 | |
| 29 | const TargetData &DataLayout; |
| 30 | public: |
| 31 | inline LowerAllocations(const TargetData &TD) : DataLayout(TD) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 32 | MallocFunc = FreeFunc = 0; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 35 | const char *getPassName() const { return "Lower Allocations"; } |
| 36 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 48 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 50 | // createLowerAllocationsPass - Interface to this file... |
| 51 | Pass *createLowerAllocationsPass(const TargetData &TD) { |
| 52 | return new LowerAllocations(TD); |
| 53 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0686e43 | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 56 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 57 | // module contains a declaration for a malloc and a free function. |
| 58 | // |
| 59 | // This function is always successful. |
| 60 | // |
Chris Lattner | 0686e43 | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 61 | bool LowerAllocations::doInitialization(Module *M) { |
Chris Lattner | c13563d | 2002-03-29 03:38:05 +0000 | [diff] [blame] | 62 | const FunctionType *MallocType = |
| 63 | FunctionType::get(PointerType::get(Type::SByteTy), |
| 64 | vector<const Type*>(1, Type::UIntTy), false); |
| 65 | const FunctionType *FreeType = |
| 66 | FunctionType::get(Type::VoidTy, |
| 67 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
| 68 | false); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 69 | |
Chris Lattner | c13563d | 2002-03-29 03:38:05 +0000 | [diff] [blame] | 70 | MallocFunc = M->getOrInsertFunction("malloc", MallocType); |
| 71 | FreeFunc = M->getOrInsertFunction("free" , FreeType); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 73 | return true; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 76 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 77 | // instructions over, assuming that the pass has already been initialized. |
| 78 | // |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 79 | bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { |
Chris Lattner | 6fea032 | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 80 | bool Changed = false; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 81 | assert(MallocFunc && FreeFunc && BB && "Pass not initialized!"); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 82 | |
| 83 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 84 | for (unsigned i = 0; i < BB->size(); ++i) { |
| 85 | BasicBlock::InstListType &BBIL = BB->getInstList(); |
| 86 | if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) { |
| 87 | BBIL.remove(BBIL.begin()+i); // remove the malloc instr... |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 9b55e5a | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 89 | const Type *AllocTy = cast<PointerType>(MI->getType())->getElementType(); |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 90 | |
| 91 | // Get the number of bytes to be allocated for one element of the |
| 92 | // requested type... |
| 93 | unsigned Size = DataLayout.getTypeSize(AllocTy); |
| 94 | |
| 95 | // malloc(type) becomes sbyte *malloc(constint) |
| 96 | Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); |
| 97 | if (MI->getNumOperands() && Size == 1) { |
| 98 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 99 | } else if (MI->getNumOperands()) { |
| 100 | // Multiply it by the array size if neccesary... |
| 101 | MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0), |
| 102 | MallocArg); |
| 103 | BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg)); |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 105 | |
| 106 | // Create the call to Malloc... |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 107 | CallInst *MCall = new CallInst(MallocFunc, |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 108 | vector<Value*>(1, MallocArg)); |
| 109 | BBIL.insert(BBIL.begin()+i, MCall); |
| 110 | |
| 111 | // Create a cast instruction to convert to the right type... |
| 112 | CastInst *MCast = new CastInst(MCall, MI->getType()); |
| 113 | BBIL.insert(BBIL.begin()+i+1, MCast); |
| 114 | |
| 115 | // Replace all uses of the old malloc inst with the cast inst |
| 116 | MI->replaceAllUsesWith(MCast); |
| 117 | delete MI; // Delete the malloc inst |
| 118 | Changed = true; |
| 119 | } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) { |
| 120 | BBIL.remove(BB->getInstList().begin()+i); |
| 121 | |
| 122 | // Cast the argument to free into a ubyte*... |
| 123 | CastInst *MCast = new CastInst(FI->getOperand(0), |
| 124 | PointerType::get(Type::UByteTy)); |
| 125 | BBIL.insert(BBIL.begin()+i, MCast); |
| 126 | |
| 127 | // Insert a call to the free function... |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 128 | CallInst *FCall = new CallInst(FreeFunc, |
Chris Lattner | d07471d | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 129 | vector<Value*>(1, MCast)); |
| 130 | BBIL.insert(BBIL.begin()+i+1, FCall); |
| 131 | |
| 132 | // Delete the old free instruction |
| 133 | delete FI; |
| 134 | Changed = true; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Chris Lattner | 6fea032 | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 138 | return Changed; |
Chris Lattner | 1bb5f8e | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 139 | } |