Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 1 | //===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===// |
| 2 | // |
| 3 | // This pass is designed to be a very quick global transformation that |
| 4 | // eliminates global common subexpressions from a function. It does this by |
Chris Lattner | 2964f36 | 2002-08-30 22:53:30 +0000 | [diff] [blame] | 5 | // using an existing value numbering implementation to identify the common |
| 6 | // subexpressions, eliminating them when possible. |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 11 | #include "llvm/iMemory.h" |
Chris Lattner | 2964f36 | 2002-08-30 22:53:30 +0000 | [diff] [blame] | 12 | #include "llvm/Type.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/ValueNumbering.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 16 | #include "Support/Statistic.h" |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 18 | |
| 19 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 20 | Statistic<> NumInstRemoved("gcse", "Number of instructions removed"); |
| 21 | Statistic<> NumLoadRemoved("gcse", "Number of loads removed"); |
| 22 | Statistic<> NumNonInsts ("gcse", "Number of instructions removed due " |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 23 | "to non-instruction values"); |
| 24 | |
| 25 | class GCSE : public FunctionPass { |
Chris Lattner | 2964f36 | 2002-08-30 22:53:30 +0000 | [diff] [blame] | 26 | std::set<Instruction*> WorkList; |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 27 | DominatorSet *DomSetInfo; |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 28 | ValueNumbering *VN; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 29 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 30 | virtual bool runOnFunction(Function &F); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 31 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 32 | private: |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 33 | bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues); |
| 34 | Instruction *EliminateCSE(Instruction *I, Instruction *Other); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 35 | void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI); |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 36 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 37 | // This transformation requires dominator and immediate dominator info |
| 38 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 39 | AU.setPreservesCFG(); |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 40 | AU.addRequired<DominatorSet>(); |
| 41 | AU.addRequired<ImmediateDominators>(); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 42 | AU.addRequired<ValueNumbering>(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 43 | } |
| 44 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 45 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 46 | RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination"); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // createGCSEPass - The public interface to this file... |
| 50 | Pass *createGCSEPass() { return new GCSE(); } |
| 51 | |
| 52 | |
| 53 | // GCSE::runOnFunction - This is the main transformation entry point for a |
| 54 | // function. |
| 55 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 56 | bool GCSE::runOnFunction(Function &F) { |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 57 | bool Changed = false; |
| 58 | |
Chris Lattner | d456ec9 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 59 | // Get pointers to the analysis results that we will be using... |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 60 | DomSetInfo = &getAnalysis<DominatorSet>(); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 61 | VN = &getAnalysis<ValueNumbering>(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 62 | |
| 63 | // Step #1: Add all instructions in the function to the worklist for |
| 64 | // processing. All of the instructions are considered to be our |
| 65 | // subexpressions to eliminate if possible. |
| 66 | // |
| 67 | WorkList.insert(inst_begin(F), inst_end(F)); |
| 68 | |
| 69 | // Step #2: WorkList processing. Iterate through all of the instructions, |
| 70 | // checking to see if there are any additionally defined subexpressions in the |
| 71 | // program. If so, eliminate them! |
| 72 | // |
| 73 | while (!WorkList.empty()) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 74 | Instruction &I = **WorkList.begin(); // Get an instruction from the worklist |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 75 | WorkList.erase(WorkList.begin()); |
| 76 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 77 | // If this instruction computes a value, try to fold together common |
| 78 | // instructions that compute it. |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 79 | // |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 80 | if (I.getType() != Type::VoidTy) { |
| 81 | std::vector<Value*> EqualValues; |
| 82 | VN->getEqualNumberNodes(&I, EqualValues); |
| 83 | |
| 84 | if (!EqualValues.empty()) |
| 85 | Changed |= EliminateRedundancies(&I, EqualValues); |
| 86 | } |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 88 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 89 | // When the worklist is empty, return whether or not we changed anything... |
| 90 | return Changed; |
| 91 | } |
| 92 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 93 | bool GCSE::EliminateRedundancies(Instruction *I, |
| 94 | std::vector<Value*> &EqualValues) { |
| 95 | // If the EqualValues set contains any non-instruction values, then we know |
| 96 | // that all of the instructions can be replaced with the non-instruction value |
| 97 | // because it is guaranteed to dominate all of the instructions in the |
| 98 | // function. We only have to do hard work if all we have are instructions. |
| 99 | // |
| 100 | for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) |
| 101 | if (!isa<Instruction>(EqualValues[i])) { |
| 102 | // Found a non-instruction. Replace all instructions with the |
| 103 | // non-instruction. |
| 104 | // |
| 105 | Value *Replacement = EqualValues[i]; |
| 106 | |
| 107 | // Make sure we get I as well... |
| 108 | EqualValues[i] = I; |
| 109 | |
| 110 | // Replace all instructions with the Replacement value. |
| 111 | for (i = 0; i != e; ++i) |
| 112 | if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) { |
| 113 | // Change all users of I to use Replacement. |
| 114 | I->replaceAllUsesWith(Replacement); |
| 115 | |
| 116 | if (isa<LoadInst>(I)) |
| 117 | ++NumLoadRemoved; // Keep track of loads eliminated |
| 118 | ++NumInstRemoved; // Keep track of number of instructions eliminated |
| 119 | ++NumNonInsts; // Keep track of number of insts repl with values |
| 120 | |
| 121 | // Erase the instruction from the program. |
| 122 | I->getParent()->getInstList().erase(I); |
Chris Lattner | bea68b3 | 2003-06-17 03:57:18 +0000 | [diff] [blame] | 123 | WorkList.erase(I); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | // Remove duplicate entries from EqualValues... |
| 130 | std::sort(EqualValues.begin(), EqualValues.end()); |
| 131 | EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()), |
| 132 | EqualValues.end()); |
| 133 | |
| 134 | // From this point on, EqualValues is logically a vector of instructions. |
| 135 | // |
| 136 | bool Changed = false; |
| 137 | EqualValues.push_back(I); // Make sure I is included... |
| 138 | while (EqualValues.size() > 1) { |
| 139 | // FIXME, this could be done better than simple iteration! |
| 140 | Instruction *Test = cast<Instruction>(EqualValues.back()); |
| 141 | EqualValues.pop_back(); |
| 142 | |
| 143 | for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) |
| 144 | if (Instruction *Ret = EliminateCSE(Test, |
| 145 | cast<Instruction>(EqualValues[i]))) { |
| 146 | if (Ret == Test) // Eliminated EqualValues[i] |
| 147 | EqualValues[i] = Test; // Make sure that we reprocess I at some point |
| 148 | Changed = true; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | return Changed; |
| 153 | } |
| 154 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 155 | |
| 156 | // ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all |
| 157 | // uses of the instruction use First now instead. |
| 158 | // |
| 159 | void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 160 | Instruction &Second = *SI; |
Chris Lattner | 8b054c0 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 161 | |
| 162 | //cerr << "DEL " << (void*)Second << Second; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 163 | |
| 164 | // Add the first instruction back to the worklist |
| 165 | WorkList.insert(First); |
| 166 | |
| 167 | // Add all uses of the second instruction to the worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 168 | for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end(); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 169 | UI != UE; ++UI) |
| 170 | WorkList.insert(cast<Instruction>(*UI)); |
| 171 | |
| 172 | // Make all users of 'Second' now use 'First' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 173 | Second.replaceAllUsesWith(First); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 174 | |
| 175 | // Erase the second instruction from the program |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 176 | Second.getParent()->getInstList().erase(SI); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 179 | // EliminateCSE - The two instruction I & Other have been found to be common |
| 180 | // subexpressions. This function is responsible for eliminating one of them, |
| 181 | // and for fixing the worklist to be correct. The instruction that is preserved |
| 182 | // is returned from the function if the other is eliminated, otherwise null is |
| 183 | // returned. |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 184 | // |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 185 | Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) { |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 186 | assert(I != Other); |
Chris Lattner | 8b054c0 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 188 | WorkList.erase(I); |
Chris Lattner | 8b054c0 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 189 | WorkList.erase(Other); // Other may not actually be on the worklist anymore... |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 190 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 191 | // Handle the easy case, where both instructions are in the same basic block |
| 192 | BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent(); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 193 | Instruction *Ret = 0; |
| 194 | |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 195 | if (BB1 == BB2) { |
| 196 | // Eliminate the second occuring instruction. Add all uses of the second |
| 197 | // instruction to the worklist. |
| 198 | // |
| 199 | // Scan the basic block looking for the "first" instruction |
| 200 | BasicBlock::iterator BI = BB1->begin(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 201 | while (&*BI != I && &*BI != Other) { |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 202 | ++BI; |
| 203 | assert(BI != BB1->end() && "Instructions not found in parent BB!"); |
| 204 | } |
| 205 | |
| 206 | // Keep track of which instructions occurred first & second |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 207 | Instruction *First = BI; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 208 | Instruction *Second = I != First ? I : Other; // Get iterator to second inst |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 209 | BI = Second; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 210 | |
| 211 | // Destroy Second, using First instead. |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 212 | ReplaceInstWithInst(First, BI); |
| 213 | Ret = First; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 214 | |
| 215 | // Otherwise, the two instructions are in different basic blocks. If one |
| 216 | // dominates the other instruction, we can simply use it |
| 217 | // |
| 218 | } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other? |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 219 | ReplaceInstWithInst(I, Other); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 220 | Ret = I; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 221 | } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I? |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 222 | ReplaceInstWithInst(Other, I); |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 223 | Ret = Other; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 224 | } else { |
Chris Lattner | be1ecf6 | 2002-08-02 18:06:01 +0000 | [diff] [blame] | 225 | // This code is disabled because it has several problems: |
| 226 | // One, the actual assumption is wrong, as shown by this code: |
| 227 | // int "test"(int %X, int %Y) { |
| 228 | // %Z = add int %X, %Y |
| 229 | // ret int %Z |
| 230 | // Unreachable: |
| 231 | // %Q = add int %X, %Y |
| 232 | // ret int %Q |
| 233 | // } |
| 234 | // |
| 235 | // Here there are no shared dominators. Additionally, this had the habit of |
| 236 | // moving computations where they were not always computed. For example, in |
Chris Lattner | bac7458 | 2003-02-01 04:50:59 +0000 | [diff] [blame] | 237 | // a case like this: |
Chris Lattner | be1ecf6 | 2002-08-02 18:06:01 +0000 | [diff] [blame] | 238 | // if (c) { |
| 239 | // if (d) ... |
| 240 | // else ... X+Y ... |
| 241 | // } else { |
| 242 | // ... X+Y ... |
| 243 | // } |
| 244 | // |
| 245 | // In thiscase, the expression would be hoisted to outside the 'if' stmt, |
| 246 | // causing the expression to be evaluated, even for the if (d) path, which |
| 247 | // could cause problems, if, for example, it caused a divide by zero. In |
| 248 | // general the problem this case is trying to solve is better addressed with |
| 249 | // PRE than GCSE. |
| 250 | // |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 251 | return 0; |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 254 | if (isa<LoadInst>(Ret)) |
| 255 | ++NumLoadRemoved; // Keep track of loads eliminated |
| 256 | ++NumInstRemoved; // Keep track of number of instructions eliminated |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 258 | // Add all users of Ret to the worklist... |
| 259 | for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I) |
| 260 | if (Instruction *Inst = dyn_cast<Instruction>(*I)) |
| 261 | WorkList.insert(Inst); |
Chris Lattner | d80e973 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 14987f1 | 2002-08-30 20:22:29 +0000 | [diff] [blame] | 263 | return Ret; |
Chris Lattner | 18fb2a6 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 264 | } |