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