mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-25 00:33:15 +00:00
There's no need to have an Expression class... Value works just as well! This simplifies a lot of code.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37401 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
c621ae7bba
commit
a724ac1a73
@ -36,6 +36,23 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
|
struct ExprLT {
|
||||||
|
bool operator()(Value* left, Value* right) {
|
||||||
|
if (!isa<BinaryOperator>(left) || !isa<BinaryOperator>(right))
|
||||||
|
return left < right;
|
||||||
|
|
||||||
|
BinaryOperator* BO1 = cast<BinaryOperator>(left);
|
||||||
|
BinaryOperator* BO2 = cast<BinaryOperator>(right);
|
||||||
|
|
||||||
|
if ((*this)(BO1->getOperand(0), BO2->getOperand(0)))
|
||||||
|
return true;
|
||||||
|
else if ((*this)(BO2->getOperand(0), BO1->getOperand(0)))
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return (*this)(BO1->getOperand(1), BO2->getOperand(1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
|
class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
|
||||||
@ -46,49 +63,7 @@ namespace {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t nextValueNumber;
|
uint32_t nextValueNumber;
|
||||||
|
typedef std::map<Value*, uint32_t, ExprLT> ValueTable;
|
||||||
struct Expression {
|
|
||||||
char opcode;
|
|
||||||
Value* value;
|
|
||||||
uint32_t lhs;
|
|
||||||
uint32_t rhs;
|
|
||||||
|
|
||||||
bool operator<(const Expression& other) const {
|
|
||||||
if (opcode < other.opcode)
|
|
||||||
return true;
|
|
||||||
else if (other.opcode < opcode)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (opcode == 0) {
|
|
||||||
return value < other.value;
|
|
||||||
} else {
|
|
||||||
if (lhs < other.lhs)
|
|
||||||
return true;
|
|
||||||
else if (other.lhs < lhs)
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return rhs < other.rhs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const Expression& other) const {
|
|
||||||
if (opcode != other.opcode)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (value != other.value)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (lhs != other.lhs)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (rhs != other.rhs)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::map<Expression, uint32_t> ValueTable;
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
||||||
AU.setPreservesCFG();
|
AU.setPreservesCFG();
|
||||||
@ -98,30 +73,27 @@ namespace {
|
|||||||
|
|
||||||
// Helper fuctions
|
// Helper fuctions
|
||||||
// FIXME: eliminate or document these better
|
// FIXME: eliminate or document these better
|
||||||
void dump(ValueTable& VN, std::set<Expression>& s);
|
void dump(ValueTable& VN, std::set<Value*, ExprLT>& s);
|
||||||
void clean(ValueTable VN, std::set<Expression>& set);
|
void clean(ValueTable VN, std::set<Value*, ExprLT>& set);
|
||||||
Expression add(ValueTable& VN, std::set<Expression>& MS, Instruction* V);
|
bool add(ValueTable& VN, std::set<Value*, ExprLT>& MS, Value* V);
|
||||||
ValueTable::iterator lookup(ValueTable& VN, Value* V);
|
ValueTable::iterator lookup(ValueTable& VN, Value* V);
|
||||||
Expression buildExpression(ValueTable& VN, Value* V);
|
Value* find_leader(ValueTable VN, std::set<Value*, ExprLT>& vals, uint32_t v);
|
||||||
std::set<Expression>::iterator find_leader(ValueTable VN,
|
void phi_translate(ValueTable& VN, std::set<Value*, ExprLT>& MS,
|
||||||
std::set<Expression>& vals,
|
std::set<Value*, ExprLT>& anticIn, BasicBlock* B,
|
||||||
uint32_t v);
|
std::set<Value*, ExprLT>& out);
|
||||||
void phi_translate(ValueTable& VN, std::set<Expression>& MS,
|
|
||||||
std::set<Expression>& anticIn, BasicBlock* B,
|
|
||||||
std::set<Expression>& out);
|
|
||||||
|
|
||||||
void topo_sort(ValueTable& VN, std::set<Expression>& set,
|
void topo_sort(ValueTable& VN, std::set<Value*, ExprLT>& set,
|
||||||
std::vector<Expression>& vec);
|
std::vector<Value*>& vec);
|
||||||
|
|
||||||
// For a given block, calculate the generated expressions, temporaries,
|
// For a given block, calculate the generated expressions, temporaries,
|
||||||
// and the AVAIL_OUT set
|
// and the AVAIL_OUT set
|
||||||
void CalculateAvailOut(ValueTable& VN, std::set<Expression>& MS,
|
void CalculateAvailOut(ValueTable& VN, std::set<Value*, ExprLT>& MS,
|
||||||
DominatorTree::Node* DI,
|
DominatorTree::Node* DI,
|
||||||
std::set<Expression>& currExps,
|
std::set<Value*, ExprLT>& currExps,
|
||||||
std::set<PHINode*>& currPhis,
|
std::set<PHINode*>& currPhis,
|
||||||
std::set<Expression>& currTemps,
|
std::set<Value*, ExprLT>& currTemps,
|
||||||
std::set<Expression>& currAvail,
|
std::set<Value*, ExprLT>& currAvail,
|
||||||
std::map<BasicBlock*, std::set<Expression> > availOut);
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -134,269 +106,173 @@ FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
|
|||||||
RegisterPass<GVNPRE> X("gvnpre",
|
RegisterPass<GVNPRE> X("gvnpre",
|
||||||
"Global Value Numbering/Partial Redundancy Elimination");
|
"Global Value Numbering/Partial Redundancy Elimination");
|
||||||
|
|
||||||
// Given a Value, build an Expression to represent it
|
|
||||||
GVNPRE::Expression GVNPRE::buildExpression(ValueTable& VN, Value* V) {
|
|
||||||
if (Instruction* I = dyn_cast<Instruction>(V)) {
|
|
||||||
Expression e;
|
|
||||||
|
|
||||||
switch (I->getOpcode()) {
|
|
||||||
case 7:
|
|
||||||
e.opcode = 1; // ADD
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
e.opcode = 2; // SUB
|
|
||||||
break;
|
|
||||||
case 9:
|
|
||||||
e.opcode = 3; // MUL
|
|
||||||
break;
|
|
||||||
case 10:
|
|
||||||
e.opcode = 4; // UDIV
|
|
||||||
break;
|
|
||||||
case 11:
|
|
||||||
e.opcode = 5; // SDIV
|
|
||||||
break;
|
|
||||||
case 12:
|
|
||||||
e.opcode = 6; // FDIV
|
|
||||||
break;
|
|
||||||
case 13:
|
|
||||||
e.opcode = 7; // UREM
|
|
||||||
break;
|
|
||||||
case 14:
|
|
||||||
e.opcode = 8; // SREM
|
|
||||||
break;
|
|
||||||
case 15:
|
|
||||||
e.opcode = 9; // FREM
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
e.opcode = 0; // OPAQUE
|
|
||||||
e.lhs = 0;
|
|
||||||
e.rhs = 0;
|
|
||||||
e.value = V;
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
e.value = 0;
|
|
||||||
|
|
||||||
ValueTable::iterator lhs = lookup(VN, I->getOperand(0));
|
|
||||||
if (lhs == VN.end()) {
|
|
||||||
Expression lhsExp = buildExpression(VN, I->getOperand(0));
|
|
||||||
VN.insert(std::make_pair(lhsExp, nextValueNumber));
|
|
||||||
e.lhs = nextValueNumber;
|
|
||||||
nextValueNumber++;
|
|
||||||
} else
|
|
||||||
e.lhs = lhs->second;
|
|
||||||
ValueTable::iterator rhs = lookup(VN, I->getOperand(1));
|
|
||||||
if (rhs == VN.end()) {
|
|
||||||
Expression rhsExp = buildExpression(VN, I->getOperand(1));
|
|
||||||
VN.insert(std::make_pair(rhsExp, nextValueNumber));
|
|
||||||
e.rhs = nextValueNumber;
|
|
||||||
nextValueNumber++;
|
|
||||||
} else
|
|
||||||
e.rhs = rhs->second;
|
|
||||||
|
|
||||||
return e;
|
|
||||||
} else {
|
|
||||||
Expression e;
|
|
||||||
e.opcode = 0;
|
|
||||||
e.value = V;
|
|
||||||
e.lhs = 0;
|
|
||||||
e.rhs = 0;
|
|
||||||
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GVNPRE::Expression GVNPRE::add(ValueTable& VN, std::set<Expression>& MS,
|
|
||||||
Instruction* V) {
|
bool GVNPRE::add(ValueTable& VN, std::set<Value*, ExprLT>& MS, Value* V) {
|
||||||
Expression e = buildExpression(VN, V);
|
std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(V, nextValueNumber));
|
||||||
std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(e, nextValueNumber));
|
|
||||||
if (ret.second)
|
if (ret.second)
|
||||||
nextValueNumber++;
|
nextValueNumber++;
|
||||||
if (e.opcode != 0 || (e.opcode == 0 && isa<PHINode>(e.value)))
|
if (isa<BinaryOperator>(V) || isa<PHINode>(V))
|
||||||
MS.insert(e);
|
MS.insert(V);
|
||||||
return e;
|
return ret.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
GVNPRE::ValueTable::iterator GVNPRE::lookup(ValueTable& VN, Value* V) {
|
GVNPRE::ValueTable::iterator GVNPRE::lookup(ValueTable& VN, Value* V) {
|
||||||
Expression e = buildExpression(VN, V);
|
return VN.find(V);
|
||||||
return VN.find(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<GVNPRE::Expression>::iterator GVNPRE::find_leader(GVNPRE::ValueTable VN,
|
Value* GVNPRE::find_leader(GVNPRE::ValueTable VN,
|
||||||
std::set<GVNPRE::Expression>& vals,
|
std::set<Value*, ExprLT>& vals,
|
||||||
uint32_t v) {
|
uint32_t v) {
|
||||||
for (std::set<Expression>::iterator I = vals.begin(), E = vals.end();
|
for (std::set<Value*, ExprLT>::iterator I = vals.begin(), E = vals.end();
|
||||||
I != E; ++I)
|
I != E; ++I)
|
||||||
if (VN[*I] == v)
|
if (VN[*I] == v)
|
||||||
return I;
|
return *I;
|
||||||
|
|
||||||
return vals.end();
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GVNPRE::phi_translate(GVNPRE::ValueTable& VN,
|
void GVNPRE::phi_translate(GVNPRE::ValueTable& VN,
|
||||||
std::set<GVNPRE::Expression>& MS,
|
std::set<Value*, ExprLT>& MS,
|
||||||
std::set<GVNPRE::Expression>& anticIn, BasicBlock* B,
|
std::set<Value*, ExprLT>& anticIn, BasicBlock* B,
|
||||||
std::set<GVNPRE::Expression>& out) {
|
std::set<Value*, ExprLT>& out) {
|
||||||
BasicBlock* succ = B->getTerminator()->getSuccessor(0);
|
BasicBlock* succ = B->getTerminator()->getSuccessor(0);
|
||||||
|
|
||||||
for (std::set<Expression>::iterator I = anticIn.begin(), E = anticIn.end();
|
for (std::set<Value*, ExprLT>::iterator I = anticIn.begin(), E = anticIn.end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
if (I->opcode == 0) {
|
if (!isa<BinaryOperator>(*I)) {
|
||||||
Value *v = I->value;
|
if (PHINode* p = dyn_cast<PHINode>(*I)) {
|
||||||
if (PHINode* p = dyn_cast<PHINode>(v)) {
|
|
||||||
if (p->getParent() == succ)
|
if (p->getParent() == succ)
|
||||||
out.insert(buildExpression(VN, p->getIncomingValueForBlock(B)));
|
out.insert(p);
|
||||||
} else {
|
} else {
|
||||||
out.insert(*I);
|
out.insert(*I);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::set<Expression>::iterator lhs_it = find_leader(VN, anticIn, I->lhs);
|
BinaryOperator* BO = cast<BinaryOperator>(*I);
|
||||||
if (lhs_it == anticIn.end())
|
Value* lhs = find_leader(VN, anticIn, VN[BO->getOperand(0)]);
|
||||||
|
if (lhs == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Expression lhs = *lhs_it;
|
if (PHINode* p = dyn_cast<PHINode>(lhs))
|
||||||
if (lhs.value != 0)
|
if (p->getParent() == succ) {
|
||||||
if (PHINode* p = dyn_cast<PHINode>(lhs.value))
|
lhs = p->getIncomingValueForBlock(B);
|
||||||
if (p->getParent() == succ) {
|
out.insert(lhs);
|
||||||
Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
|
}
|
||||||
lhs.opcode = t.opcode;
|
|
||||||
lhs.value = t.value;
|
|
||||||
lhs.lhs = t.lhs;
|
|
||||||
lhs.rhs = t.rhs;
|
|
||||||
|
|
||||||
out.insert(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::set<Expression>::iterator rhs_it = find_leader(VN, anticIn, I->rhs);
|
Value* rhs = find_leader(VN, anticIn, VN[BO->getOperand(1)]);
|
||||||
if (rhs_it == anticIn.end())
|
if (rhs == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Expression rhs = *rhs_it;
|
if (PHINode* p = dyn_cast<PHINode>(rhs))
|
||||||
if (rhs.value != 0)
|
if (p->getParent() == succ) {
|
||||||
if (PHINode* p = dyn_cast<PHINode>(rhs.value))
|
rhs = p->getIncomingValueForBlock(B);
|
||||||
if (p->getParent() == succ) {
|
out.insert(rhs);
|
||||||
Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
|
}
|
||||||
rhs.opcode = t.opcode;
|
|
||||||
rhs.value = t.value;
|
|
||||||
rhs.lhs = t.lhs;
|
|
||||||
rhs.rhs = t.rhs;
|
|
||||||
|
|
||||||
out.insert(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
Expression e;
|
if (lhs != BO->getOperand(0) || rhs != BO->getOperand(1)) {
|
||||||
e.opcode = I->opcode;
|
BO = BinaryOperator::create(BO->getOpcode(), lhs, rhs, BO->getName()+".gvnpre");
|
||||||
e.value = 0;
|
if (VN.insert(std::make_pair(BO, nextValueNumber)).second)
|
||||||
e.lhs = VN[lhs];
|
nextValueNumber++;
|
||||||
e.rhs = VN[rhs];
|
MS.insert(BO);
|
||||||
|
}
|
||||||
|
|
||||||
|
out.insert(BO);
|
||||||
|
|
||||||
if (VN.insert(std::make_pair(e, nextValueNumber)).second)
|
|
||||||
nextValueNumber++;
|
|
||||||
MS.insert(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove all expressions whose operands are not themselves in the set
|
// Remove all expressions whose operands are not themselves in the set
|
||||||
void GVNPRE::clean(GVNPRE::ValueTable VN, std::set<GVNPRE::Expression>& set) {
|
void GVNPRE::clean(GVNPRE::ValueTable VN, std::set<Value*, ExprLT>& set) {
|
||||||
std::vector<Expression> worklist;
|
std::vector<Value*> worklist;
|
||||||
topo_sort(VN, set, worklist);
|
topo_sort(VN, set, worklist);
|
||||||
|
|
||||||
while (!worklist.empty()) {
|
while (!worklist.empty()) {
|
||||||
Expression e = worklist.back();
|
Value* v = worklist.back();
|
||||||
worklist.pop_back();
|
worklist.pop_back();
|
||||||
|
|
||||||
if (e.opcode == 0) // OPAQUE
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(v)) {
|
||||||
continue;
|
bool lhsValid = false;
|
||||||
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
||||||
|
I != E; ++I)
|
||||||
|
if (VN[*I] == VN[BO->getOperand(0)]);
|
||||||
|
lhsValid = true;
|
||||||
|
|
||||||
|
bool rhsValid = false;
|
||||||
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
||||||
|
I != E; ++I)
|
||||||
|
if (VN[*I] == VN[BO->getOperand(1)]);
|
||||||
|
rhsValid = true;
|
||||||
|
|
||||||
bool lhsValid = false;
|
if (!lhsValid || !rhsValid)
|
||||||
for (std::set<Expression>::iterator I = set.begin(), E = set.end();
|
set.erase(BO);
|
||||||
I != E; ++I)
|
}
|
||||||
if (VN[*I] == e.lhs);
|
|
||||||
lhsValid = true;
|
|
||||||
|
|
||||||
bool rhsValid = false;
|
|
||||||
for (std::set<Expression>::iterator I = set.begin(), E = set.end();
|
|
||||||
I != E; ++I)
|
|
||||||
if (VN[*I] == e.rhs);
|
|
||||||
rhsValid = true;
|
|
||||||
|
|
||||||
if (!lhsValid || !rhsValid)
|
|
||||||
set.erase(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GVNPRE::topo_sort(GVNPRE::ValueTable& VN,
|
void GVNPRE::topo_sort(GVNPRE::ValueTable& VN,
|
||||||
std::set<GVNPRE::Expression>& set,
|
std::set<Value*, ExprLT>& set,
|
||||||
std::vector<GVNPRE::Expression>& vec) {
|
std::vector<Value*>& vec) {
|
||||||
std::set<Expression> toErase;
|
std::set<Value*, ExprLT> toErase;
|
||||||
for (std::set<Expression>::iterator I = set.begin(), E = set.end();
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
for (std::set<Expression>::iterator SI = set.begin(); SI != E; ++SI) {
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(*I))
|
||||||
if (I->lhs == VN[*SI] || I->rhs == VN[*SI]) {
|
for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
|
||||||
toErase.insert(*SI);
|
if (VN[BO->getOperand(0)] == VN[*SI] || VN[BO->getOperand(1)] == VN[*SI]) {
|
||||||
}
|
toErase.insert(BO);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Expression> Q;
|
std::vector<Value*> Q;
|
||||||
std::insert_iterator<std::vector<Expression> > q_ins(Q, Q.begin());
|
std::insert_iterator<std::vector<Value*> > q_ins(Q, Q.begin());
|
||||||
std::set_difference(set.begin(), set.end(),
|
std::set_difference(set.begin(), set.end(),
|
||||||
toErase.begin(), toErase.end(),
|
toErase.begin(), toErase.end(),
|
||||||
q_ins);
|
q_ins, ExprLT());
|
||||||
|
|
||||||
std::set<Expression> visited;
|
std::set<Value*, ExprLT> visited;
|
||||||
while (!Q.empty()) {
|
while (!Q.empty()) {
|
||||||
Expression e = Q.back();
|
Value* e = Q.back();
|
||||||
|
|
||||||
if (e.opcode == 0) {
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(e)) {
|
||||||
|
Value* l = find_leader(VN, set, VN[BO->getOperand(0)]);
|
||||||
|
Value* r = find_leader(VN, set, VN[BO->getOperand(1)]);
|
||||||
|
|
||||||
|
if (l != 0 && visited.find(l) == visited.end())
|
||||||
|
Q.push_back(l);
|
||||||
|
else if (r != 0 && visited.find(r) == visited.end())
|
||||||
|
Q.push_back(r);
|
||||||
|
else {
|
||||||
|
vec.push_back(e);
|
||||||
|
visited.insert(e);
|
||||||
|
Q.pop_back();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
visited.insert(e);
|
visited.insert(e);
|
||||||
vec.push_back(e);
|
vec.push_back(e);
|
||||||
Q.pop_back();
|
Q.pop_back();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::set<Expression>::iterator l = find_leader(VN, set, e.lhs);
|
|
||||||
std::set<Expression>::iterator r = find_leader(VN, set, e.rhs);
|
|
||||||
|
|
||||||
if (l != set.end() && visited.find(*l) == visited.end())
|
|
||||||
Q.push_back(*l);
|
|
||||||
else if (r != set.end() && visited.find(*r) == visited.end())
|
|
||||||
Q.push_back(*r);
|
|
||||||
else {
|
|
||||||
vec.push_back(e);
|
|
||||||
visited.insert(e);
|
|
||||||
Q.pop_back();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GVNPRE::dump(GVNPRE::ValueTable& VN, std::set<GVNPRE::Expression>& s) {
|
void GVNPRE::dump(GVNPRE::ValueTable& VN, std::set<Value*, ExprLT>& s) {
|
||||||
std::vector<Expression> sorted;
|
std::vector<Value*> sorted;
|
||||||
topo_sort(VN, s, sorted);
|
topo_sort(VN, s, sorted);
|
||||||
DOUT << "{ ";
|
DOUT << "{ ";
|
||||||
for (std::vector<Expression>::iterator I = sorted.begin(), E = sorted.end();
|
for (std::vector<Value*>::iterator I = sorted.begin(), E = sorted.end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
DOUT << VN[*I] << ": ";
|
DEBUG((*I)->dump());
|
||||||
DOUT << "( ";
|
|
||||||
DOUT << (char)(I->opcode+48);
|
|
||||||
DOUT << ", ";
|
|
||||||
if (I->value == 0)
|
|
||||||
DOUT << "0";
|
|
||||||
else
|
|
||||||
DEBUG(I->value->dump());
|
|
||||||
DOUT << ", value." << I->lhs << ", value." << I->rhs << " ) ";
|
|
||||||
}
|
}
|
||||||
DOUT << "}\n\n";
|
DOUT << "}\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Expression>& MS,
|
void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Value*, ExprLT>& MS,
|
||||||
DominatorTree::Node* DI,
|
DominatorTree::Node* DI,
|
||||||
std::set<Expression>& currExps,
|
std::set<Value*, ExprLT>& currExps,
|
||||||
std::set<PHINode*>& currPhis,
|
std::set<PHINode*>& currPhis,
|
||||||
std::set<Expression>& currTemps,
|
std::set<Value*, ExprLT>& currTemps,
|
||||||
std::set<Expression>& currAvail,
|
std::set<Value*, ExprLT>& currAvail,
|
||||||
std::map<BasicBlock*, std::set<Expression> > availOut) {
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut) {
|
||||||
|
|
||||||
BasicBlock* BB = DI->getBlock();
|
BasicBlock* BB = DI->getBlock();
|
||||||
|
|
||||||
@ -416,36 +292,37 @@ void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Expression>& MS,
|
|||||||
|
|
||||||
// Handle binary ops...
|
// Handle binary ops...
|
||||||
} else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
|
} else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
|
||||||
Expression leftValue = buildExpression(VN, BO->getOperand(0));
|
Value* leftValue = BO->getOperand(0);
|
||||||
Expression rightValue = buildExpression(VN, BO->getOperand(1));
|
Value* rightValue = BO->getOperand(1);
|
||||||
|
|
||||||
Expression e = add(VN, MS, BO);
|
add(VN, MS, BO);
|
||||||
|
|
||||||
currExps.insert(leftValue);
|
currExps.insert(leftValue);
|
||||||
currExps.insert(rightValue);
|
currExps.insert(rightValue);
|
||||||
currExps.insert(e);
|
currExps.insert(BO);
|
||||||
|
|
||||||
currTemps.insert(e);
|
currTemps.insert(BO);
|
||||||
|
|
||||||
// Handle unsupported ops
|
// Handle unsupported ops
|
||||||
} else {
|
} else if (!BI->isTerminator()){
|
||||||
Expression e = add(VN, MS, BI);
|
add(VN, MS, BI);
|
||||||
currTemps.insert(e);
|
currTemps.insert(BI);
|
||||||
}
|
}
|
||||||
|
|
||||||
currAvail.insert(buildExpression(VN, BI));
|
if (!BI->isTerminator())
|
||||||
|
currAvail.insert(BI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GVNPRE::runOnFunction(Function &F) {
|
bool GVNPRE::runOnFunction(Function &F) {
|
||||||
ValueTable VN;
|
ValueTable VN;
|
||||||
std::set<Expression> maximalSet;
|
std::set<Value*, ExprLT> maximalSet;
|
||||||
|
|
||||||
std::map<BasicBlock*, std::set<Expression> > generatedExpressions;
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > generatedExpressions;
|
||||||
std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
|
std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
|
||||||
std::map<BasicBlock*, std::set<Expression> > generatedTemporaries;
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > generatedTemporaries;
|
||||||
std::map<BasicBlock*, std::set<Expression> > availableOut;
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availableOut;
|
||||||
std::map<BasicBlock*, std::set<Expression> > anticipatedIn;
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > anticipatedIn;
|
||||||
|
|
||||||
DominatorTree &DT = getAnalysis<DominatorTree>();
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
||||||
|
|
||||||
@ -456,10 +333,10 @@ bool GVNPRE::runOnFunction(Function &F) {
|
|||||||
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
||||||
|
|
||||||
// Get the sets to update for this block
|
// Get the sets to update for this block
|
||||||
std::set<Expression>& currExps = generatedExpressions[DI->getBlock()];
|
std::set<Value*, ExprLT>& currExps = generatedExpressions[DI->getBlock()];
|
||||||
std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
|
std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
|
||||||
std::set<Expression>& currTemps = generatedTemporaries[DI->getBlock()];
|
std::set<Value*, ExprLT>& currTemps = generatedTemporaries[DI->getBlock()];
|
||||||
std::set<Expression>& currAvail = availableOut[DI->getBlock()];
|
std::set<Value*, ExprLT>& currAvail = availableOut[DI->getBlock()];
|
||||||
|
|
||||||
CalculateAvailOut(VN, maximalSet, *DI, currExps, currPhis,
|
CalculateAvailOut(VN, maximalSet, *DI, currExps, currPhis,
|
||||||
currTemps, currAvail, availableOut);
|
currTemps, currAvail, availableOut);
|
||||||
@ -475,7 +352,7 @@ bool GVNPRE::runOnFunction(Function &F) {
|
|||||||
unsigned iterations = 0;
|
unsigned iterations = 0;
|
||||||
while (changed) {
|
while (changed) {
|
||||||
changed = false;
|
changed = false;
|
||||||
std::set<Expression> anticOut;
|
std::set<Value*, ExprLT> anticOut;
|
||||||
|
|
||||||
// Top-down walk of the postdominator tree
|
// Top-down walk of the postdominator tree
|
||||||
for (df_iterator<PostDominatorTree::Node*> PDI =
|
for (df_iterator<PostDominatorTree::Node*> PDI =
|
||||||
@ -485,8 +362,8 @@ bool GVNPRE::runOnFunction(Function &F) {
|
|||||||
|
|
||||||
visited.insert(BB);
|
visited.insert(BB);
|
||||||
|
|
||||||
std::set<Expression>& anticIn = anticipatedIn[BB];
|
std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
|
||||||
std::set<Expression> old (anticIn.begin(), anticIn.end());
|
std::set<Value*, ExprLT> old (anticIn.begin(), anticIn.end());
|
||||||
|
|
||||||
if (BB->getTerminator()->getNumSuccessors() == 1) {
|
if (BB->getTerminator()->getNumSuccessors() == 1) {
|
||||||
if (visited.find(BB) == visited.end())
|
if (visited.find(BB) == visited.end())
|
||||||
@ -496,43 +373,43 @@ bool GVNPRE::runOnFunction(Function &F) {
|
|||||||
} else if (BB->getTerminator()->getNumSuccessors() > 1) {
|
} else if (BB->getTerminator()->getNumSuccessors() > 1) {
|
||||||
for (unsigned i = 0; i < BB->getTerminator()->getNumSuccessors(); ++i) {
|
for (unsigned i = 0; i < BB->getTerminator()->getNumSuccessors(); ++i) {
|
||||||
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
|
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
|
||||||
std::set<Expression> temp;
|
std::set<Value*, ExprLT> temp;
|
||||||
if (visited.find(currSucc) == visited.end())
|
if (visited.find(currSucc) == visited.end())
|
||||||
temp.insert(maximalSet.begin(), maximalSet.end());
|
temp.insert(maximalSet.begin(), maximalSet.end());
|
||||||
else
|
else
|
||||||
temp.insert(anticIn.begin(), anticIn.end());
|
temp.insert(anticIn.begin(), anticIn.end());
|
||||||
|
|
||||||
anticIn.clear();
|
anticIn.clear();
|
||||||
std::insert_iterator<std::set<Expression> > ai_ins(anticIn,
|
std::insert_iterator<std::set<Value*, ExprLT> > ai_ins(anticIn,
|
||||||
anticIn.begin());
|
anticIn.begin());
|
||||||
|
|
||||||
std::set_difference(anticipatedIn[currSucc].begin(),
|
std::set_difference(anticipatedIn[currSucc].begin(),
|
||||||
anticipatedIn[currSucc].end(),
|
anticipatedIn[currSucc].end(),
|
||||||
temp.begin(),
|
temp.begin(),
|
||||||
temp.end(),
|
temp.end(),
|
||||||
ai_ins);
|
ai_ins,
|
||||||
|
ExprLT());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<Expression> S;
|
std::set<Value*, ExprLT> S;
|
||||||
std::insert_iterator<std::set<Expression> > s_ins(S, S.begin());
|
std::insert_iterator<std::set<Value*, ExprLT> > s_ins(S, S.begin());
|
||||||
std::set_union(anticOut.begin(), anticOut.end(),
|
std::set_union(anticOut.begin(), anticOut.end(),
|
||||||
generatedExpressions[BB].begin(),
|
generatedExpressions[BB].begin(),
|
||||||
generatedExpressions[BB].end(),
|
generatedExpressions[BB].end(),
|
||||||
s_ins);
|
s_ins, ExprLT());
|
||||||
|
|
||||||
anticIn.clear();
|
anticIn.clear();
|
||||||
std::insert_iterator<std::set<Expression> > antic_ins(anticIn,
|
std::insert_iterator<std::set<Value*, ExprLT> > antic_ins(anticIn,
|
||||||
anticIn.begin());
|
anticIn.begin());
|
||||||
std::set_difference(S.begin(), S.end(),
|
std::set_difference(S.begin(), S.end(),
|
||||||
generatedTemporaries[BB].begin(),
|
generatedTemporaries[BB].begin(),
|
||||||
generatedTemporaries[BB].end(),
|
generatedTemporaries[BB].end(),
|
||||||
antic_ins);
|
antic_ins,
|
||||||
|
ExprLT());
|
||||||
|
|
||||||
clean(VN, anticIn);
|
clean(VN, anticIn);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (old != anticIn)
|
if (old != anticIn)
|
||||||
changed = true;
|
changed = true;
|
||||||
|
|
||||||
@ -557,6 +434,10 @@ bool GVNPRE::runOnFunction(Function &F) {
|
|||||||
DOUT << "ANTIC_IN: ";
|
DOUT << "ANTIC_IN: ";
|
||||||
dump(VN, anticipatedIn[I]);
|
dump(VN, anticipatedIn[I]);
|
||||||
DOUT << "\n";
|
DOUT << "\n";
|
||||||
|
|
||||||
|
DOUT << "AVAIL_OUT: ";
|
||||||
|
dump(VN, availableOut[I]);
|
||||||
|
DOUT << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user