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