refactor some code to shrink PromoteMem2Reg::run a bit

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40805 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-08-04 01:41:18 +00:00
parent 483ce14bf4
commit bbe104002f

View File

@ -16,6 +16,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "mem2reg"
#include "llvm/Transforms/Utils/PromoteMemToReg.h" #include "llvm/Transforms/Utils/PromoteMemToReg.h"
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
@ -26,12 +27,17 @@
#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"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h" #include "llvm/Support/Compiler.h"
#include <algorithm> #include <algorithm>
using namespace llvm; using namespace llvm;
STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
STATISTIC(NumSingleStore, "Number of alloca's promoted with a single store");
STATISTIC(NumDeadAlloca, "Number of dead alloca's removed");
// Provide DenseMapKeyInfo for all pointers. // Provide DenseMapKeyInfo for all pointers.
namespace llvm { namespace llvm {
template<> template<>
@ -153,6 +159,12 @@ namespace {
} }
private: private:
void RemoveFromAllocasList(unsigned &AllocaIdx) {
Allocas[AllocaIdx] = Allocas.back();
Allocas.pop_back();
--AllocaIdx;
}
void MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum, void MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum,
SmallPtrSet<PHINode*, 16> &DeadPHINodes); SmallPtrSet<PHINode*, 16> &DeadPHINodes);
bool PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI); bool PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI);
@ -166,54 +178,35 @@ namespace {
SmallPtrSet<PHINode*, 16> &InsertedPHINodes); SmallPtrSet<PHINode*, 16> &InsertedPHINodes);
}; };
} // end of anonymous namespace struct AllocaInfo {
void PromoteMem2Reg::run() {
Function &F = *DF.getRoot()->getParent();
// LocallyUsedAllocas - Keep track of all of the alloca instructions which are
// only used in a single basic block. These instructions can be efficiently
// promoted by performing a single linear scan over that one block. Since
// individual basic blocks are sometimes large, we group together all allocas
// that are live in a single basic block by the basic block they are live in.
std::map<BasicBlock*, std::vector<AllocaInst*> > LocallyUsedAllocas;
if (AST) PointerAllocaValues.resize(Allocas.size());
for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
AllocaInst *AI = Allocas[AllocaNum];
assert(isAllocaPromotable(AI) &&
"Cannot promote non-promotable alloca!");
assert(AI->getParent()->getParent() == &F &&
"All allocas should be in the same function, which is same as DF!");
if (AI->use_empty()) {
// If there are no uses of the alloca, just delete it now.
if (AST) AST->deleteValue(AI);
AI->eraseFromParent();
// Remove the alloca from the Allocas list, since it has been processed
Allocas[AllocaNum] = Allocas.back();
Allocas.pop_back();
--AllocaNum;
continue;
}
// Calculate the set of read and write-locations for each alloca. This is
// analogous to finding the 'uses' and 'definitions' of each variable.
std::vector<BasicBlock*> DefiningBlocks; std::vector<BasicBlock*> DefiningBlocks;
std::vector<BasicBlock*> UsingBlocks; std::vector<BasicBlock*> UsingBlocks;
StoreInst *OnlyStore = 0; StoreInst *OnlyStore;
BasicBlock *OnlyBlock = 0; BasicBlock *OnlyBlock;
bool OnlyUsedInOneBlock = true; bool OnlyUsedInOneBlock;
// As we scan the uses of the alloca instruction, keep track of stores, and Value *AllocaPointerVal;
// decide whether all of the loads and stores to the alloca are within the
// same basic block. void clear() {
Value *AllocaPointerVal = 0; DefiningBlocks.clear();
for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E;++U){ UsingBlocks.clear();
OnlyStore = 0;
OnlyBlock = 0;
OnlyUsedInOneBlock = true;
AllocaPointerVal = 0;
}
/// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our
/// ivars.
void AnalyzeAlloca(AllocaInst *AI) {
clear();
// As we scan the uses of the alloca instruction, keep track of stores,
// and decide whether all of the loads and stores to the alloca are within
// the same basic block.
for (Value::use_iterator U = AI->use_begin(), E = AI->use_end();
U != E; ++U){
Instruction *User = cast<Instruction>(*U); Instruction *User = cast<Instruction>(*U);
if (StoreInst *SI = dyn_cast<StoreInst>(User)) { if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// Remember the basic blocks which define new values for the alloca // Remember the basic blocks which define new values for the alloca
@ -234,40 +227,79 @@ void PromoteMem2Reg::run() {
OnlyUsedInOneBlock = false; OnlyUsedInOneBlock = false;
} }
} }
}
};
} // end of anonymous namespace
void PromoteMem2Reg::run() {
Function &F = *DF.getRoot()->getParent();
// LocallyUsedAllocas - Keep track of all of the alloca instructions which are
// only used in a single basic block. These instructions can be efficiently
// promoted by performing a single linear scan over that one block. Since
// individual basic blocks are sometimes large, we group together all allocas
// that are live in a single basic block by the basic block they are live in.
std::map<BasicBlock*, std::vector<AllocaInst*> > LocallyUsedAllocas;
if (AST) PointerAllocaValues.resize(Allocas.size());
AllocaInfo Info;
for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
AllocaInst *AI = Allocas[AllocaNum];
assert(isAllocaPromotable(AI) &&
"Cannot promote non-promotable alloca!");
assert(AI->getParent()->getParent() == &F &&
"All allocas should be in the same function, which is same as DF!");
if (AI->use_empty()) {
// If there are no uses of the alloca, just delete it now.
if (AST) AST->deleteValue(AI);
AI->eraseFromParent();
// Remove the alloca from the Allocas list, since it has been processed
RemoveFromAllocasList(AllocaNum);
++NumDeadAlloca;
continue;
}
// Calculate the set of read and write-locations for each alloca. This is
// analogous to finding the 'uses' and 'definitions' of each variable.
Info.AnalyzeAlloca(AI);
// If the alloca is only read and written in one basic block, just perform a // If the alloca is only read and written in one basic block, just perform a
// linear sweep over the block to eliminate it. // linear sweep over the block to eliminate it.
if (OnlyUsedInOneBlock) { if (Info.OnlyUsedInOneBlock) {
LocallyUsedAllocas[OnlyBlock].push_back(AI); LocallyUsedAllocas[Info.OnlyBlock].push_back(AI);
// Remove the alloca from the Allocas list, since it will be processed. // Remove the alloca from the Allocas list, since it will be processed.
Allocas[AllocaNum] = Allocas.back(); RemoveFromAllocasList(AllocaNum);
Allocas.pop_back();
--AllocaNum;
continue; continue;
} }
// If there is only a single store to this value, replace any loads of // If there is only a single store to this value, replace any loads of
// it that are directly dominated by the definition with the value stored. // it that are directly dominated by the definition with the value stored.
if (DefiningBlocks.size() == 1) { if (Info.DefiningBlocks.size() == 1) {
// Be aware of loads before the store. // Be aware of loads before the store.
std::set<BasicBlock*> ProcessedBlocks; std::set<BasicBlock*> ProcessedBlocks;
for (unsigned i = 0, e = UsingBlocks.size(); i != e; ++i) for (unsigned i = 0, e = Info.UsingBlocks.size(); i != e; ++i)
// If the store dominates the block and if we haven't processed it yet, // If the store dominates the block and if we haven't processed it yet,
// do so now. // do so now.
if (dominates(OnlyStore->getParent(), UsingBlocks[i])) if (dominates(Info.OnlyStore->getParent(), Info.UsingBlocks[i]))
if (ProcessedBlocks.insert(UsingBlocks[i]).second) { if (ProcessedBlocks.insert(Info.UsingBlocks[i]).second) {
BasicBlock *UseBlock = UsingBlocks[i]; BasicBlock *UseBlock = Info.UsingBlocks[i];
// If the use and store are in the same block, do a quick scan to // If the use and store are in the same block, do a quick scan to
// verify that there are no uses before the store. // verify that there are no uses before the store.
if (UseBlock == OnlyStore->getParent()) { if (UseBlock == Info.OnlyStore->getParent()) {
BasicBlock::iterator I = UseBlock->begin(); BasicBlock::iterator I = UseBlock->begin();
for (; &*I != OnlyStore; ++I) { // scan block for store. for (; &*I != Info.OnlyStore; ++I) { // scan block for store.
if (isa<LoadInst>(I) && I->getOperand(0) == AI) if (isa<LoadInst>(I) && I->getOperand(0) == AI)
break; break;
} }
if (&*I != OnlyStore) break; // Do not handle this case. if (&*I != Info.OnlyStore) break; // Do not handle this case.
} }
// Otherwise, if this is a different block or if all uses happen // Otherwise, if this is a different block or if all uses happen
@ -277,7 +309,7 @@ void PromoteMem2Reg::run() {
I != E; ) { I != E; ) {
if (LoadInst *LI = dyn_cast<LoadInst>(I++)) { if (LoadInst *LI = dyn_cast<LoadInst>(I++)) {
if (LI->getOperand(0) == AI) { if (LI->getOperand(0) == AI) {
LI->replaceAllUsesWith(OnlyStore->getOperand(0)); LI->replaceAllUsesWith(Info.OnlyStore->getOperand(0));
if (AST && isa<PointerType>(LI->getType())) if (AST && isa<PointerType>(LI->getType()))
AST->deleteValue(LI); AST->deleteValue(LI);
LI->eraseFromParent(); LI->eraseFromParent();
@ -286,23 +318,22 @@ void PromoteMem2Reg::run() {
} }
// Finally, remove this block from the UsingBlock set. // Finally, remove this block from the UsingBlock set.
UsingBlocks[i] = UsingBlocks.back(); Info.UsingBlocks[i] = Info.UsingBlocks.back();
--i; --e; --i; --e;
} }
// Finally, after the scan, check to see if the store is all that is left. // Finally, after the scan, check to see if the store is all that is left.
if (UsingBlocks.empty()) { if (Info.UsingBlocks.empty()) {
++NumSingleStore;
// The alloca has been processed, move on. // The alloca has been processed, move on.
Allocas[AllocaNum] = Allocas.back(); RemoveFromAllocasList(AllocaNum);
Allocas.pop_back();
--AllocaNum;
continue; continue;
} }
} }
if (AST) if (AST)
PointerAllocaValues[AllocaNum] = AllocaPointerVal; PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal;
// If we haven't computed a numbering for the BB's in the function, do so // If we haven't computed a numbering for the BB's in the function, do so
// now. // now.
@ -318,9 +349,9 @@ void PromoteMem2Reg::run() {
unsigned CurrentVersion = 0; unsigned CurrentVersion = 0;
SmallPtrSet<PHINode*, 16> InsertedPHINodes; SmallPtrSet<PHINode*, 16> InsertedPHINodes;
std::vector<std::pair<unsigned, BasicBlock*> > DFBlocks; std::vector<std::pair<unsigned, BasicBlock*> > DFBlocks;
while (!DefiningBlocks.empty()) { while (!Info.DefiningBlocks.empty()) {
BasicBlock *BB = DefiningBlocks.back(); BasicBlock *BB = Info.DefiningBlocks.back();
DefiningBlocks.pop_back(); Info.DefiningBlocks.pop_back();
// Look up the DF for this write, add it to PhiNodes // Look up the DF for this write, add it to PhiNodes
DominanceFrontier::const_iterator it = DF.find(BB); DominanceFrontier::const_iterator it = DF.find(BB);
@ -342,7 +373,7 @@ void PromoteMem2Reg::run() {
for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) { for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) {
BasicBlock *BB = DFBlocks[i].second; BasicBlock *BB = DFBlocks[i].second;
if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes)) if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes))
DefiningBlocks.push_back(BB); Info.DefiningBlocks.push_back(BB);
} }
DFBlocks.clear(); DFBlocks.clear();
} }
@ -355,9 +386,9 @@ void PromoteMem2Reg::run() {
// marked alive because of loads which are dominated by stores, but there // marked alive because of loads which are dominated by stores, but there
// will be no unmarked PHI nodes which are actually used. // will be no unmarked PHI nodes which are actually used.
// //
for (unsigned i = 0, e = UsingBlocks.size(); i != e; ++i) for (unsigned i = 0, e = Info.UsingBlocks.size(); i != e; ++i)
MarkDominatingPHILive(UsingBlocks[i], AllocaNum, InsertedPHINodes); MarkDominatingPHILive(Info.UsingBlocks[i], AllocaNum, InsertedPHINodes);
UsingBlocks.clear(); Info.UsingBlocks.clear();
// If there are any PHI nodes which are now known to be dead, remove them! // If there are any PHI nodes which are now known to be dead, remove them!
for (SmallPtrSet<PHINode*, 16>::iterator I = InsertedPHINodes.begin(), for (SmallPtrSet<PHINode*, 16>::iterator I = InsertedPHINodes.begin(),
@ -623,6 +654,8 @@ bool PromoteMem2Reg::PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI) {
assert(AI->use_empty() && "Uses of alloca from more than one BB??"); assert(AI->use_empty() && "Uses of alloca from more than one BB??");
if (AST) AST->deleteValue(AI); if (AST) AST->deleteValue(AI);
AI->getParent()->getInstList().erase(AI); AI->getParent()->getInstList().erase(AI);
++NumLocalPromoted;
return false; return false;
} }