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 FunctionPass, public ValueNumbering {
43 /// Pass Implementation stuff. This isn't much of a pass.
45 bool runOnFunction(Function &) { return false; }
47 /// getAnalysisUsage - Does not modify anything.
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 /// getEqualNumberNodes - Return nodes with the same value number as the
54 /// specified Value. This fills in the argument vector with any equal
57 /// This is where our implementation is.
59 virtual void getEqualNumberNodes(Value *V1,
60 std::vector<Value*> &RetVals) const;
63 // Register this pass...
65 X("basicvn", "Basic Value Numbering (default GVN impl)");
67 // Declare that we implement the ValueNumbering interface
68 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
69 } // End of anonymous namespace
72 /// BVNImpl - Implement BasicVN in terms of a visitor class that
73 /// handles the different types of instructions as appropriate.
75 struct BVNImpl : public InstVisitor<BVNImpl> {
76 std::vector<Value*> &RetVals;
77 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
79 void handleBinaryInst(Instruction &I);
80 void visitBinaryOperator(BinaryOperator &I) {
81 handleBinaryInst((Instruction&)I);
83 void visitGetElementPtrInst(GetElementPtrInst &I);
84 void visitCastInst(CastInst &I);
85 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
86 void visitInstruction(Instruction &) {
87 // Cannot value number calls or terminator instructions...
92 // getEqualNumberNodes - Return nodes with the same value number as the
93 // specified Value. This fills in the argument vector with any equal values.
95 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
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);
103 void BVNImpl::visitCastInst(CastInst &CI) {
104 Instruction &I = (Instruction&)CI;
105 Value *Op = I.getOperand(0);
106 Function *F = I.getParent()->getParent();
108 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
110 if (Instruction *Other = dyn_cast<Instruction>(*UI))
111 // Check to see if this new cast is not I, but has the same operand...
112 if (Other != &I && Other->getOpcode() == I.getOpcode() &&
113 Other->getOperand(0) == Op && // Is the operand the same?
114 // Is it embeded in the same function? (This could be false if LHS
115 // is a constant or global!)
116 Other->getParent()->getParent() == F &&
118 // Check that the types are the same, since this code handles casts...
119 Other->getType() == I.getType()) {
121 // These instructions are identical. Add to list...
122 RetVals.push_back(Other);
127 // isIdenticalBinaryInst - Return true if the two binary instructions are
130 static inline bool isIdenticalBinaryInst(const Instruction &I1,
131 const Instruction *I2) {
132 // Is it embeded in the same function? (This could be false if LHS
133 // is a constant or global!)
134 if (I1.getOpcode() != I2->getOpcode() ||
135 I1.getParent()->getParent() != I2->getParent()->getParent())
138 // They are identical if both operands are the same!
139 if (I1.getOperand(0) == I2->getOperand(0) &&
140 I1.getOperand(1) == I2->getOperand(1))
143 // If the instruction is commutative and associative, the instruction can
144 // match if the operands are swapped!
146 if ((I1.getOperand(0) == I2->getOperand(1) &&
147 I1.getOperand(1) == I2->getOperand(0)) &&
148 (I1.getOpcode() == Instruction::Add ||
149 I1.getOpcode() == Instruction::Mul ||
150 I1.getOpcode() == Instruction::And ||
151 I1.getOpcode() == Instruction::Or ||
152 I1.getOpcode() == Instruction::Xor))
158 void BVNImpl::handleBinaryInst(Instruction &I) {
159 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
160 Function *F = I.getParent()->getParent();
162 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
164 if (Instruction *Other = dyn_cast<Instruction>(*UI))
165 // Check to see if this new binary operator is not I, but same operand...
166 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
167 // These instructions are identical. Handle the situation.
168 RetVals.push_back(Other);
172 // IdenticalComplexInst - Return true if the two instructions are the same, by
173 // using a brute force comparison. This is useful for instructions with an
174 // arbitrary number of arguments.
176 static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
177 assert(I1->getOpcode() == I2->getOpcode());
178 // Equal if they are in the same function...
179 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
180 // And return the same type...
181 I1->getType() == I2->getType() &&
182 // And have the same number of operands...
183 I1->getNumOperands() == I2->getNumOperands() &&
184 // And all of the operands are equal.
185 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
188 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
189 Value *Op = I.getOperand(0);
190 Function *F = I.getParent()->getParent();
192 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
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);