Newer
Older
Chris Lattner
committed
}
// Simple DCE.
if (isInstructionTriviallyDead(I)) {
DEBUG(errs() << "Remove dead instruction '" << *I);
Chris Lattner
committed
// Add uses to the worklist, which may be dead now.
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
Worklist.push_back(Use);
Chris Lattner
committed
RemoveFromWorklist(I, Worklist);
I->eraseFromParent();
Chris Lattner
committed
++NumSimplify;
continue;
}
// Special case hacks that appear commonly in unswitched code.
switch (I->getOpcode()) {
case Instruction::Select:
if (ConstantInt *CB = dyn_cast<ConstantInt>(I->getOperand(0))) {
ReplaceUsesOfWith(I, I->getOperand(!CB->getZExtValue()+1), Worklist, L,
LPM);
Chris Lattner
committed
continue;
}
break;
case Instruction::And:
// constant -> RHS
I->getOperand(0)->getType() == Type::getInt1Ty(I->getContext()))
Chris Lattner
committed
cast<BinaryOperator>(I)->swapOperands();
if (ConstantInt *CB = dyn_cast<ConstantInt>(I->getOperand(1)))
if (CB->getType() == Type::getInt1Ty(I->getContext())) {
if (CB->isOne()) // X & 1 -> X
ReplaceUsesOfWith(I, I->getOperand(0), Worklist, L, LPM);
ReplaceUsesOfWith(I, I->getOperand(1), Worklist, L, LPM);
Chris Lattner
committed
break;
case Instruction::Or:
// constant -> RHS
I->getOperand(0)->getType() == Type::getInt1Ty(I->getContext()))
Chris Lattner
committed
cast<BinaryOperator>(I)->swapOperands();
if (ConstantInt *CB = dyn_cast<ConstantInt>(I->getOperand(1)))
if (CB->getType() == Type::getInt1Ty(I->getContext())) {
if (CB->isOne()) // X | 1 -> 1
ReplaceUsesOfWith(I, I->getOperand(1), Worklist, L, LPM);
ReplaceUsesOfWith(I, I->getOperand(0), Worklist, L, LPM);
Chris Lattner
committed
break;
case Instruction::Br: {
BranchInst *BI = cast<BranchInst>(I);
if (BI->isUnconditional()) {
// If BI's parent is the only pred of the successor, fold the two blocks
// together.
BasicBlock *Pred = BI->getParent();
BasicBlock *Succ = BI->getSuccessor(0);
BasicBlock *SinglePred = Succ->getSinglePredecessor();
if (!SinglePred) continue; // Nothing to do.
assert(SinglePred == Pred && "CFG broken");
Daniel Dunbar
committed
DEBUG(errs() << "Merging blocks: " << Pred->getName() << " <- "
<< Succ->getName() << "\n");
Chris Lattner
committed
// Resolve any single entry PHI nodes in Succ.
while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
Chris Lattner
committed
// Move all of the successor contents from Succ to Pred.
Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
Succ->end());
LPM->deleteSimpleAnalysisValue(BI, L);
BI->eraseFromParent();
Chris Lattner
committed
RemoveFromWorklist(BI, Worklist);
// If Succ has any successors with PHI nodes, update them to have
// entries coming from Pred instead of Succ.
Succ->replaceAllUsesWith(Pred);
// Remove Succ from the loop tree.
LI->removeBlock(Succ);
LPM->deleteSimpleAnalysisValue(Succ, L);
Succ->eraseFromParent();
Chris Lattner
committed
++NumSimplify;
} else if (ConstantInt *CB = dyn_cast<ConstantInt>(BI->getCondition())){
// Conditional branch. Turn it into an unconditional branch, then
// remove dead blocks.
break; // FIXME: Enable.
DEBUG(errs() << "Folded branch: " << *BI);
BasicBlock *DeadSucc = BI->getSuccessor(CB->getZExtValue());
BasicBlock *LiveSucc = BI->getSuccessor(!CB->getZExtValue());
DeadSucc->removePredecessor(BI->getParent(), true);
Worklist.push_back(BranchInst::Create(LiveSucc, BI));
LPM->deleteSimpleAnalysisValue(BI, L);
BI->eraseFromParent();
RemoveFromWorklist(BI, Worklist);
++NumSimplify;
RemoveBlockIfDead(DeadSucc, Worklist, L);
Chris Lattner
committed
}
Chris Lattner
committed
break;
Chris Lattner
committed
}
Chris Lattner
committed
}
}