Chris Lattner | f516c69 | 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" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | Statistic<> NumArgumentsProped("ipconstprop", |
| 28 | "Number of args turned into constants"); |
| 29 | |
| 30 | /// IPCP - The interprocedural constant propagation pass |
| 31 | /// |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 32 | struct IPCP : public ModulePass { |
| 33 | bool runOnModule(Module &M); |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 34 | private: |
| 35 | bool processFunction(Function &F); |
| 36 | }; |
| 37 | RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation"); |
| 38 | } |
| 39 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 40 | ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); } |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 42 | bool IPCP::runOnModule(Module &M) { |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 43 | bool Changed = false; |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 44 | bool LocalChange = true; |
| 45 | |
| 46 | // FIXME: instead of using smart algorithms, we just iterate until we stop |
| 47 | // making changes. |
| 48 | while (LocalChange) { |
| 49 | LocalChange = false; |
| 50 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 51 | if (!I->isExternal() && I->hasInternalLinkage()) |
| 52 | LocalChange |= processFunction(*I); |
| 53 | Changed |= LocalChange; |
| 54 | } |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 55 | return Changed; |
| 56 | } |
| 57 | |
| 58 | /// processFunction - Look at all uses of the specified function. If all uses |
| 59 | /// are direct call sites, and all pass a particular constant in for an |
| 60 | /// argument, propagate that constant in as the argument. |
| 61 | /// |
| 62 | bool IPCP::processFunction(Function &F) { |
| 63 | if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit. |
| 64 | |
Chris Lattner | 0d3773d | 2004-11-09 20:47:30 +0000 | [diff] [blame] | 65 | // Delete any klingons. |
| 66 | F.removeDeadConstantUsers(); |
| 67 | |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 68 | std::vector<std::pair<Constant*, bool> > ArgumentConstants; |
| 69 | ArgumentConstants.resize(F.asize()); |
| 70 | |
| 71 | unsigned NumNonconstant = 0; |
| 72 | |
| 73 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) |
| 74 | if (!isa<Instruction>(*I)) |
| 75 | return false; // Used by a non-instruction, do not transform |
| 76 | else { |
| 77 | CallSite CS = CallSite::get(cast<Instruction>(*I)); |
| 78 | if (CS.getInstruction() == 0 || |
| 79 | CS.getCalledFunction() != &F) |
| 80 | return false; // Not a direct call site? |
| 81 | |
| 82 | // Check out all of the potentially constant arguments |
| 83 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | c1d24cd | 2004-11-10 19:43:59 +0000 | [diff] [blame^] | 84 | Function::aiterator Arg = F.abegin(); |
| 85 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; |
| 86 | ++i, ++AI, ++Arg) { |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 87 | if (*AI == &F) return false; // Passes the function into itself |
| 88 | |
| 89 | if (!ArgumentConstants[i].second) { |
Reid Spencer | 199aeb7 | 2004-07-18 08:31:18 +0000 | [diff] [blame] | 90 | if (Constant *C = dyn_cast<Constant>(*AI)) { |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 91 | if (!ArgumentConstants[i].first) |
| 92 | ArgumentConstants[i].first = C; |
| 93 | else if (ArgumentConstants[i].first != C) { |
| 94 | // Became non-constant |
| 95 | ArgumentConstants[i].second = true; |
| 96 | ++NumNonconstant; |
| 97 | if (NumNonconstant == ArgumentConstants.size()) return false; |
| 98 | } |
Chris Lattner | c1d24cd | 2004-11-10 19:43:59 +0000 | [diff] [blame^] | 99 | } else if (*AI != &*Arg) { // Ignore recursive calls with same arg |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 100 | // This is not a constant argument. Mark the argument as |
| 101 | // non-constant. |
| 102 | ArgumentConstants[i].second = true; |
| 103 | ++NumNonconstant; |
| 104 | if (NumNonconstant == ArgumentConstants.size()) return false; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // If we got to this point, there is a constant argument! |
| 111 | assert(NumNonconstant != ArgumentConstants.size()); |
| 112 | Function::aiterator AI = F.abegin(); |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 113 | bool MadeChange = false; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 114 | for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) |
| 115 | // Do we have a constant argument!? |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 116 | if (!ArgumentConstants[i].second && !AI->use_empty()) { |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 117 | assert(ArgumentConstants[i].first && "Unknown constant value!"); |
Chris Lattner | 12b78db | 2003-10-23 18:49:23 +0000 | [diff] [blame] | 118 | Value *V = ArgumentConstants[i].first; |
Chris Lattner | 12b78db | 2003-10-23 18:49:23 +0000 | [diff] [blame] | 119 | AI->replaceAllUsesWith(V); |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 120 | ++NumArgumentsProped; |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 121 | MadeChange = true; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 5e004e8 | 2003-10-27 21:09:00 +0000 | [diff] [blame] | 123 | return MadeChange; |
Chris Lattner | f516c69 | 2003-10-23 16:52:27 +0000 | [diff] [blame] | 124 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 125 | |