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