Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame] | 1 | //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame] | 10 | // This file defines the RaiseAllocations pass which convert malloc and free |
| 11 | // calls to malloc and free instructions. |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/iMemory.h" |
Chris Lattner | 39f398b | 2003-09-16 19:42:21 +0000 | [diff] [blame] | 19 | #include "llvm/iTerminators.h" |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 20 | #include "llvm/iOther.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CallSite.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 23 | #include "Support/Statistic.h" |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame^] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 26 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 27 | Statistic<> NumRaised("raiseallocs", "Number of allocations raised"); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 28 | |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 29 | // RaiseAllocations - Turn %malloc and %free calls into the appropriate |
| 30 | // instruction. |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 32 | class RaiseAllocations : public Pass { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 33 | Function *MallocFunc; // Functions in the module we are processing |
| 34 | Function *FreeFunc; // Initialized by doPassInitializationVirt |
| 35 | public: |
| 36 | RaiseAllocations() : MallocFunc(0), FreeFunc(0) {} |
| 37 | |
| 38 | // doPassInitialization - For the raise allocations pass, this finds a |
| 39 | // declaration for malloc and free if they exist. |
| 40 | // |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 41 | void doInitialization(Module &M); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 43 | // run - This method does the actual work of converting instructions over. |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 44 | // |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 45 | bool run(Module &M); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 48 | RegisterOpt<RaiseAllocations> |
| 49 | X("raiseallocs", "Raise allocations from calls to instructions"); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 50 | } // end anonymous namespace |
| 51 | |
| 52 | |
| 53 | // createRaiseAllocationsPass - The interface to this file... |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame^] | 54 | Pass *llvm::createRaiseAllocationsPass() { |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 55 | return new RaiseAllocations(); |
| 56 | } |
| 57 | |
| 58 | |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 59 | // If the module has a symbol table, they might be referring to the malloc and |
| 60 | // free functions. If this is the case, grab the method pointers that the |
| 61 | // module is using. |
| 62 | // |
| 63 | // Lookup %malloc and %free in the symbol table, for later use. If they don't |
| 64 | // exist, or are not external, we do not worry about converting calls to that |
| 65 | // function into the appropriate instruction. |
| 66 | // |
| 67 | void RaiseAllocations::doInitialization(Module &M) { |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 68 | const FunctionType *MallocType = // Get the type for malloc |
| 69 | FunctionType::get(PointerType::get(Type::SByteTy), |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 70 | std::vector<const Type*>(1, Type::ULongTy), false); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 71 | |
| 72 | const FunctionType *FreeType = // Get the type for free |
| 73 | FunctionType::get(Type::VoidTy, |
| 74 | std::vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
| 75 | false); |
| 76 | |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 77 | // Get Malloc and free prototypes if they exist! |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 78 | MallocFunc = M.getFunction("malloc", MallocType); |
| 79 | FreeFunc = M.getFunction("free" , FreeType); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 80 | |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 81 | // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc |
| 82 | // This handles the common declaration of: 'void *malloc(unsigned);' |
| 83 | if (MallocFunc == 0) { |
| 84 | MallocType = FunctionType::get(PointerType::get(Type::SByteTy), |
| 85 | std::vector<const Type*>(1, Type::UIntTy), false); |
| 86 | MallocFunc = M.getFunction("malloc", MallocType); |
| 87 | } |
| 88 | |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 89 | // Check to see if the prototype is missing, giving us sbyte*(...) * malloc |
Chris Lattner | dfe0418 | 2002-07-18 00:18:01 +0000 | [diff] [blame] | 90 | // This handles the common declaration of: 'void *malloc();' |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 91 | if (MallocFunc == 0) { |
| 92 | MallocType = FunctionType::get(PointerType::get(Type::SByteTy), |
| 93 | std::vector<const Type*>(), true); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | MallocFunc = M.getFunction("malloc", MallocType); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Check to see if the prototype was forgotten, giving us void (...) * free |
| 98 | // This handles the common forward declaration of: 'void free();' |
| 99 | if (FreeFunc == 0) { |
| 100 | FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 101 | FreeFunc = M.getFunction("free", FreeType); |
Chris Lattner | e3da298 | 2002-05-24 20:29:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 603e007 | 2003-08-11 15:05:08 +0000 | [diff] [blame] | 104 | // One last try, check to see if we can find free as 'int (...)* free'. This |
| 105 | // handles the case where NOTHING was declared. |
| 106 | if (FreeFunc == 0) { |
| 107 | FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true); |
| 108 | FreeFunc = M.getFunction("free", FreeType); |
| 109 | } |
| 110 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 111 | // Don't mess with locally defined versions of these functions... |
| 112 | if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0; |
| 113 | if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0; |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 116 | // run - Transform calls into instructions... |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 117 | // |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 118 | bool RaiseAllocations::run(Module &M) { |
| 119 | // Find the malloc/free prototypes... |
| 120 | doInitialization(M); |
| 121 | |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 122 | bool Changed = false; |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 124 | // First, process all of the malloc calls... |
| 125 | if (MallocFunc) { |
| 126 | std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end()); |
| 127 | while (!Users.empty()) { |
| 128 | if (Instruction *I = dyn_cast<Instruction>(Users.back())) { |
| 129 | CallSite CS = CallSite::get(I); |
| 130 | if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc && |
| 131 | CS.arg_begin() != CS.arg_end()) { |
| 132 | Value *Source = *CS.arg_begin(); |
| 133 | |
| 134 | // If no prototype was provided for malloc, we may need to cast the |
| 135 | // source size. |
| 136 | if (Source->getType() != Type::UIntTy) |
| 137 | Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I); |
| 138 | |
| 139 | std::string Name(I->getName()); I->setName(""); |
| 140 | MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I); |
| 141 | I->replaceAllUsesWith(MI); |
Chris Lattner | 39f398b | 2003-09-16 19:42:21 +0000 | [diff] [blame] | 142 | |
| 143 | // If the old instruction was an invoke, add an unconditional branch |
| 144 | // before the invoke, which will become the new terminator. |
| 145 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 146 | new BranchInst(II->getNormalDest(), I); |
| 147 | |
| 148 | // Delete the old call site |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 149 | MI->getParent()->getInstList().erase(I); |
| 150 | Changed = true; |
| 151 | ++NumRaised; |
| 152 | } |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 154 | |
| 155 | Users.pop_back(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Next, process all free calls... |
| 160 | if (FreeFunc) { |
| 161 | std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end()); |
| 162 | |
| 163 | while (!Users.empty()) { |
| 164 | if (Instruction *I = dyn_cast<Instruction>(Users.back())) { |
| 165 | CallSite CS = CallSite::get(I); |
| 166 | if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc && |
| 167 | CS.arg_begin() != CS.arg_end()) { |
| 168 | |
| 169 | // If no prototype was provided for free, we may need to cast the |
| 170 | // source pointer. This should be really uncommon, but it's necessary |
| 171 | // just in case we are dealing with wierd code like this: |
| 172 | // free((long)ptr); |
| 173 | // |
| 174 | Value *Source = *CS.arg_begin(); |
| 175 | if (!isa<PointerType>(Source->getType())) |
| 176 | Source = new CastInst(Source, PointerType::get(Type::SByteTy), |
| 177 | "FreePtrCast", I); |
| 178 | new FreeInst(Source, I); |
Chris Lattner | 39f398b | 2003-09-16 19:42:21 +0000 | [diff] [blame] | 179 | |
| 180 | // If the old instruction was an invoke, add an unconditional branch |
| 181 | // before the invoke, which will become the new terminator. |
| 182 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 183 | new BranchInst(II->getNormalDest(), I); |
| 184 | |
| 185 | // Delete the old call site |
Chris Lattner | 8ef1b88 | 2003-09-01 03:14:56 +0000 | [diff] [blame] | 186 | I->getParent()->getInstList().erase(I); |
| 187 | Changed = true; |
| 188 | ++NumRaised; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | Users.pop_back(); |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return Changed; |
| 197 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 198 | |