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