2007-05-29 21:53:49 +00: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-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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.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>
|
2007-05-31 22:44:11 +00:00
|
|
|
#include <vector>
|
2007-05-29 21:53:49 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
struct ExprLT {
|
|
|
|
bool operator()(Value* left, Value* right) {
|
2007-06-09 18:35:31 +00:00
|
|
|
if (BinaryOperator* leftBO = dyn_cast<BinaryOperator>(left)) {
|
|
|
|
if (BinaryOperator* rightBO = dyn_cast<BinaryOperator>(right))
|
|
|
|
return cmpBinaryOperator(leftBO, rightBO);
|
|
|
|
else
|
2007-06-12 00:50:47 +00:00
|
|
|
if (isa<CmpInst>(right)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-09 18:35:31 +00:00
|
|
|
} else if (CmpInst* leftCmp = dyn_cast<CmpInst>(left)) {
|
|
|
|
if (CmpInst* rightCmp = dyn_cast<CmpInst>(right))
|
|
|
|
return cmpComparison(leftCmp, rightCmp);
|
|
|
|
else
|
2007-06-12 00:50:47 +00:00
|
|
|
return true;
|
2007-06-09 18:35:31 +00:00
|
|
|
} else {
|
2007-06-12 00:50:47 +00:00
|
|
|
if (isa<BinaryOperator>(right) || isa<CmpInst>(right))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return left < right;
|
2007-06-09 18:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmpBinaryOperator(BinaryOperator* left, BinaryOperator* right) {
|
|
|
|
if (left->getOpcode() != right->getOpcode())
|
|
|
|
return left->getOpcode() < right->getOpcode();
|
|
|
|
else if ((*this)(left->getOperand(0), right->getOperand(0)))
|
|
|
|
return true;
|
|
|
|
else if ((*this)(right->getOperand(0), left->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return (*this)(left->getOperand(1), right->getOperand(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmpComparison(CmpInst* left, CmpInst* right) {
|
|
|
|
if (left->getOpcode() != right->getOpcode())
|
|
|
|
return left->getOpcode() < right->getOpcode();
|
|
|
|
else if (left->getPredicate() != right->getPredicate())
|
|
|
|
return left->getPredicate() < right->getPredicate();
|
|
|
|
else if ((*this)(left->getOperand(0), right->getOperand(0)))
|
2007-06-03 05:55:58 +00:00
|
|
|
return true;
|
2007-06-09 18:35:31 +00:00
|
|
|
else if ((*this)(right->getOperand(0), left->getOperand(0)))
|
2007-06-03 05:55:58 +00:00
|
|
|
return false;
|
|
|
|
else
|
2007-06-09 18:35:31 +00:00
|
|
|
return (*this)(left->getOperand(1), right->getOperand(1));
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-09 18:35:31 +00:00
|
|
|
GVNPRE() : FunctionPass((intptr_t)&ID) { nextValueNumber = 1; }
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t nextValueNumber;
|
2007-06-03 05:55:58 +00:00
|
|
|
typedef std::map<Value*, uint32_t, ExprLT> ValueTable;
|
2007-06-08 01:03:01 +00:00
|
|
|
ValueTable VN;
|
|
|
|
std::set<Value*, ExprLT> MS;
|
2007-06-12 00:50:47 +00:00
|
|
|
std::vector<Instruction*> createdExpressions;
|
2007-05-31 00:42:15 +00:00
|
|
|
|
2007-05-29 21:53:49 +00: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-08 01:52:45 +00:00
|
|
|
void dump(const std::set<Value*>& s) const;
|
|
|
|
void dump_unique(const std::set<Value*, ExprLT>& s) const;
|
2007-06-08 01:03:01 +00:00
|
|
|
void clean(std::set<Value*, ExprLT>& set);
|
|
|
|
bool add(Value* V, uint32_t number);
|
|
|
|
Value* find_leader(std::set<Value*, ExprLT>& vals,
|
|
|
|
Value* v);
|
|
|
|
Value* phi_translate(std::set<Value*, ExprLT>& set,
|
2007-06-12 00:50:47 +00:00
|
|
|
Value* V, BasicBlock* pred, BasicBlock* succ);
|
|
|
|
void phi_translate_set(std::set<Value*, ExprLT>& anticIn, BasicBlock* pred,
|
|
|
|
BasicBlock* succ, std::set<Value*, ExprLT>& out);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
void topo_sort(std::set<Value*, ExprLT>& set,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::vector<Value*>& vec);
|
2007-05-31 22:44:11 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
// For a given block, calculate the generated expressions, temporaries,
|
|
|
|
// and the AVAIL_OUT set
|
2007-06-08 01:03:01 +00:00
|
|
|
void CalculateAvailOut(DomTreeNode* DI,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currExps,
|
2007-05-29 21:53:49 +00:00
|
|
|
std::set<PHINode*>& currPhis,
|
2007-06-03 22:02:14 +00:00
|
|
|
std::set<Value*>& currTemps,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currAvail,
|
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
char GVNPRE::ID = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
|
|
|
|
|
|
|
|
RegisterPass<GVNPRE> X("gvnpre",
|
|
|
|
"Global Value Numbering/Partial Redundancy Elimination");
|
|
|
|
|
|
|
|
|
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-03 05:55:58 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
bool GVNPRE::add(Value* V, uint32_t number) {
|
|
|
|
std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(V, number));
|
2007-06-09 18:35:31 +00:00
|
|
|
if (isa<BinaryOperator>(V) || isa<PHINode>(V) || isa<CmpInst>(V))
|
2007-06-03 05:55:58 +00:00
|
|
|
MS.insert(V);
|
|
|
|
return ret.second;
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
Value* GVNPRE::find_leader(std::set<Value*, ExprLT>& vals, Value* v) {
|
2007-06-03 05:55:58 +00:00
|
|
|
for (std::set<Value*, ExprLT>::iterator I = vals.begin(), E = vals.end();
|
2007-06-08 01:03:01 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
assert(VN.find(v) != VN.end() && "Value not numbered?");
|
|
|
|
assert(VN.find(*I) != VN.end() && "Value not numbered?");
|
|
|
|
if (VN[v] == VN[*I])
|
2007-06-03 05:55:58 +00:00
|
|
|
return *I;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
return 0;
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
Value* GVNPRE::phi_translate(std::set<Value*, ExprLT>& set,
|
2007-06-12 00:50:47 +00:00
|
|
|
Value* V, BasicBlock* pred, BasicBlock* succ) {
|
2007-06-04 18:05:26 +00:00
|
|
|
if (V == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
|
2007-06-04 23:28:33 +00:00
|
|
|
Value* newOp1 = isa<Instruction>(BO->getOperand(0))
|
2007-06-08 01:03:01 +00:00
|
|
|
? phi_translate(set,
|
2007-06-06 01:27:49 +00:00
|
|
|
find_leader(set, BO->getOperand(0)),
|
2007-06-12 00:50:47 +00:00
|
|
|
pred, succ)
|
2007-06-04 23:28:33 +00:00
|
|
|
: BO->getOperand(0);
|
2007-06-04 18:05:26 +00:00
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-04 23:28:33 +00:00
|
|
|
Value* newOp2 = isa<Instruction>(BO->getOperand(1))
|
2007-06-08 01:03:01 +00:00
|
|
|
? phi_translate(set,
|
2007-06-06 01:27:49 +00:00
|
|
|
find_leader(set, BO->getOperand(1)),
|
2007-06-12 00:50:47 +00:00
|
|
|
pred, succ)
|
2007-06-04 23:28:33 +00:00
|
|
|
: BO->getOperand(1);
|
2007-06-04 18:05:26 +00:00
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != BO->getOperand(0) || newOp2 != BO->getOperand(1)) {
|
2007-06-08 01:03:01 +00:00
|
|
|
Instruction* newVal = BinaryOperator::create(BO->getOpcode(),
|
2007-06-04 18:05:26 +00:00
|
|
|
newOp1, newOp2,
|
|
|
|
BO->getName()+".gvnpre");
|
2007-06-08 20:44:02 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
if (add(newVal, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
2007-06-06 01:27:49 +00:00
|
|
|
if (!find_leader(set, newVal)) {
|
2007-06-08 01:03:01 +00:00
|
|
|
DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
|
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-08 20:44:02 +00:00
|
|
|
ValueTable::iterator I = VN.find(newVal);
|
|
|
|
if (I->first == newVal)
|
|
|
|
VN.erase(newVal);
|
|
|
|
|
|
|
|
std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
|
|
|
|
if (*F == newVal)
|
|
|
|
MS.erase(newVal);
|
|
|
|
|
2007-06-05 22:11:49 +00:00
|
|
|
delete newVal;
|
2007-06-04 18:05:26 +00:00
|
|
|
return 0;
|
2007-06-05 22:11:49 +00:00
|
|
|
}
|
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);
|
2007-06-09 18:35:31 +00:00
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
|
|
|
|
Value* newOp1 = isa<Instruction>(C->getOperand(0))
|
|
|
|
? phi_translate(set,
|
|
|
|
find_leader(set, C->getOperand(0)),
|
2007-06-12 00:50:47 +00:00
|
|
|
pred, succ)
|
2007-06-09 18:35:31 +00:00
|
|
|
: C->getOperand(0);
|
|
|
|
if (newOp1 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Value* newOp2 = isa<Instruction>(C->getOperand(1))
|
|
|
|
? phi_translate(set,
|
|
|
|
find_leader(set, C->getOperand(1)),
|
2007-06-12 00:50:47 +00:00
|
|
|
pred, succ)
|
2007-06-09 18:35:31 +00:00
|
|
|
: C->getOperand(1);
|
|
|
|
if (newOp2 == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (newOp1 != C->getOperand(0) || newOp2 != C->getOperand(1)) {
|
|
|
|
Instruction* newVal = CmpInst::create(C->getOpcode(),
|
|
|
|
C->getPredicate(),
|
|
|
|
newOp1, newOp2,
|
|
|
|
C->getName()+".gvnpre");
|
|
|
|
|
|
|
|
if (add(newVal, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
|
|
|
if (!find_leader(set, newVal)) {
|
|
|
|
DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
|
2007-06-12 00:50:47 +00:00
|
|
|
createdExpressions.push_back(newVal);
|
2007-06-09 18:35:31 +00:00
|
|
|
return newVal;
|
|
|
|
} else {
|
|
|
|
ValueTable::iterator I = VN.find(newVal);
|
|
|
|
if (I->first == newVal)
|
|
|
|
VN.erase(newVal);
|
|
|
|
|
|
|
|
std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
|
|
|
|
if (*F == newVal)
|
|
|
|
MS.erase(newVal);
|
|
|
|
|
|
|
|
delete newVal;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-06-04 18:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2007-06-12 00:50:47 +00:00
|
|
|
void GVNPRE::phi_translate_set(std::set<Value*, ExprLT>& anticIn,
|
|
|
|
BasicBlock* pred, BasicBlock* succ,
|
|
|
|
std::set<Value*, ExprLT>& out) {
|
2007-06-04 18:05:26 +00:00
|
|
|
for (std::set<Value*, ExprLT>::iterator I = anticIn.begin(),
|
|
|
|
E = anticIn.end(); I != E; ++I) {
|
2007-06-12 00:50:47 +00:00
|
|
|
Value* V = phi_translate(anticIn, *I, pred, succ);
|
2007-06-04 18:05:26 +00:00
|
|
|
if (V != 0)
|
|
|
|
out.insert(V);
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all expressions whose operands are not themselves in the set
|
2007-06-08 01:03:01 +00:00
|
|
|
void GVNPRE::clean(std::set<Value*, ExprLT>& set) {
|
2007-06-03 05:55:58 +00:00
|
|
|
std::vector<Value*> worklist;
|
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-06-03 05:55:58 +00:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(v)) {
|
2007-06-08 01:03:01 +00:00
|
|
|
bool lhsValid = !isa<Instruction>(BO->getOperand(0));
|
|
|
|
if (!lhsValid)
|
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (VN[*I] == VN[BO->getOperand(0)]) {
|
|
|
|
lhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-03 05:55:58 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
bool rhsValid = !isa<Instruction>(BO->getOperand(1));
|
|
|
|
if (!rhsValid)
|
2007-06-03 05:55:58 +00:00
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
|
|
|
I != E; ++I)
|
2007-06-08 01:03:01 +00:00
|
|
|
if (VN[*I] == VN[BO->getOperand(1)]) {
|
2007-06-03 05:55:58 +00:00
|
|
|
rhsValid = true;
|
2007-06-08 01:03:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
if (!lhsValid || !rhsValid)
|
|
|
|
set.erase(BO);
|
2007-06-09 18:35:31 +00:00
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(v)) {
|
|
|
|
bool lhsValid = !isa<Instruction>(C->getOperand(0));
|
|
|
|
if (!lhsValid)
|
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (VN[*I] == VN[C->getOperand(0)]) {
|
|
|
|
lhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rhsValid = !isa<Instruction>(C->getOperand(1));
|
|
|
|
if (!rhsValid)
|
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (VN[*I] == VN[C->getOperand(1)]) {
|
|
|
|
rhsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lhsValid || !rhsValid)
|
|
|
|
set.erase(C);
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
void GVNPRE::topo_sort(std::set<Value*, ExprLT>& set,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::vector<Value*>& vec) {
|
2007-06-08 01:03:01 +00:00
|
|
|
std::set<Value*, ExprLT> toErase;
|
2007-06-03 05:55:58 +00:00
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
2007-05-31 22:44:11 +00:00
|
|
|
I != E; ++I) {
|
2007-06-03 05:55:58 +00:00
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(*I))
|
|
|
|
for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
|
2007-06-08 01:03:01 +00:00
|
|
|
if (VN[BO->getOperand(0)] == VN[*SI] ||
|
|
|
|
VN[BO->getOperand(1)] == VN[*SI]) {
|
|
|
|
toErase.insert(*SI);
|
2007-06-03 05:55:58 +00:00
|
|
|
}
|
2007-06-09 18:35:31 +00:00
|
|
|
}
|
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(*I))
|
|
|
|
for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
|
|
|
|
if (VN[C->getOperand(0)] == VN[*SI] ||
|
|
|
|
VN[C->getOperand(1)] == VN[*SI]) {
|
|
|
|
toErase.insert(*SI);
|
|
|
|
}
|
|
|
|
}
|
2007-05-31 22:44:11 +00:00
|
|
|
}
|
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
std::vector<Value*> Q;
|
2007-06-08 01:03:01 +00:00
|
|
|
for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (toErase.find(*I) == toErase.end())
|
|
|
|
Q.push_back(*I);
|
|
|
|
}
|
2007-05-31 22:44:11 +00:00
|
|
|
|
2007-06-05 23:46:12 +00:00
|
|
|
std::set<Value*> visited;
|
2007-05-31 22:44:11 +00:00
|
|
|
while (!Q.empty()) {
|
2007-06-03 05:55:58 +00:00
|
|
|
Value* e = Q.back();
|
|
|
|
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(e)) {
|
2007-06-06 01:27:49 +00:00
|
|
|
Value* l = find_leader(set, BO->getOperand(0));
|
|
|
|
Value* r = find_leader(set, BO->getOperand(1));
|
2007-06-03 05:55:58 +00:00
|
|
|
|
2007-06-09 18:35:31 +00:00
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
|
|
|
visited.find(l) == visited.end())
|
|
|
|
Q.push_back(l);
|
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
|
|
|
visited.find(r) == visited.end())
|
|
|
|
Q.push_back(r);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(e)) {
|
|
|
|
Value* l = find_leader(set, C->getOperand(0));
|
|
|
|
Value* r = find_leader(set, C->getOperand(1));
|
|
|
|
|
2007-06-05 23:46:12 +00:00
|
|
|
if (l != 0 && isa<Instruction>(l) &&
|
|
|
|
visited.find(l) == visited.end())
|
2007-06-03 05:55:58 +00:00
|
|
|
Q.push_back(l);
|
2007-06-05 23:46:12 +00:00
|
|
|
else if (r != 0 && isa<Instruction>(r) &&
|
|
|
|
visited.find(r) == visited.end())
|
2007-06-03 05:55:58 +00:00
|
|
|
Q.push_back(r);
|
|
|
|
else {
|
|
|
|
vec.push_back(e);
|
|
|
|
visited.insert(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
} else {
|
2007-05-31 22:44:11 +00:00
|
|
|
visited.insert(e);
|
|
|
|
vec.push_back(e);
|
|
|
|
Q.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-03 22:02:14 +00:00
|
|
|
|
2007-06-08 01:52:45 +00:00
|
|
|
void GVNPRE::dump(const std::set<Value*>& s) const {
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "{ ";
|
2007-06-03 22:02:14 +00:00
|
|
|
for (std::set<Value*>::iterator I = s.begin(), E = s.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
DEBUG((*I)->dump());
|
|
|
|
}
|
|
|
|
DOUT << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:52:45 +00:00
|
|
|
void GVNPRE::dump_unique(const std::set<Value*, ExprLT>& s) const {
|
2007-06-03 22:02:14 +00:00
|
|
|
DOUT << "{ ";
|
|
|
|
for (std::set<Value*>::iterator I = s.begin(), E = s.end();
|
2007-05-31 22:44:11 +00:00
|
|
|
I != E; ++I) {
|
2007-06-03 05:55:58 +00:00
|
|
|
DEBUG((*I)->dump());
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "}\n\n";
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
void GVNPRE::CalculateAvailOut(DomTreeNode* DI,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currExps,
|
2007-05-29 21:53:49 +00:00
|
|
|
std::set<PHINode*>& currPhis,
|
2007-06-03 22:02:14 +00:00
|
|
|
std::set<Value*>& currTemps,
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currAvail,
|
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut) {
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
|
|
|
// A block inherits AVAIL_OUT from its dominator
|
|
|
|
if (DI->getIDom() != 0)
|
|
|
|
currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
|
|
|
|
availOut[DI->getIDom()->getBlock()].end());
|
|
|
|
|
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
// Handle PHI nodes...
|
|
|
|
if (PHINode* p = dyn_cast<PHINode>(BI)) {
|
2007-06-08 01:03:01 +00:00
|
|
|
if (add(p, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
2007-05-29 21:53:49 +00:00
|
|
|
currPhis.insert(p);
|
|
|
|
|
|
|
|
// Handle binary ops...
|
|
|
|
} else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
|
2007-06-03 05:55:58 +00:00
|
|
|
Value* leftValue = BO->getOperand(0);
|
|
|
|
Value* rightValue = BO->getOperand(1);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
if (add(BO, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-04 23:28:33 +00:00
|
|
|
if (isa<Instruction>(leftValue))
|
|
|
|
currExps.insert(leftValue);
|
|
|
|
if (isa<Instruction>(rightValue))
|
|
|
|
currExps.insert(rightValue);
|
2007-06-03 05:55:58 +00:00
|
|
|
currExps.insert(BO);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-09 18:35:31 +00:00
|
|
|
// Handle cmp ops...
|
|
|
|
} else if (CmpInst* C = dyn_cast<CmpInst>(BI)) {
|
|
|
|
Value* leftValue = C->getOperand(0);
|
|
|
|
Value* rightValue = C->getOperand(1);
|
|
|
|
|
|
|
|
if (add(C, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
|
|
|
|
|
|
|
if (isa<Instruction>(leftValue))
|
|
|
|
currExps.insert(leftValue);
|
|
|
|
if (isa<Instruction>(rightValue))
|
|
|
|
currExps.insert(rightValue);
|
|
|
|
currExps.insert(C);
|
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
// Handle unsupported ops
|
2007-06-03 05:55:58 +00:00
|
|
|
} else if (!BI->isTerminator()){
|
2007-06-08 01:03:01 +00:00
|
|
|
if (add(BI, nextValueNumber))
|
|
|
|
nextValueNumber++;
|
2007-06-03 05:55:58 +00:00
|
|
|
currTemps.insert(BI);
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
2007-06-03 05:55:58 +00:00
|
|
|
|
|
|
|
if (!BI->isTerminator())
|
|
|
|
currAvail.insert(BI);
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GVNPRE::runOnFunction(Function &F) {
|
2007-06-08 01:03:01 +00:00
|
|
|
VN.clear();
|
|
|
|
MS.clear();
|
|
|
|
createdExpressions.clear();
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > generatedExpressions;
|
2007-05-29 21:53:49 +00:00
|
|
|
std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
|
2007-06-03 22:02:14 +00:00
|
|
|
std::map<BasicBlock*, std::set<Value*> > generatedTemporaries;
|
2007-06-03 05:55:58 +00:00
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > availableOut;
|
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > anticipatedIn;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
// Phase 1: BuildSets
|
|
|
|
|
|
|
|
// 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-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currExps = generatedExpressions[DI->getBlock()];
|
2007-05-29 21:53:49 +00:00
|
|
|
std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
|
2007-06-03 22:02:14 +00:00
|
|
|
std::set<Value*>& currTemps = generatedTemporaries[DI->getBlock()];
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& currAvail = availableOut[DI->getBlock()];
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
CalculateAvailOut(*DI, currExps, currPhis,
|
2007-05-29 21:53:49 +00:00
|
|
|
currTemps, currAvail, availableOut);
|
|
|
|
}
|
|
|
|
|
2007-06-05 17:31:23 +00:00
|
|
|
DOUT << "Maximal Set: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(MS);
|
2007-06-05 17:31:23 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
|
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
// Phase 1, Part 2: calculate ANTIC_IN
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-05-29 23:26:30 +00:00
|
|
|
std::set<BasicBlock*> visited;
|
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
bool changed = true;
|
|
|
|
unsigned iterations = 0;
|
|
|
|
while (changed) {
|
|
|
|
changed = false;
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT> anticOut;
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
// Top-down walk of the postdominator tree
|
2007-06-04 00:32:22 +00:00
|
|
|
for (df_iterator<DomTreeNode*> PDI =
|
2007-06-08 01:03:01 +00:00
|
|
|
df_begin(PDT.getRootNode()), E = df_end(PDT.getRootNode());
|
2007-05-29 21:53:49 +00:00
|
|
|
PDI != E; ++PDI) {
|
|
|
|
BasicBlock* BB = PDI->getBlock();
|
2007-06-11 16:25:17 +00:00
|
|
|
if (BB == 0)
|
|
|
|
continue;
|
|
|
|
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "Block: " << BB->getName() << "\n";
|
|
|
|
DOUT << "TMP_GEN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump(generatedTemporaries[BB]);
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
|
|
|
DOUT << "EXP_GEN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(generatedExpressions[BB]);
|
2007-05-29 23:26:30 +00:00
|
|
|
visited.insert(BB);
|
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
|
|
|
|
std::set<Value*, ExprLT> old (anticIn.begin(), anticIn.end());
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
if (BB->getTerminator()->getNumSuccessors() == 1) {
|
2007-06-05 17:31:23 +00:00
|
|
|
if (visited.find(BB->getTerminator()->getSuccessor(0)) ==
|
|
|
|
visited.end())
|
2007-06-12 00:50:47 +00:00
|
|
|
phi_translate_set(MS, BB, BB->getTerminator()->getSuccessor(0),
|
|
|
|
anticOut);
|
2007-05-31 00:42:15 +00:00
|
|
|
else
|
2007-06-12 00:50:47 +00:00
|
|
|
phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
|
|
|
|
BB, BB->getTerminator()->getSuccessor(0),
|
|
|
|
anticOut);
|
2007-05-29 21:53:49 +00:00
|
|
|
} else if (BB->getTerminator()->getNumSuccessors() > 1) {
|
2007-06-04 23:28:33 +00:00
|
|
|
BasicBlock* first = BB->getTerminator()->getSuccessor(0);
|
|
|
|
anticOut.insert(anticipatedIn[first].begin(),
|
|
|
|
anticipatedIn[first].end());
|
|
|
|
for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
|
2007-05-29 21:53:49 +00:00
|
|
|
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
|
2007-06-04 23:28:33 +00:00
|
|
|
std::set<Value*, ExprLT>& succAnticIn = anticipatedIn[currSucc];
|
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT> temp;
|
2007-06-04 23:28:33 +00:00
|
|
|
std::insert_iterator<std::set<Value*, ExprLT> > temp_ins(temp,
|
|
|
|
temp.begin());
|
|
|
|
std::set_intersection(anticOut.begin(), anticOut.end(),
|
|
|
|
succAnticIn.begin(), succAnticIn.end(),
|
|
|
|
temp_ins, ExprLT());
|
|
|
|
|
|
|
|
anticOut.clear();
|
|
|
|
anticOut.insert(temp.begin(), temp.end());
|
2007-05-29 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "ANTIC_OUT: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(anticOut);
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
2007-06-03 05:55:58 +00:00
|
|
|
std::set<Value*, ExprLT> S;
|
|
|
|
std::insert_iterator<std::set<Value*, ExprLT> > s_ins(S, S.begin());
|
2007-05-29 21:53:49 +00:00
|
|
|
std::set_union(anticOut.begin(), anticOut.end(),
|
|
|
|
generatedExpressions[BB].begin(),
|
|
|
|
generatedExpressions[BB].end(),
|
2007-06-03 05:55:58 +00:00
|
|
|
s_ins, ExprLT());
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
anticIn.clear();
|
2007-06-04 23:34:56 +00:00
|
|
|
|
|
|
|
for (std::set<Value*, ExprLT>::iterator I = S.begin(), E = S.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (generatedTemporaries[BB].find(*I) == generatedTemporaries[BB].end())
|
|
|
|
anticIn.insert(*I);
|
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
clean(anticIn);
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "ANTIC_IN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(anticIn);
|
2007-06-04 23:28:33 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
2007-06-05 23:46:12 +00:00
|
|
|
if (old.size() != anticIn.size())
|
2007-05-29 21:53:49 +00:00
|
|
|
changed = true;
|
|
|
|
|
|
|
|
anticOut.clear();
|
|
|
|
}
|
2007-06-04 18:05:26 +00:00
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
iterations++;
|
|
|
|
}
|
|
|
|
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "Iterations: " << iterations << "\n";
|
2007-05-29 21:53:49 +00:00
|
|
|
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "Name: " << I->getName().c_str() << "\n";
|
|
|
|
|
|
|
|
DOUT << "TMP_GEN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump(generatedTemporaries[I]);
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
|
|
|
DOUT << "EXP_GEN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(generatedExpressions[I]);
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
|
|
|
|
DOUT << "ANTIC_IN: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(anticipatedIn[I]);
|
2007-05-29 23:15:21 +00:00
|
|
|
DOUT << "\n";
|
2007-06-03 05:55:58 +00:00
|
|
|
|
|
|
|
DOUT << "AVAIL_OUT: ";
|
2007-06-08 01:03:01 +00:00
|
|
|
dump_unique(availableOut[I]);
|
2007-06-03 05:55:58 +00:00
|
|
|
DOUT << "\n";
|
2007-05-29 23:15:21 +00:00
|
|
|
}
|
2007-05-29 21:53:49 +00:00
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
|
|
|
|
// Phase 2: Insert
|
2007-06-08 01:03:01 +00:00
|
|
|
DOUT<< "\nPhase 2: Insertion\n";
|
|
|
|
|
|
|
|
std::map<BasicBlock*, std::set<Value*, ExprLT> > new_sets;
|
|
|
|
unsigned i_iterations = 0;
|
|
|
|
bool new_stuff = true;
|
|
|
|
while (new_stuff) {
|
|
|
|
new_stuff = false;
|
|
|
|
DOUT << "Iteration: " << i_iterations << "\n\n";
|
|
|
|
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-06-08 01:03:01 +00:00
|
|
|
std::set<Value*, ExprLT>& new_set = new_sets[BB];
|
|
|
|
std::set<Value*, ExprLT>& availOut = availableOut[BB];
|
|
|
|
std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
|
|
|
|
|
2007-06-08 20:44:02 +00:00
|
|
|
new_set.clear();
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
// Replace leaders with leaders inherited from dominator
|
|
|
|
if (DI->getIDom() != 0) {
|
|
|
|
std::set<Value*, ExprLT>& dom_set = new_sets[DI->getIDom()->getBlock()];
|
|
|
|
for (std::set<Value*, ExprLT>::iterator I = dom_set.begin(),
|
|
|
|
E = dom_set.end(); I != E; ++I) {
|
|
|
|
new_set.insert(*I);
|
|
|
|
|
2007-06-08 20:44:02 +00:00
|
|
|
Value* val = find_leader(availOut, *I);
|
|
|
|
while (val != 0) {
|
2007-06-08 01:52:45 +00:00
|
|
|
availOut.erase(val);
|
2007-06-08 20:44:02 +00:00
|
|
|
val = find_leader(availOut, *I);
|
|
|
|
}
|
2007-06-08 01:52:45 +00:00
|
|
|
availOut.insert(*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)) {
|
|
|
|
std::vector<Value*> workList;
|
|
|
|
topo_sort(anticIn, workList);
|
|
|
|
|
|
|
|
DOUT << "Merge Block: " << BB->getName() << "\n";
|
|
|
|
DOUT << "ANTIC_IN: ";
|
|
|
|
dump_unique(anticIn);
|
|
|
|
DOUT << "\n";
|
|
|
|
|
2007-06-12 00:50:47 +00:00
|
|
|
for (unsigned i = 0; i < workList.size(); ++i) {
|
|
|
|
Value* e = workList[i];
|
2007-06-08 01:03:01 +00:00
|
|
|
|
2007-06-09 18:35:31 +00:00
|
|
|
if (isa<BinaryOperator>(e) || isa<CmpInst>(e)) {
|
2007-06-08 01:03:01 +00:00
|
|
|
if (find_leader(availableOut[DI->getIDom()->getBlock()], 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) {
|
2007-06-12 00:50:47 +00:00
|
|
|
Value *e2 = phi_translate(anticIn, e, *PI, BB);
|
2007-06-08 01:03:01 +00:00
|
|
|
Value *e3 = find_leader(availableOut[*PI], 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))) {
|
|
|
|
DOUT << "Processing Value: ";
|
|
|
|
DEBUG(e->dump());
|
|
|
|
DOUT << "\n\n";
|
|
|
|
|
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
Value* e2 = avail[*PI];
|
|
|
|
if (!find_leader(availableOut[*PI], e2)) {
|
2007-06-09 18:35:31 +00:00
|
|
|
User* U = cast<User>(e2);
|
2007-06-08 01:03:01 +00:00
|
|
|
|
|
|
|
Value* s1 = 0;
|
2007-06-09 18:35:31 +00:00
|
|
|
if (isa<Instruction>(U->getOperand(0)))
|
2007-06-12 00:50:47 +00:00
|
|
|
s1 = find_leader(availableOut[*PI],
|
|
|
|
phi_translate(availableOut[*PI],
|
|
|
|
U->getOperand(0),
|
|
|
|
*PI, BB)
|
|
|
|
);
|
2007-06-08 01:03:01 +00:00
|
|
|
else
|
2007-06-09 18:35:31 +00:00
|
|
|
s1 = U->getOperand(0);
|
2007-06-08 01:03:01 +00:00
|
|
|
|
|
|
|
Value* s2 = 0;
|
2007-06-09 18:35:31 +00:00
|
|
|
if (isa<Instruction>(U->getOperand(1)))
|
2007-06-12 00:50:47 +00:00
|
|
|
s2 = find_leader(availableOut[*PI],
|
|
|
|
phi_translate(availableOut[*PI],
|
|
|
|
U->getOperand(1),
|
|
|
|
*PI, BB)
|
|
|
|
);
|
2007-06-08 01:03:01 +00:00
|
|
|
else
|
2007-06-09 18:35:31 +00:00
|
|
|
s2 = U->getOperand(1);
|
2007-06-08 01:03:01 +00:00
|
|
|
|
2007-06-09 18:35:31 +00:00
|
|
|
Value* newVal = 0;
|
|
|
|
if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
|
|
|
|
newVal = BinaryOperator::create(BO->getOpcode(),
|
2007-06-08 01:03:01 +00:00
|
|
|
s1, s2,
|
|
|
|
BO->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
2007-06-09 18:35:31 +00:00
|
|
|
else if (CmpInst* C = dyn_cast<CmpInst>(U))
|
|
|
|
newVal = CmpInst::create(C->getOpcode(),
|
|
|
|
C->getPredicate(),
|
|
|
|
s1, s2,
|
|
|
|
C->getName()+".gvnpre",
|
|
|
|
(*PI)->getTerminator());
|
|
|
|
|
|
|
|
add(newVal, VN[U]);
|
2007-06-08 20:44:02 +00:00
|
|
|
|
|
|
|
std::set<Value*, ExprLT>& predAvail = availableOut[*PI];
|
|
|
|
Value* val = find_leader(predAvail, newVal);
|
|
|
|
while (val != 0) {
|
|
|
|
predAvail.erase(val);
|
|
|
|
val = find_leader(predAvail, newVal);
|
|
|
|
}
|
|
|
|
predAvail.insert(newVal);
|
2007-06-08 01:03:01 +00:00
|
|
|
|
|
|
|
DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
|
|
|
|
|
|
|
|
std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
|
|
|
|
if (av != avail.end())
|
|
|
|
avail.erase(av);
|
|
|
|
avail.insert(std::make_pair(*PI, newVal));
|
2007-06-08 22:02:36 +00:00
|
|
|
|
|
|
|
++NumInsertedVals;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
add(p, VN[e]);
|
|
|
|
DOUT << "Creating value: " << std::hex << p << std::dec << "\n";
|
|
|
|
|
2007-06-08 20:44:02 +00:00
|
|
|
Value* val = find_leader(availOut, p);
|
|
|
|
while (val != 0) {
|
|
|
|
availOut.erase(val);
|
|
|
|
val = find_leader(availOut, p);
|
|
|
|
}
|
2007-06-08 01:03:01 +00:00
|
|
|
availOut.insert(p);
|
2007-06-08 20:44:02 +00:00
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
new_stuff = true;
|
|
|
|
|
|
|
|
DOUT << "Preds After Processing: ";
|
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
PI != PE; ++PI)
|
|
|
|
DEBUG((*PI)->dump());
|
|
|
|
DOUT << "\n\n";
|
|
|
|
|
|
|
|
DOUT << "Merge Block After Processing: ";
|
|
|
|
DEBUG(BB->dump());
|
|
|
|
DOUT << "\n\n";
|
|
|
|
|
|
|
|
new_set.insert(p);
|
2007-06-08 22:02:36 +00:00
|
|
|
|
|
|
|
++NumInsertedPhis;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i_iterations++;
|
|
|
|
}
|
2007-06-06 01:27:49 +00:00
|
|
|
|
|
|
|
// Phase 3: Eliminate
|
2007-06-08 20:44:02 +00:00
|
|
|
DOUT << "\n\nPhase 3: Elimination\n\n";
|
|
|
|
|
|
|
|
std::vector<std::pair<Instruction*, Value*> > replace;
|
|
|
|
std::vector<Instruction*> erase;
|
|
|
|
|
2007-06-06 01:27:49 +00:00
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
BasicBlock* BB = DI->getBlock();
|
|
|
|
|
2007-06-08 20:44:02 +00:00
|
|
|
DOUT << "Block: " << BB->getName() << "\n";
|
|
|
|
dump_unique(availableOut[BB]);
|
|
|
|
DOUT << "\n\n";
|
2007-06-06 01:27:49 +00:00
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE; ++BI) {
|
2007-06-08 20:44:02 +00:00
|
|
|
|
2007-06-09 18:35:31 +00:00
|
|
|
if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI)) {
|
2007-06-08 20:44:02 +00:00
|
|
|
Value *leader = find_leader(availableOut[BB], BI);
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
if (leader != 0)
|
|
|
|
if (Instruction* Instr = dyn_cast<Instruction>(leader))
|
|
|
|
if (Instr->getParent() != 0 && Instr != BI) {
|
2007-06-08 20:44:02 +00:00
|
|
|
replace.push_back(std::make_pair(BI, leader));
|
2007-06-08 01:03:01 +00:00
|
|
|
erase.push_back(BI);
|
2007-06-08 22:02:36 +00:00
|
|
|
++NumEliminated;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-06 01:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 20:44:02 +00:00
|
|
|
while (!replace.empty()) {
|
|
|
|
std::pair<Instruction*, Value*> rep = replace.back();
|
|
|
|
replace.pop_back();
|
|
|
|
rep.first->replaceAllUsesWith(rep.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
|
|
|
|
I != E; ++I)
|
|
|
|
(*I)->eraseFromParent();
|
|
|
|
|
2007-06-08 01:03:01 +00:00
|
|
|
// Phase 4: Cleanup
|
2007-06-12 00:50:47 +00:00
|
|
|
while (!createdExpressions.empty()) {
|
|
|
|
Instruction* I = createdExpressions.back();
|
|
|
|
createdExpressions.pop_back();
|
|
|
|
|
|
|
|
delete I;
|
2007-06-08 01:03:01 +00:00
|
|
|
}
|
|
|
|
|
2007-05-29 21:53:49 +00:00
|
|
|
return false;
|
|
|
|
}
|