mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-17 21:29:20 +00:00
Fold two consequtive branches that share a common destination between them.
This implements SimplifyCFG/branch-fold.ll, and is useful on ?:/min/max heavy code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23410 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
9a82a3ccac
commit
263d1e469d
@ -1207,6 +1207,8 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
|
||||
if (!UncondBranchPreds.empty()) {
|
||||
while (!UncondBranchPreds.empty()) {
|
||||
BasicBlock *Pred = UncondBranchPreds.back();
|
||||
DEBUG(std::cerr << "FOLDING: " << *BB
|
||||
<< "INTO UNCOND BRANCH PRED: " << *Pred);
|
||||
UncondBranchPreds.pop_back();
|
||||
Instruction *UncondBranch = Pred->getTerminator();
|
||||
// Clone the return and add it to the end of the predecessor.
|
||||
@ -1387,7 +1389,6 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
|
||||
if (FoldCondBranchOnPHI(BI))
|
||||
return SimplifyCFG(BB) | true;
|
||||
|
||||
|
||||
// If this basic block is ONLY a setcc and a branch, and if a predecessor
|
||||
// branches to us and one of our successors, fold the setcc into the
|
||||
// predecessor and use logical operations to pick the right destination.
|
||||
@ -1442,13 +1443,15 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
|
||||
}
|
||||
}
|
||||
|
||||
// Scan predessor blocks for conditional branchs.
|
||||
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
||||
if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
|
||||
if (PBI != BI && PBI->isConditional()) {
|
||||
|
||||
// If this block ends with a branch instruction, and if there is a
|
||||
// predecessor that ends on a branch of the same condition, make this
|
||||
// conditional branch redundant.
|
||||
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
||||
if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
|
||||
if (PBI != BI && PBI->isConditional() &&
|
||||
PBI->getCondition() == BI->getCondition() &&
|
||||
if (PBI->getCondition() == BI->getCondition() &&
|
||||
PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
|
||||
// Okay, the outcome of this conditional branch is statically
|
||||
// knowable. If this block had a single pred, handle specially.
|
||||
@ -1481,6 +1484,89 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
|
||||
return SimplifyCFG(BB) | true;
|
||||
}
|
||||
}
|
||||
|
||||
// If this is a conditional branch in an empty block, and if any
|
||||
// predecessors is a conditional branch to one of our destinations,
|
||||
// fold the conditions into logical ops and one cond br.
|
||||
if (&BB->front() == BI) {
|
||||
int PBIOp, BIOp;
|
||||
if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
|
||||
PBIOp = BIOp = 0;
|
||||
} else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
|
||||
PBIOp = 0; BIOp = 1;
|
||||
} else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
|
||||
PBIOp = 1; BIOp = 0;
|
||||
} else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
|
||||
PBIOp = BIOp = 1;
|
||||
} else {
|
||||
PBIOp = BIOp = -1;
|
||||
}
|
||||
|
||||
// Finally, if everything is ok, fold the branches to logical ops.
|
||||
if (PBIOp != -1) {
|
||||
BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
|
||||
BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
|
||||
|
||||
DEBUG(std::cerr << "FOLDING BRs:" << *PBI->getParent()
|
||||
<< "AND: " << *BI->getParent());
|
||||
|
||||
// BI may have other predecessors. Because of this, we leave
|
||||
// it alone, but modify PBI.
|
||||
|
||||
// Make sure we get to CommonDest on True&True directions.
|
||||
Value *PBICond = PBI->getCondition();
|
||||
if (PBIOp)
|
||||
PBICond = BinaryOperator::createNot(PBICond,
|
||||
PBICond->getName()+".not",
|
||||
PBI);
|
||||
Value *BICond = BI->getCondition();
|
||||
if (BIOp)
|
||||
BICond = BinaryOperator::createNot(BICond,
|
||||
BICond->getName()+".not",
|
||||
PBI);
|
||||
// Merge the conditions.
|
||||
Value *Cond =
|
||||
BinaryOperator::createOr(PBICond, BICond, "brmerge", PBI);
|
||||
|
||||
// Modify PBI to branch on the new condition to the new dests.
|
||||
PBI->setCondition(Cond);
|
||||
PBI->setSuccessor(0, CommonDest);
|
||||
PBI->setSuccessor(1, OtherDest);
|
||||
|
||||
// OtherDest may have phi nodes. If so, add an entry from PBI's
|
||||
// block that are identical to the entries for BI's block.
|
||||
PHINode *PN;
|
||||
for (BasicBlock::iterator II = OtherDest->begin();
|
||||
(PN = dyn_cast<PHINode>(II)); ++II) {
|
||||
Value *V = PN->getIncomingValueForBlock(BB);
|
||||
PN->addIncoming(V, PBI->getParent());
|
||||
}
|
||||
|
||||
// We know that the CommonDest already had an edge from PBI to
|
||||
// it. If it has PHIs though, the PHIs may have different
|
||||
// entries for BB and PBI's BB. If so, insert a select to make
|
||||
// them agree.
|
||||
for (BasicBlock::iterator II = CommonDest->begin();
|
||||
(PN = dyn_cast<PHINode>(II)); ++II) {
|
||||
Value * BIV = PN->getIncomingValueForBlock(BB);
|
||||
unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
|
||||
Value *PBIV = PN->getIncomingValue(PBBIdx);
|
||||
if (BIV != PBIV) {
|
||||
// Insert a select in PBI to pick the right value.
|
||||
Value *NV = new SelectInst(PBICond, PBIV, BIV,
|
||||
PBIV->getName()+".mux", PBI);
|
||||
PN->setIncomingValue(PBBIdx, NV);
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG(std::cerr << "INTO: " << *PBI->getParent());
|
||||
|
||||
// This basic block is probably dead. We know it has at least
|
||||
// one fewer predecessor.
|
||||
return SimplifyCFG(BB) | true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isa<UnreachableInst>(BB->getTerminator())) {
|
||||
// If there are any instructions immediately before the unreachable that can
|
||||
|
Loading…
Reference in New Issue
Block a user