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