Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame] | 1 | //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===// |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame] | 3 | // This file defines the RaiseAllocations pass which convert malloc and free |
| 4 | // calls to malloc and free instructions. |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Chris Lattner | 42706e4 | 2002-07-23 22:04:17 +0000 | [diff] [blame] | 8 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 10 | #include "llvm/Module.h" |
| 11 | #include "llvm/Function.h" |
| 12 | #include "llvm/DerivedTypes.h" |
| 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/Pass.h" |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 16 | #include "Support/StatisticReporter.h" |
| 17 | |
| 18 | static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised"); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // RaiseAllocations - Turn %malloc and %free calls into the appropriate |
| 23 | // instruction. |
| 24 | // |
| 25 | class RaiseAllocations : public BasicBlockPass { |
| 26 | Function *MallocFunc; // Functions in the module we are processing |
| 27 | Function *FreeFunc; // Initialized by doPassInitializationVirt |
| 28 | public: |
Chris Lattner | 6788f25 | 2002-07-23 18:06:30 +0000 | [diff] [blame] | 29 | RaiseAllocations() : MallocFunc(0), FreeFunc(0) {} |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 30 | |
| 31 | // doPassInitialization - For the raise allocations pass, this finds a |
| 32 | // declaration for malloc and free if they exist. |
| 33 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 34 | bool doInitialization(Module &M); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 35 | |
| 36 | // runOnBasicBlock - This method does the actual work of converting |
| 37 | // instructions over, assuming that the pass has already been initialized. |
| 38 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | bool runOnBasicBlock(BasicBlock &BB); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame^] | 42 | RegisterOpt<RaiseAllocations> |
| 43 | X("raiseallocs", "Raise allocations from calls to instructions"); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 44 | } // end anonymous namespace |
| 45 | |
| 46 | |
| 47 | // createRaiseAllocationsPass - The interface to this file... |
| 48 | Pass *createRaiseAllocationsPass() { |
| 49 | return new RaiseAllocations(); |
| 50 | } |
| 51 | |
| 52 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 53 | bool RaiseAllocations::doInitialization(Module &M) { |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 54 | // If the module has a symbol table, they might be referring to the malloc |
| 55 | // and free functions. If this is the case, grab the method pointers that |
| 56 | // the module is using. |
| 57 | // |
| 58 | // Lookup %malloc and %free in the symbol table, for later use. If they |
| 59 | // don't exist, or are not external, we do not worry about converting calls |
| 60 | // to that function into the appropriate instruction. |
| 61 | // |
| 62 | const FunctionType *MallocType = // Get the type for malloc |
| 63 | FunctionType::get(PointerType::get(Type::SByteTy), |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 64 | std::vector<const Type*>(1, Type::ULongTy), false); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 65 | |
| 66 | const FunctionType *FreeType = // Get the type for free |
| 67 | FunctionType::get(Type::VoidTy, |
| 68 | std::vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
| 69 | false); |
| 70 | |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 71 | // Get Malloc and free prototypes if they exist! |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 72 | MallocFunc = M.getFunction("malloc", MallocType); |
| 73 | FreeFunc = M.getFunction("free" , FreeType); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 74 | |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 75 | // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc |
| 76 | // This handles the common declaration of: 'void *malloc(unsigned);' |
| 77 | if (MallocFunc == 0) { |
| 78 | MallocType = FunctionType::get(PointerType::get(Type::SByteTy), |
| 79 | std::vector<const Type*>(1, Type::UIntTy), false); |
| 80 | MallocFunc = M.getFunction("malloc", MallocType); |
| 81 | } |
| 82 | |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 83 | // Check to see if the prototype is missing, giving us sbyte*(...) * malloc |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 84 | // This handles the common declaration of: 'void *malloc();' |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 85 | if (MallocFunc == 0) { |
| 86 | MallocType = FunctionType::get(PointerType::get(Type::SByteTy), |
| 87 | std::vector<const Type*>(), true); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 88 | MallocFunc = M.getFunction("malloc", MallocType); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Check to see if the prototype was forgotten, giving us void (...) * free |
| 92 | // This handles the common forward declaration of: 'void free();' |
| 93 | if (FreeFunc == 0) { |
| 94 | FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 95 | FreeFunc = M.getFunction("free", FreeType); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 99 | // Don't mess with locally defined versions of these functions... |
| 100 | if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0; |
| 101 | if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0; |
| 102 | return false; |
| 103 | } |
| 104 | |
Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame] | 105 | // runOnBasicBlock - Process a basic block, fixing it up... |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 106 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 107 | bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) { |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 108 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 109 | BasicBlock::InstListType &BIL = BB.getInstList(); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 111 | for (BasicBlock::iterator BI = BB.begin(); BI != BB.end();) { |
| 112 | Instruction *I = BI; |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 113 | |
| 114 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 115 | if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc? |
| 116 | const Type *PtrSByte = PointerType::get(Type::SByteTy); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 117 | Value *Source = CI->getOperand(1); |
| 118 | |
| 119 | // If no prototype was provided for malloc, we may need to cast the |
| 120 | // source size. |
| 121 | if (Source->getType() != Type::UIntTy) { |
| 122 | CastInst *New = new CastInst(Source, Type::UIntTy, "MallocAmtCast"); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 123 | BI = ++BIL.insert(BI, New); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 124 | Source = New; |
| 125 | } |
| 126 | |
| 127 | MallocInst *MallocI = new MallocInst(PtrSByte, Source, CI->getName()); |
| 128 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 129 | CI->setName(""); |
| 130 | ReplaceInstWithInst(BIL, BI, MallocI); |
| 131 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 132 | ++NumRaised; |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 133 | continue; // Skip the ++BI |
| 134 | } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free? |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 135 | // If no prototype was provided for free, we may need to cast the |
| 136 | // source pointer. This should be really uncommon, but it's neccesary |
| 137 | // just in case we are dealing with wierd code like this: |
| 138 | // free((long)ptr); |
| 139 | // |
| 140 | Value *Source = CI->getOperand(1); |
| 141 | if (!isa<PointerType>(Source->getType())) { |
| 142 | CastInst *New = new CastInst(Source, PointerType::get(Type::SByteTy), |
| 143 | "FreePtrCast"); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 144 | BI = ++BIL.insert(BI, New); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 145 | Source = New; |
| 146 | } |
| 147 | |
| 148 | ReplaceInstWithInst(BIL, BI, new FreeInst(Source)); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 149 | Changed = true; |
| 150 | continue; // Skip the ++BI |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 151 | ++NumRaised; |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | ++BI; |
| 156 | } |
| 157 | |
| 158 | return Changed; |
| 159 | } |