Dan Gohman | 2e4337e | 2007-08-27 16:11:48 +0000 | [diff] [blame] | 1 | //===- RaiseAllocations.cpp - Convert @malloc & @free calls to insts ------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the RaiseAllocations pass which convert malloc and free |
| 11 | // calls to malloc and free instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "raiseallocs" |
| 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | #include "llvm/Support/CallSite.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include <algorithm> |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumRaised, "Number of allocations raised"); |
| 29 | |
| 30 | namespace { |
Dan Gohman | 2e4337e | 2007-08-27 16:11:48 +0000 | [diff] [blame] | 31 | // RaiseAllocations - Turn @malloc and @free calls into the appropriate |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | // instruction. |
| 33 | // |
| 34 | class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass { |
| 35 | Function *MallocFunc; // Functions in the module we are processing |
| 36 | Function *FreeFunc; // Initialized by doPassInitializationVirt |
| 37 | public: |
| 38 | static char ID; // Pass identification, replacement for typeid |
| 39 | RaiseAllocations() |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 40 | : ModulePass(&ID), MallocFunc(0), FreeFunc(0) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | |
| 42 | // doPassInitialization - For the raise allocations pass, this finds a |
| 43 | // declaration for malloc and free if they exist. |
| 44 | // |
| 45 | void doInitialization(Module &M); |
| 46 | |
| 47 | // run - This method does the actual work of converting instructions over. |
| 48 | // |
| 49 | bool runOnModule(Module &M); |
| 50 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | } // end anonymous namespace |
| 52 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 53 | char RaiseAllocations::ID = 0; |
| 54 | static RegisterPass<RaiseAllocations> |
| 55 | X("raiseallocs", "Raise allocations from calls to instructions"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | |
| 57 | // createRaiseAllocationsPass - The interface to this file... |
| 58 | ModulePass *llvm::createRaiseAllocationsPass() { |
| 59 | return new RaiseAllocations(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | // If the module has a symbol table, they might be referring to the malloc and |
| 64 | // free functions. If this is the case, grab the method pointers that the |
| 65 | // module is using. |
| 66 | // |
Dan Gohman | 2e4337e | 2007-08-27 16:11:48 +0000 | [diff] [blame] | 67 | // Lookup @malloc and @free in the symbol table, for later use. If they don't |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | // exist, or are not external, we do not worry about converting calls to that |
| 69 | // function into the appropriate instruction. |
| 70 | // |
| 71 | void RaiseAllocations::doInitialization(Module &M) { |
| 72 | |
| 73 | // Get Malloc and free prototypes if they exist! |
| 74 | MallocFunc = M.getFunction("malloc"); |
| 75 | if (MallocFunc) { |
| 76 | const FunctionType* TyWeHave = MallocFunc->getFunctionType(); |
| 77 | |
| 78 | // Get the expected prototype for malloc |
| 79 | const FunctionType *Malloc1Type = |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 80 | FunctionType::get(PointerType::getUnqual(Type::Int8Ty), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | std::vector<const Type*>(1, Type::Int64Ty), false); |
| 82 | |
| 83 | // Chck to see if we got the expected malloc |
| 84 | if (TyWeHave != Malloc1Type) { |
| 85 | // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc |
| 86 | // This handles the common declaration of: 'void *malloc(unsigned);' |
| 87 | const FunctionType *Malloc2Type = |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 88 | FunctionType::get(PointerType::getUnqual(Type::Int8Ty), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | std::vector<const Type*>(1, Type::Int32Ty), false); |
| 90 | if (TyWeHave != Malloc2Type) { |
| 91 | // Check to see if the prototype is missing, giving us |
| 92 | // sbyte*(...) * malloc |
| 93 | // This handles the common declaration of: 'void *malloc();' |
| 94 | const FunctionType *Malloc3Type = |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 95 | FunctionType::get(PointerType::getUnqual(Type::Int8Ty), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | std::vector<const Type*>(), true); |
| 97 | if (TyWeHave != Malloc3Type) |
| 98 | // Give up |
| 99 | MallocFunc = 0; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | FreeFunc = M.getFunction("free"); |
| 105 | if (FreeFunc) { |
| 106 | const FunctionType* TyWeHave = FreeFunc->getFunctionType(); |
| 107 | |
| 108 | // Get the expected prototype for void free(i8*) |
| 109 | const FunctionType *Free1Type = FunctionType::get(Type::VoidTy, |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 110 | std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)), false); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 111 | |
| 112 | if (TyWeHave != Free1Type) { |
| 113 | // Check to see if the prototype was forgotten, giving us |
| 114 | // void (...) * free |
| 115 | // This handles the common forward declaration of: 'void free();' |
| 116 | const FunctionType* Free2Type = FunctionType::get(Type::VoidTy, |
| 117 | std::vector<const Type*>(),true); |
| 118 | |
| 119 | if (TyWeHave != Free2Type) { |
| 120 | // One last try, check to see if we can find free as |
| 121 | // int (...)* free. This handles the case where NOTHING was declared. |
| 122 | const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty, |
| 123 | std::vector<const Type*>(),true); |
| 124 | |
| 125 | if (TyWeHave != Free3Type) { |
| 126 | // Give up. |
| 127 | FreeFunc = 0; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Don't mess with locally defined versions of these functions... |
| 134 | if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0; |
| 135 | if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0; |
| 136 | } |
| 137 | |
| 138 | // run - Transform calls into instructions... |
| 139 | // |
| 140 | bool RaiseAllocations::runOnModule(Module &M) { |
| 141 | // Find the malloc/free prototypes... |
| 142 | doInitialization(M); |
| 143 | |
| 144 | bool Changed = false; |
| 145 | |
| 146 | // First, process all of the malloc calls... |
| 147 | if (MallocFunc) { |
| 148 | std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end()); |
| 149 | std::vector<Value*> EqPointers; // Values equal to MallocFunc |
| 150 | while (!Users.empty()) { |
| 151 | User *U = Users.back(); |
| 152 | Users.pop_back(); |
| 153 | |
| 154 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
| 155 | CallSite CS = CallSite::get(I); |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 156 | if (CS.getInstruction() && !CS.arg_empty() && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | (CS.getCalledFunction() == MallocFunc || |
| 158 | std::find(EqPointers.begin(), EqPointers.end(), |
| 159 | CS.getCalledValue()) != EqPointers.end())) { |
| 160 | |
| 161 | Value *Source = *CS.arg_begin(); |
| 162 | |
| 163 | // If no prototype was provided for malloc, we may need to cast the |
| 164 | // source size. |
| 165 | if (Source->getType() != Type::Int32Ty) |
| 166 | Source = |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 167 | CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | "MallocAmtCast", I); |
| 169 | |
| 170 | MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I); |
| 171 | MI->takeName(I); |
| 172 | I->replaceAllUsesWith(MI); |
| 173 | |
| 174 | // If the old instruction was an invoke, add an unconditional branch |
| 175 | // before the invoke, which will become the new terminator. |
| 176 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 177 | BranchInst::Create(II->getNormalDest(), I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | |
| 179 | // Delete the old call site |
Dan Gohman | de08737 | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 180 | I->eraseFromParent(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | Changed = true; |
| 182 | ++NumRaised; |
| 183 | } |
| 184 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) { |
| 185 | Users.insert(Users.end(), GV->use_begin(), GV->use_end()); |
| 186 | EqPointers.push_back(GV); |
| 187 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 188 | if (CE->isCast()) { |
| 189 | Users.insert(Users.end(), CE->use_begin(), CE->use_end()); |
| 190 | EqPointers.push_back(CE); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Next, process all free calls... |
| 197 | if (FreeFunc) { |
| 198 | std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end()); |
| 199 | std::vector<Value*> EqPointers; // Values equal to FreeFunc |
| 200 | |
| 201 | while (!Users.empty()) { |
| 202 | User *U = Users.back(); |
| 203 | Users.pop_back(); |
| 204 | |
| 205 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Devang Patel | ea682b3 | 2007-10-17 20:12:58 +0000 | [diff] [blame] | 206 | if (isa<InvokeInst>(I)) |
| 207 | continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 208 | CallSite CS = CallSite::get(I); |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 209 | if (CS.getInstruction() && !CS.arg_empty() && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | (CS.getCalledFunction() == FreeFunc || |
| 211 | std::find(EqPointers.begin(), EqPointers.end(), |
| 212 | CS.getCalledValue()) != EqPointers.end())) { |
| 213 | |
| 214 | // If no prototype was provided for free, we may need to cast the |
| 215 | // source pointer. This should be really uncommon, but it's necessary |
| 216 | // just in case we are dealing with weird code like this: |
| 217 | // free((long)ptr); |
| 218 | // |
| 219 | Value *Source = *CS.arg_begin(); |
| 220 | if (!isa<PointerType>(Source->getType())) |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 221 | Source = new IntToPtrInst(Source, |
| 222 | PointerType::getUnqual(Type::Int8Ty), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | "FreePtrCast", I); |
| 224 | new FreeInst(Source, I); |
| 225 | |
| 226 | // If the old instruction was an invoke, add an unconditional branch |
| 227 | // before the invoke, which will become the new terminator. |
| 228 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 229 | BranchInst::Create(II->getNormalDest(), I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | |
| 231 | // Delete the old call site |
| 232 | if (I->getType() != Type::VoidTy) |
| 233 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 234 | I->eraseFromParent(); |
| 235 | Changed = true; |
| 236 | ++NumRaised; |
| 237 | } |
| 238 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) { |
| 239 | Users.insert(Users.end(), GV->use_begin(), GV->use_end()); |
| 240 | EqPointers.push_back(GV); |
| 241 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 242 | if (CE->isCast()) { |
| 243 | Users.insert(Users.end(), CE->use_begin(), CE->use_end()); |
| 244 | EqPointers.push_back(CE); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return Changed; |
| 251 | } |