blob: 191e190e20e118905cf244cac5a821f9a5cc4309 [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 Lattner3e813b32002-08-30 22:30:36 +000018#include "llvm/Pass.h"
Chris Lattner74542b62002-08-30 20:29:02 +000019#include "llvm/Type.h"
20#include "llvm/iMemory.h"
Chris Lattner74542b62002-08-30 20:29:02 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
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 Lattner3e813b32002-08-30 22:30:36 +000063 RegisterOpt<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 Lattner3e813b32002-08-30 22:30:36 +000067 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
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 Lattner3e813b32002-08-30 22:30:36 +000076 void handleBinaryInst(Instruction &I);
77 void visitBinaryOperator(BinaryOperator &I) {
78 handleBinaryInst((Instruction&)I);
79 }
Chris Lattner74542b62002-08-30 20:29:02 +000080 void visitGetElementPtrInst(GetElementPtrInst &I);
81 void visitCastInst(CastInst &I);
Chris Lattner3e813b32002-08-30 22:30:36 +000082 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
Chris Lattner74542b62002-08-30 20:29:02 +000083 void visitInstruction(Instruction &) {
84 // Cannot value number calls or terminator instructions...
85 }
86 };
87}
88
89// getEqualNumberNodes - Return nodes with the same value number as the
90// specified Value. This fills in the argument vector with any equal values.
91//
Chris Lattner3e813b32002-08-30 22:30:36 +000092void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +000093 assert(V->getType() != Type::VoidTy &&
94 "Can only value number non-void values!");
95 // We can only handle the case where I is an instruction!
96 if (Instruction *I = dyn_cast<Instruction>(V))
97 BVNImpl(RetVals).visit(I);
98}
99
100void BVNImpl::visitCastInst(CastInst &CI) {
101 Instruction &I = (Instruction&)CI;
102 Value *Op = I.getOperand(0);
103 Function *F = I.getParent()->getParent();
104
105 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
106 UI != UE; ++UI)
107 if (Instruction *Other = dyn_cast<Instruction>(*UI))
108 // Check to see if this new cast is not I, but has the same operand...
109 if (Other != &I && Other->getOpcode() == I.getOpcode() &&
110 Other->getOperand(0) == Op && // Is the operand the same?
Misha Brukman2f2d0652003-09-11 18:14:24 +0000111 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000112 // is a constant or global!)
113 Other->getParent()->getParent() == F &&
114
115 // Check that the types are the same, since this code handles casts...
116 Other->getType() == I.getType()) {
117
118 // These instructions are identical. Add to list...
119 RetVals.push_back(Other);
120 }
121}
122
123
124// isIdenticalBinaryInst - Return true if the two binary instructions are
125// identical.
126//
127static inline bool isIdenticalBinaryInst(const Instruction &I1,
128 const Instruction *I2) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000129 // Is it embedded in the same function? (This could be false if LHS
Chris Lattner74542b62002-08-30 20:29:02 +0000130 // is a constant or global!)
131 if (I1.getOpcode() != I2->getOpcode() ||
132 I1.getParent()->getParent() != I2->getParent()->getParent())
133 return false;
134
135 // They are identical if both operands are the same!
136 if (I1.getOperand(0) == I2->getOperand(0) &&
137 I1.getOperand(1) == I2->getOperand(1))
138 return true;
139
Chris Lattnerf37c3442002-10-31 04:20:07 +0000140 // If the instruction is commutative, the instruction can match if the
141 // operands are swapped!
Chris Lattner74542b62002-08-30 20:29:02 +0000142 //
143 if ((I1.getOperand(0) == I2->getOperand(1) &&
144 I1.getOperand(1) == I2->getOperand(0)) &&
Chris Lattnerf37c3442002-10-31 04:20:07 +0000145 I1.isCommutative())
Chris Lattner74542b62002-08-30 20:29:02 +0000146 return true;
147
148 return false;
149}
150
Chris Lattner3e813b32002-08-30 22:30:36 +0000151void BVNImpl::handleBinaryInst(Instruction &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000152 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
153 Function *F = I.getParent()->getParent();
154
155 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
156 UI != UE; ++UI)
157 if (Instruction *Other = dyn_cast<Instruction>(*UI))
158 // Check to see if this new binary operator is not I, but same operand...
159 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
160 // These instructions are identical. Handle the situation.
161 RetVals.push_back(Other);
162 }
163}
164
165// IdenticalComplexInst - Return true if the two instructions are the same, by
166// using a brute force comparison. This is useful for instructions with an
167// arbitrary number of arguments.
168//
169static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
170 assert(I1->getOpcode() == I2->getOpcode());
171 // Equal if they are in the same function...
172 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
173 // And return the same type...
174 I1->getType() == I2->getType() &&
175 // And have the same number of operands...
176 I1->getNumOperands() == I2->getNumOperands() &&
177 // And all of the operands are equal.
178 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
179}
180
181void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
182 Value *Op = I.getOperand(0);
183 Function *F = I.getParent()->getParent();
184
185 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
186 UI != UE; ++UI)
187 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
188 // Check to see if this new getelementptr is not I, but same operand...
189 if (Other != &I && IdenticalComplexInst(&I, Other)) {
190 // These instructions are identical. Handle the situation.
191 RetVals.push_back(Other);
192 }
193}
Brian Gaeked0fde302003-11-11 22:41:34 +0000194
195} // End llvm namespace