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 // The ValueNumbering analysis pass is mostly deprecated. It is only used by the
14 // Global Common Subexpression Elimination pass, which is deprecated by the
15 // Global Value Numbering pass (which does its value numbering on its own).
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/ValueNumbering.h"
21 #include "llvm/Support/InstVisitor.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Type.h"
26 #include "llvm/Support/Compiler.h"
29 char ValueNumbering::ID = 0;
30 // Register the ValueNumbering interface, providing a nice name to refer to.
31 static RegisterAnalysisGroup<ValueNumbering> V("Value Numbering");
33 /// ValueNumbering destructor: DO NOT move this to the header file for
34 /// ValueNumbering or else clients of the ValueNumbering class may not depend on
35 /// the ValueNumbering.o file in the current .a file, causing alias analysis
36 /// support to not be included in the tool correctly!
38 ValueNumbering::~ValueNumbering() {}
40 //===----------------------------------------------------------------------===//
41 // Basic ValueNumbering Pass Implementation
42 //===----------------------------------------------------------------------===//
44 // Because of the way .a files work, the implementation of the BasicVN class
45 // MUST be in the ValueNumbering file itself, or else we run the risk of
46 // ValueNumbering being used, but the default implementation not being linked
47 // into the tool that uses it. As such, we register and implement the class
52 /// BasicVN - This class is the default implementation of the ValueNumbering
53 /// interface. It walks the SSA def-use chains to trivially identify
54 /// lexically identical expressions. This does not require any ahead of time
55 /// analysis, so it is a very fast default implementation.
57 struct VISIBILITY_HIDDEN BasicVN
58 : public ImmutablePass, public ValueNumbering {
59 static char ID; // Class identification, replacement for typeinfo
60 BasicVN() : ImmutablePass((intptr_t)&ID) {}
62 /// getEqualNumberNodes - Return nodes with the same value number as the
63 /// specified Value. This fills in the argument vector with any equal
66 /// This is where our implementation is.
68 virtual void getEqualNumberNodes(Value *V1,
69 std::vector<Value*> &RetVals) const;
74 // Register this pass...
75 static RegisterPass<BasicVN>
76 X("basicvn", "Basic Value Numbering (default GVN impl)", false, true);
78 // Declare that we implement the ValueNumbering interface
79 static RegisterAnalysisGroup<ValueNumbering, true> Y(X);
82 /// BVNImpl - Implement BasicVN in terms of a visitor class that
83 /// handles the different types of instructions as appropriate.
85 struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
86 std::vector<Value*> &RetVals;
87 explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
89 void visitCastInst(CastInst &I);
90 void visitGetElementPtrInst(GetElementPtrInst &I);
91 void visitCmpInst(CmpInst &I);
93 void handleBinaryInst(Instruction &I);
94 void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); }
95 void visitShiftInst(Instruction &I) { handleBinaryInst(I); }
96 void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
98 void handleTernaryInst(Instruction &I);
99 void visitSelectInst(Instruction &I) { handleTernaryInst(I); }
100 void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); }
101 void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); }
102 void visitInstruction(Instruction &) {
103 // Cannot value number calls or terminator instructions.
108 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
110 // getEqualNumberNodes - Return nodes with the same value number as the
111 // specified Value. This fills in the argument vector with any equal values.
113 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
114 assert(V->getType() != Type::VoidTy &&
115 "Can only value number non-void values!");
116 // We can only handle the case where I is an instruction!
117 if (Instruction *I = dyn_cast<Instruction>(V))
118 BVNImpl(RetVals).visit(I);
121 void BVNImpl::visitCastInst(CastInst &CI) {
122 Instruction &I = (Instruction&)CI;
123 Value *Op = I.getOperand(0);
124 Function *F = I.getParent()->getParent();
126 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
128 if (CastInst *Other = dyn_cast<CastInst>(*UI))
129 // Check that the opcode is the same
130 if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
131 // Check that the destination types are the same
132 Other->getType() == I.getType() &&
133 // Is it embedded in the same function? (This could be false if LHS
134 // is a constant or global!)
135 Other->getParent()->getParent() == F &&
136 // Check to see if this new cast is not I.
138 // These instructions are identical. Add to list...
139 RetVals.push_back(Other);
143 void BVNImpl::visitCmpInst(CmpInst &CI1) {
144 Value *LHS = CI1.getOperand(0);
145 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
147 if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
148 // Check to see if this compare instruction is not CI, but same opcode,
149 // same predicate, and in the same function.
150 if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
151 CI2->getPredicate() == CI1.getPredicate() &&
152 CI2->getParent()->getParent() == CI1.getParent()->getParent())
153 // If the operands are the same
154 if ((CI2->getOperand(0) == CI1.getOperand(0) &&
155 CI2->getOperand(1) == CI1.getOperand(1)) ||
156 // Or the compare is commutative and the operands are reversed
157 (CI1.isCommutative() &&
158 CI2->getOperand(0) == CI1.getOperand(1) &&
159 CI2->getOperand(1) == CI1.getOperand(0)))
160 // Then the instructiosn are identical, add to list.
161 RetVals.push_back(CI2);
166 // isIdenticalBinaryInst - Return true if the two binary instructions are
169 static inline bool isIdenticalBinaryInst(const Instruction &I1,
170 const Instruction *I2) {
171 // Is it embedded in the same function? (This could be false if LHS
172 // is a constant or global!)
173 if (I1.getOpcode() != I2->getOpcode() ||
174 I1.getParent()->getParent() != I2->getParent()->getParent())
177 // If they are CmpInst instructions, check their predicates
178 if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
179 if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
182 // They are identical if both operands are the same!
183 if (I1.getOperand(0) == I2->getOperand(0) &&
184 I1.getOperand(1) == I2->getOperand(1))
187 // If the instruction is commutative, the instruction can match if the
188 // operands are swapped!
190 if ((I1.getOperand(0) == I2->getOperand(1) &&
191 I1.getOperand(1) == I2->getOperand(0)) &&
198 // isIdenticalTernaryInst - Return true if the two ternary instructions are
201 static inline bool isIdenticalTernaryInst(const Instruction &I1,
202 const Instruction *I2) {
203 // Is it embedded in the same function? (This could be false if LHS
204 // is a constant or global!)
205 if (I1.getParent()->getParent() != I2->getParent()->getParent())
208 // They are identical if all operands are the same!
209 return I1.getOperand(0) == I2->getOperand(0) &&
210 I1.getOperand(1) == I2->getOperand(1) &&
211 I1.getOperand(2) == I2->getOperand(2);
216 void BVNImpl::handleBinaryInst(Instruction &I) {
217 Value *LHS = I.getOperand(0);
219 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
221 if (Instruction *Other = dyn_cast<Instruction>(*UI))
222 // Check to see if this new binary operator is not I, but same operand...
223 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
224 // These instructions are identical. Handle the situation.
225 RetVals.push_back(Other);
229 // IdenticalComplexInst - Return true if the two instructions are the same, by
230 // using a brute force comparison. This is useful for instructions with an
231 // arbitrary number of arguments.
233 static inline bool IdenticalComplexInst(const Instruction *I1,
234 const Instruction *I2) {
235 assert(I1->getOpcode() == I2->getOpcode());
236 // Equal if they are in the same function...
237 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
238 // And return the same type...
239 I1->getType() == I2->getType() &&
240 // And have the same number of operands...
241 I1->getNumOperands() == I2->getNumOperands() &&
242 // And all of the operands are equal.
243 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
246 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
247 Value *Op = I.getOperand(0);
249 // Try to pick a local operand if possible instead of a constant or a global
250 // that might have a lot of uses.
251 for (User::op_iterator i = I.op_begin() + 1, e = I.op_end(); i != e; ++i)
252 if (isa<Instruction>(*i) || isa<Argument>(*i)) {
257 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
259 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
260 // Check to see if this new getelementptr is not I, but same operand...
261 if (Other != &I && IdenticalComplexInst(&I, Other)) {
262 // These instructions are identical. Handle the situation.
263 RetVals.push_back(Other);
267 void BVNImpl::handleTernaryInst(Instruction &I) {
268 Value *Op0 = I.getOperand(0);
269 Instruction *OtherInst;
271 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
273 if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
274 OtherInst->getOpcode() == I.getOpcode()) {
275 // Check to see if this new select is not I, but has the same operands.
276 if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
277 // These instructions are identical. Handle the situation.
278 RetVals.push_back(OtherInst);
285 // Ensure that users of ValueNumbering.h will link with this file
286 DEFINING_FILE_FOR(BasicValueNumbering)