clean up this code a bit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80767 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-09-02 06:15:37 +00:00
parent 3e8b6631e6
commit 20a2faa980

View File

@ -43,20 +43,31 @@ namespace {
AU.addPreservedID(BreakCriticalEdgesID); AU.addPreservedID(BreakCriticalEdgesID);
} }
bool valueEscapes(Instruction* i) { bool valueEscapes(const Instruction *Inst) const {
BasicBlock* bb = i->getParent(); const BasicBlock *BB = Inst->getParent();
for (Value::use_iterator ii = i->use_begin(), ie = i->use_end(); for (Value::use_const_iterator UI = Inst->use_begin(),E = Inst->use_end();
ii != ie; ++ii) UI != E; ++UI)
if (cast<Instruction>(*ii)->getParent() != bb || if (cast<Instruction>(*UI)->getParent() != BB ||
isa<PHINode>(*ii)) isa<PHINode>(*UI))
return true; return true;
return false; return false;
} }
virtual bool runOnFunction(Function &F) { virtual bool runOnFunction(Function &F);
if (!F.isDeclaration()) { };
}
char RegToMem::ID = 0;
static RegisterPass<RegToMem>
X("reg2mem", "Demote all values to stack slots");
bool RegToMem::runOnFunction(Function &F) {
if (F.isDeclaration())
return false;
// Insert all new allocas into entry block. // Insert all new allocas into entry block.
BasicBlock* BBEntry = &F.getEntryBlock(); BasicBlock *BBEntry = &F.getEntryBlock();
assert(pred_begin(BBEntry) == pred_end(BBEntry) && assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
"Entry block to function must not have predecessors!"); "Entry block to function must not have predecessors!");
@ -67,31 +78,30 @@ namespace {
while (isa<AllocaInst>(I)) ++I; while (isa<AllocaInst>(I)) ++I;
CastInst *AllocaInsertionPoint = CastInst *AllocaInsertionPoint =
CastInst::Create(Instruction::BitCast, new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
Constant::getNullValue(Type::getInt32Ty(F.getContext())),
Type::getInt32Ty(F.getContext()), Type::getInt32Ty(F.getContext()),
"reg2mem alloca point", I); "reg2mem alloca point", I);
// Find the escaped instructions. But don't create stack slots for // Find the escaped instructions. But don't create stack slots for
// allocas in entry block. // allocas in entry block.
std::list<Instruction*> worklist; std::list<Instruction*> WorkList;
for (Function::iterator ibb = F.begin(), ibe = F.end(); for (Function::iterator ibb = F.begin(), ibe = F.end();
ibb != ibe; ++ibb) ibb != ibe; ++ibb)
for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
iib != iie; ++iib) { iib != iie; ++iib) {
if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
valueEscapes(iib)) { valueEscapes(iib)) {
worklist.push_front(&*iib); WorkList.push_front(&*iib);
} }
} }
// Demote escaped instructions // Demote escaped instructions
NumRegsDemoted += worklist.size(); NumRegsDemoted += WorkList.size();
for (std::list<Instruction*>::iterator ilb = worklist.begin(), for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
ile = worklist.end(); ilb != ile; ++ilb) ile = WorkList.end(); ilb != ile; ++ilb)
DemoteRegToStack(**ilb, false, AllocaInsertionPoint); DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
worklist.clear(); WorkList.clear();
// Find all phi's // Find all phi's
for (Function::iterator ibb = F.begin(), ibe = F.end(); for (Function::iterator ibb = F.begin(), ibe = F.end();
@ -99,24 +109,17 @@ namespace {
for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
iib != iie; ++iib) iib != iie; ++iib)
if (isa<PHINode>(iib)) if (isa<PHINode>(iib))
worklist.push_front(&*iib); WorkList.push_front(&*iib);
// Demote phi nodes // Demote phi nodes
NumPhisDemoted += worklist.size(); NumPhisDemoted += WorkList.size();
for (std::list<Instruction*>::iterator ilb = worklist.begin(), for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
ile = worklist.end(); ilb != ile; ++ilb) ile = WorkList.end(); ilb != ile; ++ilb)
DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
return true; return true;
}
return false;
}
};
} }
char RegToMem::ID = 0;
static RegisterPass<RegToMem>
X("reg2mem", "Demote all values to stack slots");
// createDemoteRegisterToMemory - Provide an entry point to create this pass. // createDemoteRegisterToMemory - Provide an entry point to create this pass.
// //