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