mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-14 17:34:41 +00:00
Implement deletion of dead blocks, currently disabled.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26285 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
b0d04a7dea
commit
db41024a85
@ -82,8 +82,8 @@ namespace {
|
|||||||
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt);
|
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt);
|
||||||
void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,Constant *Val,
|
void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,Constant *Val,
|
||||||
bool isEqual);
|
bool isEqual);
|
||||||
bool TryToRemoveEdge(TerminatorInst *TI, unsigned SuccNo,
|
void RemoveBlockIfDead(BasicBlock *BB,
|
||||||
std::vector<Instruction*> &Worklist);
|
std::vector<Instruction*> &Worklist);
|
||||||
};
|
};
|
||||||
RegisterOpt<LoopUnswitch> X("loop-unswitch", "Unswitch loops");
|
RegisterOpt<LoopUnswitch> X("loop-unswitch", "Unswitch loops");
|
||||||
}
|
}
|
||||||
@ -718,24 +718,68 @@ static void ReplaceUsesOfWith(Instruction *I, Value *V,
|
|||||||
++NumSimplify;
|
++NumSimplify;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TryToRemoveEdge - Determine whether this is a case where we're smart enough
|
/// RemoveBlockIfDead - If the specified block is dead, remove it, update loop
|
||||||
/// to remove the specified edge from the CFG and know how to update loop
|
/// information, and remove any dead successors it has.
|
||||||
/// information. If it is, update SSA and the loop information for the future
|
///
|
||||||
/// change, then return true. If not, return false.
|
void LoopUnswitch::RemoveBlockIfDead(BasicBlock *BB,
|
||||||
bool LoopUnswitch::TryToRemoveEdge(TerminatorInst *TI, unsigned DeadSuccNo,
|
std::vector<Instruction*> &Worklist) {
|
||||||
std::vector<Instruction*> &Worklist) {
|
if (pred_begin(BB) != pred_end(BB)) return; // not dead.
|
||||||
BasicBlock *BB = TI->getParent(), *Succ = TI->getSuccessor(DeadSuccNo);
|
|
||||||
Loop *BBLoop = LI->getLoopFor(BB);
|
|
||||||
Loop *SuccLoop = LI->getLoopFor(Succ);
|
|
||||||
|
|
||||||
// If this edge is not in a loop, or if this edge is leaving a loop to a
|
DEBUG(std::cerr << "Nuking dead block: " << *BB);
|
||||||
// non-loop area, this is trivial.
|
|
||||||
if (SuccLoop == 0) {
|
// Remove the instructions in the basic block from the worklist.
|
||||||
Succ->removePredecessor(BB, true);
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
||||||
return true;
|
RemoveFromWorklist(I, Worklist);
|
||||||
|
|
||||||
|
// If this is the header block for a loop, remove the loop and all subloops.
|
||||||
|
// We leave all of the blocks that used to be part of this loop in the parent
|
||||||
|
// loop if any.
|
||||||
|
if (Loop *BBLoop = LI->getLoopFor(BB)) {
|
||||||
|
if (BBLoop->getHeader() == BB) {
|
||||||
|
if (Loop *ParentLoop = BBLoop->getParentLoop()) { // not a top-level loop.
|
||||||
|
for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
|
||||||
|
++I) {
|
||||||
|
assert(I != E && "Couldn't find loop");
|
||||||
|
if (*I == BBLoop) {
|
||||||
|
ParentLoop->removeChildLoop(I);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
|
||||||
|
assert(I != E && "Couldn't find loop");
|
||||||
|
if (*I == BBLoop) {
|
||||||
|
LI->removeLoop(I);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete BBLoop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the block from the loop info, which removes it from any loops it
|
||||||
|
// was in.
|
||||||
|
LI->removeBlock(BB);
|
||||||
|
|
||||||
|
|
||||||
|
// Remove phi node entries in successors for this block.
|
||||||
|
TerminatorInst *TI = BB->getTerminator();
|
||||||
|
std::vector<BasicBlock*> Succs;
|
||||||
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
|
||||||
|
Succs.push_back(TI->getSuccessor(i));
|
||||||
|
TI->getSuccessor(i)->removePredecessor(BB);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
// Unique the successors.
|
||||||
|
std::sort(Succs.begin(), Succs.end());
|
||||||
|
Succs.erase(std::unique(Succs.begin(), Succs.end()), Succs.end());
|
||||||
|
|
||||||
|
// Remove the basic block, including all of the instructions contained in it.
|
||||||
|
BB->eraseFromParent();
|
||||||
|
|
||||||
|
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
|
||||||
|
RemoveBlockIfDead(Succs[i], Worklist);
|
||||||
}
|
}
|
||||||
|
|
||||||
// RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
|
// RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
|
||||||
@ -895,17 +939,20 @@ void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
|
|||||||
LI->removeBlock(Succ);
|
LI->removeBlock(Succ);
|
||||||
Succ->eraseFromParent();
|
Succ->eraseFromParent();
|
||||||
++NumSimplify;
|
++NumSimplify;
|
||||||
break;
|
|
||||||
} else if (ConstantBool *CB = dyn_cast<ConstantBool>(BI->getCondition())){
|
} else if (ConstantBool *CB = dyn_cast<ConstantBool>(BI->getCondition())){
|
||||||
// Conditional branch.
|
break; // FIXME: disabled
|
||||||
if (TryToRemoveEdge(BI, CB->getValue(), Worklist)) {
|
// Conditional branch. Turn it into an unconditional branch, then
|
||||||
DEBUG(std::cerr << "Folded branch: " << *BI);
|
// remove dead blocks.
|
||||||
new BranchInst(BI->getSuccessor(!CB->getValue()), BI);
|
DEBUG(std::cerr << "Folded branch: " << *BI);
|
||||||
BI->eraseFromParent();
|
BasicBlock *DeadSucc = BI->getSuccessor(CB->getValue());
|
||||||
RemoveFromWorklist(BI, Worklist);
|
BasicBlock *LiveSucc = BI->getSuccessor(!CB->getValue());
|
||||||
++NumSimplify;
|
DeadSucc->removePredecessor(BI->getParent(), true);
|
||||||
break;
|
Worklist.push_back(new BranchInst(LiveSucc, BI));
|
||||||
}
|
BI->eraseFromParent();
|
||||||
|
RemoveFromWorklist(BI, Worklist);
|
||||||
|
++NumSimplify;
|
||||||
|
|
||||||
|
RemoveBlockIfDead(DeadSucc, Worklist);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user