Minor fix.
[oota-llvm.git] / lib / Analysis / ValueNumbering.cpp
1 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the non-abstract Value Numbering methods as well as a
11 // default implementation for the analysis group.
12 //
13 //===----------------------------------------------------------------------===//
14
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 using namespace llvm;
23
24 // Register the ValueNumbering interface, providing a nice name to refer to.
25 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
26
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!
31 ///
32 ValueNumbering::~ValueNumbering() {}
33
34 //===----------------------------------------------------------------------===//
35 // Basic ValueNumbering Pass Implementation
36 //===----------------------------------------------------------------------===//
37 //
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
42 // here.
43 //
44
45 namespace {
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.
50   ///
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
54     /// values.
55     ///
56     /// This is where our implementation is.
57     ///
58     virtual void getEqualNumberNodes(Value *V1,
59                                      std::vector<Value*> &RetVals) const;
60   };
61
62   // Register this pass...
63   RegisterPass<BasicVN>
64   X("basicvn", "Basic Value Numbering (default GVN impl)");
65
66   // Declare that we implement the ValueNumbering interface
67   RegisterAnalysisGroup<ValueNumbering, true> Y(X);
68
69   /// BVNImpl - Implement BasicVN in terms of a visitor class that
70   /// handles the different types of instructions as appropriate.
71   ///
72   struct BVNImpl : public InstVisitor<BVNImpl> {
73     std::vector<Value*> &RetVals;
74     BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
75
76     void visitCastInst(CastInst &I);
77     void visitGetElementPtrInst(GetElementPtrInst &I);
78     void visitCmpInst(CmpInst &I);
79
80     void handleBinaryInst(Instruction &I);
81     void visitBinaryOperator(Instruction &I)     { handleBinaryInst(I); }
82     void visitShiftInst(Instruction &I)          { handleBinaryInst(I); }
83     void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
84
85     void handleTernaryInst(Instruction &I);
86     void visitSelectInst(Instruction &I)         { handleTernaryInst(I); }
87     void visitInsertElementInst(Instruction &I)  { handleTernaryInst(I); }
88     void visitShuffleVectorInst(Instruction &I)  { handleTernaryInst(I); }
89     void visitInstruction(Instruction &) {
90       // Cannot value number calls or terminator instructions.
91     }
92   };
93 }
94
95 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
96
97 // getEqualNumberNodes - Return nodes with the same value number as the
98 // specified Value.  This fills in the argument vector with any equal values.
99 //
100 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
101   assert(V->getType() != Type::VoidTy &&
102          "Can only value number non-void values!");
103   // We can only handle the case where I is an instruction!
104   if (Instruction *I = dyn_cast<Instruction>(V))
105     BVNImpl(RetVals).visit(I);
106 }
107
108 void BVNImpl::visitCastInst(CastInst &CI) {
109   Instruction &I = (Instruction&)CI;
110   Value *Op = I.getOperand(0);
111   Function *F = I.getParent()->getParent();
112
113   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
114        UI != UE; ++UI)
115     if (CastInst *Other = dyn_cast<CastInst>(*UI))
116       // Check that the opcode is the same
117       if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
118           // Check that the destination types are the same
119           Other->getType() == I.getType() &&
120           // Is it embedded in the same function?  (This could be false if LHS
121           // is a constant or global!)
122           Other->getParent()->getParent() == F &&
123           // Check to see if this new cast is not I.
124           Other != &I) {
125         // These instructions are identical.  Add to list...
126         RetVals.push_back(Other);
127       }
128 }
129
130 void  BVNImpl::visitCmpInst(CmpInst &CI1) {
131   Value *LHS = CI1.getOperand(0);
132   for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
133        UI != UE; ++UI)
134     if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
135       // Check to see if this compare instruction is not CI, but same opcode,
136       // same predicate, and in the same function.
137       if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
138           CI2->getPredicate() == CI1.getPredicate() &&
139           CI2->getParent()->getParent() == CI1.getParent()->getParent())
140         // If the operands are the same
141         if ((CI2->getOperand(0) == CI1.getOperand(0) &&
142             CI2->getOperand(1) == CI1.getOperand(1)) ||
143             // Or the compare is commutative and the operands are reversed 
144             (CI1.isCommutative() && 
145              CI2->getOperand(0) == CI1.getOperand(1) &&
146              CI2->getOperand(1) == CI1.getOperand(0)))
147           // Then the instructiosn are identical, add to list.
148           RetVals.push_back(CI2);
149 }
150
151
152
153 // isIdenticalBinaryInst - Return true if the two binary instructions are
154 // identical.
155 //
156 static inline bool isIdenticalBinaryInst(const Instruction &I1,
157                                          const Instruction *I2) {
158   // Is it embedded in the same function?  (This could be false if LHS
159   // is a constant or global!)
160   if (I1.getOpcode() != I2->getOpcode() ||
161       I1.getParent()->getParent() != I2->getParent()->getParent())
162     return false;
163
164   // If they are CmpInst instructions, check their predicates
165   if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
166     if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
167       return false;
168
169   // They are identical if both operands are the same!
170   if (I1.getOperand(0) == I2->getOperand(0) &&
171       I1.getOperand(1) == I2->getOperand(1))
172     return true;
173
174   // If the instruction is commutative, the instruction can match if the
175   // operands are swapped!
176   //
177   if ((I1.getOperand(0) == I2->getOperand(1) &&
178        I1.getOperand(1) == I2->getOperand(0)) &&
179       I1.isCommutative())
180     return true;
181
182   return false;
183 }
184
185 // isIdenticalTernaryInst - Return true if the two ternary instructions are
186 // identical.
187 //
188 static inline bool isIdenticalTernaryInst(const Instruction &I1,
189                                           const Instruction *I2) {
190   // Is it embedded in the same function?  (This could be false if LHS
191   // is a constant or global!)
192   if (I1.getParent()->getParent() != I2->getParent()->getParent())
193     return false;
194   
195   // They are identical if all operands are the same!
196   return I1.getOperand(0) == I2->getOperand(0) &&
197          I1.getOperand(1) == I2->getOperand(1) &&
198          I1.getOperand(2) == I2->getOperand(2);
199 }
200
201
202
203 void BVNImpl::handleBinaryInst(Instruction &I) {
204   Value *LHS = I.getOperand(0);
205
206   for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
207        UI != UE; ++UI)
208     if (Instruction *Other = dyn_cast<Instruction>(*UI))
209       // Check to see if this new binary operator is not I, but same operand...
210       if (Other != &I && isIdenticalBinaryInst(I, Other)) {
211         // These instructions are identical.  Handle the situation.
212         RetVals.push_back(Other);
213       }
214 }
215
216 // IdenticalComplexInst - Return true if the two instructions are the same, by
217 // using a brute force comparison.  This is useful for instructions with an
218 // arbitrary number of arguments.
219 //
220 static inline bool IdenticalComplexInst(const Instruction *I1,
221                                         const Instruction *I2) {
222   assert(I1->getOpcode() == I2->getOpcode());
223   // Equal if they are in the same function...
224   return I1->getParent()->getParent() == I2->getParent()->getParent() &&
225     // And return the same type...
226     I1->getType() == I2->getType() &&
227     // And have the same number of operands...
228     I1->getNumOperands() == I2->getNumOperands() &&
229     // And all of the operands are equal.
230     std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
231 }
232
233 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
234   Value *Op = I.getOperand(0);
235
236   // Try to pick a local operand if possible instead of a constant or a global
237   // that might have a lot of uses.
238   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
239     if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
240       Op = I.getOperand(i);
241       break;
242     }
243
244   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
245        UI != UE; ++UI)
246     if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
247       // Check to see if this new getelementptr is not I, but same operand...
248       if (Other != &I && IdenticalComplexInst(&I, Other)) {
249         // These instructions are identical.  Handle the situation.
250         RetVals.push_back(Other);
251       }
252 }
253
254 void BVNImpl::handleTernaryInst(Instruction &I) {
255   Value *Op0 = I.getOperand(0);
256   Instruction *OtherInst;
257   
258   for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
259        UI != UE; ++UI)
260     if ((OtherInst = dyn_cast<Instruction>(*UI)) && 
261         OtherInst->getOpcode() == I.getOpcode()) {
262       // Check to see if this new select is not I, but has the same operands.
263       if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
264         // These instructions are identical.  Handle the situation.
265         RetVals.push_back(OtherInst);
266       }
267         
268     }
269 }
270
271
272 // Ensure that users of ValueNumbering.h will link with this file
273 DEFINING_FILE_FOR(BasicValueNumbering)