blob: 2e1174b69e7ce5fa508b86a8414f25ff97bc2dfc [file] [log] [blame]
Chris Lattner74542b62002-08-30 20:29:02 +00001//===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
Chris Lattner3e813b32002-08-30 22:30:36 +000015#include "llvm/Analysis/ValueNumbering.h"
Chris Lattner74542b62002-08-30 20:29:02 +000016#include "llvm/Support/InstVisitor.h"
17#include "llvm/BasicBlock.h"
Chris Lattner40c57672004-02-11 03:57:16 +000018#include "llvm/Instructions.h"
Chris Lattner3e813b32002-08-30 22:30:36 +000019#include "llvm/Pass.h"
Chris Lattner74542b62002-08-30 20:29:02 +000020#include "llvm/Type.h"
Chris Lattner698c4a42004-03-25 22:56:03 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner74542b62002-08-30 20:29:02 +000023// Register the ValueNumbering interface, providing a nice name to refer to.
24static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
25
26/// ValueNumbering destructor: DO NOT move this to the header file for
27/// ValueNumbering or else clients of the ValueNumbering class may not depend on
28/// the ValueNumbering.o file in the current .a file, causing alias analysis
29/// support to not be included in the tool correctly!
30///
31ValueNumbering::~ValueNumbering() {}
32
33//===----------------------------------------------------------------------===//
Chris Lattner3e813b32002-08-30 22:30:36 +000034// Basic ValueNumbering Pass Implementation
Chris Lattner74542b62002-08-30 20:29:02 +000035//===----------------------------------------------------------------------===//
36//
Chris Lattner3e813b32002-08-30 22:30:36 +000037// Because of the way .a files work, the implementation of the BasicVN class
38// MUST be in the ValueNumbering file itself, or else we run the risk of
39// ValueNumbering being used, but the default implementation not being linked
40// into the tool that uses it. As such, we register and implement the class
41// here.
Chris Lattner74542b62002-08-30 20:29:02 +000042//
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Chris Lattner74542b62002-08-30 20:29:02 +000044namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000045 /// BasicVN - This class is the default implementation of the ValueNumbering
46 /// interface. It walks the SSA def-use chains to trivially identify
47 /// lexically identical expressions. This does not require any ahead of time
48 /// analysis, so it is a very fast default implementation.
49 ///
Chris Lattnerc3a38812002-09-25 22:27:25 +000050 struct BasicVN : public ImmutablePass, public ValueNumbering {
Chris Lattner3e813b32002-08-30 22:30:36 +000051 /// getEqualNumberNodes - Return nodes with the same value number as the
52 /// specified Value. This fills in the argument vector with any equal
53 /// values.
54 ///
55 /// This is where our implementation is.
56 ///
57 virtual void getEqualNumberNodes(Value *V1,
58 std::vector<Value*> &RetVals) const;
59 };
60
Chris Lattner74542b62002-08-30 20:29:02 +000061 // Register this pass...
Chris Lattner3e813b32002-08-30 22:30:36 +000062 RegisterOpt<BasicVN>
Chris Lattner74542b62002-08-30 20:29:02 +000063 X("basicvn", "Basic Value Numbering (default GVN impl)");
64
65 // Declare that we implement the ValueNumbering interface
Chris Lattner3e813b32002-08-30 22:30:36 +000066 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
Chris Lattner74542b62002-08-30 20:29:02 +000067
Chris Lattner3e813b32002-08-30 22:30:36 +000068 /// BVNImpl - Implement BasicVN in terms of a visitor class that
Chris Lattner74542b62002-08-30 20:29:02 +000069 /// handles the different types of instructions as appropriate.
70 ///
71 struct BVNImpl : public InstVisitor<BVNImpl> {
72 std::vector<Value*> &RetVals;
73 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
74
Chris Lattner3e813b32002-08-30 22:30:36 +000075 void handleBinaryInst(Instruction &I);
76 void visitBinaryOperator(BinaryOperator &I) {
77 handleBinaryInst((Instruction&)I);
78 }
Chris Lattner74542b62002-08-30 20:29:02 +000079 void visitGetElementPtrInst(GetElementPtrInst &I);
80 void visitCastInst(CastInst &I);
Chris Lattner3e813b32002-08-30 22:30:36 +000081 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
Chris Lattner74542b62002-08-30 20:29:02 +000082 void visitInstruction(Instruction &) {
83 // Cannot value number calls or terminator instructions...
84 }
85 };
86}
87
88// getEqualNumberNodes - Return nodes with the same value number as the
89// specified Value. This fills in the argument vector with any equal values.
90//
Chris Lattner3e813b32002-08-30 22:30:36 +000091void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +000092 assert(V->getType() != Type::VoidTy &&
93 "Can only value number non-void values!");
94 // We can only handle the case where I is an instruction!
95 if (Instruction *I = dyn_cast<Instruction>(V))
96 BVNImpl(RetVals).visit(I);
97}
98
99void BVNImpl::visitCastInst(CastInst &CI) {
100 Instruction &I = (Instruction&)CI;
101 Value *Op = I.getOperand(0);
102 Function *F = I.getParent()->getParent();
103
104 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
105 UI != UE; ++UI)
Chris Lattner40c57672004-02-11 03:57:16 +0000106 if (CastInst *Other = dyn_cast<CastInst>(*UI))
107 // Check that the types are the same, since this code handles casts...
108 if (Other->getType() == I.getType() &&
Misha Brukman2f2d0652003-09-11 18:14:24 +0000109 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000110 // is a constant or global!)
111 Other->getParent()->getParent() == F &&
Chris Lattner40c57672004-02-11 03:57:16 +0000112 // Check to see if this new cast is not I.
113 Other != &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000114 // These instructions are identical. Add to list...
115 RetVals.push_back(Other);
116 }
117}
118
119
120// isIdenticalBinaryInst - Return true if the two binary instructions are
121// identical.
122//
123static inline bool isIdenticalBinaryInst(const Instruction &I1,
124 const Instruction *I2) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000125 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000126 // is a constant or global!)
127 if (I1.getOpcode() != I2->getOpcode() ||
128 I1.getParent()->getParent() != I2->getParent()->getParent())
129 return false;
130
131 // They are identical if both operands are the same!
132 if (I1.getOperand(0) == I2->getOperand(0) &&
133 I1.getOperand(1) == I2->getOperand(1))
134 return true;
135
Chris Lattnerf37c3442002-10-31 04:20:07 +0000136 // If the instruction is commutative, the instruction can match if the
137 // operands are swapped!
Chris Lattner74542b62002-08-30 20:29:02 +0000138 //
139 if ((I1.getOperand(0) == I2->getOperand(1) &&
140 I1.getOperand(1) == I2->getOperand(0)) &&
Chris Lattnerf37c3442002-10-31 04:20:07 +0000141 I1.isCommutative())
Chris Lattner74542b62002-08-30 20:29:02 +0000142 return true;
143
144 return false;
145}
146
Chris Lattner3e813b32002-08-30 22:30:36 +0000147void BVNImpl::handleBinaryInst(Instruction &I) {
Reid Spencer1e296bf2004-12-23 21:13:26 +0000148 Value *LHS = I.getOperand(0);
Chris Lattner74542b62002-08-30 20:29:02 +0000149
150 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
151 UI != UE; ++UI)
152 if (Instruction *Other = dyn_cast<Instruction>(*UI))
153 // Check to see if this new binary operator is not I, but same operand...
154 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
155 // These instructions are identical. Handle the situation.
156 RetVals.push_back(Other);
157 }
158}
159
160// IdenticalComplexInst - Return true if the two instructions are the same, by
161// using a brute force comparison. This is useful for instructions with an
162// arbitrary number of arguments.
163//
Reid Spencer1e296bf2004-12-23 21:13:26 +0000164static inline bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
Chris Lattner74542b62002-08-30 20:29:02 +0000165 assert(I1->getOpcode() == I2->getOpcode());
166 // Equal if they are in the same function...
167 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
168 // And return the same type...
169 I1->getType() == I2->getType() &&
170 // And have the same number of operands...
171 I1->getNumOperands() == I2->getNumOperands() &&
172 // And all of the operands are equal.
173 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
174}
175
176void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
177 Value *Op = I.getOperand(0);
Chris Lattner698c4a42004-03-25 22:56:03 +0000178
179 // Try to pick a local operand if possible instead of a constant or a global
180 // that might have a lot of uses.
181 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
182 if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
183 Op = I.getOperand(i);
184 break;
185 }
Chris Lattner74542b62002-08-30 20:29:02 +0000186
187 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
188 UI != UE; ++UI)
189 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
190 // Check to see if this new getelementptr is not I, but same operand...
191 if (Other != &I && IdenticalComplexInst(&I, Other)) {
192 // These instructions are identical. Handle the situation.
193 RetVals.push_back(Other);
194 }
195}
Brian Gaeked0fde302003-11-11 22:41:34 +0000196
Chris Lattner698c4a42004-03-25 22:56:03 +0000197void llvm::BasicValueNumberingStub() { }