Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 1 | //===-- GCSE.cpp - SSA-based Global Common Subexpression Elimination ------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass is designed to be a very quick global transformation that |
| 11 | // eliminates global common subexpressions from a function. It does this by |
Chris Lattner | 2964f36 | 2002-08-30 22:53:30 +0000 | [diff] [blame] | 12 | // using an existing value numbering implementation to identify the common |
| 13 | // subexpressions, eliminating them when possible. |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 18 | #include "llvm/BasicBlock.h" |
| 19 | #include "llvm/Constant.h" |
| 20 | #include "llvm/Instructions.h" |
Chris Lattner | 2964f36 | 2002-08-30 22:53:30 +0000 | [diff] [blame] | 21 | #include "llvm/Type.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueNumbering.h" |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DepthFirstIterator.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 31 | Statistic<> NumInstRemoved("gcse", "Number of instructions removed"); |
| 32 | Statistic<> NumLoadRemoved("gcse", "Number of loads removed"); |
Chris Lattner | 46234fd | 2004-03-15 05:46:59 +0000 | [diff] [blame] | 33 | Statistic<> NumCallRemoved("gcse", "Number of calls removed"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 34 | Statistic<> NumNonInsts ("gcse", "Number of instructions removed due " |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 35 | "to non-instruction values"); |
Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 36 | Statistic<> NumArgsRepl ("gcse", "Number of function arguments replaced " |
| 37 | "with constant values"); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 39 | struct GCSE : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | virtual bool runOnFunction(Function &F); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 41 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 42 | private: |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 43 | void ReplaceInstructionWith(Instruction *I, Value *V); |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 45 | // This transformation requires dominator and immediate dominator info |
| 46 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 47 | AU.setPreservesCFG(); |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 48 | AU.addRequired<DominatorSet>(); |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 49 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 50 | AU.addRequired<ValueNumbering>(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 51 | } |
| 52 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 53 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 54 | RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination"); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // createGCSEPass - The public interface to this file... |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 58 | FunctionPass *llvm::createGCSEPass() { return new GCSE(); } |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 59 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 60 | // GCSE::runOnFunction - This is the main transformation entry point for a |
| 61 | // function. |
| 62 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 63 | bool GCSE::runOnFunction(Function &F) { |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 64 | bool Changed = false; |
| 65 | |
Chris Lattner | d456ec9 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 66 | // Get pointers to the analysis results that we will be using... |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 67 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 68 | ValueNumbering &VN = getAnalysis<ValueNumbering>(); |
| 69 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 71 | std::vector<Value*> EqualValues; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 73 | // Check for value numbers of arguments. If the value numbering |
| 74 | // implementation can prove that an incoming argument is a constant or global |
| 75 | // value address, substitute it, making the argument dead. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 76 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI) |
Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 77 | if (!AI->use_empty()) { |
| 78 | VN.getEqualNumberNodes(AI, EqualValues); |
| 79 | if (!EqualValues.empty()) { |
| 80 | for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) |
Reid Spencer | 460f16c | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 81 | if (isa<Constant>(EqualValues[i])) { |
Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 82 | AI->replaceAllUsesWith(EqualValues[i]); |
| 83 | ++NumArgsRepl; |
| 84 | Changed = true; |
| 85 | break; |
| 86 | } |
| 87 | EqualValues.clear(); |
| 88 | } |
| 89 | } |
| 90 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 91 | // Traverse the CFG of the function in dominator order, so that we see each |
| 92 | // instruction after we see its operands. |
| 93 | for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()), |
| 94 | E = df_end(DT.getRootNode()); DI != E; ++DI) { |
| 95 | BasicBlock *BB = DI->getBlock(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 97 | // Remember which instructions we've seen in this basic block as we scan. |
| 98 | std::set<Instruction*> BlockInsts; |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 100 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 101 | Instruction *Inst = I++; |
| 102 | |
Chris Lattner | fb851ab | 2004-12-12 18:23:20 +0000 | [diff] [blame] | 103 | if (Constant *C = ConstantFoldInstruction(Inst)) { |
| 104 | ReplaceInstructionWith(Inst, C); |
| 105 | } else if (Inst->getType() != Type::VoidTy) { |
| 106 | // If this instruction computes a value, try to fold together common |
| 107 | // instructions that compute it. |
| 108 | // |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 109 | VN.getEqualNumberNodes(Inst, EqualValues); |
| 110 | |
| 111 | // If this instruction computes a value that is already computed |
| 112 | // elsewhere, try to recycle the old value. |
| 113 | if (!EqualValues.empty()) { |
| 114 | if (Inst == &*BB->begin()) |
| 115 | I = BB->end(); |
| 116 | else { |
| 117 | I = Inst; --I; |
| 118 | } |
| 119 | |
| 120 | // First check to see if we were able to value number this instruction |
| 121 | // to a non-instruction value. If so, prefer that value over other |
| 122 | // instructions which may compute the same thing. |
| 123 | for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) |
| 124 | if (!isa<Instruction>(EqualValues[i])) { |
| 125 | ++NumNonInsts; // Keep track of # of insts repl with values |
| 126 | |
| 127 | // Change all users of Inst to use the replacement and remove it |
| 128 | // from the program. |
| 129 | ReplaceInstructionWith(Inst, EqualValues[i]); |
| 130 | Inst = 0; |
| 131 | EqualValues.clear(); // don't enter the next loop |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | // If there were no non-instruction values that this instruction |
| 136 | // produces, find a dominating instruction that produces the same |
| 137 | // value. If we find one, use it's value instead of ours. |
| 138 | for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) { |
| 139 | Instruction *OtherI = cast<Instruction>(EqualValues[i]); |
| 140 | bool Dominates = false; |
| 141 | if (OtherI->getParent() == BB) |
| 142 | Dominates = BlockInsts.count(OtherI); |
| 143 | else |
| 144 | Dominates = DS.dominates(OtherI->getParent(), BB); |
| 145 | |
| 146 | if (Dominates) { |
| 147 | // Okay, we found an instruction with the same value as this one |
| 148 | // and that dominates this one. Replace this instruction with the |
| 149 | // specified one. |
| 150 | ReplaceInstructionWith(Inst, OtherI); |
| 151 | Inst = 0; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | EqualValues.clear(); |
| 157 | |
| 158 | if (Inst) { |
| 159 | I = Inst; ++I; // Deleted no instructions |
| 160 | } else if (I == BB->end()) { // Deleted first instruction |
| 161 | I = BB->begin(); |
| 162 | } else { // Deleted inst in middle of block. |
| 163 | ++I; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (Inst) |
| 168 | BlockInsts.insert(Inst); |
| 169 | } |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 173 | // When the worklist is empty, return whether or not we changed anything... |
| 174 | return Changed; |
| 175 | } |
| 176 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 178 | void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) { |
| 179 | if (isa<LoadInst>(I)) |
| 180 | ++NumLoadRemoved; // Keep track of loads eliminated |
| 181 | if (isa<CallInst>(I)) |
| 182 | ++NumCallRemoved; // Keep track of calls eliminated |
| 183 | ++NumInstRemoved; // Keep track of number of insts eliminated |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 184 | |
Chris Lattner | adb7c0d | 2004-04-10 22:33:34 +0000 | [diff] [blame] | 185 | // Update value numbering |
Chris Lattner | 057f78a | 2004-05-23 21:19:55 +0000 | [diff] [blame] | 186 | getAnalysis<ValueNumbering>().deleteValue(I); |
Chris Lattner | adb7c0d | 2004-04-10 22:33:34 +0000 | [diff] [blame] | 187 | |
Chris Lattner | fb851ab | 2004-12-12 18:23:20 +0000 | [diff] [blame] | 188 | I->replaceAllUsesWith(V); |
| 189 | |
Chris Lattner | 1708d12 | 2004-04-12 05:15:13 +0000 | [diff] [blame] | 190 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 191 | // Removing an invoke instruction requires adding a branch to the normal |
| 192 | // destination and removing PHI node entries in the exception destination. |
| 193 | new BranchInst(II->getNormalDest(), II); |
| 194 | II->getUnwindDest()->removePredecessor(II->getParent()); |
| 195 | } |
Chris Lattner | fb851ab | 2004-12-12 18:23:20 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 38e66bd | 2004-04-10 21:11:11 +0000 | [diff] [blame] | 197 | // Erase the instruction from the program. |
| 198 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 199 | } |