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