-/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
-/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
-/// DestB, remove any other MBB successors from the CFG. DestA and DestB can
-/// be null.
-static bool CorrectExtraCFGEdges(MachineBasicBlock &MBB,
- MachineBasicBlock *DestA,
- MachineBasicBlock *DestB,
- bool isCond,
- MachineFunction::iterator FallThru) {
- bool MadeChange = false;
- bool AddedFallThrough = false;
-
- // If this block ends with a conditional branch that falls through to its
- // successor, set DestB as the successor.
- if (isCond) {
- if (DestB == 0 && FallThru != MBB.getParent()->end()) {
- DestB = FallThru;
- AddedFallThrough = true;
- }
- } else {
- // If this is an unconditional branch with no explicit dest, it must just be
- // a fallthrough into DestB.
- if (DestA == 0 && FallThru != MBB.getParent()->end()) {
- DestA = FallThru;
- AddedFallThrough = true;
- }
- }
-
- MachineBasicBlock::pred_iterator SI = MBB.succ_begin();
- while (SI != MBB.succ_end()) {
- if (*SI == DestA) {
- DestA = 0;
- ++SI;
- } else if (*SI == DestB) {
- DestB = 0;
- ++SI;
- } else if ((*SI)->isLandingPad()) {
- ++SI;
- } else {
- // Otherwise, this is a superfluous edge, remove it.
- MBB.removeSuccessor(SI);
- MadeChange = true;
- }
- }
- if (!AddedFallThrough) {
- assert(DestA == 0 && DestB == 0 &&
- "MachineCFG is missing edges!");
- } else if (isCond) {
- assert(DestA == 0 && "MachineCFG is missing edges!");
- }
- return MadeChange;
-}
-
-
-/// ReplaceUsesOfBlockWith - Given a machine basic block 'BB' that branched to
-/// 'Old', change the code and CFG so that it branches to 'New' instead.
-static void ReplaceUsesOfBlockWith(MachineBasicBlock *BB,
- MachineBasicBlock *Old,
- MachineBasicBlock *New,
- const TargetInstrInfo *TII) {
- assert(Old != New && "Cannot replace self with self!");
-
- MachineBasicBlock::iterator I = BB->end();
- while (I != BB->begin()) {
- --I;
- if (!TII->isTerminatorInstr(I->getOpcode())) break;
-
- // Scan the operands of this machine instruction, replacing any uses of Old
- // with New.
- for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
- if (I->getOperand(i).isMachineBasicBlock() &&
- I->getOperand(i).getMachineBasicBlock() == Old)
- I->getOperand(i).setMachineBasicBlock(New);
- }
-
- // Update the successor information.
- std::vector<MachineBasicBlock*> Succs(BB->succ_begin(), BB->succ_end());
- for (int i = Succs.size()-1; i >= 0; --i)
- if (Succs[i] == Old) {
- BB->removeSuccessor(Old);
- BB->addSuccessor(New);
- }
-}
-