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