blob: 32e4439eaaba591352d68eed59b1847e6053aad2 [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 ///
41 struct BasicVN : public FunctionPass, public ValueNumbering {
42
43 /// Pass Implementation stuff. This isn't much of a pass.
44 ///
45 bool runOnFunction(Function &) { return false; }
46
47 /// getAnalysisUsage - Does not modify anything.
48 ///
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 }
52
53 /// getEqualNumberNodes - Return nodes with the same value number as the
54 /// specified Value. This fills in the argument vector with any equal
55 /// values.
56 ///
57 /// This is where our implementation is.
58 ///
59 virtual void getEqualNumberNodes(Value *V1,
60 std::vector<Value*> &RetVals) const;
61 };
62
Chris Lattner74542b62002-08-30 20:29:02 +000063 // Register this pass...
Chris Lattner3e813b32002-08-30 22:30:36 +000064 RegisterOpt<BasicVN>
Chris Lattner74542b62002-08-30 20:29:02 +000065 X("basicvn", "Basic Value Numbering (default GVN impl)");
66
67 // Declare that we implement the ValueNumbering interface
Chris Lattner3e813b32002-08-30 22:30:36 +000068 RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
Chris Lattner74542b62002-08-30 20:29:02 +000069} // End of anonymous namespace
70
71namespace {
Chris Lattner3e813b32002-08-30 22:30:36 +000072 /// BVNImpl - Implement BasicVN in terms of a visitor class that
Chris Lattner74542b62002-08-30 20:29:02 +000073 /// handles the different types of instructions as appropriate.
74 ///
75 struct BVNImpl : public InstVisitor<BVNImpl> {
76 std::vector<Value*> &RetVals;
77 BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
78
Chris Lattner3e813b32002-08-30 22:30:36 +000079 void handleBinaryInst(Instruction &I);
80 void visitBinaryOperator(BinaryOperator &I) {
81 handleBinaryInst((Instruction&)I);
82 }
Chris Lattner74542b62002-08-30 20:29:02 +000083 void visitGetElementPtrInst(GetElementPtrInst &I);
84 void visitCastInst(CastInst &I);
Chris Lattner3e813b32002-08-30 22:30:36 +000085 void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
Chris Lattner74542b62002-08-30 20:29:02 +000086 void visitInstruction(Instruction &) {
87 // Cannot value number calls or terminator instructions...
88 }
89 };
90}
91
92// getEqualNumberNodes - Return nodes with the same value number as the
93// specified Value. This fills in the argument vector with any equal values.
94//
Chris Lattner3e813b32002-08-30 22:30:36 +000095void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
Chris Lattner74542b62002-08-30 20:29:02 +000096 assert(V->getType() != Type::VoidTy &&
97 "Can only value number non-void values!");
98 // We can only handle the case where I is an instruction!
99 if (Instruction *I = dyn_cast<Instruction>(V))
100 BVNImpl(RetVals).visit(I);
101}
102
103void BVNImpl::visitCastInst(CastInst &CI) {
104 Instruction &I = (Instruction&)CI;
105 Value *Op = I.getOperand(0);
106 Function *F = I.getParent()->getParent();
107
108 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
109 UI != UE; ++UI)
110 if (Instruction *Other = dyn_cast<Instruction>(*UI))
111 // Check to see if this new cast is not I, but has the same operand...
112 if (Other != &I && Other->getOpcode() == I.getOpcode() &&
113 Other->getOperand(0) == Op && // Is the operand the same?
114 // Is it embeded in the same function? (This could be false if LHS
115 // is a constant or global!)
116 Other->getParent()->getParent() == F &&
117
118 // Check that the types are the same, since this code handles casts...
119 Other->getType() == I.getType()) {
120
121 // These instructions are identical. Add to list...
122 RetVals.push_back(Other);
123 }
124}
125
126
127// isIdenticalBinaryInst - Return true if the two binary instructions are
128// identical.
129//
130static inline bool isIdenticalBinaryInst(const Instruction &I1,
131 const Instruction *I2) {
132 // Is it embeded in the same function? (This could be false if LHS
133 // is a constant or global!)
134 if (I1.getOpcode() != I2->getOpcode() ||
135 I1.getParent()->getParent() != I2->getParent()->getParent())
136 return false;
137
138 // They are identical if both operands are the same!
139 if (I1.getOperand(0) == I2->getOperand(0) &&
140 I1.getOperand(1) == I2->getOperand(1))
141 return true;
142
143 // If the instruction is commutative and associative, the instruction can
144 // match if the operands are swapped!
145 //
146 if ((I1.getOperand(0) == I2->getOperand(1) &&
147 I1.getOperand(1) == I2->getOperand(0)) &&
148 (I1.getOpcode() == Instruction::Add ||
149 I1.getOpcode() == Instruction::Mul ||
150 I1.getOpcode() == Instruction::And ||
151 I1.getOpcode() == Instruction::Or ||
152 I1.getOpcode() == Instruction::Xor))
153 return true;
154
155 return false;
156}
157
Chris Lattner3e813b32002-08-30 22:30:36 +0000158void BVNImpl::handleBinaryInst(Instruction &I) {
Chris Lattner74542b62002-08-30 20:29:02 +0000159 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
160 Function *F = I.getParent()->getParent();
161
162 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
163 UI != UE; ++UI)
164 if (Instruction *Other = dyn_cast<Instruction>(*UI))
165 // Check to see if this new binary operator is not I, but same operand...
166 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
167 // These instructions are identical. Handle the situation.
168 RetVals.push_back(Other);
169 }
170}
171
172// IdenticalComplexInst - Return true if the two instructions are the same, by
173// using a brute force comparison. This is useful for instructions with an
174// arbitrary number of arguments.
175//
176static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
177 assert(I1->getOpcode() == I2->getOpcode());
178 // Equal if they are in the same function...
179 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
180 // And return the same type...
181 I1->getType() == I2->getType() &&
182 // And have the same number of operands...
183 I1->getNumOperands() == I2->getNumOperands() &&
184 // And all of the operands are equal.
185 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
186}
187
188void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
189 Value *Op = I.getOperand(0);
190 Function *F = I.getParent()->getParent();
191
192 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
193 UI != UE; ++UI)
194 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
195 // Check to see if this new getelementptr is not I, but same operand...
196 if (Other != &I && IdenticalComplexInst(&I, Other)) {
197 // These instructions are identical. Handle the situation.
198 RetVals.push_back(Other);
199 }
200}