blob: 03b9f6b3eaee7d667515d6864e3ca7427679db4d [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"
Chris Lattner698c4a42004-03-25 22:56:03 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner74542b62002-08-30 20:29:02 +000024// Register the ValueNumbering interface, providing a nice name to refer to.
25static 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///
32ValueNumbering::~ValueNumbering() {}
33
34//===----------------------------------------------------------------------===//
Chris Lattner3e813b32002-08-30 22:30:36 +000035// Basic ValueNumbering Pass Implementation
Chris Lattner74542b62002-08-30 20:29:02 +000036//===----------------------------------------------------------------------===//
37//
Chris Lattner3e813b32002-08-30 22:30:36 +000038// 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.
Chris Lattner74542b62002-08-30 20:29:02 +000043//
Brian Gaeked0fde302003-11-11 22:41:34 +000044
Chris Lattner74542b62002-08-30 20:29:02 +000045namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000046 /// 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 ///
Chris Lattnerc3a38812002-09-25 22:27:25 +000051 struct BasicVN : public ImmutablePass, public ValueNumbering {
Chris Lattner3e813b32002-08-30 22:30:36 +000052 /// 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
Chris Lattner74542b62002-08-30 20:29:02 +000062 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000063 RegisterPass<BasicVN>
Chris Lattner74542b62002-08-30 20:29:02 +000064 X("basicvn", "Basic Value Numbering (default GVN impl)");
65
66 // Declare that we implement the ValueNumbering interface
Chris Lattnera5370172006-08-28 00:42:29 +000067 RegisterAnalysisGroup<ValueNumbering, true> Y(X);
Chris Lattner74542b62002-08-30 20:29:02 +000068
Chris Lattner3e813b32002-08-30 22:30:36 +000069 /// BVNImpl - Implement BasicVN in terms of a visitor class that
Chris Lattner74542b62002-08-30 20:29:02 +000070 /// 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
Chris Lattner74542b62002-08-30 20:29:02 +000076 void visitCastInst(CastInst &I);
Chris Lattner0fa07f92006-04-14 05:10:20 +000077 void visitGetElementPtrInst(GetElementPtrInst &I);
Reid Spencer45fb3f32006-11-20 01:22:35 +000078 void visitCmpInst(CmpInst &I);
Chris Lattner0fa07f92006-04-14 05:10:20 +000079
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); }
Chris Lattner74542b62002-08-30 20:29:02 +000089 void visitInstruction(Instruction &) {
Chris Lattner03f774a2006-02-04 09:15:29 +000090 // Cannot value number calls or terminator instructions.
Chris Lattner74542b62002-08-30 20:29:02 +000091 }
92 };
93}
94
Jeff Cohen534927d2005-01-08 22:01:16 +000095ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
96
Chris Lattner74542b62002-08-30 20:29:02 +000097// getEqualNumberNodes - Return nodes with the same value number as the
98// specified Value. This fills in the argument vector with any equal values.
99//
Chris Lattner3e813b32002-08-30 22:30:36 +0000100void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +0000101 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
108void BVNImpl::visitCastInst(CastInst &CI) {
109 Instruction &I = (Instruction&)CI;
110 Value *Op = I.getOperand(0);
111 Function *F = I.getParent()->getParent();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000112
Chris Lattner74542b62002-08-30 20:29:02 +0000113 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
114 UI != UE; ++UI)
Chris Lattner40c57672004-02-11 03:57:16 +0000115 if (CastInst *Other = dyn_cast<CastInst>(*UI))
116 // Check that the types are the same, since this code handles casts...
117 if (Other->getType() == I.getType() &&
Misha Brukman2f2d0652003-09-11 18:14:24 +0000118 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000119 // is a constant or global!)
120 Other->getParent()->getParent() == F &&
Chris Lattner40c57672004-02-11 03:57:16 +0000121 // Check to see if this new cast is not I.
122 Other != &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000123 // These instructions are identical. Add to list...
124 RetVals.push_back(Other);
125 }
126}
127
Reid Spencer45fb3f32006-11-20 01:22:35 +0000128void BVNImpl::visitCmpInst(CmpInst &CI1) {
129 Value *LHS = CI1.getOperand(0);
130 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
131 UI != UE; ++UI)
132 if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
133 // Check to see if this compare instruction is not CI, but same opcode,
134 // same predicate, and in the same function.
135 if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
136 CI2->getPredicate() == CI1.getPredicate() &&
137 CI2->getParent()->getParent() == CI1.getParent()->getParent())
138 // If the operands are the same
139 if ((CI2->getOperand(0) == CI1.getOperand(0) &&
140 CI2->getOperand(1) == CI1.getOperand(1)) ||
141 // Or the compare is commutative and the operands are reversed
142 (CI1.isCommutative() &&
143 CI2->getOperand(0) == CI1.getOperand(1) &&
144 CI2->getOperand(1) == CI1.getOperand(0)))
145 // Then the instructiosn are identical, add to list.
146 RetVals.push_back(CI2);
147}
148
149
Chris Lattner74542b62002-08-30 20:29:02 +0000150
151// isIdenticalBinaryInst - Return true if the two binary instructions are
152// identical.
153//
154static inline bool isIdenticalBinaryInst(const Instruction &I1,
155 const Instruction *I2) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000156 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000157 // is a constant or global!)
158 if (I1.getOpcode() != I2->getOpcode() ||
159 I1.getParent()->getParent() != I2->getParent()->getParent())
160 return false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000161
Chris Lattner74542b62002-08-30 20:29:02 +0000162 // They are identical if both operands are the same!
163 if (I1.getOperand(0) == I2->getOperand(0) &&
164 I1.getOperand(1) == I2->getOperand(1))
165 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000166
Chris Lattnerf37c3442002-10-31 04:20:07 +0000167 // If the instruction is commutative, the instruction can match if the
168 // operands are swapped!
Chris Lattner74542b62002-08-30 20:29:02 +0000169 //
170 if ((I1.getOperand(0) == I2->getOperand(1) &&
171 I1.getOperand(1) == I2->getOperand(0)) &&
Chris Lattnerf37c3442002-10-31 04:20:07 +0000172 I1.isCommutative())
Chris Lattner74542b62002-08-30 20:29:02 +0000173 return true;
174
175 return false;
176}
177
Chris Lattner0fa07f92006-04-14 05:10:20 +0000178// isIdenticalTernaryInst - Return true if the two ternary instructions are
179// identical.
180//
181static inline bool isIdenticalTernaryInst(const Instruction &I1,
182 const Instruction *I2) {
183 // Is it embedded in the same function? (This could be false if LHS
184 // is a constant or global!)
185 if (I1.getParent()->getParent() != I2->getParent()->getParent())
186 return false;
187
188 // They are identical if all operands are the same!
189 return I1.getOperand(0) == I2->getOperand(0) &&
190 I1.getOperand(1) == I2->getOperand(1) &&
191 I1.getOperand(2) == I2->getOperand(2);
192}
193
194
195
Chris Lattner3e813b32002-08-30 22:30:36 +0000196void BVNImpl::handleBinaryInst(Instruction &I) {
Reid Spencer1e296bf2004-12-23 21:13:26 +0000197 Value *LHS = I.getOperand(0);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000198
Chris Lattner74542b62002-08-30 20:29:02 +0000199 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
200 UI != UE; ++UI)
201 if (Instruction *Other = dyn_cast<Instruction>(*UI))
202 // Check to see if this new binary operator is not I, but same operand...
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000203 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
Chris Lattner74542b62002-08-30 20:29:02 +0000204 // These instructions are identical. Handle the situation.
205 RetVals.push_back(Other);
206 }
207}
208
209// IdenticalComplexInst - Return true if the two instructions are the same, by
210// using a brute force comparison. This is useful for instructions with an
211// arbitrary number of arguments.
212//
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000213static inline bool IdenticalComplexInst(const Instruction *I1,
Misha Brukman4d099f72004-12-23 21:17:41 +0000214 const Instruction *I2) {
Chris Lattner74542b62002-08-30 20:29:02 +0000215 assert(I1->getOpcode() == I2->getOpcode());
216 // Equal if they are in the same function...
217 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
218 // And return the same type...
219 I1->getType() == I2->getType() &&
220 // And have the same number of operands...
221 I1->getNumOperands() == I2->getNumOperands() &&
222 // And all of the operands are equal.
223 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
224}
225
226void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
227 Value *Op = I.getOperand(0);
Chris Lattner698c4a42004-03-25 22:56:03 +0000228
229 // Try to pick a local operand if possible instead of a constant or a global
230 // that might have a lot of uses.
231 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
232 if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
233 Op = I.getOperand(i);
234 break;
235 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000236
Chris Lattner74542b62002-08-30 20:29:02 +0000237 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
238 UI != UE; ++UI)
239 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
240 // Check to see if this new getelementptr is not I, but same operand...
241 if (Other != &I && IdenticalComplexInst(&I, Other)) {
242 // These instructions are identical. Handle the situation.
243 RetVals.push_back(Other);
244 }
245}
Brian Gaeked0fde302003-11-11 22:41:34 +0000246
Chris Lattner0fa07f92006-04-14 05:10:20 +0000247void BVNImpl::handleTernaryInst(Instruction &I) {
248 Value *Op0 = I.getOperand(0);
249 Instruction *OtherInst;
Chris Lattner03f774a2006-02-04 09:15:29 +0000250
Chris Lattner0fa07f92006-04-14 05:10:20 +0000251 for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
Chris Lattner03f774a2006-02-04 09:15:29 +0000252 UI != UE; ++UI)
Chris Lattner0fa07f92006-04-14 05:10:20 +0000253 if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
254 OtherInst->getOpcode() == I.getOpcode()) {
Chris Lattner03f774a2006-02-04 09:15:29 +0000255 // Check to see if this new select is not I, but has the same operands.
Chris Lattner0fa07f92006-04-14 05:10:20 +0000256 if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
Chris Lattner03f774a2006-02-04 09:15:29 +0000257 // These instructions are identical. Handle the situation.
Chris Lattner0fa07f92006-04-14 05:10:20 +0000258 RetVals.push_back(OtherInst);
Chris Lattner03f774a2006-02-04 09:15:29 +0000259 }
260
Chris Lattner0fa07f92006-04-14 05:10:20 +0000261 }
Chris Lattner03f774a2006-02-04 09:15:29 +0000262}
263
264
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000265// Ensure that users of ValueNumbering.h will link with this file
266DEFINING_FILE_FOR(BasicValueNumbering)