2007-05-30 05:53:49 +08:00
|
|
|
//===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the Owen Anderson and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-09 04:57:08 +08:00
|
|
|
// live ranges, and should be used with caution on platforms that are very
|
2007-05-30 05:53:49 +08:00
|
|
|
// sensitive to register pressure.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "gvnpre"
|
|
|
|
#include "llvm/Value.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2007-06-22 08:20:30 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2007-06-20 07:23:54 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-05-30 05:53:49 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2007-06-22 01:57:53 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-05-30 05:53:49 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-06-08 09:03:01 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-05-30 05:53:49 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-05-30 07:15:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2007-05-30 05:53:49 +08:00
|
|
|
#include <algorithm>
|
2007-06-01 06:44:11 +08:00
|
|
|
#include <deque>
|
2007-05-30 05:53:49 +08:00
|
|
|
#include <map>
|
2007-06-01 06:44:11 +08:00
|
|
|
#include <vector>
|
2007-05-30 05:53:49 +08:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-21 08:19:05 +08: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 11:31:41 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN ValueTable {
|
|
|
|
public:
|
|
|
|
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 };
|
|
|
|
|
|
|
|
ExpressionOpcode opcode;
|
|
|
|
uint32_t leftVN;
|
|
|
|
uint32_t rightVN;
|
|
|
|
|
|
|
|
bool operator< (const Expression& other) const {
|
|
|
|
if (opcode < other.opcode)
|
|
|
|
return true;
|
|
|
|
else if (opcode > other.opcode)
|
|
|
|
return false;
|
|
|
|
else if (leftVN < other.leftVN)
|
|
|
|
return true;
|
|
|
|
else if (leftVN > other.leftVN)
|
|
|
|
return false;
|
|
|
|
else if (rightVN < other.rightVN)
|
|
|
|
return true;
|
|
|
|
else if (rightVN > other.rightVN)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return false;
|
2007-06-12 08:50:47 +08:00
|
|
|
}
|
2007-06-19 11:31:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2007-06-20 07:23:54 +08:00
|
|
|
DenseMap<Value*, uint32_t> valueNumbering;
|
2007-06-19 11:31:41 +08:00
|
|
|
std::map<Expression, uint32_t> expressionNumbering;
|
|
|
|
|
|
|
|
std::set<Expression> maximalExpressions;
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32> maximalValues;
|
2007-06-19 11:31:41 +08:00
|
|
|
|
|
|
|
uint32_t nextValueNumber;
|
|
|
|
|
|
|
|
Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
|
|
|
|
Expression::ExpressionOpcode getOpcode(CmpInst* C);
|
2007-06-21 08:19:05 +08:00
|
|
|
Expression create_expression(BinaryOperator* BO);
|
|
|
|
Expression create_expression(CmpInst* C);
|
2007-06-19 11:31:41 +08:00
|
|
|
public:
|
|
|
|
ValueTable() { nextValueNumber = 1; }
|
|
|
|
uint32_t lookup_or_add(Value* V);
|
|
|
|
uint32_t lookup(Value* V);
|
|
|
|
void add(Value* V, uint32_t num);
|
|
|
|
void clear();
|
|
|
|
std::set<Expression>& getMaximalExpressions() {
|
|
|
|
return maximalExpressions;
|
|
|
|
|
|
|
|
}
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& getMaximalValues() { return maximalValues; }
|
2007-06-19 13:37:32 +08:00
|
|
|
void erase(Value* v);
|
2007-06-22 08:20:30 +08:00
|
|
|
unsigned size();
|
2007-06-19 11:31:41 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Internal Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-19 11:31:41 +08:00
|
|
|
ValueTable::Expression::ExpressionOpcode
|
2007-06-21 08:19:05 +08:00
|
|
|
ValueTable::getOpcode(BinaryOperator* BO) {
|
2007-06-19 11:31:41 +08: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTable::Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
|
|
|
|
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-10 02:35:31 +08:00
|
|
|
}
|
|
|
|
}
|
2007-06-19 11:31:41 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
ValueTable::Expression ValueTable::create_expression(BinaryOperator* BO) {
|
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.leftVN = lookup_or_add(BO->getOperand(0));
|
|
|
|
e.rightVN = lookup_or_add(BO->getOperand(1));
|
|
|
|
e.opcode = getOpcode(BO);
|
|
|
|
|
|
|
|
maximalExpressions.insert(e);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTable::Expression ValueTable::create_expression(CmpInst* C) {
|
|
|
|
Expression e;
|
|
|
|
|
|
|
|
e.leftVN = lookup_or_add(C->getOperand(0));
|
|
|
|
e.rightVN = lookup_or_add(C->getOperand(1));
|
|
|
|
e.opcode = getOpcode(C);
|
|
|
|
|
|
|
|
maximalExpressions.insert(e);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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 11:31:41 +08:00
|
|
|
uint32_t ValueTable::lookup_or_add(Value* V) {
|
|
|
|
maximalValues.insert(V);
|
|
|
|
|
2007-06-20 07:23:54 +08:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
return VI->second;
|
2007-06-10 02:35:31 +08:00
|
|
|
|
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
|
|
|
|
Expression e = create_expression(BO);
|
|
|
|
|
|
|
|
std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
|
|
|
|
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 {
|
|
|
|
valueNumbering.insert(std::make_pair(V, nextValueNumber));
|
|
|
|
return nextValueNumber++;
|
2007-06-03 13:55:58 +08:00
|
|
|
}
|
2007-06-19 11:31:41 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// lookup - Returns the value number of the specified value. Fails if
|
|
|
|
/// the value has not yet been numbered.
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t ValueTable::lookup(Value* V) {
|
2007-06-20 07:23:54 +08:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
return VI->second;
|
|
|
|
else
|
|
|
|
assert(0 && "Value not numbered?");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// add - Add the specified value with the given value number, removing
|
|
|
|
/// its old number, if any
|
2007-06-19 11:31:41 +08:00
|
|
|
void ValueTable::add(Value* V, uint32_t num) {
|
2007-06-20 07:23:54 +08:00
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
valueNumbering.erase(VI);
|
|
|
|
valueNumbering.insert(std::make_pair(V, num));
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// clear - Remove all entries from the ValueTable and the maximal sets
|
2007-06-19 11:31:41 +08:00
|
|
|
void ValueTable::clear() {
|
|
|
|
valueNumbering.clear();
|
|
|
|
expressionNumbering.clear();
|
2007-06-19 12:32:55 +08:00
|
|
|
maximalExpressions.clear();
|
|
|
|
maximalValues.clear();
|
2007-06-19 11:31:41 +08:00
|
|
|
nextValueNumber = 1;
|
|
|
|
}
|
2007-06-03 13:55:58 +08:00
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// erase - Remove a value from the value numbering and maximal sets
|
2007-06-19 13:37:32 +08:00
|
|
|
void ValueTable::erase(Value* V) {
|
|
|
|
maximalValues.erase(V);
|
|
|
|
valueNumbering.erase(V);
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V))
|
|
|
|
maximalExpressions.erase(create_expression(BO));
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(V))
|
|
|
|
maximalExpressions.erase(create_expression(C));
|
|
|
|
}
|
|
|
|
|
2007-06-22 08:20:30 +08:00
|
|
|
/// size - Return the number of assigned value numbers
|
|
|
|
unsigned ValueTable::size() {
|
|
|
|
// NOTE: zero is never assigned
|
|
|
|
return nextValueNumber;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GVNPRE Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-06-19 12:32:55 +08:00
|
|
|
GVNPRE() : FunctionPass((intptr_t)&ID) { }
|
2007-05-30 05:53:49 +08:00
|
|
|
|
|
|
|
private:
|
2007-06-08 09:03:01 +08:00
|
|
|
ValueTable VN;
|
2007-06-12 08:50:47 +08:00
|
|
|
std::vector<Instruction*> createdExpressions;
|
2007-05-31 08:42:15 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
std::map<BasicBlock*, SmallPtrSet<Value*, 32> > availableOut;
|
|
|
|
std::map<BasicBlock*, SmallPtrSet<Value*, 32> > anticipatedIn;
|
2007-06-13 06:43:57 +08:00
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
// This transformation requires dominator postdominator info
|
2007-05-30 05:53:49 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<PostDominatorTree>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper fuctions
|
|
|
|
// FIXME: eliminate or document these better
|
2007-06-22 01:57:53 +08:00
|
|
|
void dump(const SmallPtrSet<Value*, 32>& s) const;
|
|
|
|
void clean(SmallPtrSet<Value*, 32>& set);
|
|
|
|
Value* find_leader(SmallPtrSet<Value*, 32>& vals,
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t v);
|
2007-06-13 06:43:57 +08:00
|
|
|
Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
|
2007-06-22 01:57:53 +08:00
|
|
|
void phi_translate_set(SmallPtrSet<Value*, 32>& anticIn, BasicBlock* pred,
|
2007-06-22 08:20:30 +08:00
|
|
|
BasicBlock* succ, SmallPtrSet<Value*, 32>& out) ;
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
void topo_sort(SmallPtrSet<Value*, 32>& set,
|
2007-06-03 13:55:58 +08:00
|
|
|
std::vector<Value*>& vec);
|
2007-06-01 06:44:11 +08:00
|
|
|
|
2007-06-13 00:57:50 +08:00
|
|
|
void cleanup();
|
2007-06-21 06:10:02 +08:00
|
|
|
bool elimination();
|
2007-06-18 12:42:29 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
void val_insert(SmallPtrSet<Value*, 32>& s, Value* v);
|
|
|
|
void val_replace(SmallPtrSet<Value*, 32>& s, Value* v);
|
2007-06-18 12:42:29 +08:00
|
|
|
bool dependsOnInvoke(Value* V);
|
2007-06-21 06:10:02 +08:00
|
|
|
void buildsets_availout(BasicBlock::iterator I,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& currAvail,
|
|
|
|
SmallPtrSet<PHINode*, 32>& currPhis,
|
|
|
|
SmallPtrSet<Value*, 32>& currExps,
|
|
|
|
SmallPtrSet<Value*, 32>& currTemps);
|
2007-06-22 08:20:30 +08:00
|
|
|
bool buildsets_anticout(BasicBlock* BB,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& anticOut,
|
2007-06-21 06:10:02 +08:00
|
|
|
std::set<BasicBlock*>& visited);
|
2007-06-22 08:20:30 +08:00
|
|
|
unsigned buildsets_anticin(BasicBlock* BB,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& anticOut,
|
|
|
|
SmallPtrSet<Value*, 32>& currExps,
|
|
|
|
SmallPtrSet<Value*, 32>& currTemps,
|
2007-06-21 06:10:02 +08:00
|
|
|
std::set<BasicBlock*>& visited);
|
|
|
|
unsigned buildsets(Function& F);
|
|
|
|
|
|
|
|
void insertion_pre(Value* e, BasicBlock* BB,
|
|
|
|
std::map<BasicBlock*, Value*>& avail,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& new_set);
|
2007-06-21 06:10:02 +08:00
|
|
|
unsigned insertion_mergepoint(std::vector<Value*>& workList,
|
2007-06-22 08:20:30 +08:00
|
|
|
df_iterator<DomTreeNode*>& D,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& new_set);
|
2007-06-21 06:10:02 +08:00
|
|
|
bool insertion(Function& F);
|
2007-05-30 05:53:49 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
char GVNPRE::ID = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
// createGVNPREPass - The public interface to this file...
|
2007-05-30 05:53:49 +08:00
|
|
|
FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
|
|
|
|
|
|
|
|
RegisterPass<GVNPRE> X("gvnpre",
|
|
|
|
"Global Value Numbering/Partial Redundancy Elimination");
|
|
|
|
|
|
|
|
|
2007-06-09 06:02:36 +08:00
|
|
|
STATISTIC(NumInsertedVals, "Number of values inserted");
|
|
|
|
STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
|
|
|
|
STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
|
|
|
|
|
2007-06-21 08:19:05 +08: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-06-22 01:57:53 +08:00
|
|
|
Value* GVNPRE::find_leader(SmallPtrSet<Value*, 32>& vals, uint32_t v) {
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = vals.begin(), E = vals.end();
|
2007-06-19 11:31:41 +08:00
|
|
|
I != E; ++I)
|
|
|
|
if (v == VN.lookup(*I))
|
|
|
|
return *I;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-06-03 13:55:58 +08:00
|
|
|
|
2007-06-21 08:19:05 +08: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-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::val_insert(SmallPtrSet<Value*, 32>& s, Value* v) {
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t num = VN.lookup(v);
|
|
|
|
Value* leader = find_leader(s, num);
|
|
|
|
if (leader == 0)
|
|
|
|
s.insert(v);
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// val_replace - Insert a value into a set, replacing any values already in
|
|
|
|
/// the set that have the same value number
|
2007-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::val_replace(SmallPtrSet<Value*, 32>& s, Value* v) {
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t num = VN.lookup(v);
|
|
|
|
Value* leader = find_leader(s, num);
|
|
|
|
while (leader != 0) {
|
|
|
|
s.erase(leader);
|
|
|
|
leader = find_leader(s, num);
|
2007-06-08 09:03:01 +08:00
|
|
|
}
|
2007-06-19 11:31:41 +08:00
|
|
|
s.insert(v);
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-13 06:43:57 +08:00
|
|
|
Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
|
2007-06-05 02:05:26 +08:00
|
|
|
if (V == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* newOp1 = 0;
|
|
|
|
if (isa<Instruction>(BO->getOperand(0)))
|
|
|
|
newOp1 = phi_translate(find_leader(anticipatedIn[succ],
|
|
|
|
VN.lookup(BO->getOperand(0))),
|
|
|
|
pred, succ);
|
|
|
|
else
|
|
|
|
newOp1 = BO->getOperand(0);
|
|
|
|
|
2007-06-05 02:05:26 +08:00
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* newOp2 = 0;
|
|
|
|
if (isa<Instruction>(BO->getOperand(1)))
|
|
|
|
newOp2 = phi_translate(find_leader(anticipatedIn[succ],
|
|
|
|
VN.lookup(BO->getOperand(1))),
|
|
|
|
pred, succ);
|
|
|
|
else
|
|
|
|
newOp2 = BO->getOperand(1);
|
|
|
|
|
2007-06-05 02:05:26 +08:00
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != BO->getOperand(0) || newOp2 != BO->getOperand(1)) {
|
2007-06-08 09:03:01 +08:00
|
|
|
Instruction* newVal = BinaryOperator::create(BO->getOpcode(),
|
2007-06-05 02:05:26 +08:00
|
|
|
newOp1, newOp2,
|
2007-06-19 13:37:32 +08:00
|
|
|
BO->getName()+".expr");
|
2007-06-09 04:44:02 +08:00
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
2007-06-13 06:43:57 +08:00
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
2007-06-13 06:43:57 +08:00
|
|
|
if (leader == 0) {
|
2007-06-12 08:50:47 +08:00
|
|
|
createdExpressions.push_back(newVal);
|
2007-06-05 02:05:26 +08:00
|
|
|
return newVal;
|
2007-06-06 06:11:49 +08:00
|
|
|
} else {
|
2007-06-19 13:37:32 +08:00
|
|
|
VN.erase(newVal);
|
2007-06-06 06:11:49 +08:00
|
|
|
delete newVal;
|
2007-06-13 06:43:57 +08:00
|
|
|
return leader;
|
2007-06-06 06:11:49 +08:00
|
|
|
}
|
2007-06-05 02:05:26 +08:00
|
|
|
}
|
|
|
|
} else if (PHINode* P = dyn_cast<PHINode>(V)) {
|
2007-06-12 08:50:47 +08:00
|
|
|
if (P->getParent() == succ)
|
2007-06-05 02:05:26 +08:00
|
|
|
return P->getIncomingValueForBlock(pred);
|
2007-06-10 02:35:31 +08:00
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* newOp1 = 0;
|
|
|
|
if (isa<Instruction>(C->getOperand(0)))
|
|
|
|
newOp1 = phi_translate(find_leader(anticipatedIn[succ],
|
|
|
|
VN.lookup(C->getOperand(0))),
|
|
|
|
pred, succ);
|
|
|
|
else
|
|
|
|
newOp1 = C->getOperand(0);
|
|
|
|
|
2007-06-10 02:35:31 +08:00
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* newOp2 = 0;
|
|
|
|
if (isa<Instruction>(C->getOperand(1)))
|
|
|
|
newOp2 = phi_translate(find_leader(anticipatedIn[succ],
|
|
|
|
VN.lookup(C->getOperand(1))),
|
|
|
|
pred, succ);
|
|
|
|
else
|
|
|
|
newOp2 = C->getOperand(1);
|
|
|
|
|
2007-06-10 02:35:31 +08:00
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != C->getOperand(0) || newOp2 != C->getOperand(1)) {
|
|
|
|
Instruction* newVal = CmpInst::create(C->getOpcode(),
|
|
|
|
C->getPredicate(),
|
|
|
|
newOp1, newOp2,
|
2007-06-19 13:37:32 +08:00
|
|
|
C->getName()+".expr");
|
2007-06-10 02:35:31 +08:00
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
uint32_t v = VN.lookup_or_add(newVal);
|
2007-06-13 06:43:57 +08:00
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* leader = find_leader(availableOut[pred], v);
|
2007-06-13 06:43:57 +08:00
|
|
|
if (leader == 0) {
|
2007-06-12 08:50:47 +08:00
|
|
|
createdExpressions.push_back(newVal);
|
2007-06-10 02:35:31 +08:00
|
|
|
return newVal;
|
|
|
|
} else {
|
2007-06-19 13:37:32 +08:00
|
|
|
VN.erase(newVal);
|
2007-06-10 02:35:31 +08:00
|
|
|
delete newVal;
|
2007-06-13 06:43:57 +08:00
|
|
|
return leader;
|
2007-06-10 02:35:31 +08:00
|
|
|
}
|
|
|
|
}
|
2007-06-05 02:05:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// phi_translate_set - Perform phi translation on every element of a set
|
2007-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::phi_translate_set(SmallPtrSet<Value*, 32>& anticIn,
|
2007-06-12 08:50:47 +08:00
|
|
|
BasicBlock* pred, BasicBlock* succ,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& out) {
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = anticIn.begin(),
|
2007-06-05 02:05:26 +08:00
|
|
|
E = anticIn.end(); I != E; ++I) {
|
2007-06-13 06:43:57 +08:00
|
|
|
Value* V = phi_translate(*I, pred, succ);
|
2007-06-05 02:05:26 +08:00
|
|
|
if (V != 0)
|
|
|
|
out.insert(V);
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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 12:42:29 +08:00
|
|
|
bool GVNPRE::dependsOnInvoke(Value* V) {
|
2007-06-20 07:07:16 +08: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 12:42:29 +08:00
|
|
|
return true;
|
2007-06-20 07:07:16 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
2007-06-16 08:26:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::clean(SmallPtrSet<Value*, 32>& set) {
|
2007-06-03 13:55:58 +08:00
|
|
|
std::vector<Value*> worklist;
|
2007-06-22 08:43:22 +08:00
|
|
|
worklist.reserve(set.size());
|
2007-06-08 09:03:01 +08:00
|
|
|
topo_sort(set, worklist);
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-08 09:03:01 +08:00
|
|
|
for (unsigned i = 0; i < worklist.size(); ++i) {
|
|
|
|
Value* v = worklist[i];
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-03 13:55:58 +08:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(v)) {
|
2007-06-08 09:03:01 +08:00
|
|
|
bool lhsValid = !isa<Instruction>(BO->getOperand(0));
|
|
|
|
if (!lhsValid)
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-08 09:03:01 +08:00
|
|
|
I != E; ++I)
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(*I) == VN.lookup(BO->getOperand(0))) {
|
2007-06-08 09:03:01 +08:00
|
|
|
lhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-18 12:30:44 +08:00
|
|
|
if (lhsValid)
|
|
|
|
lhsValid = !dependsOnInvoke(BO->getOperand(0));
|
2007-06-03 13:55:58 +08:00
|
|
|
|
2007-06-08 09:03:01 +08:00
|
|
|
bool rhsValid = !isa<Instruction>(BO->getOperand(1));
|
|
|
|
if (!rhsValid)
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-18 12:31:21 +08:00
|
|
|
I != E; ++I)
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(*I) == VN.lookup(BO->getOperand(1))) {
|
2007-06-18 12:31:21 +08:00
|
|
|
rhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-18 12:30:44 +08:00
|
|
|
if (rhsValid)
|
|
|
|
rhsValid = !dependsOnInvoke(BO->getOperand(1));
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-03 13:55:58 +08:00
|
|
|
if (!lhsValid || !rhsValid)
|
|
|
|
set.erase(BO);
|
2007-06-10 02:35:31 +08:00
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(v)) {
|
|
|
|
bool lhsValid = !isa<Instruction>(C->getOperand(0));
|
|
|
|
if (!lhsValid)
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-10 02:35:31 +08:00
|
|
|
I != E; ++I)
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(*I) == VN.lookup(C->getOperand(0))) {
|
2007-06-10 02:35:31 +08:00
|
|
|
lhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-20 07:07:16 +08:00
|
|
|
if (lhsValid)
|
|
|
|
lhsValid = !dependsOnInvoke(C->getOperand(0));
|
2007-06-16 08:26:54 +08:00
|
|
|
|
2007-06-10 02:35:31 +08:00
|
|
|
bool rhsValid = !isa<Instruction>(C->getOperand(1));
|
|
|
|
if (!rhsValid)
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-10 02:35:31 +08:00
|
|
|
I != E; ++I)
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(*I) == VN.lookup(C->getOperand(1))) {
|
2007-06-10 02:35:31 +08:00
|
|
|
rhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-20 07:07:16 +08:00
|
|
|
if (rhsValid)
|
|
|
|
rhsValid = !dependsOnInvoke(C->getOperand(1));
|
2007-06-10 02:35:31 +08:00
|
|
|
|
|
|
|
if (!lhsValid || !rhsValid)
|
|
|
|
set.erase(C);
|
2007-06-03 13:55:58 +08:00
|
|
|
}
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// topo_sort - Given a set of values, sort them by topological
|
|
|
|
/// order into the provided vector.
|
2007-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::topo_sort(SmallPtrSet<Value*, 32>& set, std::vector<Value*>& vec) {
|
|
|
|
SmallPtrSet<Value*, 32> toErase;
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-01 06:44:11 +08:00
|
|
|
I != E; ++I) {
|
2007-06-03 13:55:58 +08:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(*I))
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator SI = set.begin(); SI != E; ++SI) {
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(BO->getOperand(0)) == VN.lookup(*SI) ||
|
|
|
|
VN.lookup(BO->getOperand(1)) == VN.lookup(*SI)) {
|
2007-06-08 09:03:01 +08:00
|
|
|
toErase.insert(*SI);
|
2007-06-03 13:55:58 +08:00
|
|
|
}
|
2007-06-10 02:35:31 +08:00
|
|
|
}
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(*I))
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator SI = set.begin(); SI != E; ++SI) {
|
2007-06-19 11:31:41 +08:00
|
|
|
if (VN.lookup(C->getOperand(0)) == VN.lookup(*SI) ||
|
|
|
|
VN.lookup(C->getOperand(1)) == VN.lookup(*SI)) {
|
2007-06-10 02:35:31 +08:00
|
|
|
toErase.insert(*SI);
|
|
|
|
}
|
|
|
|
}
|
2007-06-01 06:44:11 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 13:55:58 +08:00
|
|
|
std::vector<Value*> Q;
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = set.begin(), E = set.end();
|
2007-06-08 09:03:01 +08:00
|
|
|
I != E; ++I) {
|
2007-06-22 01:57:53 +08:00
|
|
|
if (toErase.count(*I) == 0)
|
2007-06-08 09:03:01 +08:00
|
|
|
Q.push_back(*I);
|
|
|
|
}
|
2007-06-01 06:44:11 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32> visited;
|
2007-06-01 06:44:11 +08:00
|
|
|
while (!Q.empty()) {
|
2007-06-03 13:55:58 +08:00
|
|
|
Value* e = Q.back();
|
|
|
|
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(e)) {
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* l = find_leader(set, VN.lookup(BO->getOperand(0)));
|
|
|
|
Value* r = find_leader(set, VN.lookup(BO->getOperand(1)));
|
2007-06-03 13:55:58 +08:00
|
|
|
|
2007-06-10 02:35:31 +08:00
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
2007-06-22 01:57:53 +08:00
|
|
|
visited.count(l) == 0)
|
2007-06-10 02:35:31 +08:00
|
|
|
Q.push_back(l);
|
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
2007-06-22 01:57:53 +08:00
|
|
|
visited.count(r) == 0)
|
2007-06-10 02:35:31 +08:00
|
|
|
Q.push_back(r);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(e)) {
|
2007-06-19 11:31:41 +08:00
|
|
|
Value* l = find_leader(set, VN.lookup(C->getOperand(0)));
|
|
|
|
Value* r = find_leader(set, VN.lookup(C->getOperand(1)));
|
2007-06-10 02:35:31 +08:00
|
|
|
|
2007-06-06 07:46:12 +08:00
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
2007-06-22 01:57:53 +08:00
|
|
|
visited.count(l) == 0)
|
2007-06-03 13:55:58 +08:00
|
|
|
Q.push_back(l);
|
2007-06-06 07:46:12 +08:00
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
2007-06-22 01:57:53 +08:00
|
|
|
visited.count(r) == 0)
|
2007-06-03 13:55:58 +08:00
|
|
|
Q.push_back(r);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
} else {
|
2007-06-01 06:44:11 +08:00
|
|
|
visited.insert(e);
|
|
|
|
vec.push_back(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// dump - Dump a set of values to standard error
|
2007-06-22 01:57:53 +08:00
|
|
|
void GVNPRE::dump(const SmallPtrSet<Value*, 32>& s) const {
|
2007-05-30 07:15:21 +08:00
|
|
|
DOUT << "{ ";
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = s.begin(), E = s.end();
|
2007-06-04 06:02:14 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
DEBUG((*I)->dump());
|
|
|
|
}
|
|
|
|
DOUT << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-21 06:10:02 +08:00
|
|
|
bool GVNPRE::elimination() {
|
2007-06-13 00:57:50 +08:00
|
|
|
DOUT << "\n\nPhase 3: Elimination\n\n";
|
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
bool changed_function = false;
|
|
|
|
|
2007-06-13 00:57:50 +08:00
|
|
|
std::vector<std::pair<Instruction*, Value*> > replace;
|
|
|
|
std::vector<Instruction*> erase;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
DOUT << "Block: " << BB->getName() << "\n";
|
2007-06-20 08:43:33 +08:00
|
|
|
dump(availableOut[BB]);
|
2007-06-13 00:57:50 +08:00
|
|
|
DOUT << "\n\n";
|
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI)) {
|
2007-06-19 11:31:41 +08:00
|
|
|
Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
|
2007-06-13 00:57:50 +08:00
|
|
|
|
|
|
|
if (leader != 0)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!replace.empty()) {
|
|
|
|
std::pair<Instruction*, Value*> rep = replace.back();
|
|
|
|
replace.pop_back();
|
|
|
|
rep.first->replaceAllUsesWith(rep.second);
|
2007-06-21 02:30:20 +08:00
|
|
|
changed_function = true;
|
2007-06-13 00:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
|
|
|
|
I != E; ++I)
|
|
|
|
(*I)->eraseFromParent();
|
2007-06-21 06:10:02 +08:00
|
|
|
|
|
|
|
return changed_function;
|
2007-06-13 00:57:50 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// cleanup - Delete any extraneous values that were created to represent
|
|
|
|
/// expressions without leaders.
|
2007-06-13 00:57:50 +08:00
|
|
|
void GVNPRE::cleanup() {
|
|
|
|
while (!createdExpressions.empty()) {
|
|
|
|
Instruction* I = createdExpressions.back();
|
|
|
|
createdExpressions.pop_back();
|
|
|
|
|
|
|
|
delete I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// buildsets_availout - When calculating availability, handle an instruction
|
|
|
|
/// by inserting it into the appropriate sets
|
2007-06-21 06:10:02 +08:00
|
|
|
void GVNPRE::buildsets_availout(BasicBlock::iterator I,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& currAvail,
|
|
|
|
SmallPtrSet<PHINode*, 32>& currPhis,
|
|
|
|
SmallPtrSet<Value*, 32>& currExps,
|
|
|
|
SmallPtrSet<Value*, 32>& currTemps) {
|
2007-06-21 06:10:02 +08:00
|
|
|
// Handle PHI nodes...
|
|
|
|
if (PHINode* p = dyn_cast<PHINode>(I)) {
|
|
|
|
VN.lookup_or_add(p);
|
|
|
|
currPhis.insert(p);
|
|
|
|
|
|
|
|
// Handle binary ops...
|
|
|
|
} else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(I)) {
|
|
|
|
Value* leftValue = BO->getOperand(0);
|
|
|
|
Value* rightValue = BO->getOperand(1);
|
|
|
|
|
|
|
|
VN.lookup_or_add(BO);
|
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
|
|
|
val_insert(currExps, leftValue);
|
|
|
|
if (isa<Instruction>(rightValue))
|
|
|
|
val_insert(currExps, rightValue);
|
|
|
|
val_insert(currExps, BO);
|
|
|
|
|
|
|
|
// Handle cmp ops...
|
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(I)) {
|
|
|
|
Value* leftValue = C->getOperand(0);
|
|
|
|
Value* rightValue = C->getOperand(1);
|
|
|
|
|
|
|
|
VN.lookup_or_add(C);
|
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
|
|
|
val_insert(currExps, leftValue);
|
|
|
|
if (isa<Instruction>(rightValue))
|
|
|
|
val_insert(currExps, rightValue);
|
|
|
|
val_insert(currExps, C);
|
|
|
|
|
|
|
|
// Handle unsupported ops
|
|
|
|
} else if (!I->isTerminator()){
|
|
|
|
VN.lookup_or_add(I);
|
|
|
|
currTemps.insert(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!I->isTerminator())
|
|
|
|
val_insert(currAvail, I);
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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 08:20:30 +08:00
|
|
|
bool GVNPRE::buildsets_anticout(BasicBlock* BB,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& anticOut,
|
2007-06-21 06:10:02 +08:00
|
|
|
std::set<BasicBlock*>& visited) {
|
|
|
|
if (BB->getTerminator()->getNumSuccessors() == 1) {
|
2007-06-22 08:20:30 +08:00
|
|
|
if (visited.count(BB->getTerminator()->getSuccessor(0)) == 0)
|
|
|
|
return true;
|
2007-06-21 06:10:02 +08:00
|
|
|
else
|
|
|
|
phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
|
|
|
|
BB, BB->getTerminator()->getSuccessor(0), anticOut);
|
|
|
|
} else if (BB->getTerminator()->getNumSuccessors() > 1) {
|
|
|
|
BasicBlock* first = BB->getTerminator()->getSuccessor(0);
|
|
|
|
anticOut.insert(anticipatedIn[first].begin(), anticipatedIn[first].end());
|
|
|
|
|
|
|
|
for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
|
|
|
|
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& succAnticIn = anticipatedIn[currSucc];
|
2007-06-21 06:10:02 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
std::vector<Value*> temp;
|
|
|
|
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = anticOut.begin(),
|
|
|
|
E = anticOut.end(); I != E; ++I)
|
|
|
|
if (succAnticIn.count(*I) == 0)
|
|
|
|
temp.push_back(*I);
|
|
|
|
|
|
|
|
for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
|
|
|
|
I != E; ++I)
|
|
|
|
anticOut.erase(*I);
|
2007-06-21 06:10:02 +08:00
|
|
|
}
|
|
|
|
}
|
2007-06-22 08:20:30 +08:00
|
|
|
|
|
|
|
return false;
|
2007-06-21 06:10:02 +08:00
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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 08:20:30 +08:00
|
|
|
unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& anticOut,
|
|
|
|
SmallPtrSet<Value*, 32>& currExps,
|
|
|
|
SmallPtrSet<Value*, 32>& currTemps,
|
2007-06-21 06:10:02 +08:00
|
|
|
std::set<BasicBlock*>& visited) {
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& anticIn = anticipatedIn[BB];
|
|
|
|
SmallPtrSet<Value*, 32> old (anticIn.begin(), anticIn.end());
|
2007-06-21 06:10:02 +08:00
|
|
|
|
2007-06-22 08:20:30 +08:00
|
|
|
bool defer = buildsets_anticout(BB, anticOut, visited);
|
|
|
|
if (defer)
|
|
|
|
return 0;
|
2007-06-21 06:10:02 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32> S;
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = anticOut.begin(),
|
|
|
|
E = anticOut.end(); I != E; ++I)
|
|
|
|
if (currTemps.count(*I) == 0)
|
|
|
|
S.insert(*I);
|
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
anticIn.clear();
|
2007-06-22 01:57:53 +08:00
|
|
|
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = currExps.begin(),
|
|
|
|
E = currExps.end(); I != E; ++I)
|
|
|
|
if (currTemps.count(*I) == 0)
|
|
|
|
anticIn.insert(*I);
|
2007-06-22 08:20:30 +08:00
|
|
|
|
|
|
|
BitVector numbers(VN.size());
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = anticIn.begin(),
|
|
|
|
E = anticIn.end(); I != E; ++I)
|
|
|
|
numbers.set(VN.lookup(*I)-1);
|
2007-06-22 01:57:53 +08:00
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = S.begin(), E = S.end();
|
2007-06-21 06:10:02 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
// For non-opaque values, we should already have a value numbering.
|
|
|
|
// However, for opaques, such as constants within PHI nodes, it is
|
|
|
|
// possible that they have not yet received a number. Make sure they do
|
|
|
|
// so now.
|
2007-06-21 09:59:05 +08:00
|
|
|
if (!isa<BinaryOperator>(*I) && !isa<CmpInst>(*I))
|
|
|
|
VN.lookup_or_add(*I);
|
2007-06-22 08:20:30 +08:00
|
|
|
if (!numbers.test(VN.lookup(*I)-1))
|
|
|
|
anticIn.insert(*I);
|
2007-06-21 06:10:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clean(anticIn);
|
|
|
|
anticOut.clear();
|
2007-06-21 02:30:20 +08:00
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
if (old.size() != anticIn.size())
|
2007-06-22 08:20:30 +08:00
|
|
|
return 2;
|
2007-06-21 06:10:02 +08:00
|
|
|
else
|
2007-06-22 08:20:30 +08:00
|
|
|
return 1;
|
2007-06-21 06:10:02 +08:00
|
|
|
}
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
/// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
|
|
|
|
/// and the ANTIC_IN sets.
|
2007-06-21 06:10:02 +08:00
|
|
|
unsigned GVNPRE::buildsets(Function& F) {
|
2007-06-22 01:57:53 +08:00
|
|
|
std::map<BasicBlock*, SmallPtrSet<Value*, 32> > generatedExpressions;
|
|
|
|
std::map<BasicBlock*, SmallPtrSet<PHINode*, 32> > generatedPhis;
|
|
|
|
std::map<BasicBlock*, SmallPtrSet<Value*, 32> > generatedTemporaries;
|
2007-06-21 06:10:02 +08:00
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
|
2007-06-06 09:27:49 +08:00
|
|
|
// Phase 1, Part 1: calculate AVAIL_OUT
|
2007-05-30 05:53:49 +08:00
|
|
|
|
|
|
|
// Top-down walk of the dominator tree
|
2007-06-04 08:32:22 +08:00
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
2007-05-30 05:53:49 +08:00
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
|
|
|
|
// Get the sets to update for this block
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& currExps = generatedExpressions[DI->getBlock()];
|
|
|
|
SmallPtrSet<PHINode*, 32>& currPhis = generatedPhis[DI->getBlock()];
|
|
|
|
SmallPtrSet<Value*, 32>& currTemps = generatedTemporaries[DI->getBlock()];
|
|
|
|
SmallPtrSet<Value*, 32>& currAvail = availableOut[DI->getBlock()];
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-19 11:31:41 +08:00
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
|
|
|
// A block inherits AVAIL_OUT from its dominator
|
|
|
|
if (DI->getIDom() != 0)
|
|
|
|
currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(),
|
|
|
|
availableOut[DI->getIDom()->getBlock()].end());
|
|
|
|
|
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
2007-06-21 06:10:02 +08:00
|
|
|
BI != BE; ++BI)
|
|
|
|
buildsets_availout(BI, currAvail, currPhis, currExps, currTemps);
|
2007-06-19 11:31:41 +08:00
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
|
|
|
|
2007-06-13 00:57:50 +08:00
|
|
|
// If function has no exit blocks, only perform GVN
|
2007-05-30 05:53:49 +08:00
|
|
|
PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
|
2007-06-13 00:57:50 +08:00
|
|
|
if (PDT[&F.getEntryBlock()] == 0) {
|
2007-06-21 06:10:02 +08:00
|
|
|
bool changed_function = elimination();
|
2007-06-13 00:57:50 +08:00
|
|
|
cleanup();
|
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
if (changed_function)
|
|
|
|
return 2; // Bailed early, made changes
|
|
|
|
else
|
|
|
|
return 1; // Bailed early, no changes
|
2007-06-13 00:57:50 +08:00
|
|
|
}
|
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-06-06 09:27:49 +08:00
|
|
|
// Phase 1, Part 2: calculate ANTIC_IN
|
2007-05-30 05:53:49 +08:00
|
|
|
|
2007-05-30 07:26:30 +08:00
|
|
|
std::set<BasicBlock*> visited;
|
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
bool changed = true;
|
|
|
|
unsigned iterations = 0;
|
|
|
|
while (changed) {
|
|
|
|
changed = false;
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32> anticOut;
|
2007-05-30 05:53:49 +08:00
|
|
|
|
|
|
|
// Top-down walk of the postdominator tree
|
2007-06-04 08:32:22 +08:00
|
|
|
for (df_iterator<DomTreeNode*> PDI =
|
2007-06-08 09:03:01 +08:00
|
|
|
df_begin(PDT.getRootNode()), E = df_end(PDT.getRootNode());
|
2007-05-30 05:53:49 +08:00
|
|
|
PDI != E; ++PDI) {
|
|
|
|
BasicBlock* BB = PDI->getBlock();
|
2007-06-12 00:25:17 +08:00
|
|
|
if (BB == 0)
|
|
|
|
continue;
|
|
|
|
|
2007-05-30 07:26:30 +08:00
|
|
|
|
2007-06-22 08:20:30 +08:00
|
|
|
|
|
|
|
unsigned ret = buildsets_anticin(BB, anticOut, generatedTemporaries[BB],
|
2007-06-21 06:10:02 +08:00
|
|
|
generatedExpressions[BB], visited);
|
2007-06-22 08:20:30 +08:00
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
visited.insert(BB);
|
|
|
|
changed |= (ret == 2);
|
|
|
|
}
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|
2007-06-05 02:05:26 +08:00
|
|
|
|
2007-05-30 05:53:49 +08:00
|
|
|
iterations++;
|
|
|
|
}
|
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
return 0; // No bail, no changes
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-21 06:10:02 +08:00
|
|
|
void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
|
|
|
|
std::map<BasicBlock*, Value*>& avail,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& new_set) {
|
2007-06-21 06:10:02 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
|
|
|
|
Value* e2 = avail[*PI];
|
|
|
|
if (!find_leader(availableOut[*PI], VN.lookup(e2))) {
|
|
|
|
User* U = cast<User>(e2);
|
|
|
|
|
|
|
|
Value* s1 = 0;
|
|
|
|
if (isa<BinaryOperator>(U->getOperand(0)) ||
|
|
|
|
isa<CmpInst>(U->getOperand(0)))
|
|
|
|
s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
|
|
|
|
else
|
|
|
|
s1 = U->getOperand(0);
|
|
|
|
|
|
|
|
Value* s2 = 0;
|
|
|
|
if (isa<BinaryOperator>(U->getOperand(1)) ||
|
|
|
|
isa<CmpInst>(U->getOperand(1)))
|
|
|
|
s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
|
|
|
|
else
|
|
|
|
s2 = U->getOperand(1);
|
|
|
|
|
|
|
|
Value* newVal = 0;
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
|
|
|
|
newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
|
|
|
|
BO->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(U))
|
|
|
|
newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
|
|
|
|
C->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
|
|
|
|
VN.add(newVal, VN.lookup(U));
|
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& predAvail = availableOut[*PI];
|
2007-06-21 06:10:02 +08:00
|
|
|
val_replace(predAvail, newVal);
|
2007-06-21 08:19:05 +08:00
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
|
|
|
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)
|
|
|
|
p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
|
|
|
|
|
|
|
|
p->addIncoming(avail[*PI], *PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
VN.add(p, VN.lookup(e));
|
|
|
|
val_replace(availableOut[BB], p);
|
|
|
|
new_set.insert(p);
|
|
|
|
|
|
|
|
++NumInsertedPhis;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-06-21 06:10:02 +08:00
|
|
|
unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
|
2007-06-22 08:20:30 +08:00
|
|
|
df_iterator<DomTreeNode*>& D,
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& new_set) {
|
2007-06-21 06:10:02 +08: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];
|
|
|
|
|
|
|
|
if (isa<BinaryOperator>(e) || isa<CmpInst>(e)) {
|
|
|
|
if (find_leader(availableOut[D->getIDom()->getBlock()],
|
|
|
|
VN.lookup(e)) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::map<BasicBlock*, Value*> avail;
|
|
|
|
bool by_some = false;
|
|
|
|
int num_avail = 0;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, e2));
|
|
|
|
} else {
|
|
|
|
std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, e3));
|
|
|
|
|
|
|
|
by_some = true;
|
|
|
|
num_avail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (by_some && num_avail < std::distance(pred_begin(BB), pred_end(BB))) {
|
|
|
|
insertion_pre(e, BB, avail, new_set);
|
|
|
|
|
|
|
|
changed_function = true;
|
|
|
|
new_stuff = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned retval = 0;
|
|
|
|
if (changed_function)
|
|
|
|
retval += 1;
|
|
|
|
if (new_stuff)
|
|
|
|
retval += 2;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08: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-21 06:10:02 +08:00
|
|
|
bool GVNPRE::insertion(Function& F) {
|
|
|
|
bool changed_function = false;
|
|
|
|
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
2007-06-08 09:03:01 +08:00
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
std::map<BasicBlock*, SmallPtrSet<Value*, 32> > new_sets;
|
2007-06-08 09:03:01 +08: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-12 00:25:17 +08:00
|
|
|
if (BB == 0)
|
|
|
|
continue;
|
|
|
|
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& new_set = new_sets[BB];
|
|
|
|
SmallPtrSet<Value*, 32>& availOut = availableOut[BB];
|
|
|
|
SmallPtrSet<Value*, 32>& anticIn = anticipatedIn[BB];
|
2007-06-08 09:03:01 +08:00
|
|
|
|
2007-06-09 04:44:02 +08:00
|
|
|
new_set.clear();
|
|
|
|
|
2007-06-08 09:03:01 +08:00
|
|
|
// Replace leaders with leaders inherited from dominator
|
|
|
|
if (DI->getIDom() != 0) {
|
2007-06-22 01:57:53 +08:00
|
|
|
SmallPtrSet<Value*, 32>& dom_set = new_sets[DI->getIDom()->getBlock()];
|
|
|
|
for (SmallPtrSet<Value*, 32>::iterator I = dom_set.begin(),
|
2007-06-08 09:03:01 +08:00
|
|
|
E = dom_set.end(); I != E; ++I) {
|
|
|
|
new_set.insert(*I);
|
2007-06-19 11:31:41 +08:00
|
|
|
val_replace(availOut, *I);
|
2007-06-08 09:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is more than one predecessor...
|
|
|
|
if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
|
|
|
|
std::vector<Value*> workList;
|
2007-06-22 08:43:22 +08:00
|
|
|
workList.reserve(anticIn.size());
|
2007-06-08 09:03:01 +08:00
|
|
|
topo_sort(anticIn, workList);
|
|
|
|
|
|
|
|
DOUT << "Merge Block: " << BB->getName() << "\n";
|
|
|
|
DOUT << "ANTIC_IN: ";
|
2007-06-20 08:43:33 +08:00
|
|
|
dump(anticIn);
|
2007-06-08 09:03:01 +08:00
|
|
|
DOUT << "\n";
|
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
unsigned result = insertion_mergepoint(workList, DI, new_set);
|
|
|
|
if (result & 1)
|
|
|
|
changed_function = true;
|
|
|
|
if (result & 2)
|
|
|
|
new_stuff = true;
|
2007-06-08 09:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-06 09:27:49 +08:00
|
|
|
|
2007-06-21 06:10:02 +08:00
|
|
|
return changed_function;
|
|
|
|
}
|
|
|
|
|
2007-06-21 08:19:05 +08:00
|
|
|
// GVNPRE::runOnFunction - This is the main transformation entry point for a
|
|
|
|
// function.
|
|
|
|
//
|
2007-06-21 06:10:02 +08:00
|
|
|
bool GVNPRE::runOnFunction(Function &F) {
|
2007-06-21 08:19:05 +08:00
|
|
|
// Clean out global sets from any previous functions
|
2007-06-21 06:10:02 +08:00
|
|
|
VN.clear();
|
|
|
|
createdExpressions.clear();
|
|
|
|
availableOut.clear();
|
|
|
|
anticipatedIn.clear();
|
|
|
|
|
|
|
|
bool changed_function = false;
|
|
|
|
|
|
|
|
// Phase 1: BuildSets
|
2007-06-21 08:19:05 +08:00
|
|
|
// This phase calculates the AVAIL_OUT and ANTIC_IN sets
|
|
|
|
// NOTE: If full postdom information is no available, this will bail
|
|
|
|
// early, performing GVN but not PRE
|
2007-06-21 06:10:02 +08:00
|
|
|
unsigned bail = buildsets(F);
|
|
|
|
//If a bail occurred, terminate early
|
|
|
|
if (bail != 0)
|
|
|
|
return (bail == 2);
|
|
|
|
|
|
|
|
// Phase 2: Insert
|
2007-06-21 08:19:05 +08:00
|
|
|
// This phase inserts values to make partially redundant values
|
|
|
|
// fully redundant
|
2007-06-21 06:10:02 +08:00
|
|
|
changed_function |= insertion(F);
|
|
|
|
|
2007-06-06 09:27:49 +08:00
|
|
|
// Phase 3: Eliminate
|
2007-06-21 08:19:05 +08:00
|
|
|
// This phase performs trivial full redundancy elimination
|
2007-06-21 06:10:02 +08:00
|
|
|
changed_function |= elimination();
|
2007-06-09 04:44:02 +08:00
|
|
|
|
2007-06-08 09:03:01 +08:00
|
|
|
// Phase 4: Cleanup
|
2007-06-21 08:19:05 +08:00
|
|
|
// This phase cleans up values that were created solely
|
|
|
|
// as leaders for expressions
|
2007-06-13 00:57:50 +08:00
|
|
|
cleanup();
|
2007-06-08 09:03:01 +08:00
|
|
|
|
2007-06-21 02:30:20 +08:00
|
|
|
return changed_function;
|
2007-05-30 05:53:49 +08:00
|
|
|
}
|