1 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
22 #include "llvm/Support/Compiler.h"
25 char ValueNumbering::ID = 0;
26 // Register the ValueNumbering interface, providing a nice name to refer to.
27 static RegisterAnalysisGroup<ValueNumbering> V("Value Numbering");
29 /// ValueNumbering destructor: DO NOT move this to the header file for
30 /// ValueNumbering or else clients of the ValueNumbering class may not depend on
31 /// the ValueNumbering.o file in the current .a file, causing alias analysis
32 /// support to not be included in the tool correctly!
34 ValueNumbering::~ValueNumbering() {}
36 //===----------------------------------------------------------------------===//
37 // Basic ValueNumbering Pass Implementation
38 //===----------------------------------------------------------------------===//
40 // Because of the way .a files work, the implementation of the BasicVN class
41 // MUST be in the ValueNumbering file itself, or else we run the risk of
42 // ValueNumbering being used, but the default implementation not being linked
43 // into the tool that uses it. As such, we register and implement the class
48 /// BasicVN - This class is the default implementation of the ValueNumbering
49 /// interface. It walks the SSA def-use chains to trivially identify
50 /// lexically identical expressions. This does not require any ahead of time
51 /// analysis, so it is a very fast default implementation.
53 struct VISIBILITY_HIDDEN BasicVN
54 : public ImmutablePass, public ValueNumbering {
55 static char ID; // Class identification, replacement for typeinfo
56 BasicVN() : ImmutablePass((intptr_t)&ID) {}
58 /// getEqualNumberNodes - Return nodes with the same value number as the
59 /// specified Value. This fills in the argument vector with any equal
62 /// This is where our implementation is.
64 virtual void getEqualNumberNodes(Value *V1,
65 std::vector<Value*> &RetVals) const;
70 // Register this pass...
71 static RegisterPass<BasicVN>
72 X("basicvn", "Basic Value Numbering (default GVN impl)", false, true);
74 // Declare that we implement the ValueNumbering interface
75 static RegisterAnalysisGroup<ValueNumbering, true> Y(X);
78 /// BVNImpl - Implement BasicVN in terms of a visitor class that
79 /// handles the different types of instructions as appropriate.
81 struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
82 std::vector<Value*> &RetVals;
83 explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
85 void visitCastInst(CastInst &I);
86 void visitGetElementPtrInst(GetElementPtrInst &I);
87 void visitCmpInst(CmpInst &I);
89 void handleBinaryInst(Instruction &I);
90 void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); }
91 void visitShiftInst(Instruction &I) { handleBinaryInst(I); }
92 void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
94 void handleTernaryInst(Instruction &I);
95 void visitSelectInst(Instruction &I) { handleTernaryInst(I); }
96 void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); }
97 void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); }
98 void visitInstruction(Instruction &) {
99 // Cannot value number calls or terminator instructions.
104 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
106 // getEqualNumberNodes - Return nodes with the same value number as the
107 // specified Value. This fills in the argument vector with any equal values.
109 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
110 assert(V->getType() != Type::VoidTy &&
111 "Can only value number non-void values!");
112 // We can only handle the case where I is an instruction!
113 if (Instruction *I = dyn_cast<Instruction>(V))
114 BVNImpl(RetVals).visit(I);
117 void BVNImpl::visitCastInst(CastInst &CI) {
118 Instruction &I = (Instruction&)CI;
119 Value *Op = I.getOperand(0);
120 Function *F = I.getParent()->getParent();
122 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
124 if (CastInst *Other = dyn_cast<CastInst>(*UI))
125 // Check that the opcode is the same
126 if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
127 // Check that the destination types are the same
128 Other->getType() == I.getType() &&
129 // Is it embedded in the same function? (This could be false if LHS
130 // is a constant or global!)
131 Other->getParent()->getParent() == F &&
132 // Check to see if this new cast is not I.
134 // These instructions are identical. Add to list...
135 RetVals.push_back(Other);
139 void BVNImpl::visitCmpInst(CmpInst &CI1) {
140 Value *LHS = CI1.getOperand(0);
141 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
143 if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
144 // Check to see if this compare instruction is not CI, but same opcode,
145 // same predicate, and in the same function.
146 if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
147 CI2->getPredicate() == CI1.getPredicate() &&
148 CI2->getParent()->getParent() == CI1.getParent()->getParent())
149 // If the operands are the same
150 if ((CI2->getOperand(0) == CI1.getOperand(0) &&
151 CI2->getOperand(1) == CI1.getOperand(1)) ||
152 // Or the compare is commutative and the operands are reversed
153 (CI1.isCommutative() &&
154 CI2->getOperand(0) == CI1.getOperand(1) &&
155 CI2->getOperand(1) == CI1.getOperand(0)))
156 // Then the instructiosn are identical, add to list.
157 RetVals.push_back(CI2);
162 // isIdenticalBinaryInst - Return true if the two binary instructions are
165 static inline bool isIdenticalBinaryInst(const Instruction &I1,
166 const Instruction *I2) {
167 // Is it embedded in the same function? (This could be false if LHS
168 // is a constant or global!)
169 if (I1.getOpcode() != I2->getOpcode() ||
170 I1.getParent()->getParent() != I2->getParent()->getParent())
173 // If they are CmpInst instructions, check their predicates
174 if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
175 if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
178 // They are identical if both operands are the same!
179 if (I1.getOperand(0) == I2->getOperand(0) &&
180 I1.getOperand(1) == I2->getOperand(1))
183 // If the instruction is commutative, the instruction can match if the
184 // operands are swapped!
186 if ((I1.getOperand(0) == I2->getOperand(1) &&
187 I1.getOperand(1) == I2->getOperand(0)) &&
194 // isIdenticalTernaryInst - Return true if the two ternary instructions are
197 static inline bool isIdenticalTernaryInst(const Instruction &I1,
198 const Instruction *I2) {
199 // Is it embedded in the same function? (This could be false if LHS
200 // is a constant or global!)
201 if (I1.getParent()->getParent() != I2->getParent()->getParent())
204 // They are identical if all operands are the same!
205 return I1.getOperand(0) == I2->getOperand(0) &&
206 I1.getOperand(1) == I2->getOperand(1) &&
207 I1.getOperand(2) == I2->getOperand(2);
212 void BVNImpl::handleBinaryInst(Instruction &I) {
213 Value *LHS = I.getOperand(0);
215 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
217 if (Instruction *Other = dyn_cast<Instruction>(*UI))
218 // Check to see if this new binary operator is not I, but same operand...
219 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
220 // These instructions are identical. Handle the situation.
221 RetVals.push_back(Other);
225 // IdenticalComplexInst - Return true if the two instructions are the same, by
226 // using a brute force comparison. This is useful for instructions with an
227 // arbitrary number of arguments.
229 static inline bool IdenticalComplexInst(const Instruction *I1,
230 const Instruction *I2) {
231 assert(I1->getOpcode() == I2->getOpcode());
232 // Equal if they are in the same function...
233 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
234 // And return the same type...
235 I1->getType() == I2->getType() &&
236 // And have the same number of operands...
237 I1->getNumOperands() == I2->getNumOperands() &&
238 // And all of the operands are equal.
239 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
242 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
243 Value *Op = I.getOperand(0);
245 // Try to pick a local operand if possible instead of a constant or a global
246 // that might have a lot of uses.
247 for (User::op_iterator i = I.op_begin() + 1, e = I.op_end(); i != e; ++i)
248 if (isa<Instruction>(*i) || isa<Argument>(*i)) {
253 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
255 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
256 // Check to see if this new getelementptr is not I, but same operand...
257 if (Other != &I && IdenticalComplexInst(&I, Other)) {
258 // These instructions are identical. Handle the situation.
259 RetVals.push_back(Other);
263 void BVNImpl::handleTernaryInst(Instruction &I) {
264 Value *Op0 = I.getOperand(0);
265 Instruction *OtherInst;
267 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
269 if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
270 OtherInst->getOpcode() == I.getOpcode()) {
271 // Check to see if this new select is not I, but has the same operands.
272 if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
273 // These instructions are identical. Handle the situation.
274 RetVals.push_back(OtherInst);
281 // Ensure that users of ValueNumbering.h will link with this file
282 DEFINING_FILE_FOR(BasicValueNumbering)