mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-09 11:25:55 +00:00
Final bit of simplification for FoldBranchToCommonDest.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53528 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1460,13 +1460,25 @@ static bool FoldBranchToCommonDest(BranchInst *BI) {
|
|||||||
!SafeToMergeTerminators(BI, PBI))
|
!SafeToMergeTerminators(BI, PBI))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Canonicalize the predecessors condition (inverting it) if needed to allow
|
Instruction::BinaryOps Opc;
|
||||||
// this xform to trigger.
|
bool InvertPredCond = false;
|
||||||
if (PBI->getSuccessor(0) == FalseDest ||
|
|
||||||
PBI->getSuccessor(1) == TrueDest) {
|
if (PBI->getSuccessor(0) == TrueDest)
|
||||||
|
Opc = Instruction::Or;
|
||||||
|
else if (PBI->getSuccessor(1) == FalseDest)
|
||||||
|
Opc = Instruction::And;
|
||||||
|
else if (PBI->getSuccessor(0) == FalseDest)
|
||||||
|
Opc = Instruction::And, InvertPredCond = true;
|
||||||
|
else if (PBI->getSuccessor(1) == TrueDest)
|
||||||
|
Opc = Instruction::Or, InvertPredCond = true;
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// If we need to invert the condition in the pred block to match, do so now.
|
||||||
|
if (InvertPredCond) {
|
||||||
Value *NewCond =
|
Value *NewCond =
|
||||||
BinaryOperator::CreateNot(PBI->getCondition(),
|
BinaryOperator::CreateNot(PBI->getCondition(),
|
||||||
PBI->getCondition()->getName()+".not", PBI);
|
PBI->getCondition()->getName()+".not", PBI);
|
||||||
PBI->setCondition(NewCond);
|
PBI->setCondition(NewCond);
|
||||||
BasicBlock *OldTrue = PBI->getSuccessor(0);
|
BasicBlock *OldTrue = PBI->getSuccessor(0);
|
||||||
BasicBlock *OldFalse = PBI->getSuccessor(1);
|
BasicBlock *OldFalse = PBI->getSuccessor(1);
|
||||||
@@ -1474,34 +1486,25 @@ static bool FoldBranchToCommonDest(BranchInst *BI) {
|
|||||||
PBI->setSuccessor(1, OldTrue);
|
PBI->setSuccessor(1, OldTrue);
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction::BinaryOps Opc = Instruction::Shl; // sentinel.
|
// Clone Cond into the predecessor basic block, and or/and the
|
||||||
|
// two conditions together.
|
||||||
|
Instruction *New = Cond->clone();
|
||||||
|
PredBlock->getInstList().insert(PBI, New);
|
||||||
|
New->takeName(Cond);
|
||||||
|
Cond->setName(New->getName()+".old");
|
||||||
|
|
||||||
if (PBI->getSuccessor(0) == TrueDest && FalseDest != BB)
|
Value *NewCond = BinaryOperator::Create(Opc, PBI->getCondition(),
|
||||||
Opc = Instruction::Or;
|
New, "or.cond", PBI);
|
||||||
else if (PBI->getSuccessor(1) == FalseDest && TrueDest != BB)
|
PBI->setCondition(NewCond);
|
||||||
Opc = Instruction::And;
|
if (PBI->getSuccessor(0) == BB) {
|
||||||
|
AddPredecessorToBlock(TrueDest, PredBlock, BB);
|
||||||
if (Opc != Instruction::Shl) {
|
PBI->setSuccessor(0, TrueDest);
|
||||||
// Clone Cond into the predecessor basic block, and or/and the
|
|
||||||
// two conditions together.
|
|
||||||
Instruction *New = Cond->clone();
|
|
||||||
PredBlock->getInstList().insert(PBI, New);
|
|
||||||
New->takeName(Cond);
|
|
||||||
Cond->setName(New->getName()+".old");
|
|
||||||
|
|
||||||
Value *NewCond = BinaryOperator::Create(Opc, PBI->getCondition(),
|
|
||||||
New, "or.cond", PBI);
|
|
||||||
PBI->setCondition(NewCond);
|
|
||||||
if (PBI->getSuccessor(0) == BB) {
|
|
||||||
AddPredecessorToBlock(TrueDest, PredBlock, BB);
|
|
||||||
PBI->setSuccessor(0, TrueDest);
|
|
||||||
}
|
|
||||||
if (PBI->getSuccessor(1) == BB) {
|
|
||||||
AddPredecessorToBlock(FalseDest, PredBlock, BB);
|
|
||||||
PBI->setSuccessor(1, FalseDest);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
if (PBI->getSuccessor(1) == BB) {
|
||||||
|
AddPredecessorToBlock(FalseDest, PredBlock, BB);
|
||||||
|
PBI->setSuccessor(1, FalseDest);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1742,7 +1745,6 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
|
|||||||
if (FoldBranchToCommonDest(BI))
|
if (FoldBranchToCommonDest(BI))
|
||||||
return SimplifyCFG(BB) | 1;
|
return SimplifyCFG(BB) | 1;
|
||||||
|
|
||||||
|
|
||||||
// Scan predessor blocks for conditional branches.
|
// Scan predessor blocks for conditional branches.
|
||||||
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
||||||
if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
|
if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
|
||||||
|
Reference in New Issue
Block a user