Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 1 | //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +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 | // |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements an _extremely_ simple interprocedural constant |
| 11 | // propagation pass. It could certainly be improved in many different ways, |
| 12 | // like using a worklist. This pass makes arguments dead, but does not remove |
| 13 | // them. The existing dead argument elimination pass should be run after this |
| 14 | // to clean up the mess. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "ipconstprop" |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/Instructions.h" |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 32 | STATISTIC(NumArgumentsProped, "Number of args turned into constants"); |
| 33 | STATISTIC(NumReturnValProped, "Number of return values turned into constants"); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 36 | /// IPCP - The interprocedural constant propagation pass |
| 37 | /// |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 38 | struct VISIBILITY_HIDDEN IPCP : public ModulePass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 40 | IPCP() : ModulePass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 41 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 42 | bool runOnModule(Module &M); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 43 | private: |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 44 | bool PropagateConstantsIntoArguments(Function &F); |
| 45 | bool PropagateConstantReturn(Function &F); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 46 | }; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 49 | char IPCP::ID = 0; |
| 50 | static RegisterPass<IPCP> |
| 51 | X("ipconstprop", "Interprocedural constant propagation"); |
| 52 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 53 | ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); } |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 54 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 55 | bool IPCP::runOnModule(Module &M) { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 56 | bool Changed = false; |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 57 | bool LocalChange = true; |
| 58 | |
| 59 | // FIXME: instead of using smart algorithms, we just iterate until we stop |
| 60 | // making changes. |
| 61 | while (LocalChange) { |
| 62 | LocalChange = false; |
| 63 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 64 | if (!I->isDeclaration()) { |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 65 | // Delete any klingons. |
| 66 | I->removeDeadConstantUsers(); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 67 | if (I->hasLocalLinkage()) |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 68 | LocalChange |= PropagateConstantsIntoArguments(*I); |
| 69 | Changed |= PropagateConstantReturn(*I); |
| 70 | } |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 71 | Changed |= LocalChange; |
| 72 | } |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 73 | return Changed; |
| 74 | } |
| 75 | |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 76 | /// PropagateConstantsIntoArguments - Look at all uses of the specified |
| 77 | /// function. If all uses are direct call sites, and all pass a particular |
| 78 | /// constant in for an argument, propagate that constant in as the argument. |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 79 | /// |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 80 | bool IPCP::PropagateConstantsIntoArguments(Function &F) { |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 81 | if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit. |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 83 | // For each argument, keep track of its constant value and whether it is a |
| 84 | // constant or not. The bool is driven to true when found to be non-constant. |
| 85 | SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 86 | ArgumentConstants.resize(F.arg_size()); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 87 | |
| 88 | unsigned NumNonconstant = 0; |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 89 | for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) { |
| 90 | // Used by a non-instruction, or not the callee of a function, do not |
| 91 | // transform. |
Gabor Greif | edc4d69 | 2009-01-22 21:35:57 +0000 | [diff] [blame] | 92 | if (!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI)) |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 93 | return false; |
| 94 | |
| 95 | CallSite CS = CallSite::get(cast<Instruction>(*UI)); |
Gabor Greif | edc4d69 | 2009-01-22 21:35:57 +0000 | [diff] [blame] | 96 | if (!CS.isCallee(UI)) |
| 97 | return false; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 99 | // Check out all of the potentially constant arguments. Note that we don't |
| 100 | // inspect varargs here. |
| 101 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 102 | Function::arg_iterator Arg = F.arg_begin(); |
| 103 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; |
| 104 | ++i, ++AI, ++Arg) { |
| 105 | |
| 106 | // If this argument is known non-constant, ignore it. |
| 107 | if (ArgumentConstants[i].second) |
| 108 | continue; |
| 109 | |
| 110 | Constant *C = dyn_cast<Constant>(*AI); |
| 111 | if (C && ArgumentConstants[i].first == 0) { |
| 112 | ArgumentConstants[i].first = C; // First constant seen. |
| 113 | } else if (C && ArgumentConstants[i].first == C) { |
| 114 | // Still the constant value we think it is. |
| 115 | } else if (*AI == &*Arg) { |
| 116 | // Ignore recursive calls passing argument down. |
| 117 | } else { |
| 118 | // Argument became non-constant. If all arguments are non-constant now, |
| 119 | // give up on this function. |
| 120 | if (++NumNonconstant == ArgumentConstants.size()) |
| 121 | return false; |
| 122 | ArgumentConstants[i].second = true; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 126 | |
| 127 | // If we got to this point, there is a constant argument! |
| 128 | assert(NumNonconstant != ArgumentConstants.size()); |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 129 | bool MadeChange = false; |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 130 | Function::arg_iterator AI = F.arg_begin(); |
| 131 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { |
| 132 | // Do we have a constant argument? |
| 133 | if (ArgumentConstants[i].second || AI->use_empty()) |
| 134 | continue; |
| 135 | |
| 136 | Value *V = ArgumentConstants[i].first; |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 137 | if (V == 0) V = UndefValue::get(AI->getType()); |
Chris Lattner | 4af6ad3 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 138 | AI->replaceAllUsesWith(V); |
| 139 | ++NumArgumentsProped; |
| 140 | MadeChange = true; |
| 141 | } |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 142 | return MadeChange; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 143 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 144 | |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 145 | |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 146 | // Check to see if this function returns one or more constants. If so, replace |
| 147 | // all callers that use those return values with the constant value. This will |
| 148 | // leave in the actual return values and instructions, but deadargelim will |
| 149 | // clean that up. |
Matthijs Kooijman | c2afe89 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 150 | // |
| 151 | // Additionally if a function always returns one of its arguments directly, |
| 152 | // callers will be updated to use the value they pass in directly instead of |
| 153 | // using the return value. |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 154 | bool IPCP::PropagateConstantReturn(Function &F) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame^] | 155 | if (F.getReturnType() == Type::getVoidTy(F.getContext())) |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 156 | return false; // No return value. |
| 157 | |
Chris Lattner | 18d73c2 | 2008-06-09 07:58:07 +0000 | [diff] [blame] | 158 | // If this function could be overridden later in the link stage, we can't |
| 159 | // propagate information about its results into callers. |
Nuno Lopes | 5ed3b89 | 2008-09-29 14:40:32 +0000 | [diff] [blame] | 160 | if (F.mayBeOverridden()) |
Chris Lattner | 18d73c2 | 2008-06-09 07:58:07 +0000 | [diff] [blame] | 161 | return false; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 162 | |
| 163 | LLVMContext &Context = F.getContext(); |
Chris Lattner | 18d73c2 | 2008-06-09 07:58:07 +0000 | [diff] [blame] | 164 | |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 165 | // Check to see if this function returns a constant. |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 166 | SmallVector<Value *,4> RetVals; |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 167 | const StructType *STy = dyn_cast<StructType>(F.getReturnType()); |
| 168 | if (STy) |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 170 | RetVals.push_back(UndefValue::get(STy->getElementType(i))); |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 171 | else |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 172 | RetVals.push_back(UndefValue::get(F.getReturnType())); |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 173 | |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 174 | unsigned NumNonConstant = 0; |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 175 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 176 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 177 | for (unsigned i = 0, e = RetVals.size(); i != e; ++i) { |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 178 | // Already found conflicting return values? |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 179 | Value *RV = RetVals[i]; |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 180 | if (!RV) |
| 181 | continue; |
| 182 | |
| 183 | // Find the returned value |
| 184 | Value *V; |
Dan Gohman | 792e1e9 | 2008-10-03 22:21:24 +0000 | [diff] [blame] | 185 | if (!STy) |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 186 | V = RI->getOperand(i); |
| 187 | else |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 188 | V = FindInsertedValue(RI->getOperand(0), i, Context); |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 189 | |
| 190 | if (V) { |
| 191 | // Ignore undefs, we can change them into anything |
| 192 | if (isa<UndefValue>(V)) |
| 193 | continue; |
| 194 | |
Matthijs Kooijman | c2afe89 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 195 | // Try to see if all the rets return the same constant or argument. |
| 196 | if (isa<Constant>(V) || isa<Argument>(V)) { |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 197 | if (isa<UndefValue>(RV)) { |
| 198 | // No value found yet? Try the current one. |
| 199 | RetVals[i] = V; |
| 200 | continue; |
| 201 | } |
| 202 | // Returning the same value? Good. |
| 203 | if (RV == V) |
| 204 | continue; |
| 205 | } |
| 206 | } |
| 207 | // Different or no known return value? Don't propagate this return |
| 208 | // value. |
| 209 | RetVals[i] = 0; |
| 210 | // All values non constant? Stop looking. |
| 211 | if (++NumNonConstant == RetVals.size()) |
| 212 | return false; |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 213 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 215 | |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 216 | // If we got here, the function returns at least one constant value. Loop |
| 217 | // over all users, replacing any uses of the return value with the returned |
| 218 | // constant. |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 219 | bool MadeChange = false; |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 220 | for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) { |
Matthijs Kooijman | f0da203 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 221 | CallSite CS = CallSite::get(*UI); |
| 222 | Instruction* Call = CS.getInstruction(); |
| 223 | |
| 224 | // Not a call instruction or a call instruction that's not calling F |
| 225 | // directly? |
Gabor Greif | edc4d69 | 2009-01-22 21:35:57 +0000 | [diff] [blame] | 226 | if (!Call || !CS.isCallee(UI)) |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 227 | continue; |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 228 | |
Matthijs Kooijman | f0da203 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 229 | // Call result not used? |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 230 | if (Call->use_empty()) |
| 231 | continue; |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 232 | |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 233 | MadeChange = true; |
| 234 | |
| 235 | if (STy == 0) { |
Matthijs Kooijman | c2afe89 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 236 | Value* New = RetVals[0]; |
| 237 | if (Argument *A = dyn_cast<Argument>(New)) |
| 238 | // Was an argument returned? Then find the corresponding argument in |
Matthijs Kooijman | f0da203 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 239 | // the call instruction and use that. |
| 240 | New = CS.getArgument(A->getArgNo()); |
Matthijs Kooijman | c2afe89 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 241 | Call->replaceAllUsesWith(New); |
Chris Lattner | e9625c6 | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 242 | continue; |
| 243 | } |
| 244 | |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 245 | for (Value::use_iterator I = Call->use_begin(), E = Call->use_end(); |
| 246 | I != E;) { |
Jay Foad | 0906b1b | 2009-06-06 17:49:35 +0000 | [diff] [blame] | 247 | Instruction *Ins = cast<Instruction>(*I); |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 248 | |
| 249 | // Increment now, so we can remove the use |
| 250 | ++I; |
| 251 | |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 252 | // Find the index of the retval to replace with |
| 253 | int index = -1; |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 254 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins)) |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 255 | if (EV->hasIndices()) |
| 256 | index = *EV->idx_begin(); |
| 257 | |
| 258 | // If this use uses a specific return value, and we have a replacement, |
| 259 | // replace it. |
| 260 | if (index != -1) { |
| 261 | Value *New = RetVals[index]; |
| 262 | if (New) { |
Matthijs Kooijman | c2afe89 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 263 | if (Argument *A = dyn_cast<Argument>(New)) |
| 264 | // Was an argument returned? Then find the corresponding argument in |
Matthijs Kooijman | f0da203 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 265 | // the call instruction and use that. |
| 266 | New = CS.getArgument(A->getArgNo()); |
Matthijs Kooijman | 8b0fcf3 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 267 | Ins->replaceAllUsesWith(New); |
| 268 | Ins->eraseFromParent(); |
Chris Lattner | 2ffa47b | 2004-12-11 17:00:14 +0000 | [diff] [blame] | 269 | } |
Devang Patel | 488b678 | 2008-03-20 18:30:32 +0000 | [diff] [blame] | 270 | } |
Devang Patel | 7db30ba | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (MadeChange) ++NumReturnValProped; |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 275 | return MadeChange; |
| 276 | } |