* Document instance vars better

* Fuse two parallel loops
* Use a more specific type for AllocaLookup


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8859 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2003-10-05 03:16:07 +00:00
parent 9157f041ab
commit 9e38fbf57f

View File

@@ -51,14 +51,20 @@ bool isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) {
namespace { namespace {
struct PromoteMem2Reg { struct PromoteMem2Reg {
const std::vector<AllocaInst*> &Allocas; // the alloca instructions.. // Allocas - The alloca instructions being promoted
std::vector<unsigned> VersionNumbers; // Current version counters const std::vector<AllocaInst*> &Allocas;
DominanceFrontier &DF; DominanceFrontier &DF;
const TargetData &TD; const TargetData &TD;
std::map<Instruction*, unsigned> AllocaLookup; // reverse mapping of above // AllocaLookup - Reverse mapping of Allocas
std::map<AllocaInst*, unsigned> AllocaLookup;
// VersionNumbers - Current version counters for each alloca
std::vector<unsigned> VersionNumbers;
std::vector<std::vector<BasicBlock*> > PhiNodes;// Idx corresponds 2 Allocas // PhiNodes - Each alloca contains a list of basic blocks which contain PHI
// nodes for the alloca.
std::vector<std::vector<BasicBlock*> > PhiNodes;
// NewPhiNodes - The PhiNodes we're adding. // NewPhiNodes - The PhiNodes we're adding.
std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes; std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes;
@@ -84,26 +90,25 @@ void PromoteMem2Reg::run() {
Function &F = *DF.getRoot()->getParent(); Function &F = *DF.getRoot()->getParent();
VersionNumbers.resize(Allocas.size()); VersionNumbers.resize(Allocas.size());
PhiNodes.resize(Allocas.size());
for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { for (unsigned i = 0; i != Allocas.size(); ++i) {
assert(isAllocaPromotable(Allocas[i], TD) && AllocaInst *AI = Allocas[i];
assert(isAllocaPromotable(AI, TD) &&
"Cannot promote non-promotable alloca!"); "Cannot promote non-promotable alloca!");
assert(Allocas[i]->getParent()->getParent() == &F && assert(Allocas[i]->getParent()->getParent() == &F &&
"All allocas should be in the same function, which is same as DF!"); "All allocas should be in the same function, which is same as DF!");
AllocaLookup[Allocas[i]] = i;
}
PhiNodes.resize(Allocas.size());
for (unsigned i = 0; i != Allocas.size(); ++i) {
AllocaInst *AI = Allocas[i];
// Calculate the set of write-locations for each alloca. This is analogous // Calculate the set of write-locations for each alloca. This is analogous
// to counting the number of 'redefinitions' of each variable. // to counting the number of 'redefinitions' of each variable.
std::vector<BasicBlock*> WriteSets; std::vector<BasicBlock*> WriteSets;
for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E; ++U) for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E; ++U)
if (StoreInst *SI = dyn_cast<StoreInst>(*U)) if (StoreInst *SI = dyn_cast<StoreInst>(cast<Instruction>(*U)))
// jot down the basic-block it came from // jot down the basic-block it came from
WriteSets.push_back(SI->getParent()); WriteSets.push_back(SI->getParent());
AllocaLookup[Allocas[i]] = i;
// Compute the locations where PhiNodes need to be inserted. Look at the // Compute the locations where PhiNodes need to be inserted. Look at the
// dominance frontier of EACH basic-block we have a write in. // dominance frontier of EACH basic-block we have a write in.
@@ -120,12 +125,13 @@ void PromoteMem2Reg::run() {
} }
// Perform iterative step // Perform iterative step
for (unsigned k = 0; k != PhiNodes[i].size(); k++) { std::vector<BasicBlock*> &AllocaPhiNodes = PhiNodes[i];
DominanceFrontier::const_iterator it = DF.find(PhiNodes[i][k]); for (unsigned k = 0; k != AllocaPhiNodes.size(); k++) {
DominanceFrontier::const_iterator it = DF.find(AllocaPhiNodes[k]);
if (it != DF.end()) { if (it != DF.end()) {
const DominanceFrontier::DomSetType &S = it->second; const DominanceFrontier::DomSetType &S = it->second;
for (DominanceFrontier::DomSetType::iterator P = S.begin(),PE = S.end(); for (DominanceFrontier::DomSetType::iterator
P != PE; ++P) P = S.begin(), PE = S.end(); P != PE; ++P)
QueuePhiNode(*P, i); QueuePhiNode(*P, i);
} }
} }
@@ -231,7 +237,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
if (LoadInst *LI = dyn_cast<LoadInst>(I)) { if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) { if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
std::map<Instruction*, unsigned>::iterator AI = AllocaLookup.find(Src); std::map<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src);
if (AI != AllocaLookup.end()) { if (AI != AllocaLookup.end()) {
Value *V = IncomingVals[AI->second]; Value *V = IncomingVals[AI->second];
@@ -244,7 +250,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
// Delete this instruction and mark the name as the current holder of the // Delete this instruction and mark the name as the current holder of the
// value // value
if (AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand())) { if (AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand())) {
std::map<Instruction *, unsigned>::iterator ai =AllocaLookup.find(Dest); std::map<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
if (ai != AllocaLookup.end()) { if (ai != AllocaLookup.end()) {
// what value were we writing? // what value were we writing?
IncomingVals[ai->second] = SI->getOperand(0); IncomingVals[ai->second] = SI->getOperand(0);