blob: 53d70e84b5732704b0b70e4a541724e844ed8683 [file] [log] [blame]
Chris Lattner74542b62002-08-30 20:29:02 +00001//===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
2//
3// This file implements the non-abstract Value Numbering methods as well as a
4// default implementation for the analysis group.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattner3e813b32002-08-30 22:30:36 +00008#include "llvm/Analysis/ValueNumbering.h"
Chris Lattner74542b62002-08-30 20:29:02 +00009#include "llvm/Support/InstVisitor.h"
10#include "llvm/BasicBlock.h"
Chris Lattner3e813b32002-08-30 22:30:36 +000011#include "llvm/Pass.h"
Chris Lattner74542b62002-08-30 20:29:02 +000012#include "llvm/Type.h"
13#include "llvm/iMemory.h"
Chris Lattner74542b62002-08-30 20:29:02 +000014
15// Register the ValueNumbering interface, providing a nice name to refer to.
16static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
17
18/// ValueNumbering destructor: DO NOT move this to the header file for
19/// ValueNumbering or else clients of the ValueNumbering class may not depend on
20/// the ValueNumbering.o file in the current .a file, causing alias analysis
21/// support to not be included in the tool correctly!
22///
23ValueNumbering::~ValueNumbering() {}
24
25//===----------------------------------------------------------------------===//
Chris Lattner3e813b32002-08-30 22:30:36 +000026// Basic ValueNumbering Pass Implementation
Chris Lattner74542b62002-08-30 20:29:02 +000027//===----------------------------------------------------------------------===//
28//
Chris Lattner3e813b32002-08-30 22:30:36 +000029// Because of the way .a files work, the implementation of the BasicVN class
30// MUST be in the ValueNumbering file itself, or else we run the risk of
31// ValueNumbering being used, but the default implementation not being linked
32// into the tool that uses it. As such, we register and implement the class
33// here.
Chris Lattner74542b62002-08-30 20:29:02 +000034//
35namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000036 /// BasicVN - This class is the default implementation of the ValueNumbering
37 /// interface. It walks the SSA def-use chains to trivially identify
38 /// lexically identical expressions. This does not require any ahead of time
39 /// analysis, so it is a very fast default implementation.
40 ///
Chris Lattnerc3a38812002-09-25 22:27:25 +000041 struct BasicVN : public ImmutablePass, public ValueNumbering {
Chris Lattner3e813b32002-08-30 22:30:36 +000042 /// getEqualNumberNodes - Return nodes with the same value number as the
43 /// specified Value. This fills in the argument vector with any equal
44 /// values.
45 ///
46 /// This is where our implementation is.
47 ///
48 virtual void getEqualNumberNodes(Value *V1,
49 std::vector<Value*> &RetVals) const;
50 };
51
Chris Lattner74542b62002-08-30 20:29:02 +000052 // Register this pass...
Chris Lattner3e813b32002-08-30 22:30:36 +000053 RegisterOpt<BasicVN>
Chris Lattner74542b62002-08-30 20:29:02 +000054 X("basicvn", "Basic Value Numbering (default GVN impl)");
55
56 // Declare that we implement the ValueNumbering interface
Chris Lattner3e813b32002-08-30 22:30:36 +000057 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
Chris Lattner74542b62002-08-30 20:29:02 +000058} // End of anonymous namespace
59
60namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000061 /// BVNImpl - Implement BasicVN in terms of a visitor class that
Chris Lattner74542b62002-08-30 20:29:02 +000062 /// handles the different types of instructions as appropriate.
63 ///
64 struct BVNImpl : public InstVisitor<BVNImpl> {
65 std::vector<Value*> &RetVals;
66 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
67
Chris Lattner3e813b32002-08-30 22:30:36 +000068 void handleBinaryInst(Instruction &I);
69 void visitBinaryOperator(BinaryOperator &I) {
70 handleBinaryInst((Instruction&)I);
71 }
Chris Lattner74542b62002-08-30 20:29:02 +000072 void visitGetElementPtrInst(GetElementPtrInst &I);
73 void visitCastInst(CastInst &I);
Chris Lattner3e813b32002-08-30 22:30:36 +000074 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
Chris Lattner74542b62002-08-30 20:29:02 +000075 void visitInstruction(Instruction &) {
76 // Cannot value number calls or terminator instructions...
77 }
78 };
79}
80
81// getEqualNumberNodes - Return nodes with the same value number as the
82// specified Value. This fills in the argument vector with any equal values.
83//
Chris Lattner3e813b32002-08-30 22:30:36 +000084void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +000085 assert(V->getType() != Type::VoidTy &&
86 "Can only value number non-void values!");
87 // We can only handle the case where I is an instruction!
88 if (Instruction *I = dyn_cast<Instruction>(V))
89 BVNImpl(RetVals).visit(I);
90}
91
92void BVNImpl::visitCastInst(CastInst &CI) {
93 Instruction &I = (Instruction&)CI;
94 Value *Op = I.getOperand(0);
95 Function *F = I.getParent()->getParent();
96
97 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
98 UI != UE; ++UI)
99 if (Instruction *Other = dyn_cast<Instruction>(*UI))
100 // Check to see if this new cast is not I, but has the same operand...
101 if (Other != &I && Other->getOpcode() == I.getOpcode() &&
102 Other->getOperand(0) == Op && // Is the operand the same?
103 // Is it embeded in the same function? (This could be false if LHS
104 // is a constant or global!)
105 Other->getParent()->getParent() == F &&
106
107 // Check that the types are the same, since this code handles casts...
108 Other->getType() == I.getType()) {
109
110 // These instructions are identical. Add to list...
111 RetVals.push_back(Other);
112 }
113}
114
115
116// isIdenticalBinaryInst - Return true if the two binary instructions are
117// identical.
118//
119static inline bool isIdenticalBinaryInst(const Instruction &I1,
120 const Instruction *I2) {
121 // Is it embeded in the same function? (This could be false if LHS
122 // is a constant or global!)
123 if (I1.getOpcode() != I2->getOpcode() ||
124 I1.getParent()->getParent() != I2->getParent()->getParent())
125 return false;
126
127 // They are identical if both operands are the same!
128 if (I1.getOperand(0) == I2->getOperand(0) &&
129 I1.getOperand(1) == I2->getOperand(1))
130 return true;
131
Chris Lattnerf37c3442002-10-31 04:20:07 +0000132 // If the instruction is commutative, the instruction can match if the
133 // operands are swapped!
Chris Lattner74542b62002-08-30 20:29:02 +0000134 //
135 if ((I1.getOperand(0) == I2->getOperand(1) &&
136 I1.getOperand(1) == I2->getOperand(0)) &&
Chris Lattnerf37c3442002-10-31 04:20:07 +0000137 I1.isCommutative())
Chris Lattner74542b62002-08-30 20:29:02 +0000138 return true;
139
140 return false;
141}
142
Chris Lattner3e813b32002-08-30 22:30:36 +0000143void BVNImpl::handleBinaryInst(Instruction &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000144 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
145 Function *F = I.getParent()->getParent();
146
147 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
148 UI != UE; ++UI)
149 if (Instruction *Other = dyn_cast<Instruction>(*UI))
150 // Check to see if this new binary operator is not I, but same operand...
151 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
152 // These instructions are identical. Handle the situation.
153 RetVals.push_back(Other);
154 }
155}
156
157// IdenticalComplexInst - Return true if the two instructions are the same, by
158// using a brute force comparison. This is useful for instructions with an
159// arbitrary number of arguments.
160//
161static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
162 assert(I1->getOpcode() == I2->getOpcode());
163 // Equal if they are in the same function...
164 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
165 // And return the same type...
166 I1->getType() == I2->getType() &&
167 // And have the same number of operands...
168 I1->getNumOperands() == I2->getNumOperands() &&
169 // And all of the operands are equal.
170 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
171}
172
173void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
174 Value *Op = I.getOperand(0);
175 Function *F = I.getParent()->getParent();
176
177 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
178 UI != UE; ++UI)
179 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
180 // Check to see if this new getelementptr is not I, but same operand...
181 if (Other != &I && IdenticalComplexInst(&I, Other)) {
182 // These instructions are identical. Handle the situation.
183 RetVals.push_back(Other);
184 }
185}