1 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the non-abstract Value Numbering methods as well as a
11 // default implementation for the analysis group.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Analysis/ValueNumbering.h"
17 #include "llvm/Support/InstVisitor.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Type.h"
24 // Register the ValueNumbering interface, providing a nice name to refer to.
25 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
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!
32 ValueNumbering::~ValueNumbering() {}
34 //===----------------------------------------------------------------------===//
35 // Basic ValueNumbering Pass Implementation
36 //===----------------------------------------------------------------------===//
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
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.
51 struct BasicVN : public ImmutablePass, public ValueNumbering {
52 /// getEqualNumberNodes - Return nodes with the same value number as the
53 /// specified Value. This fills in the argument vector with any equal
56 /// This is where our implementation is.
58 virtual void getEqualNumberNodes(Value *V1,
59 std::vector<Value*> &RetVals) const;
62 // Register this pass...
64 X("basicvn", "Basic Value Numbering (default GVN impl)");
66 // Declare that we implement the ValueNumbering interface
67 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
69 /// BVNImpl - Implement BasicVN in terms of a visitor class that
70 /// handles the different types of instructions as appropriate.
72 struct BVNImpl : public InstVisitor<BVNImpl> {
73 std::vector<Value*> &RetVals;
74 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
76 void visitCastInst(CastInst &I);
77 void visitGetElementPtrInst(GetElementPtrInst &I);
79 void handleBinaryInst(Instruction &I);
80 void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); }
81 void visitShiftInst(Instruction &I) { handleBinaryInst(I); }
82 void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
84 void handleTernaryInst(Instruction &I);
85 void visitSelectInst(Instruction &I) { handleTernaryInst(I); }
86 void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); }
87 void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); }
88 void visitInstruction(Instruction &) {
89 // Cannot value number calls or terminator instructions.
94 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
96 // getEqualNumberNodes - Return nodes with the same value number as the
97 // specified Value. This fills in the argument vector with any equal values.
99 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
100 assert(V->getType() != Type::VoidTy &&
101 "Can only value number non-void values!");
102 // We can only handle the case where I is an instruction!
103 if (Instruction *I = dyn_cast<Instruction>(V))
104 BVNImpl(RetVals).visit(I);
107 void BVNImpl::visitCastInst(CastInst &CI) {
108 Instruction &I = (Instruction&)CI;
109 Value *Op = I.getOperand(0);
110 Function *F = I.getParent()->getParent();
112 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
114 if (CastInst *Other = dyn_cast<CastInst>(*UI))
115 // Check that the types are the same, since this code handles casts...
116 if (Other->getType() == I.getType() &&
117 // Is it embedded in the same function? (This could be false if LHS
118 // is a constant or global!)
119 Other->getParent()->getParent() == F &&
120 // Check to see if this new cast is not I.
122 // These instructions are identical. Add to list...
123 RetVals.push_back(Other);
128 // isIdenticalBinaryInst - Return true if the two binary instructions are
131 static inline bool isIdenticalBinaryInst(const Instruction &I1,
132 const Instruction *I2) {
133 // Is it embedded in the same function? (This could be false if LHS
134 // is a constant or global!)
135 if (I1.getOpcode() != I2->getOpcode() ||
136 I1.getParent()->getParent() != I2->getParent()->getParent())
139 // They are identical if both operands are the same!
140 if (I1.getOperand(0) == I2->getOperand(0) &&
141 I1.getOperand(1) == I2->getOperand(1))
144 // If the instruction is commutative, the instruction can match if the
145 // operands are swapped!
147 if ((I1.getOperand(0) == I2->getOperand(1) &&
148 I1.getOperand(1) == I2->getOperand(0)) &&
155 // isIdenticalTernaryInst - Return true if the two ternary instructions are
158 static inline bool isIdenticalTernaryInst(const Instruction &I1,
159 const Instruction *I2) {
160 // Is it embedded in the same function? (This could be false if LHS
161 // is a constant or global!)
162 if (I1.getParent()->getParent() != I2->getParent()->getParent())
165 // They are identical if all operands are the same!
166 return I1.getOperand(0) == I2->getOperand(0) &&
167 I1.getOperand(1) == I2->getOperand(1) &&
168 I1.getOperand(2) == I2->getOperand(2);
173 void BVNImpl::handleBinaryInst(Instruction &I) {
174 Value *LHS = I.getOperand(0);
176 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
178 if (Instruction *Other = dyn_cast<Instruction>(*UI))
179 // Check to see if this new binary operator is not I, but same operand...
180 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
181 // These instructions are identical. Handle the situation.
182 RetVals.push_back(Other);
186 // IdenticalComplexInst - Return true if the two instructions are the same, by
187 // using a brute force comparison. This is useful for instructions with an
188 // arbitrary number of arguments.
190 static inline bool IdenticalComplexInst(const Instruction *I1,
191 const Instruction *I2) {
192 assert(I1->getOpcode() == I2->getOpcode());
193 // Equal if they are in the same function...
194 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
195 // And return the same type...
196 I1->getType() == I2->getType() &&
197 // And have the same number of operands...
198 I1->getNumOperands() == I2->getNumOperands() &&
199 // And all of the operands are equal.
200 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
203 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
204 Value *Op = I.getOperand(0);
206 // Try to pick a local operand if possible instead of a constant or a global
207 // that might have a lot of uses.
208 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
209 if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
210 Op = I.getOperand(i);
214 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
216 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
217 // Check to see if this new getelementptr is not I, but same operand...
218 if (Other != &I && IdenticalComplexInst(&I, Other)) {
219 // These instructions are identical. Handle the situation.
220 RetVals.push_back(Other);
224 void BVNImpl::handleTernaryInst(Instruction &I) {
225 Value *Op0 = I.getOperand(0);
226 Instruction *OtherInst;
228 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
230 if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
231 OtherInst->getOpcode() == I.getOpcode()) {
232 // Check to see if this new select is not I, but has the same operands.
233 if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
234 // These instructions are identical. Handle the situation.
235 RetVals.push_back(OtherInst);
242 void llvm::BasicValueNumberingStub() { }