blob: 1e4b306f62ec049fa9d3428030751671ca4c932e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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#include "llvm/Support/Compiler.h"
23using namespace llvm;
24
25char ValueNumbering::ID = 0;
26// Register the ValueNumbering interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000027static RegisterAnalysisGroup<ValueNumbering> V("Value Numbering");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
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!
33///
34ValueNumbering::~ValueNumbering() {}
35
36//===----------------------------------------------------------------------===//
37// Basic ValueNumbering Pass Implementation
38//===----------------------------------------------------------------------===//
39//
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
44// here.
45//
46
47namespace {
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.
52 ///
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) {}
57
58 /// getEqualNumberNodes - Return nodes with the same value number as the
59 /// specified Value. This fills in the argument vector with any equal
60 /// values.
61 ///
62 /// This is where our implementation is.
63 ///
64 virtual void getEqualNumberNodes(Value *V1,
65 std::vector<Value*> &RetVals) const;
66 };
Dan Gohman089efff2008-05-13 00:00:25 +000067}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
Dan Gohman089efff2008-05-13 00:00:25 +000069char BasicVN::ID = 0;
70// Register this pass...
71static RegisterPass<BasicVN>
72X("basicvn", "Basic Value Numbering (default GVN impl)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
Dan Gohman089efff2008-05-13 00:00:25 +000074// Declare that we implement the ValueNumbering interface
75static RegisterAnalysisGroup<ValueNumbering, true> Y(X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076
Dan Gohman089efff2008-05-13 00:00:25 +000077namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 /// BVNImpl - Implement BasicVN in terms of a visitor class that
79 /// handles the different types of instructions as appropriate.
80 ///
81 struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
82 std::vector<Value*> &RetVals;
Dan Gohman751fed72007-10-29 19:52:04 +000083 explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
85 void visitCastInst(CastInst &I);
86 void visitGetElementPtrInst(GetElementPtrInst &I);
87 void visitCmpInst(CmpInst &I);
88
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); }
93
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.
100 }
101 };
102}
103
104ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
105
106// getEqualNumberNodes - Return nodes with the same value number as the
107// specified Value. This fills in the argument vector with any equal values.
108//
109void 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);
115}
116
117void BVNImpl::visitCastInst(CastInst &CI) {
118 Instruction &I = (Instruction&)CI;
119 Value *Op = I.getOperand(0);
120 Function *F = I.getParent()->getParent();
121
122 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
123 UI != UE; ++UI)
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.
133 Other != &I) {
134 // These instructions are identical. Add to list...
135 RetVals.push_back(Other);
136 }
137}
138
139void BVNImpl::visitCmpInst(CmpInst &CI1) {
140 Value *LHS = CI1.getOperand(0);
141 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
142 UI != UE; ++UI)
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);
158}
159
160
161
162// isIdenticalBinaryInst - Return true if the two binary instructions are
163// identical.
164//
165static 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())
171 return false;
172
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())
176 return false;
177
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))
181 return true;
182
183 // If the instruction is commutative, the instruction can match if the
184 // operands are swapped!
185 //
186 if ((I1.getOperand(0) == I2->getOperand(1) &&
187 I1.getOperand(1) == I2->getOperand(0)) &&
188 I1.isCommutative())
189 return true;
190
191 return false;
192}
193
194// isIdenticalTernaryInst - Return true if the two ternary instructions are
195// identical.
196//
197static 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())
202 return false;
203
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);
208}
209
210
211
212void BVNImpl::handleBinaryInst(Instruction &I) {
213 Value *LHS = I.getOperand(0);
214
215 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
216 UI != UE; ++UI)
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);
222 }
223}
224
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.
228//
229static 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());
240}
241
242void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
243 Value *Op = I.getOperand(0);
244
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 (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
248 if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
249 Op = I.getOperand(i);
250 break;
251 }
252
253 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
254 UI != UE; ++UI)
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);
260 }
261}
262
263void BVNImpl::handleTernaryInst(Instruction &I) {
264 Value *Op0 = I.getOperand(0);
265 Instruction *OtherInst;
266
267 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
268 UI != UE; ++UI)
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);
275 }
276
277 }
278}
279
280
281// Ensure that users of ValueNumbering.h will link with this file
282DEFINING_FILE_FOR(BasicValueNumbering)