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" |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.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 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 29 | STATISTIC(NumArgumentsProped, "Number of args turned into constants"); |
| 30 | STATISTIC(NumReturnValProped, "Number of return values turned into constants"); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 33 | /// IPCP - The interprocedural constant propagation pass |
| 34 | /// |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 35 | struct VISIBILITY_HIDDEN IPCP : public ModulePass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 36 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 37 | IPCP() : ModulePass((intptr_t)&ID) {} |
| 38 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 39 | bool runOnModule(Module &M); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 40 | private: |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 41 | bool PropagateConstantsIntoArguments(Function &F); |
| 42 | bool PropagateConstantReturn(Function &F); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 43 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 44 | char IPCP::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 45 | RegisterPass<IPCP> X("ipconstprop", "Interprocedural constant propagation"); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 48 | ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); } |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 49 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 50 | bool IPCP::runOnModule(Module &M) { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 51 | bool Changed = false; |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 52 | bool LocalChange = true; |
| 53 | |
| 54 | // FIXME: instead of using smart algorithms, we just iterate until we stop |
| 55 | // making changes. |
| 56 | while (LocalChange) { |
| 57 | LocalChange = false; |
| 58 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 59 | if (!I->isDeclaration()) { |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 60 | // Delete any klingons. |
| 61 | I->removeDeadConstantUsers(); |
| 62 | if (I->hasInternalLinkage()) |
| 63 | LocalChange |= PropagateConstantsIntoArguments(*I); |
| 64 | Changed |= PropagateConstantReturn(*I); |
| 65 | } |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 66 | Changed |= LocalChange; |
| 67 | } |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 68 | return Changed; |
| 69 | } |
| 70 | |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 71 | /// PropagateConstantsIntoArguments - Look at all uses of the specified |
| 72 | /// function. If all uses are direct call sites, and all pass a particular |
| 73 | /// constant in for an argument, propagate that constant in as the argument. |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 74 | /// |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 75 | bool IPCP::PropagateConstantsIntoArguments(Function &F) { |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 76 | 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] | 77 | |
| 78 | std::vector<std::pair<Constant*, bool> > ArgumentConstants; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 79 | ArgumentConstants.resize(F.arg_size()); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 80 | |
| 81 | unsigned NumNonconstant = 0; |
| 82 | |
| 83 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) |
| 84 | if (!isa<Instruction>(*I)) |
| 85 | return false; // Used by a non-instruction, do not transform |
| 86 | else { |
| 87 | CallSite CS = CallSite::get(cast<Instruction>(*I)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 88 | if (CS.getInstruction() == 0 || |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 89 | CS.getCalledFunction() != &F) |
| 90 | return false; // Not a direct call site? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 92 | // Check out all of the potentially constant arguments |
| 93 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 94 | Function::arg_iterator Arg = F.arg_begin(); |
Chris Lattner | ff1529b | 2004-11-10 19:43:59 +0000 | [diff] [blame] | 95 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; |
| 96 | ++i, ++AI, ++Arg) { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 97 | if (*AI == &F) return false; // Passes the function into itself |
| 98 | |
| 99 | if (!ArgumentConstants[i].second) { |
Reid Spencer | 3188cab | 2004-07-18 08:31:18 +0000 | [diff] [blame] | 100 | if (Constant *C = dyn_cast<Constant>(*AI)) { |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 101 | if (!ArgumentConstants[i].first) |
| 102 | ArgumentConstants[i].first = C; |
| 103 | else if (ArgumentConstants[i].first != C) { |
| 104 | // Became non-constant |
| 105 | ArgumentConstants[i].second = true; |
| 106 | ++NumNonconstant; |
| 107 | if (NumNonconstant == ArgumentConstants.size()) return false; |
| 108 | } |
Chris Lattner | ff1529b | 2004-11-10 19:43:59 +0000 | [diff] [blame] | 109 | } else if (*AI != &*Arg) { // Ignore recursive calls with same arg |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 110 | // This is not a constant argument. Mark the argument as |
| 111 | // non-constant. |
| 112 | ArgumentConstants[i].second = true; |
| 113 | ++NumNonconstant; |
| 114 | if (NumNonconstant == ArgumentConstants.size()) return false; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // If we got to this point, there is a constant argument! |
| 121 | assert(NumNonconstant != ArgumentConstants.size()); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 122 | Function::arg_iterator AI = F.arg_begin(); |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 123 | bool MadeChange = false; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 124 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) |
| 125 | // Do we have a constant argument!? |
Chris Lattner | d79c7b4 | 2004-11-11 07:47:54 +0000 | [diff] [blame] | 126 | if (!ArgumentConstants[i].second && !AI->use_empty()) { |
Chris Lattner | 2e56dd8 | 2003-10-23 18:49:23 +0000 | [diff] [blame] | 127 | Value *V = ArgumentConstants[i].first; |
Chris Lattner | 3e062ea | 2004-11-11 07:46:29 +0000 | [diff] [blame] | 128 | if (V == 0) V = UndefValue::get(AI->getType()); |
Chris Lattner | 2e56dd8 | 2003-10-23 18:49:23 +0000 | [diff] [blame] | 129 | AI->replaceAllUsesWith(V); |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 130 | ++NumArgumentsProped; |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 131 | MadeChange = true; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 2e8dfb8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 133 | return MadeChange; |
Chris Lattner | d358e6f | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 134 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 135 | |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 136 | |
| 137 | // Check to see if this function returns a constant. If so, replace all callers |
| 138 | // that user the return value with the returned valued. If we can replace ALL |
| 139 | // callers, |
| 140 | bool IPCP::PropagateConstantReturn(Function &F) { |
| 141 | if (F.getReturnType() == Type::VoidTy) |
| 142 | return false; // No return value. |
| 143 | |
| 144 | // Check to see if this function returns a constant. |
| 145 | Value *RetVal = 0; |
| 146 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 147 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 148 | if (isa<UndefValue>(RI->getOperand(0))) { |
| 149 | // Ignore. |
| 150 | } else if (Constant *C = dyn_cast<Constant>(RI->getOperand(0))) { |
| 151 | if (RetVal == 0) |
| 152 | RetVal = C; |
| 153 | else if (RetVal != C) |
| 154 | return false; // Does not return the same constant. |
| 155 | } else { |
| 156 | return false; // Does not return a constant. |
| 157 | } |
| 158 | |
| 159 | if (RetVal == 0) RetVal = UndefValue::get(F.getReturnType()); |
| 160 | |
| 161 | // If we got here, the function returns a constant value. Loop over all |
| 162 | // users, replacing any uses of the return value with the returned constant. |
| 163 | bool ReplacedAllUsers = true; |
| 164 | bool MadeChange = false; |
| 165 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) |
| 166 | if (!isa<Instruction>(*I)) |
| 167 | ReplacedAllUsers = false; |
| 168 | else { |
| 169 | CallSite CS = CallSite::get(cast<Instruction>(*I)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 170 | if (CS.getInstruction() == 0 || |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 171 | CS.getCalledFunction() != &F) { |
| 172 | ReplacedAllUsers = false; |
| 173 | } else { |
| 174 | if (!CS.getInstruction()->use_empty()) { |
| 175 | CS.getInstruction()->replaceAllUsesWith(RetVal); |
| 176 | MadeChange = true; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // If we replace all users with the returned constant, and there can be no |
| 182 | // other callers of the function, replace the constant being returned in the |
| 183 | // function with an undef value. |
| 184 | if (ReplacedAllUsers && F.hasInternalLinkage() && !isa<UndefValue>(RetVal)) { |
| 185 | Value *RV = UndefValue::get(RetVal->getType()); |
| 186 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
Chris Lattner | 2ffa47b | 2004-12-11 17:00:14 +0000 | [diff] [blame] | 187 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 188 | if (RI->getOperand(0) != RV) { |
| 189 | RI->setOperand(0, RV); |
| 190 | MadeChange = true; |
| 191 | } |
| 192 | } |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if (MadeChange) ++NumReturnValProped; |
Chris Lattner | a990d94 | 2004-11-14 06:10:11 +0000 | [diff] [blame] | 196 | return MadeChange; |
| 197 | } |