Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 1 | //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the non-abstract Value Numbering methods as well as a |
| 11 | // default implementation for the analysis group. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ValueNumbering.h" |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 17 | #include "llvm/Support/InstVisitor.h" |
| 18 | #include "llvm/BasicBlock.h" |
Chris Lattner | 40c5767 | 2004-02-11 03:57:16 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 21 | #include "llvm/Type.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 698c4a4 | 2004-03-25 22:56:03 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 25 | const int ValueNumbering::ID = 0; |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 26 | // Register the ValueNumbering interface, providing a nice name to refer to. |
| 27 | static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering"); |
| 28 | |
| 29 | /// ValueNumbering destructor: DO NOT move this to the header file for |
| 30 | /// ValueNumbering or else clients of the ValueNumbering class may not depend on |
| 31 | /// the ValueNumbering.o file in the current .a file, causing alias analysis |
| 32 | /// support to not be included in the tool correctly! |
| 33 | /// |
| 34 | ValueNumbering::~ValueNumbering() {} |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 37 | // Basic ValueNumbering Pass Implementation |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 40 | // Because of the way .a files work, the implementation of the BasicVN class |
| 41 | // MUST be in the ValueNumbering file itself, or else we run the risk of |
| 42 | // ValueNumbering being used, but the default implementation not being linked |
| 43 | // into the tool that uses it. As such, we register and implement the class |
| 44 | // here. |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 45 | // |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 48 | /// BasicVN - This class is the default implementation of the ValueNumbering |
| 49 | /// interface. It walks the SSA def-use chains to trivially identify |
| 50 | /// lexically identical expressions. This does not require any ahead of time |
| 51 | /// analysis, so it is a very fast default implementation. |
| 52 | /// |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 53 | struct VISIBILITY_HIDDEN BasicVN |
| 54 | : public ImmutablePass, public ValueNumbering { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 55 | static const int ID; // Class identification, replacement for typeinfo |
| 56 | BasicVN() : ImmutablePass((intptr_t)&ID) {} |
| 57 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 58 | /// getEqualNumberNodes - Return nodes with the same value number as the |
| 59 | /// specified Value. This fills in the argument vector with any equal |
| 60 | /// values. |
| 61 | /// |
| 62 | /// This is where our implementation is. |
| 63 | /// |
| 64 | virtual void getEqualNumberNodes(Value *V1, |
| 65 | std::vector<Value*> &RetVals) const; |
| 66 | }; |
| 67 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 68 | const int BasicVN::ID = 0; |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 69 | // Register this pass... |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 70 | RegisterPass<BasicVN> |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 71 | X("basicvn", "Basic Value Numbering (default GVN impl)"); |
| 72 | |
| 73 | // Declare that we implement the ValueNumbering interface |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 74 | RegisterAnalysisGroup<ValueNumbering, true> Y(X); |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 76 | /// BVNImpl - Implement BasicVN in terms of a visitor class that |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 77 | /// handles the different types of instructions as appropriate. |
| 78 | /// |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 79 | struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> { |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 80 | std::vector<Value*> &RetVals; |
| 81 | BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {} |
| 82 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 83 | void visitCastInst(CastInst &I); |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 84 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 85 | void visitCmpInst(CmpInst &I); |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 86 | |
| 87 | void handleBinaryInst(Instruction &I); |
| 88 | void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); } |
| 89 | void visitShiftInst(Instruction &I) { handleBinaryInst(I); } |
| 90 | void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); } |
| 91 | |
| 92 | void handleTernaryInst(Instruction &I); |
| 93 | void visitSelectInst(Instruction &I) { handleTernaryInst(I); } |
| 94 | void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); } |
| 95 | void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); } |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 96 | void visitInstruction(Instruction &) { |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 97 | // Cannot value number calls or terminator instructions. |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 102 | ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); } |
| 103 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 104 | // getEqualNumberNodes - Return nodes with the same value number as the |
| 105 | // specified Value. This fills in the argument vector with any equal values. |
| 106 | // |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 107 | void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{ |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 108 | assert(V->getType() != Type::VoidTy && |
| 109 | "Can only value number non-void values!"); |
| 110 | // We can only handle the case where I is an instruction! |
| 111 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 112 | BVNImpl(RetVals).visit(I); |
| 113 | } |
| 114 | |
| 115 | void BVNImpl::visitCastInst(CastInst &CI) { |
| 116 | Instruction &I = (Instruction&)CI; |
| 117 | Value *Op = I.getOperand(0); |
| 118 | Function *F = I.getParent()->getParent(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 120 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 121 | UI != UE; ++UI) |
Chris Lattner | 40c5767 | 2004-02-11 03:57:16 +0000 | [diff] [blame] | 122 | if (CastInst *Other = dyn_cast<CastInst>(*UI)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 123 | // Check that the opcode is the same |
| 124 | if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) && |
| 125 | // Check that the destination types are the same |
| 126 | Other->getType() == I.getType() && |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 127 | // Is it embedded in the same function? (This could be false if LHS |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 128 | // is a constant or global!) |
| 129 | Other->getParent()->getParent() == F && |
Chris Lattner | 40c5767 | 2004-02-11 03:57:16 +0000 | [diff] [blame] | 130 | // Check to see if this new cast is not I. |
| 131 | Other != &I) { |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 132 | // These instructions are identical. Add to list... |
| 133 | RetVals.push_back(Other); |
| 134 | } |
| 135 | } |
| 136 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 137 | void BVNImpl::visitCmpInst(CmpInst &CI1) { |
| 138 | Value *LHS = CI1.getOperand(0); |
| 139 | for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end(); |
| 140 | UI != UE; ++UI) |
| 141 | if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI)) |
| 142 | // Check to see if this compare instruction is not CI, but same opcode, |
| 143 | // same predicate, and in the same function. |
| 144 | if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() && |
| 145 | CI2->getPredicate() == CI1.getPredicate() && |
| 146 | CI2->getParent()->getParent() == CI1.getParent()->getParent()) |
| 147 | // If the operands are the same |
| 148 | if ((CI2->getOperand(0) == CI1.getOperand(0) && |
| 149 | CI2->getOperand(1) == CI1.getOperand(1)) || |
| 150 | // Or the compare is commutative and the operands are reversed |
| 151 | (CI1.isCommutative() && |
| 152 | CI2->getOperand(0) == CI1.getOperand(1) && |
| 153 | CI2->getOperand(1) == CI1.getOperand(0))) |
| 154 | // Then the instructiosn are identical, add to list. |
| 155 | RetVals.push_back(CI2); |
| 156 | } |
| 157 | |
| 158 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 159 | |
| 160 | // isIdenticalBinaryInst - Return true if the two binary instructions are |
| 161 | // identical. |
| 162 | // |
| 163 | static inline bool isIdenticalBinaryInst(const Instruction &I1, |
| 164 | const Instruction *I2) { |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 165 | // Is it embedded in the same function? (This could be false if LHS |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 166 | // is a constant or global!) |
| 167 | if (I1.getOpcode() != I2->getOpcode() || |
| 168 | I1.getParent()->getParent() != I2->getParent()->getParent()) |
| 169 | return false; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 170 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 171 | // If they are CmpInst instructions, check their predicates |
| 172 | if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1))) |
| 173 | if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate()) |
| 174 | return false; |
| 175 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 176 | // They are identical if both operands are the same! |
| 177 | if (I1.getOperand(0) == I2->getOperand(0) && |
| 178 | I1.getOperand(1) == I2->getOperand(1)) |
| 179 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 180 | |
Chris Lattner | f37c344 | 2002-10-31 04:20:07 +0000 | [diff] [blame] | 181 | // If the instruction is commutative, the instruction can match if the |
| 182 | // operands are swapped! |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 183 | // |
| 184 | if ((I1.getOperand(0) == I2->getOperand(1) && |
| 185 | I1.getOperand(1) == I2->getOperand(0)) && |
Chris Lattner | f37c344 | 2002-10-31 04:20:07 +0000 | [diff] [blame] | 186 | I1.isCommutative()) |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 187 | return true; |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 192 | // isIdenticalTernaryInst - Return true if the two ternary instructions are |
| 193 | // identical. |
| 194 | // |
| 195 | static inline bool isIdenticalTernaryInst(const Instruction &I1, |
| 196 | const Instruction *I2) { |
| 197 | // Is it embedded in the same function? (This could be false if LHS |
| 198 | // is a constant or global!) |
| 199 | if (I1.getParent()->getParent() != I2->getParent()->getParent()) |
| 200 | return false; |
| 201 | |
| 202 | // They are identical if all operands are the same! |
| 203 | return I1.getOperand(0) == I2->getOperand(0) && |
| 204 | I1.getOperand(1) == I2->getOperand(1) && |
| 205 | I1.getOperand(2) == I2->getOperand(2); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 210 | void BVNImpl::handleBinaryInst(Instruction &I) { |
Reid Spencer | 1e296bf | 2004-12-23 21:13:26 +0000 | [diff] [blame] | 211 | Value *LHS = I.getOperand(0); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 213 | for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end(); |
| 214 | UI != UE; ++UI) |
| 215 | if (Instruction *Other = dyn_cast<Instruction>(*UI)) |
| 216 | // Check to see if this new binary operator is not I, but same operand... |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 217 | if (Other != &I && isIdenticalBinaryInst(I, Other)) { |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 218 | // These instructions are identical. Handle the situation. |
| 219 | RetVals.push_back(Other); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // IdenticalComplexInst - Return true if the two instructions are the same, by |
| 224 | // using a brute force comparison. This is useful for instructions with an |
| 225 | // arbitrary number of arguments. |
| 226 | // |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 227 | static inline bool IdenticalComplexInst(const Instruction *I1, |
Misha Brukman | 4d099f7 | 2004-12-23 21:17:41 +0000 | [diff] [blame] | 228 | const Instruction *I2) { |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 229 | assert(I1->getOpcode() == I2->getOpcode()); |
| 230 | // Equal if they are in the same function... |
| 231 | return I1->getParent()->getParent() == I2->getParent()->getParent() && |
| 232 | // And return the same type... |
| 233 | I1->getType() == I2->getType() && |
| 234 | // And have the same number of operands... |
| 235 | I1->getNumOperands() == I2->getNumOperands() && |
| 236 | // And all of the operands are equal. |
| 237 | std::equal(I1->op_begin(), I1->op_end(), I2->op_begin()); |
| 238 | } |
| 239 | |
| 240 | void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 241 | Value *Op = I.getOperand(0); |
Chris Lattner | 698c4a4 | 2004-03-25 22:56:03 +0000 | [diff] [blame] | 242 | |
| 243 | // Try to pick a local operand if possible instead of a constant or a global |
| 244 | // that might have a lot of uses. |
| 245 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) |
| 246 | if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) { |
| 247 | Op = I.getOperand(i); |
| 248 | break; |
| 249 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 251 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 252 | UI != UE; ++UI) |
| 253 | if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI)) |
| 254 | // Check to see if this new getelementptr is not I, but same operand... |
| 255 | if (Other != &I && IdenticalComplexInst(&I, Other)) { |
| 256 | // These instructions are identical. Handle the situation. |
| 257 | RetVals.push_back(Other); |
| 258 | } |
| 259 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 261 | void BVNImpl::handleTernaryInst(Instruction &I) { |
| 262 | Value *Op0 = I.getOperand(0); |
| 263 | Instruction *OtherInst; |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 265 | for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end(); |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 266 | UI != UE; ++UI) |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 267 | if ((OtherInst = dyn_cast<Instruction>(*UI)) && |
| 268 | OtherInst->getOpcode() == I.getOpcode()) { |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 269 | // Check to see if this new select is not I, but has the same operands. |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 270 | if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) { |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 271 | // These instructions are identical. Handle the situation. |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 272 | RetVals.push_back(OtherInst); |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 0fa07f9 | 2006-04-14 05:10:20 +0000 | [diff] [blame] | 275 | } |
Chris Lattner | 03f774a | 2006-02-04 09:15:29 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 279 | // Ensure that users of ValueNumbering.h will link with this file |
| 280 | DEFINING_FILE_FOR(BasicValueNumbering) |