1 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
3 // This file implements the non-abstract Value Numbering methods as well as a
4 // default implementation for the analysis group.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/ValueNumbering.h"
9 #include "llvm/Support/InstVisitor.h"
10 #include "llvm/BasicBlock.h"
11 #include "llvm/Pass.h"
12 #include "llvm/Type.h"
13 #include "llvm/iMemory.h"
15 // Register the ValueNumbering interface, providing a nice name to refer to.
16 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
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!
23 ValueNumbering::~ValueNumbering() {}
25 //===----------------------------------------------------------------------===//
26 // Basic ValueNumbering Pass Implementation
27 //===----------------------------------------------------------------------===//
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
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.
41 struct BasicVN : public ImmutablePass, public ValueNumbering {
42 /// getEqualNumberNodes - Return nodes with the same value number as the
43 /// specified Value. This fills in the argument vector with any equal
46 /// This is where our implementation is.
48 virtual void getEqualNumberNodes(Value *V1,
49 std::vector<Value*> &RetVals) const;
52 // Register this pass...
54 X("basicvn", "Basic Value Numbering (default GVN impl)");
56 // Declare that we implement the ValueNumbering interface
57 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
58 } // End of anonymous namespace
61 /// BVNImpl - Implement BasicVN in terms of a visitor class that
62 /// handles the different types of instructions as appropriate.
64 struct BVNImpl : public InstVisitor<BVNImpl> {
65 std::vector<Value*> &RetVals;
66 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
68 void handleBinaryInst(Instruction &I);
69 void visitBinaryOperator(BinaryOperator &I) {
70 handleBinaryInst((Instruction&)I);
72 void visitGetElementPtrInst(GetElementPtrInst &I);
73 void visitCastInst(CastInst &I);
74 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
75 void visitInstruction(Instruction &) {
76 // Cannot value number calls or terminator instructions...
81 // getEqualNumberNodes - Return nodes with the same value number as the
82 // specified Value. This fills in the argument vector with any equal values.
84 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
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);
92 void BVNImpl::visitCastInst(CastInst &CI) {
93 Instruction &I = (Instruction&)CI;
94 Value *Op = I.getOperand(0);
95 Function *F = I.getParent()->getParent();
97 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
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 embedded in the same function? (This could be false if LHS
104 // is a constant or global!)
105 Other->getParent()->getParent() == F &&
107 // Check that the types are the same, since this code handles casts...
108 Other->getType() == I.getType()) {
110 // These instructions are identical. Add to list...
111 RetVals.push_back(Other);
116 // isIdenticalBinaryInst - Return true if the two binary instructions are
119 static inline bool isIdenticalBinaryInst(const Instruction &I1,
120 const Instruction *I2) {
121 // Is it embedded 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())
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))
132 // If the instruction is commutative, the instruction can match if the
133 // operands are swapped!
135 if ((I1.getOperand(0) == I2->getOperand(1) &&
136 I1.getOperand(1) == I2->getOperand(0)) &&
143 void BVNImpl::handleBinaryInst(Instruction &I) {
144 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
145 Function *F = I.getParent()->getParent();
147 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
149 if (Instruction *Other = dyn_cast<Instruction>(*UI))
150 // Check to see if this new binary operator is not I, but same operand...
151 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
152 // These instructions are identical. Handle the situation.
153 RetVals.push_back(Other);
157 // IdenticalComplexInst - Return true if the two instructions are the same, by
158 // using a brute force comparison. This is useful for instructions with an
159 // arbitrary number of arguments.
161 static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
162 assert(I1->getOpcode() == I2->getOpcode());
163 // Equal if they are in the same function...
164 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
165 // And return the same type...
166 I1->getType() == I2->getType() &&
167 // And have the same number of operands...
168 I1->getNumOperands() == I2->getNumOperands() &&
169 // And all of the operands are equal.
170 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
173 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
174 Value *Op = I.getOperand(0);
175 Function *F = I.getParent()->getParent();
177 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
179 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
180 // Check to see if this new getelementptr is not I, but same operand...
181 if (Other != &I && IdenticalComplexInst(&I, Other)) {
182 // These instructions are identical. Handle the situation.
183 RetVals.push_back(Other);