Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 1 | //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | f516c69 | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/Instructions.h" |
| 24 | #include "llvm/IR/Module.h" |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "ipconstprop" |
| 30 | |
Chris Lattner | 1631bcb | 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 | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 35 | /// IPCP - The interprocedural constant propagation pass |
| 36 | /// |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | struct IPCP : public ModulePass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 39 | IPCP() : ModulePass(ID) { |
| 40 | initializeIPCPPass(*PassRegistry::getPassRegistry()); |
| 41 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 42 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 43 | bool runOnModule(Module &M) override; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 44 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 46 | |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 47 | /// PropagateConstantsIntoArguments - Look at all uses of the specified |
| 48 | /// function. If all uses are direct call sites, and all pass a particular |
| 49 | /// constant in for an argument, propagate that constant in as the argument. |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 50 | /// |
Davide Italiano | 296d12c | 2016-05-03 20:08:24 +0000 | [diff] [blame] | 51 | static bool PropagateConstantsIntoArguments(Function &F) { |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 52 | if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit. |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 53 | |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 54 | // For each argument, keep track of its constant value and whether it is a |
| 55 | // constant or not. The bool is driven to true when found to be non-constant. |
| 56 | SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants; |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 57 | ArgumentConstants.resize(F.arg_size()); |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 58 | |
| 59 | unsigned NumNonconstant = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 60 | for (Use &U : F.uses()) { |
| 61 | User *UR = U.getUser(); |
Chris Lattner | 1a8b80e | 2009-11-01 06:11:53 +0000 | [diff] [blame] | 62 | // Ignore blockaddress uses. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 63 | if (isa<BlockAddress>(UR)) continue; |
Chris Lattner | 1a8b80e | 2009-11-01 06:11:53 +0000 | [diff] [blame] | 64 | |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 65 | // Used by a non-instruction, or not the callee of a function, do not |
| 66 | // transform. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 67 | if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR)) |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 68 | return false; |
| 69 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 70 | CallSite CS(cast<Instruction>(UR)); |
| 71 | if (!CS.isCallee(&U)) |
Gabor Greif | f401337 | 2009-01-22 21:35:57 +0000 | [diff] [blame] | 72 | return false; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 73 | |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 74 | // Check out all of the potentially constant arguments. Note that we don't |
| 75 | // inspect varargs here. |
| 76 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 77 | Function::arg_iterator Arg = F.arg_begin(); |
| 78 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; |
| 79 | ++i, ++AI, ++Arg) { |
| 80 | |
| 81 | // If this argument is known non-constant, ignore it. |
| 82 | if (ArgumentConstants[i].second) |
| 83 | continue; |
| 84 | |
| 85 | Constant *C = dyn_cast<Constant>(*AI); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 86 | if (C && ArgumentConstants[i].first == nullptr) { |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 87 | ArgumentConstants[i].first = C; // First constant seen. |
| 88 | } else if (C && ArgumentConstants[i].first == C) { |
| 89 | // Still the constant value we think it is. |
| 90 | } else if (*AI == &*Arg) { |
| 91 | // Ignore recursive calls passing argument down. |
| 92 | } else { |
| 93 | // Argument became non-constant. If all arguments are non-constant now, |
| 94 | // give up on this function. |
| 95 | if (++NumNonconstant == ArgumentConstants.size()) |
| 96 | return false; |
| 97 | ArgumentConstants[i].second = true; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 101 | |
| 102 | // If we got to this point, there is a constant argument! |
| 103 | assert(NumNonconstant != ArgumentConstants.size()); |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 104 | bool MadeChange = false; |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 105 | Function::arg_iterator AI = F.arg_begin(); |
| 106 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { |
| 107 | // Do we have a constant argument? |
Torok Edwin | 21bd8c9 | 2009-09-24 18:33:42 +0000 | [diff] [blame] | 108 | if (ArgumentConstants[i].second || AI->use_empty() || |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 109 | AI->hasInAllocaAttr() || (AI->hasByValAttr() && !F.onlyReadsMemory())) |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 110 | continue; |
| 111 | |
| 112 | Value *V = ArgumentConstants[i].first; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 113 | if (!V) V = UndefValue::get(AI->getType()); |
Chris Lattner | a82d691 | 2008-04-23 06:16:27 +0000 | [diff] [blame] | 114 | AI->replaceAllUsesWith(V); |
| 115 | ++NumArgumentsProped; |
| 116 | MadeChange = true; |
| 117 | } |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 118 | return MadeChange; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 119 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 120 | |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 121 | |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 122 | // Check to see if this function returns one or more constants. If so, replace |
| 123 | // all callers that use those return values with the constant value. This will |
| 124 | // leave in the actual return values and instructions, but deadargelim will |
| 125 | // clean that up. |
Matthijs Kooijman | fd17357 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 126 | // |
| 127 | // Additionally if a function always returns one of its arguments directly, |
| 128 | // callers will be updated to use the value they pass in directly instead of |
| 129 | // using the return value. |
Davide Italiano | 296d12c | 2016-05-03 20:08:24 +0000 | [diff] [blame] | 130 | static bool PropagateConstantReturn(Function &F) { |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 131 | if (F.getReturnType()->isVoidTy()) |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 132 | return false; // No return value. |
| 133 | |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 134 | // We can infer and propagate the return value only when we know that the |
| 135 | // definition we'll get at link time is *exactly* the definition we see now. |
| 136 | // For more details, see GlobalValue::mayBeDerefined. |
| 137 | if (!F.isDefinitionExact()) |
Chris Lattner | dbd595f | 2008-06-09 07:58:07 +0000 | [diff] [blame] | 138 | return false; |
Davide Italiano | ec49313 | 2017-02-04 19:44:14 +0000 | [diff] [blame] | 139 | |
| 140 | // Don't touch naked functions. The may contain asm returning |
| 141 | // value we don't see, so we may end up interprocedurally propagating |
| 142 | // the return value incorrectly. |
| 143 | if (F.hasFnAttribute(Attribute::Naked)) |
| 144 | return false; |
| 145 | |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 146 | // Check to see if this function returns a constant. |
Devang Patel | 7358165 | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 147 | SmallVector<Value *,4> RetVals; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 148 | StructType *STy = dyn_cast<StructType>(F.getReturnType()); |
Devang Patel | 7358165 | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 149 | if (STy) |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 150 | for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 151 | RetVals.push_back(UndefValue::get(STy->getElementType(i))); |
Devang Patel | 7358165 | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 152 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 153 | RetVals.push_back(UndefValue::get(F.getReturnType())); |
Devang Patel | 7358165 | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 154 | |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 155 | unsigned NumNonConstant = 0; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 156 | for (BasicBlock &BB : F) |
| 157 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) { |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 158 | for (unsigned i = 0, e = RetVals.size(); i != e; ++i) { |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 159 | // Already found conflicting return values? |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 160 | Value *RV = RetVals[i]; |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 161 | if (!RV) |
| 162 | continue; |
| 163 | |
| 164 | // Find the returned value |
| 165 | Value *V; |
Dan Gohman | e219039 | 2008-10-03 22:21:24 +0000 | [diff] [blame] | 166 | if (!STy) |
Jay Foad | 1152209 | 2011-04-04 07:44:02 +0000 | [diff] [blame] | 167 | V = RI->getOperand(0); |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 168 | else |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 169 | V = FindInsertedValue(RI->getOperand(0), i); |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 170 | |
| 171 | if (V) { |
| 172 | // Ignore undefs, we can change them into anything |
| 173 | if (isa<UndefValue>(V)) |
| 174 | continue; |
| 175 | |
Matthijs Kooijman | fd17357 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 176 | // Try to see if all the rets return the same constant or argument. |
| 177 | if (isa<Constant>(V) || isa<Argument>(V)) { |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 178 | if (isa<UndefValue>(RV)) { |
| 179 | // No value found yet? Try the current one. |
| 180 | RetVals[i] = V; |
| 181 | continue; |
| 182 | } |
| 183 | // Returning the same value? Good. |
| 184 | if (RV == V) |
| 185 | continue; |
| 186 | } |
| 187 | } |
| 188 | // Different or no known return value? Don't propagate this return |
| 189 | // value. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 190 | RetVals[i] = nullptr; |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 191 | // All values non-constant? Stop looking. |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 192 | if (++NumNonConstant == RetVals.size()) |
| 193 | return false; |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 194 | } |
Anton Korobeynikov | 1bfd121 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 196 | |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 197 | // If we got here, the function returns at least one constant value. Loop |
| 198 | // over all users, replacing any uses of the return value with the returned |
| 199 | // constant. |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 200 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 201 | for (Use &U : F.uses()) { |
| 202 | CallSite CS(U.getUser()); |
Matthijs Kooijman | 0c71732 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 203 | Instruction* Call = CS.getInstruction(); |
| 204 | |
| 205 | // Not a call instruction or a call instruction that's not calling F |
| 206 | // directly? |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 207 | if (!Call || !CS.isCallee(&U)) |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 208 | continue; |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 209 | |
Matthijs Kooijman | 0c71732 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 210 | // Call result not used? |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 211 | if (Call->use_empty()) |
| 212 | continue; |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 214 | MadeChange = true; |
| 215 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 216 | if (!STy) { |
Matthijs Kooijman | fd17357 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 217 | Value* New = RetVals[0]; |
| 218 | if (Argument *A = dyn_cast<Argument>(New)) |
| 219 | // Was an argument returned? Then find the corresponding argument in |
Matthijs Kooijman | 0c71732 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 220 | // the call instruction and use that. |
| 221 | New = CS.getArgument(A->getArgNo()); |
Matthijs Kooijman | fd17357 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 222 | Call->replaceAllUsesWith(New); |
Chris Lattner | 5f1802c | 2008-04-23 05:59:23 +0000 | [diff] [blame] | 223 | continue; |
| 224 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 225 | |
| 226 | for (auto I = Call->user_begin(), E = Call->user_end(); I != E;) { |
Jay Foad | e57ba2e | 2009-06-06 17:49:35 +0000 | [diff] [blame] | 227 | Instruction *Ins = cast<Instruction>(*I); |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 228 | |
| 229 | // Increment now, so we can remove the use |
| 230 | ++I; |
| 231 | |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 232 | // Find the index of the retval to replace with |
| 233 | int index = -1; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 234 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins)) |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 235 | if (EV->hasIndices()) |
| 236 | index = *EV->idx_begin(); |
| 237 | |
| 238 | // If this use uses a specific return value, and we have a replacement, |
| 239 | // replace it. |
| 240 | if (index != -1) { |
| 241 | Value *New = RetVals[index]; |
| 242 | if (New) { |
Matthijs Kooijman | fd17357 | 2008-06-18 08:30:37 +0000 | [diff] [blame] | 243 | if (Argument *A = dyn_cast<Argument>(New)) |
| 244 | // Was an argument returned? Then find the corresponding argument in |
Matthijs Kooijman | 0c71732 | 2008-06-19 08:53:24 +0000 | [diff] [blame] | 245 | // the call instruction and use that. |
| 246 | New = CS.getArgument(A->getArgNo()); |
Matthijs Kooijman | 9703459 | 2008-06-18 08:09:27 +0000 | [diff] [blame] | 247 | Ins->replaceAllUsesWith(New); |
| 248 | Ins->eraseFromParent(); |
Chris Lattner | 263b0a1 | 2004-12-11 17:00:14 +0000 | [diff] [blame] | 249 | } |
Devang Patel | 5ca2ea6 | 2008-03-20 18:30:32 +0000 | [diff] [blame] | 250 | } |
Devang Patel | 7358165 | 2008-03-11 22:24:29 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (MadeChange) ++NumReturnValProped; |
Chris Lattner | af555ad | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 255 | return MadeChange; |
| 256 | } |
Davide Italiano | 296d12c | 2016-05-03 20:08:24 +0000 | [diff] [blame] | 257 | |
| 258 | char IPCP::ID = 0; |
| 259 | INITIALIZE_PASS(IPCP, "ipconstprop", |
| 260 | "Interprocedural constant propagation", false, false) |
| 261 | |
| 262 | ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); } |
| 263 | |
| 264 | bool IPCP::runOnModule(Module &M) { |
| 265 | if (skipModule(M)) |
| 266 | return false; |
| 267 | |
| 268 | bool Changed = false; |
| 269 | bool LocalChange = true; |
| 270 | |
| 271 | // FIXME: instead of using smart algorithms, we just iterate until we stop |
| 272 | // making changes. |
| 273 | while (LocalChange) { |
| 274 | LocalChange = false; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 275 | for (Function &F : M) |
| 276 | if (!F.isDeclaration()) { |
Davide Italiano | 296d12c | 2016-05-03 20:08:24 +0000 | [diff] [blame] | 277 | // Delete any klingons. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 278 | F.removeDeadConstantUsers(); |
| 279 | if (F.hasLocalLinkage()) |
| 280 | LocalChange |= PropagateConstantsIntoArguments(F); |
| 281 | Changed |= PropagateConstantReturn(F); |
Davide Italiano | 296d12c | 2016-05-03 20:08:24 +0000 | [diff] [blame] | 282 | } |
| 283 | Changed |= LocalChange; |
| 284 | } |
| 285 | return Changed; |
| 286 | } |