Chris Lattner | ade686e | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 1 | //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===// |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | ade686e | 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 | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Chris Lattner | e9ee3e5 | 2002-07-23 22:04:17 +0000 | [diff] [blame] | 8 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 9 | #include "llvm/Module.h" |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
| 11 | #include "llvm/iMemory.h" |
| 12 | #include "llvm/iOther.h" |
Chris Lattner | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 13 | #include "llvm/Constants.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 14 | #include "llvm/Pass.h" |
Chris Lattner | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetData.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 16 | #include "Support/Statistic.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | using std::vector; |
| 19 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 20 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 21 | Statistic<> NumLowered("lowerallocs", "Number of allocations lowered"); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 23 | /// LowerAllocations - Turn malloc and free instructions into %malloc and |
| 24 | /// %free calls. |
| 25 | /// |
| 26 | class LowerAllocations : public BasicBlockPass { |
| 27 | Function *MallocFunc; // Functions in the module we are processing |
| 28 | Function *FreeFunc; // Initialized by doInitialization |
| 29 | public: |
| 30 | LowerAllocations() : MallocFunc(0), FreeFunc(0) {} |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 32 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 33 | AU.addRequired<TargetData>(); |
| 34 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 36 | /// doPassInitialization - For the lower allocations pass, this ensures that |
| 37 | /// a 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 | }; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 47 | RegisterOpt<LowerAllocations> |
| 48 | X("lowerallocs", "Lower allocations from instructions to calls"); |
Chris Lattner | ade686e | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 49 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 50 | |
Chris Lattner | ade686e | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 51 | // createLowerAllocationsPass - Interface to this file... |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 52 | Pass *createLowerAllocationsPass() { |
| 53 | return new LowerAllocations(); |
Chris Lattner | ade686e | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 56 | |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 57 | // doInitialization - For the lower allocations pass, this ensures that a |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 58 | // module contains a declaration for a malloc and a free function. |
| 59 | // |
| 60 | // This function is always successful. |
| 61 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 62 | bool LowerAllocations::doInitialization(Module &M) { |
Chris Lattner | be591a7 | 2002-03-29 03:38:05 +0000 | [diff] [blame] | 63 | const FunctionType *MallocType = |
| 64 | FunctionType::get(PointerType::get(Type::SByteTy), |
| 65 | vector<const Type*>(1, Type::UIntTy), false); |
| 66 | const FunctionType *FreeType = |
| 67 | FunctionType::get(Type::VoidTy, |
| 68 | vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
| 69 | false); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 71 | MallocFunc = M.getOrInsertFunction("malloc", MallocType); |
| 72 | FreeFunc = M.getOrInsertFunction("free" , FreeType); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 73 | |
Chris Lattner | ade686e | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 74 | return true; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 77 | // runOnBasicBlock - This method does the actual work of converting |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 78 | // instructions over, assuming that the pass has already been initialized. |
| 79 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 80 | bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 81 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 82 | assert(MallocFunc && FreeFunc && "Pass not initialized!"); |
| 83 | |
| 84 | BasicBlock::InstListType &BBIL = BB.getInstList(); |
Chris Lattner | 3b2106f | 2002-09-25 23:47:47 +0000 | [diff] [blame] | 85 | TargetData &DataLayout = getAnalysis<TargetData>(); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 86 | |
| 87 | // Loop over all of the instructions, looking for malloc or free instructions |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 88 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
| 89 | if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 90 | const Type *AllocTy = MI->getType()->getElementType(); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 91 | |
| 92 | // Get the number of bytes to be allocated for one element of the |
| 93 | // requested type... |
| 94 | unsigned Size = DataLayout.getTypeSize(AllocTy); |
| 95 | |
| 96 | // malloc(type) becomes sbyte *malloc(constint) |
| 97 | Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size); |
| 98 | if (MI->getNumOperands() && Size == 1) { |
| 99 | MallocArg = MI->getOperand(0); // Operand * 1 = Operand |
| 100 | } else if (MI->getNumOperands()) { |
| 101 | // Multiply it by the array size if neccesary... |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 102 | MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0), |
| 103 | MallocArg, "", I); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 105 | |
| 106 | // Create the call to Malloc... |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 107 | CallInst *MCall = new CallInst(MallocFunc, |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 108 | vector<Value*>(1, MallocArg), "", I); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 109 | |
| 110 | // Create a cast instruction to convert to the right type... |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 111 | CastInst *MCast = new CastInst(MCall, MI->getType(), "", I); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 112 | |
| 113 | // Replace all uses of the old malloc inst with the cast inst |
| 114 | MI->replaceAllUsesWith(MCast); |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 115 | I = --BBIL.erase(I); // remove and delete the malloc instr... |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 116 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 117 | ++NumLowered; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 118 | } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) { |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 119 | // Cast the argument to free into a ubyte*... |
| 120 | CastInst *MCast = new CastInst(FI->getOperand(0), |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 121 | PointerType::get(Type::UByteTy), "", I); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 122 | |
| 123 | // Insert a call to the free function... |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 124 | CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast), "", I); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 125 | |
| 126 | // Delete the old free instruction |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 127 | I = --BBIL.erase(I); |
Chris Lattner | 8445372 | 2002-01-21 23:34:02 +0000 | [diff] [blame] | 128 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 129 | ++NumLowered; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 133 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 134 | } |