Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 1 | //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===// |
| 2 | // |
| 3 | // This file implements the non-abstract Value Numbering methods as well as a |
| 4 | // default implementation for the analysis group. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 8 | #include "llvm/Analysis/ValueNumbering.h" |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 9 | #include "llvm/Support/InstVisitor.h" |
| 10 | #include "llvm/BasicBlock.h" |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 11 | #include "llvm/Pass.h" |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 12 | #include "llvm/Type.h" |
| 13 | #include "llvm/iMemory.h" |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 14 | |
| 15 | // Register the ValueNumbering interface, providing a nice name to refer to. |
| 16 | static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering"); |
| 17 | |
| 18 | /// ValueNumbering destructor: DO NOT move this to the header file for |
| 19 | /// ValueNumbering or else clients of the ValueNumbering class may not depend on |
| 20 | /// the ValueNumbering.o file in the current .a file, causing alias analysis |
| 21 | /// support to not be included in the tool correctly! |
| 22 | /// |
| 23 | ValueNumbering::~ValueNumbering() {} |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 26 | // Basic ValueNumbering Pass Implementation |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 29 | // Because of the way .a files work, the implementation of the BasicVN class |
| 30 | // MUST be in the ValueNumbering file itself, or else we run the risk of |
| 31 | // ValueNumbering being used, but the default implementation not being linked |
| 32 | // into the tool that uses it. As such, we register and implement the class |
| 33 | // here. |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 34 | // |
| 35 | namespace { |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 36 | /// BasicVN - This class is the default implementation of the ValueNumbering |
| 37 | /// interface. It walks the SSA def-use chains to trivially identify |
| 38 | /// lexically identical expressions. This does not require any ahead of time |
| 39 | /// analysis, so it is a very fast default implementation. |
| 40 | /// |
Chris Lattner | c3a3881 | 2002-09-25 22:27:25 +0000 | [diff] [blame^] | 41 | struct BasicVN : public ImmutablePass, public ValueNumbering { |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 42 | /// getEqualNumberNodes - Return nodes with the same value number as the |
| 43 | /// specified Value. This fills in the argument vector with any equal |
| 44 | /// values. |
| 45 | /// |
| 46 | /// This is where our implementation is. |
| 47 | /// |
| 48 | virtual void getEqualNumberNodes(Value *V1, |
| 49 | std::vector<Value*> &RetVals) const; |
| 50 | }; |
| 51 | |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 52 | // Register this pass... |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 53 | RegisterOpt<BasicVN> |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 54 | X("basicvn", "Basic Value Numbering (default GVN impl)"); |
| 55 | |
| 56 | // Declare that we implement the ValueNumbering interface |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 57 | RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y; |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 58 | } // End of anonymous namespace |
| 59 | |
| 60 | namespace { |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 61 | /// BVNImpl - Implement BasicVN in terms of a visitor class that |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 62 | /// handles the different types of instructions as appropriate. |
| 63 | /// |
| 64 | struct BVNImpl : public InstVisitor<BVNImpl> { |
| 65 | std::vector<Value*> &RetVals; |
| 66 | BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {} |
| 67 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 68 | void handleBinaryInst(Instruction &I); |
| 69 | void visitBinaryOperator(BinaryOperator &I) { |
| 70 | handleBinaryInst((Instruction&)I); |
| 71 | } |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 72 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 73 | void visitCastInst(CastInst &I); |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 74 | void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); } |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 75 | void visitInstruction(Instruction &) { |
| 76 | // Cannot value number calls or terminator instructions... |
| 77 | } |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | // getEqualNumberNodes - Return nodes with the same value number as the |
| 82 | // specified Value. This fills in the argument vector with any equal values. |
| 83 | // |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 84 | void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{ |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 85 | assert(V->getType() != Type::VoidTy && |
| 86 | "Can only value number non-void values!"); |
| 87 | // We can only handle the case where I is an instruction! |
| 88 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 89 | BVNImpl(RetVals).visit(I); |
| 90 | } |
| 91 | |
| 92 | void BVNImpl::visitCastInst(CastInst &CI) { |
| 93 | Instruction &I = (Instruction&)CI; |
| 94 | Value *Op = I.getOperand(0); |
| 95 | Function *F = I.getParent()->getParent(); |
| 96 | |
| 97 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 98 | UI != UE; ++UI) |
| 99 | if (Instruction *Other = dyn_cast<Instruction>(*UI)) |
| 100 | // Check to see if this new cast is not I, but has the same operand... |
| 101 | if (Other != &I && Other->getOpcode() == I.getOpcode() && |
| 102 | Other->getOperand(0) == Op && // Is the operand the same? |
| 103 | // Is it embeded in the same function? (This could be false if LHS |
| 104 | // is a constant or global!) |
| 105 | Other->getParent()->getParent() == F && |
| 106 | |
| 107 | // Check that the types are the same, since this code handles casts... |
| 108 | Other->getType() == I.getType()) { |
| 109 | |
| 110 | // These instructions are identical. Add to list... |
| 111 | RetVals.push_back(Other); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // isIdenticalBinaryInst - Return true if the two binary instructions are |
| 117 | // identical. |
| 118 | // |
| 119 | static inline bool isIdenticalBinaryInst(const Instruction &I1, |
| 120 | const Instruction *I2) { |
| 121 | // Is it embeded in the same function? (This could be false if LHS |
| 122 | // is a constant or global!) |
| 123 | if (I1.getOpcode() != I2->getOpcode() || |
| 124 | I1.getParent()->getParent() != I2->getParent()->getParent()) |
| 125 | return false; |
| 126 | |
| 127 | // They are identical if both operands are the same! |
| 128 | if (I1.getOperand(0) == I2->getOperand(0) && |
| 129 | I1.getOperand(1) == I2->getOperand(1)) |
| 130 | return true; |
| 131 | |
| 132 | // If the instruction is commutative and associative, the instruction can |
| 133 | // match if the operands are swapped! |
| 134 | // |
| 135 | if ((I1.getOperand(0) == I2->getOperand(1) && |
| 136 | I1.getOperand(1) == I2->getOperand(0)) && |
| 137 | (I1.getOpcode() == Instruction::Add || |
| 138 | I1.getOpcode() == Instruction::Mul || |
| 139 | I1.getOpcode() == Instruction::And || |
| 140 | I1.getOpcode() == Instruction::Or || |
| 141 | I1.getOpcode() == Instruction::Xor)) |
| 142 | return true; |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
Chris Lattner | 3e813b3 | 2002-08-30 22:30:36 +0000 | [diff] [blame] | 147 | void BVNImpl::handleBinaryInst(Instruction &I) { |
Chris Lattner | 74542b6 | 2002-08-30 20:29:02 +0000 | [diff] [blame] | 148 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 149 | Function *F = I.getParent()->getParent(); |
| 150 | |
| 151 | for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end(); |
| 152 | UI != UE; ++UI) |
| 153 | if (Instruction *Other = dyn_cast<Instruction>(*UI)) |
| 154 | // Check to see if this new binary operator is not I, but same operand... |
| 155 | if (Other != &I && isIdenticalBinaryInst(I, Other)) { |
| 156 | // These instructions are identical. Handle the situation. |
| 157 | RetVals.push_back(Other); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // IdenticalComplexInst - Return true if the two instructions are the same, by |
| 162 | // using a brute force comparison. This is useful for instructions with an |
| 163 | // arbitrary number of arguments. |
| 164 | // |
| 165 | static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) { |
| 166 | assert(I1->getOpcode() == I2->getOpcode()); |
| 167 | // Equal if they are in the same function... |
| 168 | return I1->getParent()->getParent() == I2->getParent()->getParent() && |
| 169 | // And return the same type... |
| 170 | I1->getType() == I2->getType() && |
| 171 | // And have the same number of operands... |
| 172 | I1->getNumOperands() == I2->getNumOperands() && |
| 173 | // And all of the operands are equal. |
| 174 | std::equal(I1->op_begin(), I1->op_end(), I2->op_begin()); |
| 175 | } |
| 176 | |
| 177 | void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 178 | Value *Op = I.getOperand(0); |
| 179 | Function *F = I.getParent()->getParent(); |
| 180 | |
| 181 | for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end(); |
| 182 | UI != UE; ++UI) |
| 183 | if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI)) |
| 184 | // Check to see if this new getelementptr is not I, but same operand... |
| 185 | if (Other != &I && IdenticalComplexInst(&I, Other)) { |
| 186 | // These instructions are identical. Handle the situation. |
| 187 | RetVals.push_back(Other); |
| 188 | } |
| 189 | } |