Do away with the strange use of BitVectors in SSI, and just use normal sets. This makes the code much more C++/LLVM-ish.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83286 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-10-04 18:49:55 +00:00
parent b44fad7f95
commit 08993acada
2 changed files with 94 additions and 128 deletions

View File

@ -23,7 +23,6 @@
#define LLVM_TRANSFORMS_UTILS_SSI_H #define LLVM_TRANSFORMS_UTILS_SSI_H
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
@ -55,43 +54,36 @@ namespace llvm {
// Stores variables created by SSI // Stores variables created by SSI
SmallPtrSet<Instruction *, 16> created; SmallPtrSet<Instruction *, 16> created;
// These variables are only live for each creation
unsigned num_values;
// Has a bit for each variable, true if it needs to be created
// and false otherwise
BitVector needConstruction;
// Phis created by SSI // Phis created by SSI
DenseMap<PHINode *, unsigned> phis; DenseMap<PHINode *, Instruction*> phis;
// Sigmas created by SSI // Sigmas created by SSI
DenseMap<PHINode *, unsigned> sigmas; DenseMap<PHINode *, Instruction*> sigmas;
// Phi nodes that have a phi as operand and has to be fixed // Phi nodes that have a phi as operand and has to be fixed
SmallPtrSet<PHINode *, 1> phisToFix; SmallPtrSet<PHINode *, 1> phisToFix;
// List of definition points for every variable // List of definition points for every variable
SmallVector<SmallVector<BasicBlock *, 1>, 0> defsites; DenseMap<Instruction*, SmallVector<BasicBlock*, 4> > defsites;
// Basic Block of the original definition of each variable // Basic Block of the original definition of each variable
SmallVector<BasicBlock *, 0> value_original; DenseMap<Instruction*, BasicBlock*> value_original;
// Stack of last seen definition of a variable // Stack of last seen definition of a variable
SmallVector<SmallVector<Instruction *, 1>, 0> value_stack; DenseMap<Instruction*, SmallVector<Instruction *, 1> > value_stack;
void insertSigmaFunctions(SmallVectorImpl<Instruction *> &value); void insertSigmaFunctions(SmallPtrSet<Instruction*, 4> &value);
void insertSigma(TerminatorInst *TI, Instruction *I, unsigned pos); void insertSigma(TerminatorInst *TI, Instruction *I);
void insertPhiFunctions(SmallVectorImpl<Instruction *> &value); void insertPhiFunctions(SmallPtrSet<Instruction*, 4> &value);
void renameInit(SmallVectorImpl<Instruction *> &value); void renameInit(SmallPtrSet<Instruction*, 4> &value);
void rename(BasicBlock *BB); void rename(BasicBlock *BB);
void substituteUse(Instruction *I); void substituteUse(Instruction *I);
bool dominateAny(BasicBlock *BB, Instruction *value); bool dominateAny(BasicBlock *BB, Instruction *value);
void fixPhis(); void fixPhis();
unsigned getPositionPhi(PHINode *PN); Instruction* getPositionPhi(PHINode *PN);
unsigned getPositionSigma(PHINode *PN); Instruction* getPositionSigma(PHINode *PN);
void init(SmallVectorImpl<Instruction *> &value); void init(SmallVectorImpl<Instruction *> &value);
void clean(); void clean();

View File

@ -31,8 +31,6 @@ using namespace llvm;
static const std::string SSI_PHI = "SSI_phi"; static const std::string SSI_PHI = "SSI_phi";
static const std::string SSI_SIG = "SSI_sigma"; static const std::string SSI_SIG = "SSI_sigma";
static const unsigned UNSIGNED_INFINITE = ~0U;
STATISTIC(NumSigmaInserted, "Number of sigma functions inserted"); STATISTIC(NumSigmaInserted, "Number of sigma functions inserted");
STATISTIC(NumPhiInserted, "Number of phi functions inserted"); STATISTIC(NumPhiInserted, "Number of phi functions inserted");
@ -54,17 +52,18 @@ bool SSI::runOnFunction(Function &F) {
void SSI::createSSI(SmallVectorImpl<Instruction *> &value) { void SSI::createSSI(SmallVectorImpl<Instruction *> &value) {
init(value); init(value);
for (unsigned i = 0; i < num_values; ++i) { SmallPtrSet<Instruction*, 4> needConstruction;
if (created.insert(value[i])) { for (SmallVectorImpl<Instruction*>::iterator I = value.begin(),
needConstruction[i] = true; E = value.end(); I != E; ++I)
} if (created.insert(*I))
} needConstruction.insert(*I);
insertSigmaFunctions(value);
insertSigmaFunctions(needConstruction);
// Test if there is a need to transform to SSI // Test if there is a need to transform to SSI
if (needConstruction.any()) { if (!needConstruction.empty()) {
insertPhiFunctions(value); insertPhiFunctions(needConstruction);
renameInit(value); renameInit(needConstruction);
rename(DT_->getRoot()); rename(DT_->getRoot());
fixPhis(); fixPhis();
} }
@ -75,21 +74,19 @@ void SSI::createSSI(SmallVectorImpl<Instruction *> &value) {
/// Insert sigma functions (a sigma function is a phi function with one /// Insert sigma functions (a sigma function is a phi function with one
/// operator) /// operator)
/// ///
void SSI::insertSigmaFunctions(SmallVectorImpl<Instruction *> &value) { void SSI::insertSigmaFunctions(SmallPtrSet<Instruction*, 4> &value) {
for (unsigned i = 0; i < num_values; ++i) { for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
if (!needConstruction[i]) E = value.end(); I != E; ++I) {
continue; for (Value::use_iterator begin = (*I)->use_begin(),
end = (*I)->use_end(); begin != end; ++begin) {
for (Value::use_iterator begin = value[i]->use_begin(), end =
value[i]->use_end(); begin != end; ++begin) {
// Test if the Use of the Value is in a comparator // Test if the Use of the Value is in a comparator
if (CmpInst *CI = dyn_cast<CmpInst>(begin)) { if (CmpInst *CI = dyn_cast<CmpInst>(begin)) {
// Iterates through all uses of CmpInst // Iterates through all uses of CmpInst
for (Value::use_iterator begin_ci = CI->use_begin(), end_ci = for (Value::use_iterator begin_ci = CI->use_begin(),
CI->use_end(); begin_ci != end_ci; ++begin_ci) { end_ci = CI->use_end(); begin_ci != end_ci; ++begin_ci) {
// Test if any use of CmpInst is in a Terminator // Test if any use of CmpInst is in a Terminator
if (TerminatorInst *TI = dyn_cast<TerminatorInst>(begin_ci)) { if (TerminatorInst *TI = dyn_cast<TerminatorInst>(begin_ci)) {
insertSigma(TI, value[i], i); insertSigma(TI, *I);
} }
} }
} }
@ -100,7 +97,7 @@ void SSI::insertSigmaFunctions(SmallVectorImpl<Instruction *> &value) {
/// Inserts Sigma Functions in every BasicBlock successor to Terminator /// Inserts Sigma Functions in every BasicBlock successor to Terminator
/// Instruction TI. All inserted Sigma Function are related to Instruction I. /// Instruction TI. All inserted Sigma Function are related to Instruction I.
/// ///
void SSI::insertSigma(TerminatorInst *TI, Instruction *I, unsigned pos) { void SSI::insertSigma(TerminatorInst *TI, Instruction *I) {
// Basic Block of the Terminator Instruction // Basic Block of the Terminator Instruction
BasicBlock *BB = TI->getParent(); BasicBlock *BB = TI->getParent();
for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) { for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
@ -111,10 +108,9 @@ void SSI::insertSigma(TerminatorInst *TI, Instruction *I, unsigned pos) {
dominateAny(BB_next, I)) { dominateAny(BB_next, I)) {
PHINode *PN = PHINode::Create(I->getType(), SSI_SIG, BB_next->begin()); PHINode *PN = PHINode::Create(I->getType(), SSI_SIG, BB_next->begin());
PN->addIncoming(I, BB); PN->addIncoming(I, BB);
sigmas.insert(std::make_pair(PN, pos)); sigmas[PN] = I;
created.insert(PN); created.insert(PN);
needConstruction[pos] = true; defsites[I].push_back(BB_next);
defsites[pos].push_back(BB_next);
++NumSigmaInserted; ++NumSigmaInserted;
} }
} }
@ -122,66 +118,63 @@ void SSI::insertSigma(TerminatorInst *TI, Instruction *I, unsigned pos) {
/// Insert phi functions when necessary /// Insert phi functions when necessary
/// ///
void SSI::insertPhiFunctions(SmallVectorImpl<Instruction *> &value) { void SSI::insertPhiFunctions(SmallPtrSet<Instruction*, 4> &value) {
DominanceFrontier *DF = &getAnalysis<DominanceFrontier>(); DominanceFrontier *DF = &getAnalysis<DominanceFrontier>();
for (unsigned i = 0; i < num_values; ++i) { for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
E = value.end(); I != E; ++I) {
// Test if there were any sigmas for this variable // Test if there were any sigmas for this variable
if (needConstruction[i]) { SmallPtrSet<BasicBlock *, 16> BB_visited;
SmallPtrSet<BasicBlock *, 16> BB_visited; // Insert phi functions if there is any sigma function
while (!defsites[*I].empty()) {
// Insert phi functions if there is any sigma function BasicBlock *BB = defsites[*I].back();
while (!defsites[i].empty()) {
BasicBlock *BB = defsites[i].back(); defsites[*I].pop_back();
DominanceFrontier::iterator DF_BB = DF->find(BB);
defsites[i].pop_back(); // The BB is unreachable. Skip it.
DominanceFrontier::iterator DF_BB = DF->find(BB); if (DF_BB == DF->end())
continue;
// The BB is unreachable. Skip it. // Iterates through all the dominance frontier of BB
if (DF_BB == DF->end()) for (std::set<BasicBlock *>::iterator DF_BB_begin =
continue; DF_BB->second.begin(), DF_BB_end = DF_BB->second.end();
DF_BB_begin != DF_BB_end; ++DF_BB_begin) {
BasicBlock *BB_dominated = *DF_BB_begin;
// Iterates through all the dominance frontier of BB // Test if has not yet visited this node and if the
for (std::set<BasicBlock *>::iterator DF_BB_begin = // original definition dominates this node
DF_BB->second.begin(), DF_BB_end = DF_BB->second.end(); if (BB_visited.insert(BB_dominated) &&
DF_BB_begin != DF_BB_end; ++DF_BB_begin) { DT_->properlyDominates(value_original[*I], BB_dominated) &&
BasicBlock *BB_dominated = *DF_BB_begin; dominateAny(BB_dominated, *I)) {
PHINode *PN = PHINode::Create(
(*I)->getType(), SSI_PHI, BB_dominated->begin());
phis.insert(std::make_pair(PN, *I));
created.insert(PN);
// Test if has not yet visited this node and if the defsites[*I].push_back(BB_dominated);
// original definition dominates this node ++NumPhiInserted;
if (BB_visited.insert(BB_dominated) &&
DT_->properlyDominates(value_original[i], BB_dominated) &&
dominateAny(BB_dominated, value[i])) {
PHINode *PN = PHINode::Create(
value[i]->getType(), SSI_PHI, BB_dominated->begin());
phis.insert(std::make_pair(PN, i));
created.insert(PN);
defsites[i].push_back(BB_dominated);
++NumPhiInserted;
}
} }
} }
BB_visited.clear();
} }
BB_visited.clear();
} }
} }
/// Some initialization for the rename part /// Some initialization for the rename part
/// ///
void SSI::renameInit(SmallVectorImpl<Instruction *> &value) { void SSI::renameInit(SmallPtrSet<Instruction*, 4> &value) {
value_stack.resize(num_values); for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
for (unsigned i = 0; i < num_values; ++i) { E = value.end(); I != E; ++I)
value_stack[i].push_back(value[i]); value_stack[*I].push_back(*I);
}
} }
/// Renames all variables in the specified BasicBlock. /// Renames all variables in the specified BasicBlock.
/// Only variables that need to be rename will be. /// Only variables that need to be rename will be.
/// ///
void SSI::rename(BasicBlock *BB) { void SSI::rename(BasicBlock *BB) {
BitVector *defined = new BitVector(num_values, false); SmallPtrSet<Instruction*, 8> defined;
// Iterate through instructions and make appropriate renaming. // Iterate through instructions and make appropriate renaming.
// For SSI_PHI (b = PHI()), store b at value_stack as a new // For SSI_PHI (b = PHI()), store b at value_stack as a new
@ -195,19 +188,17 @@ void SSI::rename(BasicBlock *BB) {
begin != end; ++begin) { begin != end; ++begin) {
Instruction *I = begin; Instruction *I = begin;
if (PHINode *PN = dyn_cast<PHINode>(I)) { // Treat PHI functions if (PHINode *PN = dyn_cast<PHINode>(I)) { // Treat PHI functions
int position; Instruction* position;
// Treat SSI_PHI // Treat SSI_PHI
if ((position = getPositionPhi(PN)) != -1) { if ((position = getPositionPhi(PN))) {
value_stack[position].push_back(PN); value_stack[position].push_back(PN);
(*defined)[position] = true; defined.insert(position);
}
// Treat SSI_SIG // Treat SSI_SIG
else if ((position = getPositionSigma(PN)) != -1) { } else if ((position = getPositionSigma(PN))) {
substituteUse(I); substituteUse(I);
value_stack[position].push_back(PN); value_stack[position].push_back(PN);
(*defined)[position] = true; defined.insert(position);
} }
// Treat all other PHI functions // Treat all other PHI functions
@ -234,8 +225,8 @@ void SSI::rename(BasicBlock *BB) {
notPhi = BB_succ->getFirstNonPHI(); begin != *notPhi; ++begin) { notPhi = BB_succ->getFirstNonPHI(); begin != *notPhi; ++begin) {
Instruction *I = begin; Instruction *I = begin;
PHINode *PN = dyn_cast<PHINode>(I); PHINode *PN = dyn_cast<PHINode>(I);
int position; Instruction* position;
if (PN && ((position = getPositionPhi(PN)) != -1)) { if (PN && ((position = getPositionPhi(PN)))) {
PN->addIncoming(value_stack[position].back(), BB); PN->addIncoming(value_stack[position].back(), BB);
} }
} }
@ -253,15 +244,9 @@ void SSI::rename(BasicBlock *BB) {
// Now we remove all inserted definitions of a variable from the top of // Now we remove all inserted definitions of a variable from the top of
// the stack leaving the previous one as the top. // the stack leaving the previous one as the top.
if (defined->any()) { for (SmallPtrSet<Instruction*, 8>::iterator DI = defined.begin(),
for (unsigned i = 0; i < num_values; ++i) { DE = defined.end(); DI != DE; ++DI)
if ((*defined)[i]) { value_stack[*DI].pop_back();
value_stack[i].pop_back();
}
}
}
delete defined;
} }
/// Substitute any use in this instruction for the last definition of /// Substitute any use in this instruction for the last definition of
@ -270,23 +255,24 @@ void SSI::rename(BasicBlock *BB) {
void SSI::substituteUse(Instruction *I) { void SSI::substituteUse(Instruction *I) {
for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) { for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) {
Value *operand = I->getOperand(i); Value *operand = I->getOperand(i);
for (unsigned j = 0; j < num_values; ++j) { for (DenseMap<Instruction*, SmallVector<Instruction*, 1> >::iterator
if (operand == value_stack[j].front() && VI = value_stack.begin(), VE = value_stack.end(); VI != VE; ++VI) {
I != value_stack[j].back()) { if (operand == VI->second.front() &&
I != VI->second.back()) {
PHINode *PN_I = dyn_cast<PHINode>(I); PHINode *PN_I = dyn_cast<PHINode>(I);
PHINode *PN_vs = dyn_cast<PHINode>(value_stack[j].back()); PHINode *PN_vs = dyn_cast<PHINode>(VI->second.back());
// If a phi created in a BasicBlock is used as an operand of another // If a phi created in a BasicBlock is used as an operand of another
// created in the same BasicBlock, this step marks this second phi, // created in the same BasicBlock, this step marks this second phi,
// to fix this issue later. It cannot be fixed now, because the // to fix this issue later. It cannot be fixed now, because the
// operands of the first phi are not final yet. // operands of the first phi are not final yet.
if (PN_I && PN_vs && if (PN_I && PN_vs &&
value_stack[j].back()->getParent() == I->getParent()) { VI->second.back()->getParent() == I->getParent()) {
phisToFix.insert(PN_I); phisToFix.insert(PN_I);
} }
I->setOperand(i, value_stack[j].back()); I->setOperand(i, VI->second.back());
break; break;
} }
} }
@ -333,7 +319,7 @@ void SSI::fixPhis() {
} }
} }
for (DenseMapIterator<PHINode *, unsigned> begin = phis.begin(), for (DenseMapIterator<PHINode *, Instruction*> begin = phis.begin(),
end = phis.end(); begin != end; ++begin) { end = phis.end(); begin != end; ++begin) {
PHINode *PN = begin->first; PHINode *PN = begin->first;
BasicBlock *BB = PN->getParent(); BasicBlock *BB = PN->getParent();
@ -359,10 +345,10 @@ void SSI::fixPhis() {
/// Return which variable (position on the vector of variables) this phi /// Return which variable (position on the vector of variables) this phi
/// represents on the phis list. /// represents on the phis list.
/// ///
unsigned SSI::getPositionPhi(PHINode *PN) { Instruction* SSI::getPositionPhi(PHINode *PN) {
DenseMap<PHINode *, unsigned>::iterator val = phis.find(PN); DenseMap<PHINode *, Instruction*>::iterator val = phis.find(PN);
if (val == phis.end()) if (val == phis.end())
return UNSIGNED_INFINITE; return 0;
else else
return val->second; return val->second;
} }
@ -370,10 +356,10 @@ unsigned SSI::getPositionPhi(PHINode *PN) {
/// Return which variable (position on the vector of variables) this phi /// Return which variable (position on the vector of variables) this phi
/// represents on the sigmas list. /// represents on the sigmas list.
/// ///
unsigned SSI::getPositionSigma(PHINode *PN) { Instruction* SSI::getPositionSigma(PHINode *PN) {
DenseMap<PHINode *, unsigned>::iterator val = sigmas.find(PN); DenseMap<PHINode *, Instruction*>::iterator val = sigmas.find(PN);
if (val == sigmas.end()) if (val == sigmas.end())
return UNSIGNED_INFINITE; return 0;
else else
return val->second; return val->second;
} }
@ -381,27 +367,16 @@ unsigned SSI::getPositionSigma(PHINode *PN) {
/// Initializes /// Initializes
/// ///
void SSI::init(SmallVectorImpl<Instruction *> &value) { void SSI::init(SmallVectorImpl<Instruction *> &value) {
num_values = value.size(); for (SmallVectorImpl<Instruction *>::iterator I = value.begin(),
needConstruction.resize(num_values, false); E = value.end(); I != E; ++I) {
value_original[*I] = (*I)->getParent();
value_original.resize(num_values); defsites[*I].push_back((*I)->getParent());
defsites.resize(num_values);
for (unsigned i = 0; i < num_values; ++i) {
value_original[i] = value[i]->getParent();
defsites[i].push_back(value_original[i]);
} }
} }
/// Clean all used resources in this creation of SSI /// Clean all used resources in this creation of SSI
/// ///
void SSI::clean() { void SSI::clean() {
for (unsigned i = 0; i < num_values; ++i) {
defsites[i].clear();
if (i < value_stack.size())
value_stack[i].clear();
}
phis.clear(); phis.clear();
sigmas.clear(); sigmas.clear();
phisToFix.clear(); phisToFix.clear();
@ -409,7 +384,6 @@ void SSI::clean() {
defsites.clear(); defsites.clear();
value_stack.clear(); value_stack.clear();
value_original.clear(); value_original.clear();
needConstruction.clear();
} }
/// createSSIPass - The public interface to this file... /// createSSIPass - The public interface to this file...