"git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "4a84e97421afa6b99f13d8eb4d921f8c086d413f"
Newer
Older
/// it must not be used.
///
/// FIXME: When the loop optimizer is more mature, separate this out to a new
/// pass.
///
void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist) {
Chris Lattner
committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
while (!Worklist.empty()) {
Instruction *I = Worklist.back();
Worklist.pop_back();
// Simple constant folding.
if (Constant *C = ConstantFoldInstruction(I)) {
ReplaceUsesOfWith(I, C, Worklist);
continue;
}
// Simple DCE.
if (isInstructionTriviallyDead(I)) {
DEBUG(std::cerr << "Remove dead instruction '" << *I);
// 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);
I->eraseFromParent();
RemoveFromWorklist(I, Worklist);
++NumSimplify;
continue;
}
// Special case hacks that appear commonly in unswitched code.
switch (I->getOpcode()) {
case Instruction::Select:
if (ConstantBool *CB = dyn_cast<ConstantBool>(I->getOperand(0))) {
ReplaceUsesOfWith(I, I->getOperand(!CB->getValue()+1), Worklist);
continue;
}
break;
case Instruction::And:
if (isa<ConstantBool>(I->getOperand(0))) // constant -> RHS
cast<BinaryOperator>(I)->swapOperands();
if (ConstantBool *CB = dyn_cast<ConstantBool>(I->getOperand(1))) {
if (CB->getValue()) // X & 1 -> X
ReplaceUsesOfWith(I, I->getOperand(0), Worklist);
else // X & 0 -> 0
ReplaceUsesOfWith(I, I->getOperand(1), Worklist);
continue;
}
break;
case Instruction::Or:
if (isa<ConstantBool>(I->getOperand(0))) // constant -> RHS
cast<BinaryOperator>(I)->swapOperands();
if (ConstantBool *CB = dyn_cast<ConstantBool>(I->getOperand(1))) {
if (CB->getValue()) // X | 1 -> 1
ReplaceUsesOfWith(I, I->getOperand(1), Worklist);
else // X | 0 -> X
ReplaceUsesOfWith(I, I->getOperand(0), Worklist);
continue;
}
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");
DEBUG(std::cerr << "Merging blocks: " << Pred->getName() << " <- "
<< Succ->getName() << "\n");
// Resolve any single entry PHI nodes in Succ.
while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist);
// Move all of the successor contents from Succ to Pred.
Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
Succ->end());
BI->eraseFromParent();
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);
Succ->eraseFromParent();
Chris Lattner
committed
++NumSimplify;
} else if (ConstantBool *CB = dyn_cast<ConstantBool>(BI->getCondition())){
// Conditional branch. Turn it into an unconditional branch, then
// remove dead blocks.
break; // FIXME: Enable.
DEBUG(std::cerr << "Folded branch: " << *BI);
BasicBlock *DeadSucc = BI->getSuccessor(CB->getValue());
BasicBlock *LiveSucc = BI->getSuccessor(!CB->getValue());
DeadSucc->removePredecessor(BI->getParent(), true);
Worklist.push_back(new BranchInst(LiveSucc, BI));
BI->eraseFromParent();
RemoveFromWorklist(BI, Worklist);
++NumSimplify;
RemoveBlockIfDead(DeadSucc, Worklist);
Chris Lattner
committed
}
Chris Lattner
committed
break;
Chris Lattner
committed
}
Chris Lattner
committed
}
}