2002-08-30 20:29:02 +00:00
|
|
|
//===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-30 20:29:02 +00:00
|
|
|
//
|
|
|
|
// This file implements the non-abstract Value Numbering methods as well as a
|
|
|
|
// default implementation for the analysis group.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-08 22:01:16 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2002-08-30 22:30:36 +00:00
|
|
|
#include "llvm/Analysis/ValueNumbering.h"
|
2002-08-30 20:29:02 +00:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
2004-02-11 03:57:16 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2002-08-30 22:30:36 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-08-30 20:29:02 +00:00
|
|
|
#include "llvm/Type.h"
|
2007-02-05 23:42:17 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-03-25 22:56:03 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char ValueNumbering::ID = 0;
|
2002-08-30 20:29:02 +00:00
|
|
|
// Register the ValueNumbering interface, providing a nice name to refer to.
|
|
|
|
static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
|
|
|
|
|
|
|
|
/// ValueNumbering destructor: DO NOT move this to the header file for
|
|
|
|
/// ValueNumbering or else clients of the ValueNumbering class may not depend on
|
|
|
|
/// the ValueNumbering.o file in the current .a file, causing alias analysis
|
|
|
|
/// support to not be included in the tool correctly!
|
|
|
|
///
|
|
|
|
ValueNumbering::~ValueNumbering() {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-30 22:30:36 +00:00
|
|
|
// Basic ValueNumbering Pass Implementation
|
2002-08-30 20:29:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2002-08-30 22:30:36 +00:00
|
|
|
// Because of the way .a files work, the implementation of the BasicVN class
|
|
|
|
// MUST be in the ValueNumbering file itself, or else we run the risk of
|
|
|
|
// ValueNumbering being used, but the default implementation not being linked
|
|
|
|
// into the tool that uses it. As such, we register and implement the class
|
|
|
|
// here.
|
2002-08-30 20:29:02 +00:00
|
|
|
//
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
namespace {
|
2002-08-30 22:30:36 +00:00
|
|
|
/// BasicVN - This class is the default implementation of the ValueNumbering
|
|
|
|
/// interface. It walks the SSA def-use chains to trivially identify
|
|
|
|
/// lexically identical expressions. This does not require any ahead of time
|
|
|
|
/// analysis, so it is a very fast default implementation.
|
|
|
|
///
|
2007-02-05 23:42:17 +00:00
|
|
|
struct VISIBILITY_HIDDEN BasicVN
|
|
|
|
: public ImmutablePass, public ValueNumbering {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2007-05-01 21:15:47 +00:00
|
|
|
BasicVN() : ImmutablePass((intptr_t)&ID) {}
|
|
|
|
|
2002-08-30 22:30:36 +00:00
|
|
|
/// getEqualNumberNodes - Return nodes with the same value number as the
|
|
|
|
/// specified Value. This fills in the argument vector with any equal
|
|
|
|
/// values.
|
|
|
|
///
|
|
|
|
/// This is where our implementation is.
|
|
|
|
///
|
|
|
|
virtual void getEqualNumberNodes(Value *V1,
|
|
|
|
std::vector<Value*> &RetVals) const;
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char BasicVN::ID = 0;
|
2002-08-30 20:29:02 +00:00
|
|
|
// Register this pass...
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<BasicVN>
|
2002-08-30 20:29:02 +00:00
|
|
|
X("basicvn", "Basic Value Numbering (default GVN impl)");
|
|
|
|
|
|
|
|
// Declare that we implement the ValueNumbering interface
|
2006-08-28 00:42:29 +00:00
|
|
|
RegisterAnalysisGroup<ValueNumbering, true> Y(X);
|
2002-08-30 20:29:02 +00:00
|
|
|
|
2002-08-30 22:30:36 +00:00
|
|
|
/// BVNImpl - Implement BasicVN in terms of a visitor class that
|
2002-08-30 20:29:02 +00:00
|
|
|
/// handles the different types of instructions as appropriate.
|
|
|
|
///
|
2007-02-05 23:42:17 +00:00
|
|
|
struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
|
2002-08-30 20:29:02 +00:00
|
|
|
std::vector<Value*> &RetVals;
|
2007-10-29 19:52:04 +00:00
|
|
|
explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
|
2002-08-30 20:29:02 +00:00
|
|
|
|
|
|
|
void visitCastInst(CastInst &I);
|
2006-04-14 05:10:20 +00:00
|
|
|
void visitGetElementPtrInst(GetElementPtrInst &I);
|
2006-11-20 01:22:35 +00:00
|
|
|
void visitCmpInst(CmpInst &I);
|
2006-04-14 05:10:20 +00:00
|
|
|
|
|
|
|
void handleBinaryInst(Instruction &I);
|
|
|
|
void visitBinaryOperator(Instruction &I) { handleBinaryInst(I); }
|
|
|
|
void visitShiftInst(Instruction &I) { handleBinaryInst(I); }
|
|
|
|
void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
|
|
|
|
|
|
|
|
void handleTernaryInst(Instruction &I);
|
|
|
|
void visitSelectInst(Instruction &I) { handleTernaryInst(I); }
|
|
|
|
void visitInsertElementInst(Instruction &I) { handleTernaryInst(I); }
|
|
|
|
void visitShuffleVectorInst(Instruction &I) { handleTernaryInst(I); }
|
2002-08-30 20:29:02 +00:00
|
|
|
void visitInstruction(Instruction &) {
|
2006-02-04 09:15:29 +00:00
|
|
|
// Cannot value number calls or terminator instructions.
|
2002-08-30 20:29:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-01-08 22:01:16 +00:00
|
|
|
ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
|
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
// getEqualNumberNodes - Return nodes with the same value number as the
|
|
|
|
// specified Value. This fills in the argument vector with any equal values.
|
|
|
|
//
|
2002-08-30 22:30:36 +00:00
|
|
|
void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
|
2002-08-30 20:29:02 +00:00
|
|
|
assert(V->getType() != Type::VoidTy &&
|
|
|
|
"Can only value number non-void values!");
|
|
|
|
// We can only handle the case where I is an instruction!
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
BVNImpl(RetVals).visit(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BVNImpl::visitCastInst(CastInst &CI) {
|
|
|
|
Instruction &I = (Instruction&)CI;
|
|
|
|
Value *Op = I.getOperand(0);
|
|
|
|
Function *F = I.getParent()->getParent();
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
|
|
|
|
UI != UE; ++UI)
|
2004-02-11 03:57:16 +00:00
|
|
|
if (CastInst *Other = dyn_cast<CastInst>(*UI))
|
2006-11-27 01:05:10 +00:00
|
|
|
// Check that the opcode is the same
|
|
|
|
if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
|
|
|
|
// Check that the destination types are the same
|
|
|
|
Other->getType() == I.getType() &&
|
2003-09-11 18:14:24 +00:00
|
|
|
// Is it embedded in the same function? (This could be false if LHS
|
2002-08-30 20:29:02 +00:00
|
|
|
// is a constant or global!)
|
|
|
|
Other->getParent()->getParent() == F &&
|
2004-02-11 03:57:16 +00:00
|
|
|
// Check to see if this new cast is not I.
|
|
|
|
Other != &I) {
|
2002-08-30 20:29:02 +00:00
|
|
|
// These instructions are identical. Add to list...
|
|
|
|
RetVals.push_back(Other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-20 01:22:35 +00:00
|
|
|
void BVNImpl::visitCmpInst(CmpInst &CI1) {
|
|
|
|
Value *LHS = CI1.getOperand(0);
|
|
|
|
for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
|
|
|
|
// Check to see if this compare instruction is not CI, but same opcode,
|
|
|
|
// same predicate, and in the same function.
|
|
|
|
if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
|
|
|
|
CI2->getPredicate() == CI1.getPredicate() &&
|
|
|
|
CI2->getParent()->getParent() == CI1.getParent()->getParent())
|
|
|
|
// If the operands are the same
|
|
|
|
if ((CI2->getOperand(0) == CI1.getOperand(0) &&
|
|
|
|
CI2->getOperand(1) == CI1.getOperand(1)) ||
|
|
|
|
// Or the compare is commutative and the operands are reversed
|
|
|
|
(CI1.isCommutative() &&
|
|
|
|
CI2->getOperand(0) == CI1.getOperand(1) &&
|
|
|
|
CI2->getOperand(1) == CI1.getOperand(0)))
|
|
|
|
// Then the instructiosn are identical, add to list.
|
|
|
|
RetVals.push_back(CI2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
|
|
|
|
// isIdenticalBinaryInst - Return true if the two binary instructions are
|
|
|
|
// identical.
|
|
|
|
//
|
|
|
|
static inline bool isIdenticalBinaryInst(const Instruction &I1,
|
|
|
|
const Instruction *I2) {
|
2003-09-11 18:14:24 +00:00
|
|
|
// Is it embedded in the same function? (This could be false if LHS
|
2002-08-30 20:29:02 +00:00
|
|
|
// is a constant or global!)
|
|
|
|
if (I1.getOpcode() != I2->getOpcode() ||
|
|
|
|
I1.getParent()->getParent() != I2->getParent()->getParent())
|
|
|
|
return false;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// If they are CmpInst instructions, check their predicates
|
|
|
|
if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
|
|
|
|
if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
|
|
|
|
return false;
|
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
// They are identical if both operands are the same!
|
|
|
|
if (I1.getOperand(0) == I2->getOperand(0) &&
|
|
|
|
I1.getOperand(1) == I2->getOperand(1))
|
|
|
|
return true;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2002-10-31 04:20:07 +00:00
|
|
|
// If the instruction is commutative, the instruction can match if the
|
|
|
|
// operands are swapped!
|
2002-08-30 20:29:02 +00:00
|
|
|
//
|
|
|
|
if ((I1.getOperand(0) == I2->getOperand(1) &&
|
|
|
|
I1.getOperand(1) == I2->getOperand(0)) &&
|
2002-10-31 04:20:07 +00:00
|
|
|
I1.isCommutative())
|
2002-08-30 20:29:02 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-14 05:10:20 +00:00
|
|
|
// isIdenticalTernaryInst - Return true if the two ternary instructions are
|
|
|
|
// identical.
|
|
|
|
//
|
|
|
|
static inline bool isIdenticalTernaryInst(const Instruction &I1,
|
|
|
|
const Instruction *I2) {
|
|
|
|
// Is it embedded in the same function? (This could be false if LHS
|
|
|
|
// is a constant or global!)
|
|
|
|
if (I1.getParent()->getParent() != I2->getParent()->getParent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// They are identical if all operands are the same!
|
|
|
|
return I1.getOperand(0) == I2->getOperand(0) &&
|
|
|
|
I1.getOperand(1) == I2->getOperand(1) &&
|
|
|
|
I1.getOperand(2) == I2->getOperand(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-30 22:30:36 +00:00
|
|
|
void BVNImpl::handleBinaryInst(Instruction &I) {
|
2004-12-23 21:13:26 +00:00
|
|
|
Value *LHS = I.getOperand(0);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
if (Instruction *Other = dyn_cast<Instruction>(*UI))
|
|
|
|
// Check to see if this new binary operator is not I, but same operand...
|
2005-04-21 21:13:18 +00:00
|
|
|
if (Other != &I && isIdenticalBinaryInst(I, Other)) {
|
2002-08-30 20:29:02 +00:00
|
|
|
// These instructions are identical. Handle the situation.
|
|
|
|
RetVals.push_back(Other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdenticalComplexInst - Return true if the two instructions are the same, by
|
|
|
|
// using a brute force comparison. This is useful for instructions with an
|
|
|
|
// arbitrary number of arguments.
|
|
|
|
//
|
2005-04-21 21:13:18 +00:00
|
|
|
static inline bool IdenticalComplexInst(const Instruction *I1,
|
2004-12-23 21:17:41 +00:00
|
|
|
const Instruction *I2) {
|
2002-08-30 20:29:02 +00:00
|
|
|
assert(I1->getOpcode() == I2->getOpcode());
|
|
|
|
// Equal if they are in the same function...
|
|
|
|
return I1->getParent()->getParent() == I2->getParent()->getParent() &&
|
|
|
|
// And return the same type...
|
|
|
|
I1->getType() == I2->getType() &&
|
|
|
|
// And have the same number of operands...
|
|
|
|
I1->getNumOperands() == I2->getNumOperands() &&
|
|
|
|
// And all of the operands are equal.
|
|
|
|
std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
|
|
|
|
Value *Op = I.getOperand(0);
|
2004-03-25 22:56:03 +00:00
|
|
|
|
|
|
|
// Try to pick a local operand if possible instead of a constant or a global
|
|
|
|
// that might have a lot of uses.
|
|
|
|
for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
|
|
|
|
if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
|
|
|
|
Op = I.getOperand(i);
|
|
|
|
break;
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2002-08-30 20:29:02 +00:00
|
|
|
for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
|
|
|
|
// Check to see if this new getelementptr is not I, but same operand...
|
|
|
|
if (Other != &I && IdenticalComplexInst(&I, Other)) {
|
|
|
|
// These instructions are identical. Handle the situation.
|
|
|
|
RetVals.push_back(Other);
|
|
|
|
}
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-04-14 05:10:20 +00:00
|
|
|
void BVNImpl::handleTernaryInst(Instruction &I) {
|
|
|
|
Value *Op0 = I.getOperand(0);
|
|
|
|
Instruction *OtherInst;
|
2006-02-04 09:15:29 +00:00
|
|
|
|
2006-04-14 05:10:20 +00:00
|
|
|
for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
|
2006-02-04 09:15:29 +00:00
|
|
|
UI != UE; ++UI)
|
2006-04-14 05:10:20 +00:00
|
|
|
if ((OtherInst = dyn_cast<Instruction>(*UI)) &&
|
|
|
|
OtherInst->getOpcode() == I.getOpcode()) {
|
2006-02-04 09:15:29 +00:00
|
|
|
// Check to see if this new select is not I, but has the same operands.
|
2006-04-14 05:10:20 +00:00
|
|
|
if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
|
2006-02-04 09:15:29 +00:00
|
|
|
// These instructions are identical. Handle the situation.
|
2006-04-14 05:10:20 +00:00
|
|
|
RetVals.push_back(OtherInst);
|
2006-02-04 09:15:29 +00:00
|
|
|
}
|
|
|
|
|
2006-04-14 05:10:20 +00:00
|
|
|
}
|
2006-02-04 09:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-07 22:00:26 +00:00
|
|
|
// Ensure that users of ValueNumbering.h will link with this file
|
|
|
|
DEFINING_FILE_FOR(BasicValueNumbering)
|