blob: 963ccb90f3ea7cacb9c3680d9c21b77789556318 [file] [log] [blame]
Chris Lattner74542b62002-08-30 20:29:02 +00001//===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner74542b62002-08-30 20:29:02 +00009//
10// This file implements the non-abstract Value Numbering methods as well as a
11// default implementation for the analysis group.
12//
13//===----------------------------------------------------------------------===//
14
Jeff Cohen534927d2005-01-08 22:01:16 +000015#include "llvm/Analysis/Passes.h"
Chris Lattner3e813b32002-08-30 22:30:36 +000016#include "llvm/Analysis/ValueNumbering.h"
Chris Lattner74542b62002-08-30 20:29:02 +000017#include "llvm/Support/InstVisitor.h"
18#include "llvm/BasicBlock.h"
Chris Lattner40c57672004-02-11 03:57:16 +000019#include "llvm/Instructions.h"
Chris Lattner3e813b32002-08-30 22:30:36 +000020#include "llvm/Pass.h"
Chris Lattner74542b62002-08-30 20:29:02 +000021#include "llvm/Type.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000022#include "llvm/Support/Compiler.h"
Chris Lattner698c4a42004-03-25 22:56:03 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner74542b62002-08-30 20:29:02 +000025// Register the ValueNumbering interface, providing a nice name to refer to.
26static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
27
28/// ValueNumbering destructor: DO NOT move this to the header file for
29/// ValueNumbering or else clients of the ValueNumbering class may not depend on
30/// the ValueNumbering.o file in the current .a file, causing alias analysis
31/// support to not be included in the tool correctly!
32///
33ValueNumbering::~ValueNumbering() {}
34
35//===----------------------------------------------------------------------===//
Chris Lattner3e813b32002-08-30 22:30:36 +000036// Basic ValueNumbering Pass Implementation
Chris Lattner74542b62002-08-30 20:29:02 +000037//===----------------------------------------------------------------------===//
38//
Chris Lattner3e813b32002-08-30 22:30:36 +000039// Because of the way .a files work, the implementation of the BasicVN class
40// MUST be in the ValueNumbering file itself, or else we run the risk of
41// ValueNumbering being used, but the default implementation not being linked
42// into the tool that uses it. As such, we register and implement the class
43// here.
Chris Lattner74542b62002-08-30 20:29:02 +000044//
Brian Gaeked0fde302003-11-11 22:41:34 +000045
Chris Lattner74542b62002-08-30 20:29:02 +000046namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000047 /// BasicVN - This class is the default implementation of the ValueNumbering
48 /// interface. It walks the SSA def-use chains to trivially identify
49 /// lexically identical expressions. This does not require any ahead of time
50 /// analysis, so it is a very fast default implementation.
51 ///
Reid Spencerd7d83db2007-02-05 23:42:17 +000052 struct VISIBILITY_HIDDEN BasicVN
53 : public ImmutablePass, public ValueNumbering {
Chris Lattner3e813b32002-08-30 22:30:36 +000054 /// getEqualNumberNodes - Return nodes with the same value number as the
55 /// specified Value. This fills in the argument vector with any equal
56 /// values.
57 ///
58 /// This is where our implementation is.
59 ///
60 virtual void getEqualNumberNodes(Value *V1,
61 std::vector<Value*> &RetVals) const;
62 };
63
Chris Lattner74542b62002-08-30 20:29:02 +000064 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000065 RegisterPass<BasicVN>
Chris Lattner74542b62002-08-30 20:29:02 +000066 X("basicvn", "Basic Value Numbering (default GVN impl)");
67
68 // Declare that we implement the ValueNumbering interface
Chris Lattnera5370172006-08-28 00:42:29 +000069 RegisterAnalysisGroup<ValueNumbering, true> Y(X);
Chris Lattner74542b62002-08-30 20:29:02 +000070
Chris Lattner3e813b32002-08-30 22:30:36 +000071 /// BVNImpl - Implement BasicVN in terms of a visitor class that
Chris Lattner74542b62002-08-30 20:29:02 +000072 /// handles the different types of instructions as appropriate.
73 ///
Reid Spencerd7d83db2007-02-05 23:42:17 +000074 struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
Chris Lattner74542b62002-08-30 20:29:02 +000075 std::vector<Value*> &RetVals;
76 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
77
Chris Lattner74542b62002-08-30 20:29:02 +000078 void visitCastInst(CastInst &I);
Chris Lattner0fa07f92006-04-14 05:10:20 +000079 void visitGetElementPtrInst(GetElementPtrInst &I);
Reid Spencer45fb3f32006-11-20 01:22:35 +000080 void visitCmpInst(CmpInst &I);
Chris Lattner0fa07f92006-04-14 05:10:20 +000081
82 void handleBinaryInst(Instruction &I);
83 void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); }
84 void visitShiftInst(Instruction &I) { handleBinaryInst(I); }
85 void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
86
87 void handleTernaryInst(Instruction &I);
88 void visitSelectInst(Instruction &I) { handleTernaryInst(I); }
89 void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); }
90 void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); }
Chris Lattner74542b62002-08-30 20:29:02 +000091 void visitInstruction(Instruction &) {
Chris Lattner03f774a2006-02-04 09:15:29 +000092 // Cannot value number calls or terminator instructions.
Chris Lattner74542b62002-08-30 20:29:02 +000093 }
94 };
95}
96
Jeff Cohen534927d2005-01-08 22:01:16 +000097ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
98
Chris Lattner74542b62002-08-30 20:29:02 +000099// getEqualNumberNodes - Return nodes with the same value number as the
100// specified Value. This fills in the argument vector with any equal values.
101//
Chris Lattner3e813b32002-08-30 22:30:36 +0000102void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +0000103 assert(V->getType() != Type::VoidTy &&
104 "Can only value number non-void values!");
105 // We can only handle the case where I is an instruction!
106 if (Instruction *I = dyn_cast<Instruction>(V))
107 BVNImpl(RetVals).visit(I);
108}
109
110void BVNImpl::visitCastInst(CastInst &CI) {
111 Instruction &I = (Instruction&)CI;
112 Value *Op = I.getOperand(0);
113 Function *F = I.getParent()->getParent();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000114
Chris Lattner74542b62002-08-30 20:29:02 +0000115 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
116 UI != UE; ++UI)
Chris Lattner40c57672004-02-11 03:57:16 +0000117 if (CastInst *Other = dyn_cast<CastInst>(*UI))
Reid Spencer3da59db2006-11-27 01:05:10 +0000118 // Check that the opcode is the same
119 if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
120 // Check that the destination types are the same
121 Other->getType() == I.getType() &&
Misha Brukman2f2d0652003-09-11 18:14:24 +0000122 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000123 // is a constant or global!)
124 Other->getParent()->getParent() == F &&
Chris Lattner40c57672004-02-11 03:57:16 +0000125 // Check to see if this new cast is not I.
126 Other != &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000127 // These instructions are identical. Add to list...
128 RetVals.push_back(Other);
129 }
130}
131
Reid Spencer45fb3f32006-11-20 01:22:35 +0000132void BVNImpl::visitCmpInst(CmpInst &CI1) {
133 Value *LHS = CI1.getOperand(0);
134 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
135 UI != UE; ++UI)
136 if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
137 // Check to see if this compare instruction is not CI, but same opcode,
138 // same predicate, and in the same function.
139 if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
140 CI2->getPredicate() == CI1.getPredicate() &&
141 CI2->getParent()->getParent() == CI1.getParent()->getParent())
142 // If the operands are the same
143 if ((CI2->getOperand(0) == CI1.getOperand(0) &&
144 CI2->getOperand(1) == CI1.getOperand(1)) ||
145 // Or the compare is commutative and the operands are reversed
146 (CI1.isCommutative() &&
147 CI2->getOperand(0) == CI1.getOperand(1) &&
148 CI2->getOperand(1) == CI1.getOperand(0)))
149 // Then the instructiosn are identical, add to list.
150 RetVals.push_back(CI2);
151}
152
153
Chris Lattner74542b62002-08-30 20:29:02 +0000154
155// isIdenticalBinaryInst - Return true if the two binary instructions are
156// identical.
157//
158static inline bool isIdenticalBinaryInst(const Instruction &I1,
159 const Instruction *I2) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000160 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000161 // is a constant or global!)
162 if (I1.getOpcode() != I2->getOpcode() ||
163 I1.getParent()->getParent() != I2->getParent()->getParent())
164 return false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000165
Reid Spencere4d87aa2006-12-23 06:05:41 +0000166 // If they are CmpInst instructions, check their predicates
167 if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
168 if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
169 return false;
170
Chris Lattner74542b62002-08-30 20:29:02 +0000171 // They are identical if both operands are the same!
172 if (I1.getOperand(0) == I2->getOperand(0) &&
173 I1.getOperand(1) == I2->getOperand(1))
174 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000175
Chris Lattnerf37c3442002-10-31 04:20:07 +0000176 // If the instruction is commutative, the instruction can match if the
177 // operands are swapped!
Chris Lattner74542b62002-08-30 20:29:02 +0000178 //
179 if ((I1.getOperand(0) == I2->getOperand(1) &&
180 I1.getOperand(1) == I2->getOperand(0)) &&
Chris Lattnerf37c3442002-10-31 04:20:07 +0000181 I1.isCommutative())
Chris Lattner74542b62002-08-30 20:29:02 +0000182 return true;
183
184 return false;
185}
186
Chris Lattner0fa07f92006-04-14 05:10:20 +0000187// isIdenticalTernaryInst - Return true if the two ternary instructions are
188// identical.
189//
190static inline bool isIdenticalTernaryInst(const Instruction &I1,
191 const Instruction *I2) {
192 // Is it embedded in the same function? (This could be false if LHS
193 // is a constant or global!)
194 if (I1.getParent()->getParent() != I2->getParent()->getParent())
195 return false;
196
197 // They are identical if all operands are the same!
198 return I1.getOperand(0) == I2->getOperand(0) &&
199 I1.getOperand(1) == I2->getOperand(1) &&
200 I1.getOperand(2) == I2->getOperand(2);
201}
202
203
204
Chris Lattner3e813b32002-08-30 22:30:36 +0000205void BVNImpl::handleBinaryInst(Instruction &I) {
Reid Spencer1e296bf2004-12-23 21:13:26 +0000206 Value *LHS = I.getOperand(0);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000207
Chris Lattner74542b62002-08-30 20:29:02 +0000208 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
209 UI != UE; ++UI)
210 if (Instruction *Other = dyn_cast<Instruction>(*UI))
211 // Check to see if this new binary operator is not I, but same operand...
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000212 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
Chris Lattner74542b62002-08-30 20:29:02 +0000213 // These instructions are identical. Handle the situation.
214 RetVals.push_back(Other);
215 }
216}
217
218// IdenticalComplexInst - Return true if the two instructions are the same, by
219// using a brute force comparison. This is useful for instructions with an
220// arbitrary number of arguments.
221//
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000222static inline bool IdenticalComplexInst(const Instruction *I1,
Misha Brukman4d099f72004-12-23 21:17:41 +0000223 const Instruction *I2) {
Chris Lattner74542b62002-08-30 20:29:02 +0000224 assert(I1->getOpcode() == I2->getOpcode());
225 // Equal if they are in the same function...
226 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
227 // And return the same type...
228 I1->getType() == I2->getType() &&
229 // And have the same number of operands...
230 I1->getNumOperands() == I2->getNumOperands() &&
231 // And all of the operands are equal.
232 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
233}
234
235void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
236 Value *Op = I.getOperand(0);
Chris Lattner698c4a42004-03-25 22:56:03 +0000237
238 // Try to pick a local operand if possible instead of a constant or a global
239 // that might have a lot of uses.
240 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
241 if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
242 Op = I.getOperand(i);
243 break;
244 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000245
Chris Lattner74542b62002-08-30 20:29:02 +0000246 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
247 UI != UE; ++UI)
248 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
249 // Check to see if this new getelementptr is not I, but same operand...
250 if (Other != &I && IdenticalComplexInst(&I, Other)) {
251 // These instructions are identical. Handle the situation.
252 RetVals.push_back(Other);
253 }
254}
Brian Gaeked0fde302003-11-11 22:41:34 +0000255
Chris Lattner0fa07f92006-04-14 05:10:20 +0000256void BVNImpl::handleTernaryInst(Instruction &I) {
257 Value *Op0 = I.getOperand(0);
258 Instruction *OtherInst;
Chris Lattner03f774a2006-02-04 09:15:29 +0000259
Chris Lattner0fa07f92006-04-14 05:10:20 +0000260 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
Chris Lattner03f774a2006-02-04 09:15:29 +0000261 UI != UE; ++UI)
Chris Lattner0fa07f92006-04-14 05:10:20 +0000262 if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
263 OtherInst->getOpcode() == I.getOpcode()) {
Chris Lattner03f774a2006-02-04 09:15:29 +0000264 // Check to see if this new select is not I, but has the same operands.
Chris Lattner0fa07f92006-04-14 05:10:20 +0000265 if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
Chris Lattner03f774a2006-02-04 09:15:29 +0000266 // These instructions are identical. Handle the situation.
Chris Lattner0fa07f92006-04-14 05:10:20 +0000267 RetVals.push_back(OtherInst);
Chris Lattner03f774a2006-02-04 09:15:29 +0000268 }
269
Chris Lattner0fa07f92006-04-14 05:10:20 +0000270 }
Chris Lattner03f774a2006-02-04 09:15:29 +0000271}
272
273
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000274// Ensure that users of ValueNumbering.h will link with this file
275DEFINING_FILE_FOR(BasicValueNumbering)