blob: 55323eaa9ed1be7b86aceb5246aa5d4d92756b21 [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//
Matthijs Kooijman9aac1db2008-06-05 07:55:49 +000013// 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).
16//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017//===----------------------------------------------------------------------===//
18
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"
27using namespace llvm;
28
29char ValueNumbering::ID = 0;
30// Register the ValueNumbering interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000031static RegisterAnalysisGroup<ValueNumbering> V("Value Numbering");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032
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!
37///
38ValueNumbering::~ValueNumbering() {}
39
40//===----------------------------------------------------------------------===//
41// Basic ValueNumbering Pass Implementation
42//===----------------------------------------------------------------------===//
43//
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
48// here.
49//
50
51namespace {
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.
56 ///
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) {}
61
62 /// getEqualNumberNodes - Return nodes with the same value number as the
63 /// specified Value. This fills in the argument vector with any equal
64 /// values.
65 ///
66 /// This is where our implementation is.
67 ///
68 virtual void getEqualNumberNodes(Value *V1,
69 std::vector<Value*> &RetVals) const;
70 };
Dan Gohman089efff2008-05-13 00:00:25 +000071}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
Dan Gohman089efff2008-05-13 00:00:25 +000073char BasicVN::ID = 0;
74// Register this pass...
75static RegisterPass<BasicVN>
76X("basicvn", "Basic Value Numbering (default GVN impl)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Dan Gohman089efff2008-05-13 00:00:25 +000078// Declare that we implement the ValueNumbering interface
79static RegisterAnalysisGroup<ValueNumbering, true> Y(X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Dan Gohman089efff2008-05-13 00:00:25 +000081namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 /// BVNImpl - Implement BasicVN in terms of a visitor class that
83 /// handles the different types of instructions as appropriate.
84 ///
85 struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
86 std::vector<Value*> &RetVals;
Dan Gohman751fed72007-10-29 19:52:04 +000087 explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 void visitCastInst(CastInst &I);
90 void visitGetElementPtrInst(GetElementPtrInst &I);
91 void visitCmpInst(CmpInst &I);
92
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); }
97
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.
104 }
105 };
106}
107
108ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
109
110// getEqualNumberNodes - Return nodes with the same value number as the
111// specified Value. This fills in the argument vector with any equal values.
112//
113void 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);
119}
120
121void BVNImpl::visitCastInst(CastInst &CI) {
122 Instruction &I = (Instruction&)CI;
123 Value *Op = I.getOperand(0);
124 Function *F = I.getParent()->getParent();
125
126 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
127 UI != UE; ++UI)
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.
137 Other != &I) {
138 // These instructions are identical. Add to list...
139 RetVals.push_back(Other);
140 }
141}
142
143void BVNImpl::visitCmpInst(CmpInst &CI1) {
144 Value *LHS = CI1.getOperand(0);
145 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
146 UI != UE; ++UI)
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);
162}
163
164
165
166// isIdenticalBinaryInst - Return true if the two binary instructions are
167// identical.
168//
169static 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())
175 return false;
176
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())
180 return false;
181
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))
185 return true;
186
187 // If the instruction is commutative, the instruction can match if the
188 // operands are swapped!
189 //
190 if ((I1.getOperand(0) == I2->getOperand(1) &&
191 I1.getOperand(1) == I2->getOperand(0)) &&
192 I1.isCommutative())
193 return true;
194
195 return false;
196}
197
198// isIdenticalTernaryInst - Return true if the two ternary instructions are
199// identical.
200//
201static 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())
206 return false;
207
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);
212}
213
214
215
216void BVNImpl::handleBinaryInst(Instruction &I) {
217 Value *LHS = I.getOperand(0);
218
219 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
220 UI != UE; ++UI)
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);
226 }
227}
228
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.
232//
233static 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());
244}
245
246void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
247 Value *Op = I.getOperand(0);
248
249 // Try to pick a local operand if possible instead of a constant or a global
250 // that might have a lot of uses.
Gabor Greiffe46c572008-05-29 00:51:08 +0000251 for (User::op_iterator i = I.op_begin() + 1, e = I.op_end(); i != e; ++i)
252 if (isa<Instruction>(*i) || isa<Argument>(*i)) {
253 Op = *i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 break;
255 }
256
257 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
258 UI != UE; ++UI)
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);
264 }
265}
266
267void BVNImpl::handleTernaryInst(Instruction &I) {
268 Value *Op0 = I.getOperand(0);
269 Instruction *OtherInst;
270
271 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
272 UI != UE; ++UI)
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);
279 }
280
281 }
282}
283
284
285// Ensure that users of ValueNumbering.h will link with this file
286DEFINING_FILE_FOR(BasicValueNumbering)