Chris Lattner | 1467f64 | 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 |
| 5 | // examining the SSA value graph of the function, instead of doing slow, dense, |
| 6 | // bit-vector computations. |
| 7 | // |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 11 | #include "llvm/InstrTypes.h" |
| 12 | #include "llvm/iMemory.h" |
| 13 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/InstVisitor.h" |
| 16 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 18 | #include "Support/StatisticReporter.h" |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 20 | using std::set; |
| 21 | using std::map; |
| 22 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 24 | static Statistic<> NumInstRemoved("gcse\t\t- Number of instructions removed"); |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 25 | static Statistic<> NumLoadRemoved("gcse\t\t- Number of loads removed"); |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | class GCSE : public FunctionPass, public InstVisitor<GCSE, bool> { |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 29 | set<Instruction*> WorkList; |
| 30 | DominatorSet *DomSetInfo; |
| 31 | ImmediateDominators *ImmDominator; |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 32 | AliasAnalysis *AA; |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 34 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 35 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 36 | |
| 37 | // Visitation methods, these are invoked depending on the type of |
| 38 | // instruction being checked. They should return true if a common |
| 39 | // subexpression was folded. |
| 40 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 41 | bool visitBinaryOperator(Instruction &I); |
| 42 | bool visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 43 | bool visitCastInst(CastInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 44 | bool visitShiftInst(ShiftInst &I) { |
| 45 | return visitBinaryOperator((Instruction&)I); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 47 | bool visitLoadInst(LoadInst &LI); |
| 48 | bool visitInstruction(Instruction &) { return false; } |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI); |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 52 | bool CommonSubExpressionFound(Instruction *I, Instruction *Other); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 53 | |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 54 | // TryToRemoveALoad - Try to remove one of L1 or L2. The problem with |
| 55 | // removing loads is that intervening stores might make otherwise identical |
| 56 | // load's yield different values. To ensure that this is not the case, we |
| 57 | // check that there are no intervening stores or calls between the |
| 58 | // instructions. |
| 59 | // |
| 60 | bool TryToRemoveALoad(LoadInst *L1, LoadInst *L2); |
| 61 | |
| 62 | // CheckForInvalidatingInst - Return true if BB or any of the predecessors |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 63 | // of BB (until DestBB) contain an instruction that might invalidate Ptr. |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 64 | // |
| 65 | bool CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB, |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 66 | Value *Ptr, set<BasicBlock*> &VisitedSet); |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 68 | // This transformation requires dominator and immediate dominator info |
| 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 70 | AU.preservesCFG(); |
Chris Lattner | f0ed55d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 71 | AU.addRequired<DominatorSet>(); |
| 72 | AU.addRequired<ImmediateDominators>(); |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 73 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 74 | } |
| 75 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 76 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 77 | RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination"); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // createGCSEPass - The public interface to this file... |
| 81 | Pass *createGCSEPass() { return new GCSE(); } |
| 82 | |
| 83 | |
| 84 | // GCSE::runOnFunction - This is the main transformation entry point for a |
| 85 | // function. |
| 86 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 87 | bool GCSE::runOnFunction(Function &F) { |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 88 | bool Changed = false; |
| 89 | |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 90 | // Get pointers to the analysis results that we will be using... |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 91 | DomSetInfo = &getAnalysis<DominatorSet>(); |
| 92 | ImmDominator = &getAnalysis<ImmediateDominators>(); |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 93 | AA = &getAnalysis<AliasAnalysis>(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 94 | |
| 95 | // Step #1: Add all instructions in the function to the worklist for |
| 96 | // processing. All of the instructions are considered to be our |
| 97 | // subexpressions to eliminate if possible. |
| 98 | // |
| 99 | WorkList.insert(inst_begin(F), inst_end(F)); |
| 100 | |
| 101 | // Step #2: WorkList processing. Iterate through all of the instructions, |
| 102 | // checking to see if there are any additionally defined subexpressions in the |
| 103 | // program. If so, eliminate them! |
| 104 | // |
| 105 | while (!WorkList.empty()) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 106 | Instruction &I = **WorkList.begin(); // Get an instruction from the worklist |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 107 | WorkList.erase(WorkList.begin()); |
| 108 | |
| 109 | // Visit the instruction, dispatching to the correct visit function based on |
| 110 | // the instruction type. This does the checking. |
| 111 | // |
| 112 | Changed |= visit(I); |
| 113 | } |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 115 | // When the worklist is empty, return whether or not we changed anything... |
| 116 | return Changed; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all |
| 121 | // uses of the instruction use First now instead. |
| 122 | // |
| 123 | void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 124 | Instruction &Second = *SI; |
Chris Lattner | 2dfc667 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 125 | |
| 126 | //cerr << "DEL " << (void*)Second << Second; |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 127 | |
| 128 | // Add the first instruction back to the worklist |
| 129 | WorkList.insert(First); |
| 130 | |
| 131 | // Add all uses of the second instruction to the worklist |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 132 | for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 133 | UI != UE; ++UI) |
| 134 | WorkList.insert(cast<Instruction>(*UI)); |
| 135 | |
| 136 | // Make all users of 'Second' now use 'First' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 137 | Second.replaceAllUsesWith(First); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 138 | |
| 139 | // Erase the second instruction from the program |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 140 | Second.getParent()->getInstList().erase(SI); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // CommonSubExpressionFound - The two instruction I & Other have been found to |
| 144 | // be common subexpressions. This function is responsible for eliminating one |
| 145 | // of them, and for fixing the worklist to be correct. |
| 146 | // |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 147 | bool GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) { |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 148 | assert(I != Other); |
Chris Lattner | 2dfc667 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 149 | |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 150 | WorkList.erase(I); |
Chris Lattner | 2dfc667 | 2002-04-29 16:20:25 +0000 | [diff] [blame] | 151 | WorkList.erase(Other); // Other may not actually be on the worklist anymore... |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 153 | ++NumInstRemoved; // Keep track of number of instructions eliminated |
| 154 | |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 155 | // Handle the easy case, where both instructions are in the same basic block |
| 156 | BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent(); |
| 157 | if (BB1 == BB2) { |
| 158 | // Eliminate the second occuring instruction. Add all uses of the second |
| 159 | // instruction to the worklist. |
| 160 | // |
| 161 | // Scan the basic block looking for the "first" instruction |
| 162 | BasicBlock::iterator BI = BB1->begin(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 163 | while (&*BI != I && &*BI != Other) { |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 164 | ++BI; |
| 165 | assert(BI != BB1->end() && "Instructions not found in parent BB!"); |
| 166 | } |
| 167 | |
| 168 | // Keep track of which instructions occurred first & second |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 169 | Instruction *First = BI; |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 170 | Instruction *Second = I != First ? I : Other; // Get iterator to second inst |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 171 | BI = Second; |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 172 | |
| 173 | // Destroy Second, using First instead. |
| 174 | ReplaceInstWithInst(First, BI); |
| 175 | |
| 176 | // Otherwise, the two instructions are in different basic blocks. If one |
| 177 | // dominates the other instruction, we can simply use it |
| 178 | // |
| 179 | } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other? |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 180 | ReplaceInstWithInst(I, Other); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 181 | } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I? |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 182 | ReplaceInstWithInst(Other, I); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 183 | } else { |
Chris Lattner | f56bd89 | 2002-08-02 18:06:01 +0000 | [diff] [blame] | 184 | // This code is disabled because it has several problems: |
| 185 | // One, the actual assumption is wrong, as shown by this code: |
| 186 | // int "test"(int %X, int %Y) { |
| 187 | // %Z = add int %X, %Y |
| 188 | // ret int %Z |
| 189 | // Unreachable: |
| 190 | // %Q = add int %X, %Y |
| 191 | // ret int %Q |
| 192 | // } |
| 193 | // |
| 194 | // Here there are no shared dominators. Additionally, this had the habit of |
| 195 | // moving computations where they were not always computed. For example, in |
| 196 | // a cast like this: |
| 197 | // if (c) { |
| 198 | // if (d) ... |
| 199 | // else ... X+Y ... |
| 200 | // } else { |
| 201 | // ... X+Y ... |
| 202 | // } |
| 203 | // |
| 204 | // In thiscase, the expression would be hoisted to outside the 'if' stmt, |
| 205 | // causing the expression to be evaluated, even for the if (d) path, which |
| 206 | // could cause problems, if, for example, it caused a divide by zero. In |
| 207 | // general the problem this case is trying to solve is better addressed with |
| 208 | // PRE than GCSE. |
| 209 | // |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 210 | return false; |
Chris Lattner | f56bd89 | 2002-08-02 18:06:01 +0000 | [diff] [blame] | 211 | |
| 212 | #if 0 |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 213 | // Handle the most general case now. In this case, neither I dom Other nor |
| 214 | // Other dom I. Because we are in SSA form, we are guaranteed that the |
| 215 | // operands of the two instructions both dominate the uses, so we _know_ |
| 216 | // that there must exist a block that dominates both instructions (if the |
| 217 | // operands of the instructions are globals or constants, worst case we |
| 218 | // would get the entry node of the function). Search for this block now. |
| 219 | // |
| 220 | |
| 221 | // Search up the immediate dominator chain of BB1 for the shared dominator |
| 222 | BasicBlock *SharedDom = (*ImmDominator)[BB1]; |
| 223 | while (!DomSetInfo->dominates(SharedDom, BB2)) |
| 224 | SharedDom = (*ImmDominator)[SharedDom]; |
| 225 | |
| 226 | // At this point, shared dom must dominate BOTH BB1 and BB2... |
| 227 | assert(SharedDom && DomSetInfo->dominates(SharedDom, BB1) && |
| 228 | DomSetInfo->dominates(SharedDom, BB2) && "Dominators broken!"); |
| 229 | |
| 230 | // Rip 'I' out of BB1, and move it to the end of SharedDom. |
| 231 | BB1->getInstList().remove(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 232 | SharedDom->getInstList().insert(--SharedDom->end(), I); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 233 | |
| 234 | // Eliminate 'Other' now. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 235 | ReplaceInstWithInst(I, Other); |
Chris Lattner | f56bd89 | 2002-08-02 18:06:01 +0000 | [diff] [blame] | 236 | #endif |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 238 | return true; |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | // |
| 243 | // Visitation methods, these are invoked depending on the type of instruction |
| 244 | // being checked. They should return true if a common subexpression was folded. |
| 245 | // |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | |
Chris Lattner | b80b69c | 2002-08-14 18:22:19 +0000 | [diff] [blame] | 248 | bool GCSE::visitCastInst(CastInst &CI) { |
| 249 | Instruction &I = (Instruction&)CI; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 250 | Value *Op = I.getOperand(0); |
| 251 | Function *F = I.getParent()->getParent(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 252 | |
| 253 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 254 | UI != UE; ++UI) |
| 255 | if (Instruction *Other = dyn_cast<Instruction>(*UI)) |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 256 | // Check to see if this new cast is not I, but has the same operand... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 257 | if (Other != &I && Other->getOpcode() == I.getOpcode() && |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 258 | Other->getOperand(0) == Op && // Is the operand the same? |
| 259 | // Is it embeded in the same function? (This could be false if LHS |
| 260 | // is a constant or global!) |
| 261 | Other->getParent()->getParent() == F && |
| 262 | |
| 263 | // Check that the types are the same, since this code handles casts... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 264 | Other->getType() == I.getType()) { |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 265 | |
| 266 | // These instructions are identical. Handle the situation. |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 267 | if (CommonSubExpressionFound(&I, Other)) |
| 268 | return true; // One instruction eliminated! |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
Chris Lattner | cd9837d | 2002-05-14 19:57:25 +0000 | [diff] [blame] | 274 | // isIdenticalBinaryInst - Return true if the two binary instructions are |
| 275 | // identical. |
| 276 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 277 | static inline bool isIdenticalBinaryInst(const Instruction &I1, |
Chris Lattner | cd9837d | 2002-05-14 19:57:25 +0000 | [diff] [blame] | 278 | const Instruction *I2) { |
| 279 | // Is it embeded in the same function? (This could be false if LHS |
| 280 | // is a constant or global!) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 281 | if (I1.getOpcode() != I2->getOpcode() || |
| 282 | I1.getParent()->getParent() != I2->getParent()->getParent()) |
Chris Lattner | cd9837d | 2002-05-14 19:57:25 +0000 | [diff] [blame] | 283 | return false; |
| 284 | |
| 285 | // They are identical if both operands are the same! |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 286 | if (I1.getOperand(0) == I2->getOperand(0) && |
| 287 | I1.getOperand(1) == I2->getOperand(1)) |
Chris Lattner | cd9837d | 2002-05-14 19:57:25 +0000 | [diff] [blame] | 288 | return true; |
| 289 | |
| 290 | // If the instruction is commutative and associative, the instruction can |
| 291 | // match if the operands are swapped! |
| 292 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 293 | if ((I1.getOperand(0) == I2->getOperand(1) && |
| 294 | I1.getOperand(1) == I2->getOperand(0)) && |
| 295 | (I1.getOpcode() == Instruction::Add || |
| 296 | I1.getOpcode() == Instruction::Mul || |
| 297 | I1.getOpcode() == Instruction::And || |
| 298 | I1.getOpcode() == Instruction::Or || |
| 299 | I1.getOpcode() == Instruction::Xor)) |
Chris Lattner | cd9837d | 2002-05-14 19:57:25 +0000 | [diff] [blame] | 300 | return true; |
| 301 | |
| 302 | return false; |
| 303 | } |
| 304 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 305 | bool GCSE::visitBinaryOperator(Instruction &I) { |
| 306 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 307 | Function *F = I.getParent()->getParent(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 308 | |
| 309 | for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end(); |
| 310 | UI != UE; ++UI) |
| 311 | if (Instruction *Other = dyn_cast<Instruction>(*UI)) |
| 312 | // Check to see if this new binary operator is not I, but same operand... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 313 | if (Other != &I && isIdenticalBinaryInst(I, Other)) { |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 314 | // These instructions are identical. Handle the situation. |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 315 | if (CommonSubExpressionFound(&I, Other)) |
| 316 | return true; // One instruction eliminated! |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return false; |
| 320 | } |
| 321 | |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 322 | // IdenticalComplexInst - Return true if the two instructions are the same, by |
| 323 | // using a brute force comparison. |
| 324 | // |
| 325 | static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) { |
| 326 | assert(I1->getOpcode() == I2->getOpcode()); |
| 327 | // Equal if they are in the same function... |
| 328 | return I1->getParent()->getParent() == I2->getParent()->getParent() && |
| 329 | // And return the same type... |
| 330 | I1->getType() == I2->getType() && |
| 331 | // And have the same number of operands... |
| 332 | I1->getNumOperands() == I2->getNumOperands() && |
| 333 | // And all of the operands are equal. |
| 334 | std::equal(I1->op_begin(), I1->op_end(), I2->op_begin()); |
| 335 | } |
| 336 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 337 | bool GCSE::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 338 | Value *Op = I.getOperand(0); |
| 339 | Function *F = I.getParent()->getParent(); |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 340 | |
| 341 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 342 | UI != UE; ++UI) |
| 343 | if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI)) |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 344 | // Check to see if this new getelementptr is not I, but same operand... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 345 | if (Other != &I && IdenticalComplexInst(&I, Other)) { |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 346 | // These instructions are identical. Handle the situation. |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 347 | if (CommonSubExpressionFound(&I, Other)) |
| 348 | return true; // One instruction eliminated! |
Chris Lattner | 1467f64 | 2002-04-28 00:47:11 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | return false; |
| 352 | } |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 354 | bool GCSE::visitLoadInst(LoadInst &LI) { |
| 355 | Value *Op = LI.getOperand(0); |
| 356 | Function *F = LI.getParent()->getParent(); |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 357 | |
| 358 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 359 | UI != UE; ++UI) |
| 360 | if (LoadInst *Other = dyn_cast<LoadInst>(*UI)) |
| 361 | // Check to see if this new load is not LI, but has the same operands... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 362 | if (Other != &LI && IdenticalComplexInst(&LI, Other) && |
| 363 | TryToRemoveALoad(&LI, Other)) |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 364 | return true; // An instruction was eliminated! |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 369 | // TryToRemoveALoad - Try to remove one of L1 or L2. The problem with removing |
| 370 | // loads is that intervening stores might make otherwise identical load's yield |
| 371 | // different values. To ensure that this is not the case, we check that there |
| 372 | // are no intervening stores or calls between the instructions. |
| 373 | // |
| 374 | bool GCSE::TryToRemoveALoad(LoadInst *L1, LoadInst *L2) { |
| 375 | // Figure out which load dominates the other one. If neither dominates the |
| 376 | // other we cannot eliminate one... |
| 377 | // |
| 378 | if (DomSetInfo->dominates(L2, L1)) |
| 379 | std::swap(L1, L2); // Make L1 dominate L2 |
| 380 | else if (!DomSetInfo->dominates(L1, L2)) |
| 381 | return false; // Neither instruction dominates the other one... |
| 382 | |
| 383 | BasicBlock *BB1 = L1->getParent(), *BB2 = L2->getParent(); |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 384 | Value *LoadAddress = L1->getOperand(0); |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 385 | |
| 386 | // L1 now dominates L2. Check to see if the intervening instructions between |
| 387 | // the two loads include a store or call... |
| 388 | // |
| 389 | if (BB1 == BB2) { // In same basic block? |
| 390 | // In this degenerate case, no checking of global basic blocks has to occur |
| 391 | // just check the instructions BETWEEN L1 & L2... |
| 392 | // |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 393 | if (AA->canInstructionRangeModify(*L1, *L2, LoadAddress)) |
| 394 | return false; // Cannot eliminate load |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 395 | |
| 396 | ++NumLoadRemoved; |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 397 | if (CommonSubExpressionFound(L1, L2)) |
| 398 | return true; |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 399 | } else { |
| 400 | // Make sure that there are no store instructions between L1 and the end of |
| 401 | // it's basic block... |
| 402 | // |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 403 | if (AA->canInstructionRangeModify(*L1, *BB1->getTerminator(), LoadAddress)) |
| 404 | return false; // Cannot eliminate load |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 405 | |
| 406 | // Make sure that there are no store instructions between the start of BB2 |
| 407 | // and the second load instruction... |
| 408 | // |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 409 | if (AA->canInstructionRangeModify(BB2->front(), *L2, LoadAddress)) |
| 410 | return false; // Cannot eliminate load |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 411 | |
| 412 | // Do a depth first traversal of the inverse CFG starting at L2's block, |
| 413 | // looking for L1's block. The inverse CFG is made up of the predecessor |
| 414 | // nodes of a block... so all of the edges in the graph are "backward". |
| 415 | // |
| 416 | set<BasicBlock*> VisitedSet; |
| 417 | for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI) |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 418 | if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, VisitedSet)) |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 419 | return false; |
| 420 | |
| 421 | ++NumLoadRemoved; |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 422 | return CommonSubExpressionFound(L1, L2); |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 423 | } |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // CheckForInvalidatingInst - Return true if BB or any of the predecessors of BB |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 428 | // (until DestBB) contain an instruction that might invalidate Ptr. |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 429 | // |
| 430 | bool GCSE::CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB, |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 431 | Value *Ptr, set<BasicBlock*> &VisitedSet) { |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 432 | // Found the termination point! |
| 433 | if (BB == DestBB || VisitedSet.count(BB)) return false; |
| 434 | |
| 435 | // Avoid infinite recursion! |
| 436 | VisitedSet.insert(BB); |
| 437 | |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 438 | // Can this basic block modify Ptr? |
| 439 | if (AA->canBasicBlockModify(*BB, Ptr)) |
| 440 | return true; |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 441 | |
| 442 | // Check all of our predecessor blocks... |
| 443 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) |
Chris Lattner | 879cb97 | 2002-08-22 18:24:48 +0000 | [diff] [blame] | 444 | if (CheckForInvalidatingInst(*PI, DestBB, Ptr, VisitedSet)) |
Chris Lattner | d38ddb1 | 2002-05-14 05:02:40 +0000 | [diff] [blame] | 445 | return true; |
| 446 | |
| 447 | // None of our predecessor blocks contain a store, and we don't either! |
| 448 | return false; |
| 449 | } |