Fixed spelling and grammar.
[oota-llvm.git] / lib / Analysis / ValueNumbering.cpp
1 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
2 //
3 // This file implements the non-abstract Value Numbering methods as well as a
4 // default implementation for the analysis group.
5 //
6 //===----------------------------------------------------------------------===//
7
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"
14
15 // Register the ValueNumbering interface, providing a nice name to refer to.
16 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
17
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!
22 ///
23 ValueNumbering::~ValueNumbering() {}
24
25 //===----------------------------------------------------------------------===//
26 // Basic ValueNumbering Pass Implementation
27 //===----------------------------------------------------------------------===//
28 //
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
33 // here.
34 //
35 namespace {
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.
40   ///
41   struct BasicVN : public ImmutablePass, public ValueNumbering {
42     /// getEqualNumberNodes - Return nodes with the same value number as the
43     /// specified Value.  This fills in the argument vector with any equal
44     /// values.
45     ///
46     /// This is where our implementation is.
47     ///
48     virtual void getEqualNumberNodes(Value *V1,
49                                      std::vector<Value*> &RetVals) const;
50   };
51
52   // Register this pass...
53   RegisterOpt<BasicVN>
54   X("basicvn", "Basic Value Numbering (default GVN impl)");
55
56   // Declare that we implement the ValueNumbering interface
57   RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
58 }  // End of anonymous namespace
59
60 namespace {
61   /// BVNImpl - Implement BasicVN in terms of a visitor class that
62   /// handles the different types of instructions as appropriate.
63   ///
64   struct BVNImpl : public InstVisitor<BVNImpl> {
65     std::vector<Value*> &RetVals;
66     BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
67
68     void handleBinaryInst(Instruction &I);
69     void visitBinaryOperator(BinaryOperator &I) {
70       handleBinaryInst((Instruction&)I);
71     }
72     void visitGetElementPtrInst(GetElementPtrInst &I);
73     void visitCastInst(CastInst &I);
74     void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
75     void visitInstruction(Instruction &) {
76       // Cannot value number calls or terminator instructions...
77     }
78   };
79 }
80
81 // getEqualNumberNodes - Return nodes with the same value number as the
82 // specified Value.  This fills in the argument vector with any equal values.
83 //
84 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
85   assert(V->getType() != Type::VoidTy &&
86          "Can only value number non-void values!");
87   // We can only handle the case where I is an instruction!
88   if (Instruction *I = dyn_cast<Instruction>(V))
89     BVNImpl(RetVals).visit(I);
90 }
91
92 void BVNImpl::visitCastInst(CastInst &CI) {
93   Instruction &I = (Instruction&)CI;
94   Value *Op = I.getOperand(0);
95   Function *F = I.getParent()->getParent();
96   
97   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
98        UI != UE; ++UI)
99     if (Instruction *Other = dyn_cast<Instruction>(*UI))
100       // Check to see if this new cast is not I, but has the same operand...
101       if (Other != &I && Other->getOpcode() == I.getOpcode() &&
102           Other->getOperand(0) == Op &&     // Is the operand the same?
103           // Is it embedded in the same function?  (This could be false if LHS
104           // is a constant or global!)
105           Other->getParent()->getParent() == F &&
106
107           // Check that the types are the same, since this code handles casts...
108           Other->getType() == I.getType()) {
109         
110         // These instructions are identical.  Add to list...
111         RetVals.push_back(Other);
112       }
113 }
114
115
116 // isIdenticalBinaryInst - Return true if the two binary instructions are
117 // identical.
118 //
119 static inline bool isIdenticalBinaryInst(const Instruction &I1,
120                                          const Instruction *I2) {
121   // Is it embedded in the same function?  (This could be false if LHS
122   // is a constant or global!)
123   if (I1.getOpcode() != I2->getOpcode() ||
124       I1.getParent()->getParent() != I2->getParent()->getParent())
125     return false;
126   
127   // They are identical if both operands are the same!
128   if (I1.getOperand(0) == I2->getOperand(0) &&
129       I1.getOperand(1) == I2->getOperand(1))
130     return true;
131   
132   // If the instruction is commutative, the instruction can match if the
133   // operands are swapped!
134   //
135   if ((I1.getOperand(0) == I2->getOperand(1) &&
136        I1.getOperand(1) == I2->getOperand(0)) &&
137       I1.isCommutative())
138     return true;
139
140   return false;
141 }
142
143 void BVNImpl::handleBinaryInst(Instruction &I) {
144   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
145   Function *F = I.getParent()->getParent();
146   
147   for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
148        UI != UE; ++UI)
149     if (Instruction *Other = dyn_cast<Instruction>(*UI))
150       // Check to see if this new binary operator is not I, but same operand...
151       if (Other != &I && isIdenticalBinaryInst(I, Other)) {        
152         // These instructions are identical.  Handle the situation.
153         RetVals.push_back(Other);
154       }
155 }
156
157 // IdenticalComplexInst - Return true if the two instructions are the same, by
158 // using a brute force comparison.  This is useful for instructions with an
159 // arbitrary number of arguments.
160 //
161 static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
162   assert(I1->getOpcode() == I2->getOpcode());
163   // Equal if they are in the same function...
164   return I1->getParent()->getParent() == I2->getParent()->getParent() &&
165     // And return the same type...
166     I1->getType() == I2->getType() &&
167     // And have the same number of operands...
168     I1->getNumOperands() == I2->getNumOperands() &&
169     // And all of the operands are equal.
170     std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
171 }
172
173 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
174   Value *Op = I.getOperand(0);
175   Function *F = I.getParent()->getParent();
176   
177   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
178        UI != UE; ++UI)
179     if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
180       // Check to see if this new getelementptr is not I, but same operand...
181       if (Other != &I && IdenticalComplexInst(&I, Other)) {
182         // These instructions are identical.  Handle the situation.
183         RetVals.push_back(Other);
184       }
185 }