2007-05-29 21:53:49 +00:00
|
|
|
//===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-29 21:53:49 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs a hybrid of global value numbering and partial redundancy
|
|
|
|
// elimination, known as GVN-PRE. It performs partial redundancy elimination on
|
|
|
|
// values, rather than lexical expressions, allowing a more comprehensive view
|
|
|
|
// the optimization. It replaces redundant values with uses of earlier
|
|
|
|
// occurences of the same value. While this is beneficial in that it eliminates
|
|
|
|
// unneeded computation, it also increases register pressure by creating large
|
2007-06-08 20:57:08 +00:00
|
|
|
// live ranges, and should be used with caution on platforms that are very
|
2007-05-29 21:53:49 +00:00
|
|
|
// sensitive to register pressure.
|
|
|
|
//
|
2008-06-05 07:55:49 +00:00
|
|
|
// Note that this pass does the value numbering itself, it does not use the
|
|
|
|
// ValueNumbering analysis passes.
|
|
|
|
//
|
2007-05-29 21:53:49 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "gvnpre"
|
|
|
|
#include "llvm/Value.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Function.h"
|
2007-06-29 00:40:05 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-05-29 21:53:49 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2007-06-22 00:20:30 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2007-06-19 23:23:54 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-05-29 21:53:49 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2007-06-25 18:25:31 +00:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2007-06-21 17:57:53 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-07-19 06:13:15 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-05-29 21:53:49 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-07-05 23:11:26 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2007-06-08 01:03:01 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-05-29 21:53:49 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-05-29 23:15:21 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2007-05-29 21:53:49 +00:00
|
|
|
#include <algorithm>
|
2007-05-31 22:44:11 +00:00
|
|
|
#include <deque>
|
2007-05-29 21:53:49 +00:00
|
|
|
#include <map>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// This class holds the mapping between values and value numbers. It is used
|
|
|
|
/// as an efficient mechanism to determine the expression-wise equivalence of
|
|
|
|
/// two values.
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
struct Expression {
|
|
|
|
enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
|
|
|
|
FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
|
|
|
|
ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
|
|
|
|
ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
|
|
|
|
FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
|
|
|
|
FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
|
|
|
|
FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
|
|
|
|
SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
|
|
|
|
FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
|
|
|
|
PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY,
|
|
|
|
TOMBSTONE };
|
|
|
|
|
|
|
|
ExpressionOpcode opcode;
|
|
|
|
const Type* type;
|
|
|
|
uint32_t firstVN;
|
|
|
|
uint32_t secondVN;
|
|
|
|
uint32_t thirdVN;
|
|
|
|
SmallVector<uint32_t, 4> varargs;
|
|
|
|
|
|
|
|
Expression() { }
|
2007-09-24 15:48:49 +00:00
|
|
|
explicit Expression(ExpressionOpcode o) : opcode(o) { }
|
2007-07-19 06:13:15 +00:00
|
|
|
|
|
|
|
bool operator==(const Expression &other) const {
|
|
|
|
if (opcode != other.opcode)
|
|
|
|
return false;
|
|
|
|
else if (opcode == EMPTY || opcode == TOMBSTONE)
|
|
|
|
return true;
|
|
|
|
else if (type != other.type)
|
|
|
|
return false;
|
|
|
|
else if (firstVN != other.firstVN)
|
|
|
|
return false;
|
|
|
|
else if (secondVN != other.secondVN)
|
|
|
|
return false;
|
|
|
|
else if (thirdVN != other.thirdVN)
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
if (varargs.size() != other.varargs.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < varargs.size(); ++i)
|
|
|
|
if (varargs[i] != other.varargs[i])
|
|
|
|
return false;
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Expression &other) const {
|
|
|
|
if (opcode != other.opcode)
|
|
|
|
return true;
|
|
|
|
else if (opcode == EMPTY || opcode == TOMBSTONE)
|
|
|
|
return false;
|
|
|
|
else if (type != other.type)
|
|
|
|
return true;
|
|
|
|
else if (firstVN != other.firstVN)
|
|
|
|
return true;
|
|
|
|
else if (secondVN != other.secondVN)
|
|
|
|
return true;
|
|
|
|
else if (thirdVN != other.thirdVN)
|
|
|
|
return true;
|
|
|
|
else {
|
|
|
|
if (varargs.size() != other.varargs.size())
|
|
|
|
return true;
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
for (size_t i = 0; i < varargs.size(); ++i)
|
|
|
|
if (varargs[i] != other.varargs[i])
|
|
|
|
return true;
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2007-07-19 06:13:15 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN ValueTable {
|
2007-06-19 03:31:41 +00:00
|
|
|
private:
|
2007-06-19 23:23:54 +00:00
|
|
|
DenseMap<Value*, uint32_t> valueNumbering;
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t> expressionNumbering;
|
2007-06-19 03:31:41 +00:00
|
|
|
|
|
|
|
uint32_t nextValueNumber;
|
|
|
|
|
|
|
|
Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
|
|
|
|
Expression::ExpressionOpcode getOpcode(CmpInst* C);
|
2007-06-29 00:51:03 +00:00
|
|
|
Expression::ExpressionOpcode getOpcode(CastInst* C);
|
2007-06-21 00:19:05 +00:00
|
|
|
Expression create_expression(BinaryOperator* BO);
|
|
|
|
Expression create_expression(CmpInst* C);
|
2007-06-27 04:10:46 +00:00
|
|
|
Expression create_expression(ShuffleVectorInst* V);
|
|
|
|
Expression create_expression(ExtractElementInst* C);
|
|
|
|
Expression create_expression(InsertElementInst* V);
|
2007-06-28 23:51:21 +00:00
|
|
|
Expression create_expression(SelectInst* V);
|
2007-06-29 00:51:03 +00:00
|
|
|
Expression create_expression(CastInst* C);
|
2007-07-03 22:50:56 +00:00
|
|
|
Expression create_expression(GetElementPtrInst* G);
|
2007-06-19 03:31:41 +00:00
|
|
|
public:
|
|
|
|
ValueTable() { nextValueNumber = 1; }
|
|
|
|
uint32_t lookup_or_add(Value* V);
|
2007-06-28 23:51:21 +00:00
|
|
|
uint32_t lookup(Value* V) const;
|
2007-06-19 03:31:41 +00:00
|
|
|
void add(Value* V, uint32_t num);
|
|
|
|
void clear();
|
2007-06-19 05:37:32 +00:00
|
|
|
void erase(Value* v);
|
2007-06-22 00:20:30 +00:00
|
|
|
unsigned size();
|
2007-06-19 03:31:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
namespace llvm {
|
2007-09-17 18:34:04 +00:00
|
|
|
template <> struct DenseMapInfo<Expression> {
|
2007-08-02 18:20:52 +00:00
|
|
|
static inline Expression getEmptyKey() {
|
|
|
|
return Expression(Expression::EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Expression getTombstoneKey() {
|
|
|
|
return Expression(Expression::TOMBSTONE);
|
|
|
|
}
|
2007-07-19 06:13:15 +00:00
|
|
|
|
|
|
|
static unsigned getHashValue(const Expression e) {
|
|
|
|
unsigned hash = e.opcode;
|
|
|
|
|
|
|
|
hash = e.firstVN + hash * 37;
|
|
|
|
hash = e.secondVN + hash * 37;
|
|
|
|
hash = e.thirdVN + hash * 37;
|
|
|
|
|
2008-02-20 11:26:25 +00:00
|
|
|
hash = ((unsigned)((uintptr_t)e.type >> 4) ^
|
|
|
|
(unsigned)((uintptr_t)e.type >> 9)) +
|
|
|
|
hash * 37;
|
2007-07-19 06:13:15 +00:00
|
|
|
|
2007-08-02 18:20:52 +00:00
|
|
|
for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
|
|
|
|
E = e.varargs.end(); I != E; ++I)
|
2007-07-19 06:13:15 +00:00
|
|
|
hash = *I + hash * 37;
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
2007-09-17 18:34:04 +00:00
|
|
|
static bool isEqual(const Expression &LHS, const Expression &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
2007-07-19 06:13:15 +00:00
|
|
|
static bool isPod() { return true; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Internal Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression::ExpressionOpcode
|
2007-06-21 00:19:05 +00:00
|
|
|
ValueTable::getOpcode(BinaryOperator* BO) {
|
2007-06-19 03:31:41 +00:00
|
|
|
switch(BO->getOpcode()) {
|
|
|
|
case Instruction::Add:
|
|
|
|
return Expression::ADD;
|
|
|
|
case Instruction::Sub:
|
|
|
|
return Expression::SUB;
|
|
|
|
case Instruction::Mul:
|
|
|
|
return Expression::MUL;
|
|
|
|
case Instruction::UDiv:
|
|
|
|
return Expression::UDIV;
|
|
|
|
case Instruction::SDiv:
|
|
|
|
return Expression::SDIV;
|
|
|
|
case Instruction::FDiv:
|
|
|
|
return Expression::FDIV;
|
|
|
|
case Instruction::URem:
|
|
|
|
return Expression::UREM;
|
|
|
|
case Instruction::SRem:
|
|
|
|
return Expression::SREM;
|
|
|
|
case Instruction::FRem:
|
|
|
|
return Expression::FREM;
|
|
|
|
case Instruction::Shl:
|
|
|
|
return Expression::SHL;
|
|
|
|
case Instruction::LShr:
|
|
|
|
return Expression::LSHR;
|
|
|
|
case Instruction::AShr:
|
|
|
|
return Expression::ASHR;
|
|
|
|
case Instruction::And:
|
|
|
|
return Expression::AND;
|
|
|
|
case Instruction::Or:
|
|
|
|
return Expression::OR;
|
|
|
|
case Instruction::Xor:
|
|
|
|
return Expression::XOR;
|
|
|
|
|
|
|
|
// THIS SHOULD NEVER HAPPEN
|
|
|
|
default:
|
|
|
|
assert(0 && "Binary operator with unknown opcode?");
|
|
|
|
return Expression::ADD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
|
2007-06-19 03:31:41 +00:00
|
|
|
if (C->getOpcode() == Instruction::ICmp) {
|
|
|
|
switch (C->getPredicate()) {
|
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
return Expression::ICMPEQ;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
return Expression::ICMPNE;
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
return Expression::ICMPUGT;
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
return Expression::ICMPUGE;
|
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
return Expression::ICMPULT;
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
return Expression::ICMPULE;
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
return Expression::ICMPSGT;
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
return Expression::ICMPSGE;
|
|
|
|
case ICmpInst::ICMP_SLT:
|
|
|
|
return Expression::ICMPSLT;
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
return Expression::ICMPSLE;
|
|
|
|
|
|
|
|
// THIS SHOULD NEVER HAPPEN
|
|
|
|
default:
|
|
|
|
assert(0 && "Comparison with unknown predicate?");
|
|
|
|
return Expression::ICMPEQ;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (C->getPredicate()) {
|
|
|
|
case FCmpInst::FCMP_OEQ:
|
|
|
|
return Expression::FCMPOEQ;
|
|
|
|
case FCmpInst::FCMP_OGT:
|
|
|
|
return Expression::FCMPOGT;
|
|
|
|
case FCmpInst::FCMP_OGE:
|
|
|
|
return Expression::FCMPOGE;
|
|
|
|
case FCmpInst::FCMP_OLT:
|
|
|
|
return Expression::FCMPOLT;
|
|
|
|
case FCmpInst::FCMP_OLE:
|
|
|
|
return Expression::FCMPOLE;
|
|
|
|
case FCmpInst::FCMP_ONE:
|
|
|
|
return Expression::FCMPONE;
|
|
|
|
case FCmpInst::FCMP_ORD:
|
|
|
|
return Expression::FCMPORD;
|
|
|
|
case FCmpInst::FCMP_UNO:
|
|
|
|
return Expression::FCMPUNO;
|
|
|
|
case FCmpInst::FCMP_UEQ:
|
|
|
|
return Expression::FCMPUEQ;
|
|
|
|
case FCmpInst::FCMP_UGT:
|
|
|
|
return Expression::FCMPUGT;
|
|
|
|
case FCmpInst::FCMP_UGE:
|
|
|
|
return Expression::FCMPUGE;
|
|
|
|
case FCmpInst::FCMP_ULT:
|
|
|
|
return Expression::FCMPULT;
|
|
|
|
case FCmpInst::FCMP_ULE:
|
|
|
|
return Expression::FCMPULE;
|
|
|
|
case FCmpInst::FCMP_UNE:
|
|
|
|
return Expression::FCMPUNE;
|
|
|
|
|
|
|
|
// THIS SHOULD NEVER HAPPEN
|
|
|
|
default:
|
|
|
|
assert(0 && "Comparison with unknown predicate?");
|
|
|
|
return Expression::FCMPOEQ;
|
2007-06-09 18:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-19 03:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression::ExpressionOpcode
|
2007-06-29 00:51:03 +00:00
|
|
|
ValueTable::getOpcode(CastInst* C) {
|
|
|
|
switch(C->getOpcode()) {
|
|
|
|
case Instruction::Trunc:
|
|
|
|
return Expression::TRUNC;
|
|
|
|
case Instruction::ZExt:
|
|
|
|
return Expression::ZEXT;
|
|
|
|
case Instruction::SExt:
|
|
|
|
return Expression::SEXT;
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
return Expression::FPTOUI;
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
return Expression::FPTOSI;
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
return Expression::UITOFP;
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
return Expression::SITOFP;
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
return Expression::FPTRUNC;
|
|
|
|
case Instruction::FPExt:
|
|
|
|
return Expression::FPEXT;
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
return Expression::PTRTOINT;
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
return Expression::INTTOPTR;
|
|
|
|
case Instruction::BitCast:
|
|
|
|
return Expression::BITCAST;
|
|
|
|
|
|
|
|
// THIS SHOULD NEVER HAPPEN
|
|
|
|
default:
|
|
|
|
assert(0 && "Cast operator with unknown opcode?");
|
|
|
|
return Expression::BITCAST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(BinaryOperator* BO) {
|
2007-06-21 00:19:05 +00:00
|
|
|
Expression e;
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
e.firstVN = lookup_or_add(BO->getOperand(0));
|
|
|
|
e.secondVN = lookup_or_add(BO->getOperand(1));
|
|
|
|
e.thirdVN = 0;
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = BO->getType();
|
2007-06-21 00:19:05 +00:00
|
|
|
e.opcode = getOpcode(BO);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(CmpInst* C) {
|
2007-06-21 00:19:05 +00:00
|
|
|
Expression e;
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
e.firstVN = lookup_or_add(C->getOperand(0));
|
|
|
|
e.secondVN = lookup_or_add(C->getOperand(1));
|
|
|
|
e.thirdVN = 0;
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = C->getType();
|
2007-06-21 00:19:05 +00:00
|
|
|
e.opcode = getOpcode(C);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(CastInst* C) {
|
2007-06-29 00:51:03 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(C->getOperand(0));
|
|
|
|
e.secondVN = 0;
|
|
|
|
e.thirdVN = 0;
|
|
|
|
e.type = C->getType();
|
|
|
|
e.opcode = getOpcode(C);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(ShuffleVectorInst* S) {
|
2007-06-27 04:10:46 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(S->getOperand(0));
|
|
|
|
e.secondVN = lookup_or_add(S->getOperand(1));
|
|
|
|
e.thirdVN = lookup_or_add(S->getOperand(2));
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = S->getType();
|
2007-06-27 04:10:46 +00:00
|
|
|
e.opcode = Expression::SHUFFLE;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(ExtractElementInst* E) {
|
2007-06-27 04:10:46 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(E->getOperand(0));
|
|
|
|
e.secondVN = lookup_or_add(E->getOperand(1));
|
|
|
|
e.thirdVN = 0;
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = E->getType();
|
2007-06-27 04:10:46 +00:00
|
|
|
e.opcode = Expression::EXTRACT;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(InsertElementInst* I) {
|
2007-06-27 04:10:46 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(I->getOperand(0));
|
|
|
|
e.secondVN = lookup_or_add(I->getOperand(1));
|
|
|
|
e.thirdVN = lookup_or_add(I->getOperand(2));
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = I->getType();
|
2007-06-27 04:10:46 +00:00
|
|
|
e.opcode = Expression::INSERT;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(SelectInst* I) {
|
2007-06-28 23:51:21 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(I->getCondition());
|
|
|
|
e.secondVN = lookup_or_add(I->getTrueValue());
|
|
|
|
e.thirdVN = lookup_or_add(I->getFalseValue());
|
2007-06-29 00:40:05 +00:00
|
|
|
e.type = I->getType();
|
2007-06-28 23:51:21 +00:00
|
|
|
e.opcode = Expression::SELECT;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
Expression ValueTable::create_expression(GetElementPtrInst* G) {
|
2007-07-03 22:50:56 +00:00
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.firstVN = lookup_or_add(G->getPointerOperand());
|
|
|
|
e.secondVN = 0;
|
|
|
|
e.thirdVN = 0;
|
|
|
|
e.type = G->getType();
|
2007-07-20 08:19:20 +00:00
|
|
|
e.opcode = Expression::GEP;
|
2007-07-03 22:50:56 +00:00
|
|
|
|
|
|
|
for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
|
|
|
|
I != E; ++I)
|
|
|
|
e.varargs.push_back(lookup_or_add(*I));
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable External Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// lookup_or_add - Returns the value number for the specified value, assigning
|
|
|
|
/// it a new number if it did not have one before.
|
2007-06-19 03:31:41 +00:00
|
|
|
uint32_t ValueTable::lookup_or_add(Value* V) {
|
2007-06-19 23:23:54 +00:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 03:31:41 +00:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
return VI->second;
|
2007-06-09 18:35:31 +00:00
|
|
|
|
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
|
|
|
|
Expression e = create_expression(BO);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-19 03:31:41 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
|
|
|
|
Expression e = create_expression(C);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-27 04:10:46 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-27 04:10:46 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-27 04:10:46 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-28 23:51:21 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-19 03:31:41 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
2007-06-29 00:51:03 +00:00
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (CastInst* U = dyn_cast<CastInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-07-03 23:51:19 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
|
|
|
|
Expression e = create_expression(U);
|
|
|
|
|
2007-07-19 06:13:15 +00:00
|
|
|
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
2007-06-29 00:51:03 +00:00
|
|
|
if (EI != expressionNumbering.end()) {
|
2007-06-19 03:31:41 +00:00
|
|
|
valueNumbering.insert(std::make_pair(V, EI->second));
|
|
|
|
return EI->second;
|
|
|
|
} else {
|
|
|
|
expressionNumbering.insert(std::make_pair(e, nextValueNumber));
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
return nextValueNumber++;
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
2007-06-19 03:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// lookup - Returns the value number of the specified value. Fails if
|
|
|
|
/// the value has not yet been numbered.
|
2007-06-28 23:51:21 +00:00
|
|
|
uint32_t ValueTable::lookup(Value* V) const {
|
2007-06-19 23:23:54 +00:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 03:31:41 +00:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
return VI->second;
|
|
|
|
else
|
|
|
|
assert(0 && "Value not numbered?");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// add - Add the specified value with the given value number, removing
|
|
|
|
/// its old number, if any
|
2007-06-19 03:31:41 +00:00
|
|
|
void ValueTable::add(Value* V, uint32_t num) {
|
2007-06-19 23:23:54 +00:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 03:31:41 +00:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
valueNumbering.erase(VI);
|
|
|
|
valueNumbering.insert(std::make_pair(V, num));
|
|
|
|
}
|
|
|
|
|
2007-06-25 05:41:12 +00:00
|
|
|
/// clear - Remove all entries from the ValueTable
|
2007-06-19 03:31:41 +00:00
|
|
|
void ValueTable::clear() {
|
|
|
|
valueNumbering.clear();
|
|
|
|
expressionNumbering.clear();
|
|
|
|
nextValueNumber = 1;
|
|
|
|
}
|
2007-06-03 05:55:58 +00:00
|
|
|
|
2007-06-25 05:41:12 +00:00
|
|
|
/// erase - Remove a value from the value numbering
|
2007-06-19 05:37:32 +00:00
|
|
|
void ValueTable::erase(Value* V) {
|
|
|
|
valueNumbering.erase(V);
|
|
|
|
}
|
|
|
|
|
2007-06-22 00:20:30 +00:00
|
|
|
/// size - Return the number of assigned value numbers
|
|
|
|
unsigned ValueTable::size() {
|
|
|
|
// NOTE: zero is never assigned
|
|
|
|
return nextValueNumber;
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-09 16:43:55 +00:00
|
|
|
// ValueNumberedSet Class
|
2007-07-09 06:50:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class ValueNumberedSet {
|
|
|
|
private:
|
|
|
|
SmallPtrSet<Value*, 8> contents;
|
|
|
|
BitVector numbers;
|
|
|
|
public:
|
|
|
|
ValueNumberedSet() { numbers.resize(1); }
|
2007-07-10 00:27:22 +00:00
|
|
|
ValueNumberedSet(const ValueNumberedSet& other) {
|
|
|
|
numbers = other.numbers;
|
|
|
|
contents = other.contents;
|
|
|
|
}
|
2007-07-09 06:50:06 +00:00
|
|
|
|
|
|
|
typedef SmallPtrSet<Value*, 8>::iterator iterator;
|
|
|
|
|
|
|
|
iterator begin() { return contents.begin(); }
|
|
|
|
iterator end() { return contents.end(); }
|
|
|
|
|
|
|
|
bool insert(Value* v) { return contents.insert(v); }
|
|
|
|
void insert(iterator I, iterator E) { contents.insert(I, E); }
|
|
|
|
void erase(Value* v) { contents.erase(v); }
|
2007-07-10 00:09:25 +00:00
|
|
|
unsigned count(Value* v) { return contents.count(v); }
|
2007-07-09 06:50:06 +00:00
|
|
|
size_t size() { return contents.size(); }
|
|
|
|
|
2007-07-09 07:56:55 +00:00
|
|
|
void set(unsigned i) {
|
2007-07-09 06:50:06 +00:00
|
|
|
if (i >= numbers.size())
|
|
|
|
numbers.resize(i+1);
|
|
|
|
|
|
|
|
numbers.set(i);
|
|
|
|
}
|
|
|
|
|
2007-07-09 22:29:50 +00:00
|
|
|
void operator=(const ValueNumberedSet& other) {
|
|
|
|
contents = other.contents;
|
2007-07-09 07:56:55 +00:00
|
|
|
numbers = other.numbers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(unsigned i) {
|
2007-07-09 06:50:06 +00:00
|
|
|
if (i < numbers.size())
|
|
|
|
numbers.reset(i);
|
|
|
|
}
|
|
|
|
|
2007-07-09 07:56:55 +00:00
|
|
|
bool test(unsigned i) {
|
2007-07-09 06:50:06 +00:00
|
|
|
if (i >= numbers.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return numbers.test(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
contents.clear();
|
|
|
|
numbers.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GVNPRE Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-06-19 04:32:55 +00:00
|
|
|
GVNPRE() : FunctionPass((intptr_t)&ID) { }
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
private:
|
2007-06-08 01:03:01 +00:00
|
|
|
ValueTable VN;
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Instruction*, 8> createdExpressions;
|
2007-05-31 00:42:15 +00:00
|
|
|
|
2007-07-10 00:27:22 +00:00
|
|
|
DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
|
|
|
|
DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn;
|
|
|
|
DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis;
|
2007-06-12 22:43:57 +00:00
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
// This transformation requires dominator postdominator info
|
2007-05-29 21:53:49 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
2007-07-06 18:12:36 +00:00
|
|
|
AU.addRequiredID(BreakCriticalEdgesID);
|
2007-07-05 23:11:26 +00:00
|
|
|
AU.addRequired<UnifyFunctionExitNodes>();
|
2007-05-29 21:53:49 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper fuctions
|
|
|
|
// FIXME: eliminate or document these better
|
2007-07-09 07:56:55 +00:00
|
|
|
void dump(ValueNumberedSet& s) const ;
|
|
|
|
void clean(ValueNumberedSet& set) ;
|
|
|
|
Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
|
|
|
|
Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ;
|
2007-07-09 06:50:06 +00:00
|
|
|
void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred,
|
2007-07-09 07:56:55 +00:00
|
|
|
BasicBlock* succ, ValueNumberedSet& out) ;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
void topo_sort(ValueNumberedSet& set,
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 8>& vec) ;
|
2007-05-31 22:44:11 +00:00
|
|
|
|
2007-07-09 07:56:55 +00:00
|
|
|
void cleanup() ;
|
|
|
|
bool elimination() ;
|
2007-06-18 04:42:29 +00:00
|
|
|
|
2007-07-09 07:56:55 +00:00
|
|
|
void val_insert(ValueNumberedSet& s, Value* v) ;
|
|
|
|
void val_replace(ValueNumberedSet& s, Value* v) ;
|
|
|
|
bool dependsOnInvoke(Value* V) ;
|
2007-06-20 22:10:02 +00:00
|
|
|
void buildsets_availout(BasicBlock::iterator I,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& currAvail,
|
|
|
|
ValueNumberedSet& currPhis,
|
|
|
|
ValueNumberedSet& currExps,
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallPtrSet<Value*, 16>& currTemps);
|
2007-06-22 00:20:30 +00:00
|
|
|
bool buildsets_anticout(BasicBlock* BB,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& anticOut,
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8>& visited);
|
2007-06-22 00:20:30 +00:00
|
|
|
unsigned buildsets_anticin(BasicBlock* BB,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& anticOut,
|
|
|
|
ValueNumberedSet& currExps,
|
2007-06-28 00:34:34 +00:00
|
|
|
SmallPtrSet<Value*, 16>& currTemps,
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8>& visited);
|
2007-07-09 07:56:55 +00:00
|
|
|
void buildsets(Function& F) ;
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
void insertion_pre(Value* e, BasicBlock* BB,
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*>& avail,
|
|
|
|
std::map<BasicBlock*,ValueNumberedSet>& new_set);
|
|
|
|
unsigned insertion_mergepoint(SmallVector<Value*, 8>& workList,
|
2007-06-22 00:20:30 +00:00
|
|
|
df_iterator<DomTreeNode*>& D,
|
2007-07-19 06:37:56 +00:00
|
|
|
std::map<BasicBlock*, ValueNumberedSet>& new_set);
|
2007-07-09 07:56:55 +00:00
|
|
|
bool insertion(Function& F) ;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
char GVNPRE::ID = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
// createGVNPREPass - The public interface to this file...
|
2007-05-29 21:53:49 +00:00
|
|
|
FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
|
|
|
|
|
2007-07-10 20:20:19 +00:00
|
|
|
static RegisterPass<GVNPRE> X("gvnpre",
|
2007-08-02 18:20:52 +00:00
|
|
|
"Global Value Numbering/Partial Redundancy Elimination");
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
|
2007-06-08 22:02:36 +00:00
|
|
|
STATISTIC(NumInsertedVals, "Number of values inserted");
|
|
|
|
STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
|
|
|
|
STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// find_leader - Given a set and a value number, return the first
|
|
|
|
/// element of the set with that value number, or 0 if no such element
|
|
|
|
/// is present
|
2007-07-09 06:50:06 +00:00
|
|
|
Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
|
|
|
|
if (!vals.test(v))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
|
2007-06-19 03:31:41 +00:00
|
|
|
I != E; ++I)
|
|
|
|
if (v == VN.lookup(*I))
|
|
|
|
return *I;
|
|
|
|
|
2007-07-09 23:57:18 +00:00
|
|
|
assert(0 && "No leader found, but present bit is set?");
|
2007-06-19 03:31:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-03 05:55:58 +00:00
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// val_insert - Insert a value into a set only if there is not a value
|
|
|
|
/// with the same value number already in the set
|
2007-07-09 06:50:06 +00:00
|
|
|
void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) {
|
2007-06-19 03:31:41 +00:00
|
|
|
uint32_t num = VN.lookup(v);
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!s.test(num))
|
2007-06-19 03:31:41 +00:00
|
|
|
s.insert(v);
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// val_replace - Insert a value into a set, replacing any values already in
|
|
|
|
/// the set that have the same value number
|
2007-07-09 06:50:06 +00:00
|
|
|
void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) {
|
2007-07-19 19:57:13 +00:00
|
|
|
if (s.count(v)) return;
|
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
uint32_t num = VN.lookup(v);
|
|
|
|
Value* leader = find_leader(s, num);
|
2007-07-09 22:29:50 +00:00
|
|
|
if (leader != 0)
|
2007-06-19 03:31:41 +00:00
|
|
|
s.erase(leader);
|
|
|
|
s.insert(v);
|
2007-07-09 22:29:50 +00:00
|
|
|
s.set(num);
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// phi_translate - Given a value, its parent block, and a predecessor of its
|
|
|
|
/// parent, translate the value into legal for the predecessor block. This
|
|
|
|
/// means translating its operands (and recursively, their operands) through
|
|
|
|
/// any phi nodes in the parent into values available in the predecessor
|
2007-06-12 22:43:57 +00:00
|
|
|
Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
|
2007-06-04 18:05:26 +00:00
|
|
|
if (V == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-07-03 18:37:08 +00:00
|
|
|
// Unary Operations
|
2007-07-03 19:01:42 +00:00
|
|
|
if (CastInst* U = dyn_cast<CastInst>(V)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
Value* newOp1 = 0;
|
|
|
|
if (isa<Instruction>(U->getOperand(0)))
|
|
|
|
newOp1 = phi_translate(U->getOperand(0), pred, succ);
|
|
|
|
else
|
|
|
|
newOp1 = U->getOperand(0);
|
|
|
|
|
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != U->getOperand(0)) {
|
|
|
|
Instruction* newVal = 0;
|
|
|
|
if (CastInst* C = dyn_cast<CastInst>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = CastInst::Create(C->getOpcode(),
|
2007-07-03 18:37:08 +00:00
|
|
|
newOp1, C->getType(),
|
|
|
|
C->getName()+".expr");
|
|
|
|
|
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
|
|
|
|
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
|
|
|
if (leader == 0) {
|
|
|
|
createdExpressions.push_back(newVal);
|
|
|
|
return newVal;
|
|
|
|
} else {
|
|
|
|
VN.erase(newVal);
|
|
|
|
delete newVal;
|
|
|
|
return leader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
// Binary Operations
|
2007-07-03 18:37:08 +00:00
|
|
|
} if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
|
2007-06-27 04:10:46 +00:00
|
|
|
isa<ExtractElementInst>(V)) {
|
2007-06-25 05:41:12 +00:00
|
|
|
User* U = cast<User>(V);
|
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
Value* newOp1 = 0;
|
2007-06-25 05:41:12 +00:00
|
|
|
if (isa<Instruction>(U->getOperand(0)))
|
|
|
|
newOp1 = phi_translate(U->getOperand(0), pred, succ);
|
2007-06-19 03:31:41 +00:00
|
|
|
else
|
2007-06-25 05:41:12 +00:00
|
|
|
newOp1 = U->getOperand(0);
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-06-04 18:05:26 +00:00
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
Value* newOp2 = 0;
|
2007-06-25 05:41:12 +00:00
|
|
|
if (isa<Instruction>(U->getOperand(1)))
|
|
|
|
newOp2 = phi_translate(U->getOperand(1), pred, succ);
|
2007-06-19 03:31:41 +00:00
|
|
|
else
|
2007-06-25 05:41:12 +00:00
|
|
|
newOp2 = U->getOperand(1);
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-06-04 18:05:26 +00:00
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-25 05:41:12 +00:00
|
|
|
if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
|
|
|
|
Instruction* newVal = 0;
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = BinaryOperator::Create(BO->getOpcode(),
|
2007-06-25 05:41:12 +00:00
|
|
|
newOp1, newOp2,
|
|
|
|
BO->getName()+".expr");
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = CmpInst::Create(C->getOpcode(),
|
2007-06-25 05:41:12 +00:00
|
|
|
C->getPredicate(),
|
|
|
|
newOp1, newOp2,
|
|
|
|
C->getName()+".expr");
|
2007-06-27 04:10:46 +00:00
|
|
|
else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
|
|
|
|
newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
|
|
|
|
|
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
|
|
|
|
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
|
|
|
if (leader == 0) {
|
|
|
|
createdExpressions.push_back(newVal);
|
|
|
|
return newVal;
|
|
|
|
} else {
|
|
|
|
VN.erase(newVal);
|
|
|
|
delete newVal;
|
|
|
|
return leader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ternary Operations
|
2007-06-28 23:51:21 +00:00
|
|
|
} else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
|
|
|
|
isa<SelectInst>(V)) {
|
2007-06-27 04:10:46 +00:00
|
|
|
User* U = cast<User>(V);
|
|
|
|
|
|
|
|
Value* newOp1 = 0;
|
|
|
|
if (isa<Instruction>(U->getOperand(0)))
|
|
|
|
newOp1 = phi_translate(U->getOperand(0), pred, succ);
|
|
|
|
else
|
|
|
|
newOp1 = U->getOperand(0);
|
|
|
|
|
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Value* newOp2 = 0;
|
|
|
|
if (isa<Instruction>(U->getOperand(1)))
|
|
|
|
newOp2 = phi_translate(U->getOperand(1), pred, succ);
|
|
|
|
else
|
|
|
|
newOp2 = U->getOperand(1);
|
|
|
|
|
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Value* newOp3 = 0;
|
|
|
|
if (isa<Instruction>(U->getOperand(2)))
|
|
|
|
newOp3 = phi_translate(U->getOperand(2), pred, succ);
|
|
|
|
else
|
|
|
|
newOp3 = U->getOperand(2);
|
|
|
|
|
|
|
|
if (newOp3 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != U->getOperand(0) ||
|
|
|
|
newOp2 != U->getOperand(1) ||
|
|
|
|
newOp3 != U->getOperand(2)) {
|
|
|
|
Instruction* newVal = 0;
|
|
|
|
if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
|
|
|
|
newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
|
2008-05-15 10:04:30 +00:00
|
|
|
S->getName() + ".expr");
|
2007-06-27 04:10:46 +00:00
|
|
|
else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
|
2008-04-06 20:25:17 +00:00
|
|
|
newVal = InsertElementInst::Create(newOp1, newOp2, newOp3,
|
2008-05-15 10:04:30 +00:00
|
|
|
I->getName() + ".expr");
|
2007-06-28 23:51:21 +00:00
|
|
|
else if (SelectInst* I = dyn_cast<SelectInst>(U))
|
2008-05-15 10:04:30 +00:00
|
|
|
newVal = SelectInst::Create(newOp1, newOp2, newOp3,
|
|
|
|
I->getName() + ".expr");
|
2007-06-08 20:44:02 +00:00
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
2007-06-12 22:43:57 +00:00
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
2007-06-12 22:43:57 +00:00
|
|
|
if (leader == 0) {
|
2007-06-12 00:50:47 +00:00
|
|
|
createdExpressions.push_back(newVal);
|
2007-06-04 18:05:26 +00:00
|
|
|
return newVal;
|
2007-06-05 22:11:49 +00:00
|
|
|
} else {
|
2007-06-19 05:37:32 +00:00
|
|
|
VN.erase(newVal);
|
2007-06-05 22:11:49 +00:00
|
|
|
delete newVal;
|
2007-06-12 22:43:57 +00:00
|
|
|
return leader;
|
2007-06-05 22:11:49 +00:00
|
|
|
}
|
2007-06-04 18:05:26 +00:00
|
|
|
}
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-07-03 23:51:19 +00:00
|
|
|
// Varargs operators
|
|
|
|
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
|
|
|
|
Value* newOp1 = 0;
|
|
|
|
if (isa<Instruction>(U->getPointerOperand()))
|
|
|
|
newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
|
|
|
|
else
|
|
|
|
newOp1 = U->getPointerOperand();
|
|
|
|
|
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bool changed_idx = false;
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 4> newIdx;
|
2007-07-03 23:51:19 +00:00
|
|
|
for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (isa<Instruction>(*I)) {
|
|
|
|
Value* newVal = phi_translate(*I, pred, succ);
|
|
|
|
newIdx.push_back(newVal);
|
|
|
|
if (newVal != *I)
|
|
|
|
changed_idx = true;
|
|
|
|
} else {
|
|
|
|
newIdx.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newOp1 != U->getPointerOperand() || changed_idx) {
|
2008-04-06 20:25:17 +00:00
|
|
|
Instruction* newVal =
|
|
|
|
GetElementPtrInst::Create(newOp1,
|
|
|
|
newIdx.begin(), newIdx.end(),
|
|
|
|
U->getName()+".expr");
|
2007-07-03 23:51:19 +00:00
|
|
|
|
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
|
|
|
|
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
|
|
|
if (leader == 0) {
|
|
|
|
createdExpressions.push_back(newVal);
|
|
|
|
return newVal;
|
|
|
|
} else {
|
|
|
|
VN.erase(newVal);
|
|
|
|
delete newVal;
|
|
|
|
return leader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
// PHI Nodes
|
2007-06-04 18:05:26 +00:00
|
|
|
} else if (PHINode* P = dyn_cast<PHINode>(V)) {
|
2007-06-12 00:50:47 +00:00
|
|
|
if (P->getParent() == succ)
|
2007-06-04 18:05:26 +00:00
|
|
|
return P->getIncomingValueForBlock(pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// phi_translate_set - Perform phi translation on every element of a set
|
2007-07-09 06:50:06 +00:00
|
|
|
void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn,
|
2007-06-12 00:50:47 +00:00
|
|
|
BasicBlock* pred, BasicBlock* succ,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& out) {
|
|
|
|
for (ValueNumberedSet::iterator I = anticIn.begin(),
|
2007-06-04 18:05:26 +00:00
|
|
|
E = anticIn.end(); I != E; ++I) {
|
2007-06-12 22:43:57 +00:00
|
|
|
Value* V = phi_translate(*I, pred, succ);
|
2007-07-09 06:50:06 +00:00
|
|
|
if (V != 0 && !out.test(VN.lookup_or_add(V))) {
|
2007-06-04 18:05:26 +00:00
|
|
|
out.insert(V);
|
2007-07-09 06:50:06 +00:00
|
|
|
out.set(VN.lookup(V));
|
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// dependsOnInvoke - Test if a value has an phi node as an operand, any of
|
|
|
|
/// whose inputs is an invoke instruction. If this is true, we cannot safely
|
|
|
|
/// PRE the instruction or anything that depends on it.
|
2007-06-18 04:42:29 +00:00
|
|
|
bool GVNPRE::dependsOnInvoke(Value* V) {
|
2007-06-19 23:07:16 +00:00
|
|
|
if (PHINode* p = dyn_cast<PHINode>(V)) {
|
|
|
|
for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
|
|
|
|
if (isa<InvokeInst>(*I))
|
2007-06-18 04:42:29 +00:00
|
|
|
return true;
|
2007-06-19 23:07:16 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
2007-06-16 00:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// clean - Remove all non-opaque values from the set whose operands are not
|
|
|
|
/// themselves in the set, as well as all values that depend on invokes (see
|
|
|
|
/// above)
|
2007-07-09 06:50:06 +00:00
|
|
|
void GVNPRE::clean(ValueNumberedSet& set) {
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 8> worklist;
|
2007-06-22 00:43:22 +00:00
|
|
|
worklist.reserve(set.size());
|
2007-06-08 01:03:01 +00:00
|
|
|
topo_sort(set, worklist);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
for (unsigned i = 0; i < worklist.size(); ++i) {
|
|
|
|
Value* v = worklist[i];
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-07-03 18:37:08 +00:00
|
|
|
// Handle unary ops
|
2007-07-03 19:01:42 +00:00
|
|
|
if (CastInst* U = dyn_cast<CastInst>(v)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
bool lhsValid = !isa<Instruction>(U->getOperand(0));
|
2007-07-09 06:50:06 +00:00
|
|
|
lhsValid |= set.test(VN.lookup(U->getOperand(0)));
|
2007-07-03 18:37:08 +00:00
|
|
|
if (lhsValid)
|
|
|
|
lhsValid = !dependsOnInvoke(U->getOperand(0));
|
|
|
|
|
|
|
|
if (!lhsValid) {
|
|
|
|
set.erase(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
set.reset(VN.lookup(U));
|
2007-07-03 18:37:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle binary ops
|
2007-07-03 18:37:08 +00:00
|
|
|
} else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
|
2007-06-27 17:03:03 +00:00
|
|
|
isa<ExtractElementInst>(v)) {
|
|
|
|
User* U = cast<User>(v);
|
|
|
|
|
|
|
|
bool lhsValid = !isa<Instruction>(U->getOperand(0));
|
2007-07-09 06:50:06 +00:00
|
|
|
lhsValid |= set.test(VN.lookup(U->getOperand(0)));
|
2007-06-18 04:30:44 +00:00
|
|
|
if (lhsValid)
|
2007-06-27 17:03:03 +00:00
|
|
|
lhsValid = !dependsOnInvoke(U->getOperand(0));
|
2007-06-03 05:55:58 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
bool rhsValid = !isa<Instruction>(U->getOperand(1));
|
2007-07-09 06:50:06 +00:00
|
|
|
rhsValid |= set.test(VN.lookup(U->getOperand(1)));
|
2007-06-18 04:30:44 +00:00
|
|
|
if (rhsValid)
|
2007-06-27 17:03:03 +00:00
|
|
|
rhsValid = !dependsOnInvoke(U->getOperand(1));
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-27 17:38:29 +00:00
|
|
|
if (!lhsValid || !rhsValid) {
|
2007-06-27 17:03:03 +00:00
|
|
|
set.erase(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
set.reset(VN.lookup(U));
|
2007-06-27 17:38:29 +00:00
|
|
|
}
|
2007-06-09 18:35:31 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle ternary ops
|
2007-06-28 23:51:21 +00:00
|
|
|
} else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
|
|
|
|
isa<SelectInst>(v)) {
|
2007-06-27 17:03:03 +00:00
|
|
|
User* U = cast<User>(v);
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
bool lhsValid = !isa<Instruction>(U->getOperand(0));
|
2007-07-09 06:50:06 +00:00
|
|
|
lhsValid |= set.test(VN.lookup(U->getOperand(0)));
|
2007-06-27 04:10:46 +00:00
|
|
|
if (lhsValid)
|
2007-06-27 17:03:03 +00:00
|
|
|
lhsValid = !dependsOnInvoke(U->getOperand(0));
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
bool rhsValid = !isa<Instruction>(U->getOperand(1));
|
2007-07-09 06:50:06 +00:00
|
|
|
rhsValid |= set.test(VN.lookup(U->getOperand(1)));
|
2007-06-27 04:10:46 +00:00
|
|
|
if (rhsValid)
|
2007-06-27 17:03:03 +00:00
|
|
|
rhsValid = !dependsOnInvoke(U->getOperand(1));
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
bool thirdValid = !isa<Instruction>(U->getOperand(2));
|
2007-07-09 06:50:06 +00:00
|
|
|
thirdValid |= set.test(VN.lookup(U->getOperand(2)));
|
2007-06-27 04:10:46 +00:00
|
|
|
if (thirdValid)
|
2007-06-27 17:03:03 +00:00
|
|
|
thirdValid = !dependsOnInvoke(U->getOperand(2));
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:38:29 +00:00
|
|
|
if (!lhsValid || !rhsValid || !thirdValid) {
|
2007-06-27 17:03:03 +00:00
|
|
|
set.erase(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
set.reset(VN.lookup(U));
|
2007-06-27 17:38:29 +00:00
|
|
|
}
|
2007-07-03 23:51:19 +00:00
|
|
|
|
|
|
|
// Handle varargs ops
|
|
|
|
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
|
|
|
|
bool ptrValid = !isa<Instruction>(U->getPointerOperand());
|
2007-07-09 06:50:06 +00:00
|
|
|
ptrValid |= set.test(VN.lookup(U->getPointerOperand()));
|
2007-07-03 23:51:19 +00:00
|
|
|
if (ptrValid)
|
|
|
|
ptrValid = !dependsOnInvoke(U->getPointerOperand());
|
|
|
|
|
|
|
|
bool varValid = true;
|
|
|
|
for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (varValid) {
|
2007-07-09 06:50:06 +00:00
|
|
|
varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
|
2007-07-03 23:51:19 +00:00
|
|
|
varValid &= !dependsOnInvoke(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ptrValid || !varValid) {
|
|
|
|
set.erase(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
set.reset(VN.lookup(U));
|
2007-07-03 23:51:19 +00:00
|
|
|
}
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// topo_sort - Given a set of values, sort them by topological
|
|
|
|
/// order into the provided vector.
|
2007-07-19 06:37:56 +00:00
|
|
|
void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) {
|
2007-06-28 00:34:34 +00:00
|
|
|
SmallPtrSet<Value*, 16> visited;
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 8> stack;
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = set.begin(), E = set.end();
|
2007-06-08 01:03:01 +00:00
|
|
|
I != E; ++I) {
|
2007-06-22 21:31:16 +00:00
|
|
|
if (visited.count(*I) == 0)
|
|
|
|
stack.push_back(*I);
|
|
|
|
|
|
|
|
while (!stack.empty()) {
|
|
|
|
Value* e = stack.back();
|
2007-07-03 18:37:08 +00:00
|
|
|
|
|
|
|
// Handle unary ops
|
2007-07-03 19:01:42 +00:00
|
|
|
if (CastInst* U = dyn_cast<CastInst>(e)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
|
|
|
|
|
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
|
|
|
visited.count(l) == 0)
|
|
|
|
stack.push_back(l);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle binary ops
|
2007-07-03 18:37:08 +00:00
|
|
|
} else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
|
2007-06-27 17:03:03 +00:00
|
|
|
isa<ExtractElementInst>(e)) {
|
|
|
|
User* U = cast<User>(e);
|
|
|
|
Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
|
|
|
|
Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
|
2007-06-22 21:31:16 +00:00
|
|
|
|
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
|
|
|
visited.count(l) == 0)
|
|
|
|
stack.push_back(l);
|
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
|
|
|
visited.count(r) == 0)
|
|
|
|
stack.push_back(r);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle ternary ops
|
2007-06-28 23:51:21 +00:00
|
|
|
} else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
|
|
|
|
isa<SelectInst>(e)) {
|
2007-06-27 17:03:03 +00:00
|
|
|
User* U = cast<User>(e);
|
|
|
|
Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
|
|
|
|
Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
|
|
|
|
Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
|
2007-06-27 04:10:46 +00:00
|
|
|
|
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
|
|
|
visited.count(l) == 0)
|
|
|
|
stack.push_back(l);
|
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
|
|
|
visited.count(r) == 0)
|
|
|
|
stack.push_back(r);
|
|
|
|
else if (m != 0 && isa<Instruction>(m) &&
|
|
|
|
visited.count(m) == 0)
|
2007-07-04 18:26:18 +00:00
|
|
|
stack.push_back(m);
|
2007-06-27 04:10:46 +00:00
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2007-07-03 23:51:19 +00:00
|
|
|
// Handle vararg ops
|
|
|
|
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
|
|
|
|
Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
|
|
|
|
|
|
|
|
if (p != 0 && isa<Instruction>(p) &&
|
|
|
|
visited.count(p) == 0)
|
|
|
|
stack.push_back(p);
|
|
|
|
else {
|
|
|
|
bool push_va = false;
|
|
|
|
for (GetElementPtrInst::op_iterator I = U->idx_begin(),
|
|
|
|
E = U->idx_end(); I != E; ++I) {
|
|
|
|
Value * v = find_leader(set, VN.lookup(*I));
|
|
|
|
if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) {
|
|
|
|
stack.push_back(v);
|
|
|
|
push_va = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!push_va) {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle opaque ops
|
2007-06-22 21:31:16 +00:00
|
|
|
} else {
|
2007-06-09 18:35:31 +00:00
|
|
|
visited.insert(e);
|
2007-06-03 05:55:58 +00:00
|
|
|
vec.push_back(e);
|
2007-06-22 21:31:16 +00:00
|
|
|
stack.pop_back();
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
2007-05-31 22:44:11 +00:00
|
|
|
}
|
2007-06-22 21:31:16 +00:00
|
|
|
|
|
|
|
stack.clear();
|
2007-05-31 22:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// dump - Dump a set of values to standard error
|
2007-07-09 06:50:06 +00:00
|
|
|
void GVNPRE::dump(ValueNumberedSet& s) const {
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "{ ";
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
|
2007-06-03 22:02:14 +00:00
|
|
|
I != E; ++I) {
|
2007-06-28 23:51:21 +00:00
|
|
|
DOUT << "" << VN.lookup(*I) << ": ";
|
2007-06-03 22:02:14 +00:00
|
|
|
DEBUG((*I)->dump());
|
|
|
|
}
|
|
|
|
DOUT << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// elimination - Phase 3 of the main algorithm. Perform full redundancy
|
|
|
|
/// elimination by walking the dominator tree and removing any instruction that
|
|
|
|
/// is dominated by another instruction with the same value number.
|
2007-06-20 22:10:02 +00:00
|
|
|
bool GVNPRE::elimination() {
|
|
|
|
bool changed_function = false;
|
|
|
|
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<std::pair<Instruction*, Value*>, 8> replace;
|
|
|
|
SmallVector<Instruction*, 8> erase;
|
2007-06-12 16:57:50 +00:00
|
|
|
|
|
|
|
DominatorTree& DT = getAnalysis<DominatorTree>();
|
|
|
|
|
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
|
|
|
|
isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
|
2007-07-03 18:37:08 +00:00
|
|
|
isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
|
2007-07-03 23:51:19 +00:00
|
|
|
isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
|
2007-07-10 00:09:25 +00:00
|
|
|
|
2007-08-02 18:20:52 +00:00
|
|
|
if (availableOut[BB].test(VN.lookup(BI)) &&
|
|
|
|
!availableOut[BB].count(BI)) {
|
2007-07-10 00:09:25 +00:00
|
|
|
Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
|
2007-06-12 16:57:50 +00:00
|
|
|
if (Instruction* Instr = dyn_cast<Instruction>(leader))
|
|
|
|
if (Instr->getParent() != 0 && Instr != BI) {
|
|
|
|
replace.push_back(std::make_pair(BI, leader));
|
|
|
|
erase.push_back(BI);
|
|
|
|
++NumEliminated;
|
|
|
|
}
|
2007-07-10 00:09:25 +00:00
|
|
|
}
|
2007-06-12 16:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!replace.empty()) {
|
|
|
|
std::pair<Instruction*, Value*> rep = replace.back();
|
|
|
|
replace.pop_back();
|
|
|
|
rep.first->replaceAllUsesWith(rep.second);
|
2007-06-20 18:30:20 +00:00
|
|
|
changed_function = true;
|
2007-06-12 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
2007-08-02 18:20:52 +00:00
|
|
|
for (SmallVector<Instruction*, 8>::iterator I = erase.begin(),
|
|
|
|
E = erase.end(); I != E; ++I)
|
2007-06-12 16:57:50 +00:00
|
|
|
(*I)->eraseFromParent();
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
return changed_function;
|
2007-06-12 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// cleanup - Delete any extraneous values that were created to represent
|
|
|
|
/// expressions without leaders.
|
2007-06-12 16:57:50 +00:00
|
|
|
void GVNPRE::cleanup() {
|
|
|
|
while (!createdExpressions.empty()) {
|
|
|
|
Instruction* I = createdExpressions.back();
|
|
|
|
createdExpressions.pop_back();
|
|
|
|
|
|
|
|
delete I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// buildsets_availout - When calculating availability, handle an instruction
|
|
|
|
/// by inserting it into the appropriate sets
|
2007-06-20 22:10:02 +00:00
|
|
|
void GVNPRE::buildsets_availout(BasicBlock::iterator I,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& currAvail,
|
|
|
|
ValueNumberedSet& currPhis,
|
|
|
|
ValueNumberedSet& currExps,
|
|
|
|
SmallPtrSet<Value*, 16>& currTemps) {
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle PHI nodes
|
2007-06-20 22:10:02 +00:00
|
|
|
if (PHINode* p = dyn_cast<PHINode>(I)) {
|
2007-07-09 06:50:06 +00:00
|
|
|
unsigned num = VN.lookup_or_add(p);
|
2007-06-22 03:14:03 +00:00
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
currPhis.insert(p);
|
2007-07-09 06:50:06 +00:00
|
|
|
currPhis.set(num);
|
2007-07-03 18:37:08 +00:00
|
|
|
|
|
|
|
// Handle unary ops
|
2007-07-03 19:01:42 +00:00
|
|
|
} else if (CastInst* U = dyn_cast<CastInst>(I)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
Value* leftValue = U->getOperand(0);
|
|
|
|
|
|
|
|
unsigned num = VN.lookup_or_add(U);
|
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(leftValue))) {
|
2007-07-03 18:37:08 +00:00
|
|
|
currExps.insert(leftValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(leftValue));
|
2007-07-03 18:37:08 +00:00
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(num)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
currExps.insert(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(num);
|
2007-07-03 18:37:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle binary ops
|
|
|
|
} else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
|
|
|
|
isa<ExtractElementInst>(I)) {
|
|
|
|
User* U = cast<User>(I);
|
|
|
|
Value* leftValue = U->getOperand(0);
|
|
|
|
Value* rightValue = U->getOperand(1);
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
unsigned num = VN.lookup_or_add(U);
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(leftValue))) {
|
2007-06-22 03:14:03 +00:00
|
|
|
currExps.insert(leftValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(leftValue));
|
2007-06-22 03:14:03 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
if (isa<Instruction>(rightValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(rightValue))) {
|
2007-06-22 03:14:03 +00:00
|
|
|
currExps.insert(rightValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(rightValue));
|
2007-06-22 03:14:03 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(num)) {
|
2007-06-27 17:03:03 +00:00
|
|
|
currExps.insert(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(num);
|
2007-06-27 04:10:46 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle ternary ops
|
2007-06-28 23:51:21 +00:00
|
|
|
} else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
|
|
|
|
isa<SelectInst>(I)) {
|
2007-06-27 17:03:03 +00:00
|
|
|
User* U = cast<User>(I);
|
|
|
|
Value* leftValue = U->getOperand(0);
|
|
|
|
Value* rightValue = U->getOperand(1);
|
|
|
|
Value* thirdValue = U->getOperand(2);
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
VN.lookup_or_add(U);
|
2007-06-27 04:10:46 +00:00
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
unsigned num = VN.lookup_or_add(U);
|
2007-06-27 04:10:46 +00:00
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(leftValue))) {
|
2007-06-27 04:10:46 +00:00
|
|
|
currExps.insert(leftValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(leftValue));
|
2007-06-27 04:10:46 +00:00
|
|
|
}
|
|
|
|
if (isa<Instruction>(rightValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(rightValue))) {
|
2007-06-27 04:10:46 +00:00
|
|
|
currExps.insert(rightValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(rightValue));
|
2007-06-27 04:10:46 +00:00
|
|
|
}
|
|
|
|
if (isa<Instruction>(thirdValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(thirdValue))) {
|
2007-06-27 04:10:46 +00:00
|
|
|
currExps.insert(thirdValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(thirdValue));
|
2007-06-27 04:10:46 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(num)) {
|
2007-06-27 17:03:03 +00:00
|
|
|
currExps.insert(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(num);
|
2007-06-27 04:10:46 +00:00
|
|
|
}
|
|
|
|
|
2007-07-03 23:51:19 +00:00
|
|
|
// Handle vararg ops
|
|
|
|
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
Value* ptrValue = U->getPointerOperand();
|
|
|
|
|
|
|
|
VN.lookup_or_add(U);
|
|
|
|
|
|
|
|
unsigned num = VN.lookup_or_add(U);
|
|
|
|
|
|
|
|
if (isa<Instruction>(ptrValue))
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(ptrValue))) {
|
2007-07-03 23:51:19 +00:00
|
|
|
currExps.insert(ptrValue);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(ptrValue));
|
2007-07-03 23:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
|
|
|
|
OI != OE; ++OI)
|
2007-07-09 06:50:06 +00:00
|
|
|
if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
|
2007-07-03 23:51:19 +00:00
|
|
|
currExps.insert(*OI);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(VN.lookup(*OI));
|
2007-07-03 23:51:19 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currExps.test(VN.lookup(U))) {
|
2007-07-03 23:51:19 +00:00
|
|
|
currExps.insert(U);
|
2007-07-09 06:50:06 +00:00
|
|
|
currExps.set(num);
|
2007-07-03 23:51:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 17:03:03 +00:00
|
|
|
// Handle opaque ops
|
|
|
|
} else if (!I->isTerminator()){
|
2007-06-20 22:10:02 +00:00
|
|
|
VN.lookup_or_add(I);
|
2007-06-22 03:14:03 +00:00
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
currTemps.insert(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!I->isTerminator())
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!currAvail.test(VN.lookup(I))) {
|
2007-06-22 03:14:03 +00:00
|
|
|
currAvail.insert(I);
|
2007-07-09 06:50:06 +00:00
|
|
|
currAvail.set(VN.lookup(I));
|
2007-06-22 03:14:03 +00:00
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
|
|
|
|
/// set as a function of the ANTIC_IN set of the block's predecessors
|
2007-06-22 00:20:30 +00:00
|
|
|
bool GVNPRE::buildsets_anticout(BasicBlock* BB,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& anticOut,
|
2007-07-19 03:32:44 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8>& visited) {
|
2007-06-20 22:10:02 +00:00
|
|
|
if (BB->getTerminator()->getNumSuccessors() == 1) {
|
2007-06-25 05:41:12 +00:00
|
|
|
if (BB->getTerminator()->getSuccessor(0) != BB &&
|
|
|
|
visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
|
2007-06-22 00:20:30 +00:00
|
|
|
return true;
|
2007-06-25 05:41:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-06-20 22:10:02 +00:00
|
|
|
phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
|
|
|
|
BB, BB->getTerminator()->getSuccessor(0), anticOut);
|
2007-06-25 05:41:12 +00:00
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
} else if (BB->getTerminator()->getNumSuccessors() > 1) {
|
|
|
|
BasicBlock* first = BB->getTerminator()->getSuccessor(0);
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(),
|
|
|
|
E = anticipatedIn[first].end(); I != E; ++I) {
|
|
|
|
anticOut.insert(*I);
|
|
|
|
anticOut.set(VN.lookup(*I));
|
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
|
|
|
|
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 16> temp;
|
2007-06-21 17:57:53 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = anticOut.begin(),
|
2007-06-21 17:57:53 +00:00
|
|
|
E = anticOut.end(); I != E; ++I)
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!succAnticIn.test(VN.lookup(*I)))
|
2007-06-21 17:57:53 +00:00
|
|
|
temp.push_back(*I);
|
|
|
|
|
2007-07-19 06:37:56 +00:00
|
|
|
for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end();
|
2007-07-09 06:50:06 +00:00
|
|
|
I != E; ++I) {
|
2007-06-21 17:57:53 +00:00
|
|
|
anticOut.erase(*I);
|
2007-07-09 06:50:06 +00:00
|
|
|
anticOut.reset(VN.lookup(*I));
|
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-22 00:20:30 +00:00
|
|
|
|
|
|
|
return false;
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
|
|
|
|
/// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
|
|
|
|
/// sets populated in buildsets_availout
|
2007-06-22 00:20:30 +00:00
|
|
|
unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& anticOut,
|
|
|
|
ValueNumberedSet& currExps,
|
2007-06-28 00:34:34 +00:00
|
|
|
SmallPtrSet<Value*, 16>& currTemps,
|
2007-07-19 03:32:44 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8>& visited) {
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& anticIn = anticipatedIn[BB];
|
2007-06-22 18:27:04 +00:00
|
|
|
unsigned old = anticIn.size();
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-06-22 00:20:30 +00:00
|
|
|
bool defer = buildsets_anticout(BB, anticOut, visited);
|
|
|
|
if (defer)
|
|
|
|
return 0;
|
2007-06-21 17:57:53 +00:00
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
anticIn.clear();
|
2007-06-21 17:57:53 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = anticOut.begin(),
|
2007-06-22 18:27:04 +00:00
|
|
|
E = anticOut.end(); I != E; ++I) {
|
2007-07-09 06:50:06 +00:00
|
|
|
anticIn.insert(*I);
|
|
|
|
anticIn.set(VN.lookup(*I));
|
2007-06-22 18:27:04 +00:00
|
|
|
}
|
2007-07-09 06:50:06 +00:00
|
|
|
for (ValueNumberedSet::iterator I = currExps.begin(),
|
2007-06-22 18:27:04 +00:00
|
|
|
E = currExps.end(); I != E; ++I) {
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!anticIn.test(VN.lookup(*I))) {
|
2007-06-21 17:57:53 +00:00
|
|
|
anticIn.insert(*I);
|
2007-07-09 06:50:06 +00:00
|
|
|
anticIn.set(VN.lookup(*I));
|
2007-06-22 18:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 00:34:34 +00:00
|
|
|
for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
|
2007-06-27 17:38:29 +00:00
|
|
|
E = currTemps.end(); I != E; ++I) {
|
2007-06-22 18:27:04 +00:00
|
|
|
anticIn.erase(*I);
|
2007-07-09 06:50:06 +00:00
|
|
|
anticIn.reset(VN.lookup(*I));
|
2007-06-27 17:38:29 +00:00
|
|
|
}
|
2007-06-22 00:20:30 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
clean(anticIn);
|
2007-06-27 17:38:29 +00:00
|
|
|
anticOut.clear();
|
2007-06-20 18:30:20 +00:00
|
|
|
|
2007-06-27 17:38:29 +00:00
|
|
|
if (old != anticIn.size())
|
2007-06-22 00:20:30 +00:00
|
|
|
return 2;
|
2007-06-27 17:38:29 +00:00
|
|
|
else
|
2007-06-22 00:20:30 +00:00
|
|
|
return 1;
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
|
|
|
|
/// and the ANTIC_IN sets.
|
2007-06-26 23:29:41 +00:00
|
|
|
void GVNPRE::buildsets(Function& F) {
|
2007-07-19 03:32:44 +00:00
|
|
|
DenseMap<BasicBlock*, ValueNumberedSet> generatedExpressions;
|
|
|
|
DenseMap<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
// Phase 1, Part 1: calculate AVAIL_OUT
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
// Top-down walk of the dominator tree
|
2007-06-04 00:32:22 +00:00
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
2007-05-29 21:53:49 +00:00
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
|
|
|
|
// Get the sets to update for this block
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()];
|
|
|
|
ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()];
|
2007-06-28 00:34:34 +00:00
|
|
|
SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
|
|
|
// A block inherits AVAIL_OUT from its dominator
|
2007-07-09 22:29:50 +00:00
|
|
|
if (DI->getIDom() != 0)
|
|
|
|
currAvail = availableOut[DI->getIDom()->getBlock()];
|
2007-07-09 06:50:06 +00:00
|
|
|
|
2007-06-19 03:31:41 +00:00
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
2007-06-20 22:10:02 +00:00
|
|
|
BI != BE; ++BI)
|
2007-06-22 03:14:03 +00:00
|
|
|
buildsets_availout(BI, currAvail, currPhis, currExps,
|
2007-07-09 06:50:06 +00:00
|
|
|
currTemps);
|
2007-06-19 03:31:41 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
2007-06-25 05:41:12 +00:00
|
|
|
|
|
|
|
// Phase 1, Part 2: calculate ANTIC_IN
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-07-19 03:32:44 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8> visited;
|
2007-06-26 23:29:41 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 4> block_changed;
|
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
|
|
|
|
block_changed.insert(FI);
|
2007-05-29 23:26:30 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
bool changed = true;
|
|
|
|
unsigned iterations = 0;
|
2007-06-26 23:29:41 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
while (changed) {
|
|
|
|
changed = false;
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet anticOut;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-26 23:29:41 +00:00
|
|
|
// Postorder walk of the CFG
|
2007-06-25 18:25:31 +00:00
|
|
|
for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
|
|
|
|
BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
|
2007-06-25 05:41:12 +00:00
|
|
|
BasicBlock* BB = *BBI;
|
2007-06-11 16:25:17 +00:00
|
|
|
|
2007-06-26 23:29:41 +00:00
|
|
|
if (block_changed.count(BB) != 0) {
|
|
|
|
unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
|
|
|
|
generatedTemporaries[BB], visited);
|
2007-06-22 00:20:30 +00:00
|
|
|
|
2007-06-26 23:29:41 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
changed = true;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
visited.insert(BB);
|
|
|
|
|
|
|
|
if (ret == 2)
|
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
block_changed.insert(*PI);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
block_changed.erase(BB);
|
|
|
|
|
|
|
|
changed |= (ret == 2);
|
2007-06-25 05:41:12 +00:00
|
|
|
}
|
2007-06-22 00:20:30 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
2007-06-04 18:05:26 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
iterations++;
|
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// insertion_pre - When a partial redundancy has been identified, eliminate it
|
|
|
|
/// by inserting appropriate values into the predecessors and a phi node in
|
|
|
|
/// the main block
|
2007-06-20 22:10:02 +00:00
|
|
|
void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*>& avail,
|
2007-07-09 06:50:06 +00:00
|
|
|
std::map<BasicBlock*, ValueNumberedSet>& new_sets) {
|
2007-06-20 22:10:02 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
|
|
|
|
Value* e2 = avail[*PI];
|
2007-07-09 06:50:06 +00:00
|
|
|
if (!availableOut[*PI].test(VN.lookup(e2))) {
|
2007-06-20 22:10:02 +00:00
|
|
|
User* U = cast<User>(e2);
|
|
|
|
|
|
|
|
Value* s1 = 0;
|
|
|
|
if (isa<BinaryOperator>(U->getOperand(0)) ||
|
2007-06-27 04:10:46 +00:00
|
|
|
isa<CmpInst>(U->getOperand(0)) ||
|
|
|
|
isa<ShuffleVectorInst>(U->getOperand(0)) ||
|
|
|
|
isa<ExtractElementInst>(U->getOperand(0)) ||
|
2007-06-28 23:51:21 +00:00
|
|
|
isa<InsertElementInst>(U->getOperand(0)) ||
|
2007-07-03 18:37:08 +00:00
|
|
|
isa<SelectInst>(U->getOperand(0)) ||
|
2007-07-03 23:51:19 +00:00
|
|
|
isa<CastInst>(U->getOperand(0)) ||
|
|
|
|
isa<GetElementPtrInst>(U->getOperand(0)))
|
2007-06-20 22:10:02 +00:00
|
|
|
s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
|
|
|
|
else
|
|
|
|
s1 = U->getOperand(0);
|
|
|
|
|
|
|
|
Value* s2 = 0;
|
2007-07-03 18:37:08 +00:00
|
|
|
|
|
|
|
if (isa<BinaryOperator>(U) ||
|
|
|
|
isa<CmpInst>(U) ||
|
|
|
|
isa<ShuffleVectorInst>(U) ||
|
|
|
|
isa<ExtractElementInst>(U) ||
|
|
|
|
isa<InsertElementInst>(U) ||
|
2008-02-20 11:26:25 +00:00
|
|
|
isa<SelectInst>(U)) {
|
2007-07-03 18:37:08 +00:00
|
|
|
if (isa<BinaryOperator>(U->getOperand(1)) ||
|
|
|
|
isa<CmpInst>(U->getOperand(1)) ||
|
|
|
|
isa<ShuffleVectorInst>(U->getOperand(1)) ||
|
|
|
|
isa<ExtractElementInst>(U->getOperand(1)) ||
|
|
|
|
isa<InsertElementInst>(U->getOperand(1)) ||
|
|
|
|
isa<SelectInst>(U->getOperand(1)) ||
|
2007-07-03 23:51:19 +00:00
|
|
|
isa<CastInst>(U->getOperand(1)) ||
|
|
|
|
isa<GetElementPtrInst>(U->getOperand(1))) {
|
2007-07-03 18:37:08 +00:00
|
|
|
s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
|
|
|
|
} else {
|
|
|
|
s2 = U->getOperand(1);
|
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2007-06-27 17:03:03 +00:00
|
|
|
|
|
|
|
// Ternary Operators
|
2007-06-27 04:10:46 +00:00
|
|
|
Value* s3 = 0;
|
|
|
|
if (isa<ShuffleVectorInst>(U) ||
|
2007-06-28 23:51:21 +00:00
|
|
|
isa<InsertElementInst>(U) ||
|
2008-02-20 11:26:25 +00:00
|
|
|
isa<SelectInst>(U)) {
|
2007-06-27 04:10:46 +00:00
|
|
|
if (isa<BinaryOperator>(U->getOperand(2)) ||
|
|
|
|
isa<CmpInst>(U->getOperand(2)) ||
|
|
|
|
isa<ShuffleVectorInst>(U->getOperand(2)) ||
|
|
|
|
isa<ExtractElementInst>(U->getOperand(2)) ||
|
2007-06-28 23:51:21 +00:00
|
|
|
isa<InsertElementInst>(U->getOperand(2)) ||
|
2007-07-03 18:37:08 +00:00
|
|
|
isa<SelectInst>(U->getOperand(2)) ||
|
2007-07-03 23:51:19 +00:00
|
|
|
isa<CastInst>(U->getOperand(2)) ||
|
|
|
|
isa<GetElementPtrInst>(U->getOperand(2))) {
|
2007-06-27 04:10:46 +00:00
|
|
|
s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
|
2007-07-03 18:37:08 +00:00
|
|
|
} else {
|
2007-06-27 04:10:46 +00:00
|
|
|
s3 = U->getOperand(2);
|
2007-07-03 18:37:08 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2007-06-20 22:10:02 +00:00
|
|
|
|
2007-07-03 23:51:19 +00:00
|
|
|
// Vararg operators
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 4> sVarargs;
|
2007-07-03 23:51:19 +00:00
|
|
|
if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) {
|
|
|
|
for (GetElementPtrInst::op_iterator OI = G->idx_begin(),
|
|
|
|
OE = G->idx_end(); OI != OE; ++OI) {
|
|
|
|
if (isa<BinaryOperator>(*OI) ||
|
|
|
|
isa<CmpInst>(*OI) ||
|
|
|
|
isa<ShuffleVectorInst>(*OI) ||
|
|
|
|
isa<ExtractElementInst>(*OI) ||
|
|
|
|
isa<InsertElementInst>(*OI) ||
|
|
|
|
isa<SelectInst>(*OI) ||
|
|
|
|
isa<CastInst>(*OI) ||
|
|
|
|
isa<GetElementPtrInst>(*OI)) {
|
|
|
|
sVarargs.push_back(find_leader(availableOut[*PI],
|
|
|
|
VN.lookup(*OI)));
|
|
|
|
} else {
|
|
|
|
sVarargs.push_back(*OI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
Value* newVal = 0;
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2,
|
2007-06-20 22:10:02 +00:00
|
|
|
BO->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = CmpInst::Create(C->getOpcode(), C->getPredicate(), s1, s2,
|
2007-06-20 22:10:02 +00:00
|
|
|
C->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-06-27 04:10:46 +00:00
|
|
|
else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
|
|
|
|
newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
|
2008-04-06 20:25:17 +00:00
|
|
|
newVal = InsertElementInst::Create(s1, s2, s3, S->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-06-27 04:10:46 +00:00
|
|
|
else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
|
|
|
|
newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-06-28 23:51:21 +00:00
|
|
|
else if (SelectInst* S = dyn_cast<SelectInst>(U))
|
2008-04-06 20:25:17 +00:00
|
|
|
newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-07-03 18:37:08 +00:00
|
|
|
else if (CastInst* C = dyn_cast<CastInst>(U))
|
2008-05-16 19:29:10 +00:00
|
|
|
newVal = CastInst::Create(C->getOpcode(), s1, C->getType(),
|
2007-07-03 18:37:08 +00:00
|
|
|
C->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-07-03 23:51:19 +00:00
|
|
|
else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
|
2008-04-06 20:25:17 +00:00
|
|
|
newVal = GetElementPtrInst::Create(s1, sVarargs.begin(), sVarargs.end(),
|
|
|
|
G->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
VN.add(newVal, VN.lookup(U));
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& predAvail = availableOut[*PI];
|
2007-06-20 22:10:02 +00:00
|
|
|
val_replace(predAvail, newVal);
|
2007-07-05 23:11:26 +00:00
|
|
|
val_replace(new_sets[*PI], newVal);
|
2007-07-09 06:50:06 +00:00
|
|
|
predAvail.set(VN.lookup(newVal));
|
2007-06-21 00:19:05 +00:00
|
|
|
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
2007-06-20 22:10:02 +00:00
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, newVal));
|
|
|
|
|
|
|
|
++NumInsertedVals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PHINode* p = 0;
|
|
|
|
|
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
|
|
|
|
if (p == 0)
|
2008-04-06 20:25:17 +00:00
|
|
|
p = PHINode::Create(avail[*PI]->getType(), "gvnpre-join", BB->begin());
|
2007-07-05 23:11:26 +00:00
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
p->addIncoming(avail[*PI], *PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
VN.add(p, VN.lookup(e));
|
|
|
|
val_replace(availableOut[BB], p);
|
2007-07-09 06:50:06 +00:00
|
|
|
availableOut[BB].set(VN.lookup(e));
|
2007-07-06 20:29:43 +00:00
|
|
|
generatedPhis[BB].insert(p);
|
2007-07-09 06:50:06 +00:00
|
|
|
generatedPhis[BB].set(VN.lookup(e));
|
2007-07-05 23:11:26 +00:00
|
|
|
new_sets[BB].insert(p);
|
2007-07-09 06:50:06 +00:00
|
|
|
new_sets[BB].set(VN.lookup(e));
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
++NumInsertedPhis;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// insertion_mergepoint - When walking the dom tree, check at each merge
|
|
|
|
/// block for the possibility of a partial redundancy. If present, eliminate it
|
2007-07-19 06:37:56 +00:00
|
|
|
unsigned GVNPRE::insertion_mergepoint(SmallVector<Value*, 8>& workList,
|
2007-06-22 00:20:30 +00:00
|
|
|
df_iterator<DomTreeNode*>& D,
|
2007-07-09 06:50:06 +00:00
|
|
|
std::map<BasicBlock*, ValueNumberedSet >& new_sets) {
|
2007-06-20 22:10:02 +00:00
|
|
|
bool changed_function = false;
|
|
|
|
bool new_stuff = false;
|
|
|
|
|
|
|
|
BasicBlock* BB = D->getBlock();
|
|
|
|
for (unsigned i = 0; i < workList.size(); ++i) {
|
|
|
|
Value* e = workList[i];
|
|
|
|
|
2007-06-27 04:10:46 +00:00
|
|
|
if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
|
|
|
|
isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
|
2007-07-03 23:51:19 +00:00
|
|
|
isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
|
|
|
|
isa<GetElementPtrInst>(e)) {
|
2007-07-09 06:50:06 +00:00
|
|
|
if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e)))
|
2007-06-20 22:10:02 +00:00
|
|
|
continue;
|
|
|
|
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*> avail;
|
2007-06-20 22:10:02 +00:00
|
|
|
bool by_some = false;
|
2007-07-06 20:29:43 +00:00
|
|
|
bool all_same = true;
|
|
|
|
Value * first_s = 0;
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
|
|
|
|
++PI) {
|
|
|
|
Value *e2 = phi_translate(e, *PI, BB);
|
|
|
|
Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
|
|
|
|
|
|
|
|
if (e3 == 0) {
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
2007-06-20 22:10:02 +00:00
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, e2));
|
2007-07-06 20:29:43 +00:00
|
|
|
all_same = false;
|
2007-06-20 22:10:02 +00:00
|
|
|
} else {
|
2007-07-19 06:37:56 +00:00
|
|
|
DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
2007-06-20 22:10:02 +00:00
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, e3));
|
|
|
|
|
|
|
|
by_some = true;
|
2007-07-06 20:29:43 +00:00
|
|
|
if (first_s == 0)
|
|
|
|
first_s = e3;
|
|
|
|
else if (first_s != e3)
|
|
|
|
all_same = false;
|
2007-06-20 22:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-06 20:29:43 +00:00
|
|
|
if (by_some && !all_same &&
|
2007-07-09 06:50:06 +00:00
|
|
|
!generatedPhis[BB].test(VN.lookup(e))) {
|
2007-07-05 23:11:26 +00:00
|
|
|
insertion_pre(e, BB, avail, new_sets);
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
changed_function = true;
|
|
|
|
new_stuff = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned retval = 0;
|
|
|
|
if (changed_function)
|
|
|
|
retval += 1;
|
|
|
|
if (new_stuff)
|
|
|
|
retval += 2;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
/// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
|
|
|
|
/// merge points. When one is found, check for a partial redundancy. If one is
|
|
|
|
/// present, eliminate it. Repeat this walk until no changes are made.
|
2007-06-20 22:10:02 +00:00
|
|
|
bool GVNPRE::insertion(Function& F) {
|
|
|
|
bool changed_function = false;
|
|
|
|
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
2007-06-08 01:03:01 +00:00
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
std::map<BasicBlock*, ValueNumberedSet> new_sets;
|
2007-06-08 01:03:01 +00:00
|
|
|
bool new_stuff = true;
|
|
|
|
while (new_stuff) {
|
|
|
|
new_stuff = false;
|
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
2007-06-11 16:25:17 +00:00
|
|
|
if (BB == 0)
|
|
|
|
continue;
|
|
|
|
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& availOut = availableOut[BB];
|
|
|
|
ValueNumberedSet& anticIn = anticipatedIn[BB];
|
2007-06-08 01:03:01 +00:00
|
|
|
|
|
|
|
// Replace leaders with leaders inherited from dominator
|
|
|
|
if (DI->getIDom() != 0) {
|
2007-07-09 06:50:06 +00:00
|
|
|
ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()];
|
|
|
|
for (ValueNumberedSet::iterator I = dom_set.begin(),
|
2007-06-08 01:03:01 +00:00
|
|
|
E = dom_set.end(); I != E; ++I) {
|
2007-07-05 23:11:26 +00:00
|
|
|
val_replace(new_sets[BB], *I);
|
2007-06-19 03:31:41 +00:00
|
|
|
val_replace(availOut, *I);
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is more than one predecessor...
|
|
|
|
if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
|
2007-07-19 06:37:56 +00:00
|
|
|
SmallVector<Value*, 8> workList;
|
2007-06-22 00:43:22 +00:00
|
|
|
workList.reserve(anticIn.size());
|
2007-06-08 01:03:01 +00:00
|
|
|
topo_sort(anticIn, workList);
|
|
|
|
|
2007-07-05 23:11:26 +00:00
|
|
|
unsigned result = insertion_mergepoint(workList, DI, new_sets);
|
2007-06-20 22:10:02 +00:00
|
|
|
if (result & 1)
|
|
|
|
changed_function = true;
|
|
|
|
if (result & 2)
|
|
|
|
new_stuff = true;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-06 01:27:49 +00:00
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
return changed_function;
|
|
|
|
}
|
|
|
|
|
2007-06-21 00:19:05 +00:00
|
|
|
// GVNPRE::runOnFunction - This is the main transformation entry point for a
|
|
|
|
// function.
|
|
|
|
//
|
2007-06-20 22:10:02 +00:00
|
|
|
bool GVNPRE::runOnFunction(Function &F) {
|
2007-06-21 00:19:05 +00:00
|
|
|
// Clean out global sets from any previous functions
|
2007-06-20 22:10:02 +00:00
|
|
|
VN.clear();
|
|
|
|
createdExpressions.clear();
|
|
|
|
availableOut.clear();
|
|
|
|
anticipatedIn.clear();
|
2007-07-06 20:29:43 +00:00
|
|
|
generatedPhis.clear();
|
|
|
|
|
2007-06-20 22:10:02 +00:00
|
|
|
bool changed_function = false;
|
|
|
|
|
|
|
|
// Phase 1: BuildSets
|
2007-06-21 00:19:05 +00:00
|
|
|
// This phase calculates the AVAIL_OUT and ANTIC_IN sets
|
2007-06-26 23:29:41 +00:00
|
|
|
buildsets(F);
|
2007-06-20 22:10:02 +00:00
|
|
|
|
|
|
|
// Phase 2: Insert
|
2007-06-21 00:19:05 +00:00
|
|
|
// This phase inserts values to make partially redundant values
|
|
|
|
// fully redundant
|
2007-06-20 22:10:02 +00:00
|
|
|
changed_function |= insertion(F);
|
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
// Phase 3: Eliminate
|
2007-06-21 00:19:05 +00:00
|
|
|
// This phase performs trivial full redundancy elimination
|
2007-06-20 22:10:02 +00:00
|
|
|
changed_function |= elimination();
|
2007-06-08 20:44:02 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
// Phase 4: Cleanup
|
2007-06-21 00:19:05 +00:00
|
|
|
// This phase cleans up values that were created solely
|
|
|
|
// as leaders for expressions
|
2007-06-12 16:57:50 +00:00
|
|
|
cleanup();
|
2007-06-08 01:03:01 +00:00
|
|
|
|
2007-06-20 18:30:20 +00:00
|
|
|
return changed_function;
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|