1f22ef01cSRoman Divacky //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // Collect the sequence of machine instructions for a basic block. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineBasicBlock.h" 15f22ef01cSRoman Divacky #include "llvm/BasicBlock.h" 16ffd1746dSEd Schouten #include "llvm/CodeGen/LiveVariables.h" 17ffd1746dSEd Schouten #include "llvm/CodeGen/MachineDominators.h" 18f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h" 19ffd1746dSEd Schouten #include "llvm/CodeGen/MachineLoopInfo.h" 202754fe60SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 21f22ef01cSRoman Divacky #include "llvm/MC/MCAsmInfo.h" 22f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 23f22ef01cSRoman Divacky #include "llvm/Target/TargetRegisterInfo.h" 24f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 25f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrDesc.h" 26f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrInfo.h" 27f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h" 28f22ef01cSRoman Divacky #include "llvm/Assembly/Writer.h" 29f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 30f22ef01cSRoman Divacky #include "llvm/ADT/SmallPtrSet.h" 31f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 32f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h" 33f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 34f22ef01cSRoman Divacky #include <algorithm> 35f22ef01cSRoman Divacky using namespace llvm; 36f22ef01cSRoman Divacky 37f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 38f22ef01cSRoman Divacky : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 39f22ef01cSRoman Divacky AddressTaken(false) { 40f22ef01cSRoman Divacky Insts.Parent = this; 41f22ef01cSRoman Divacky } 42f22ef01cSRoman Divacky 43f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() { 44f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(this); 45f22ef01cSRoman Divacky } 46f22ef01cSRoman Divacky 47f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block. 48f22ef01cSRoman Divacky /// 49f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const { 50f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 51f22ef01cSRoman Divacky MCContext &Ctx = MF->getContext(); 52f22ef01cSRoman Divacky const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 53f22ef01cSRoman Divacky return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 54f22ef01cSRoman Divacky Twine(MF->getFunctionNumber()) + "_" + 55f22ef01cSRoman Divacky Twine(getNumber())); 56f22ef01cSRoman Divacky } 57f22ef01cSRoman Divacky 58f22ef01cSRoman Divacky 59f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 60f22ef01cSRoman Divacky MBB.print(OS); 61f22ef01cSRoman Divacky return OS; 62f22ef01cSRoman Divacky } 63f22ef01cSRoman Divacky 64f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 65f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the 66f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers. 67f22ef01cSRoman Divacky /// 68f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 69f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a 70f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1. 71f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 72f22ef01cSRoman Divacky MachineFunction &MF = *N->getParent(); 73f22ef01cSRoman Divacky N->Number = MF.addToMBBNumbering(N); 74f22ef01cSRoman Divacky 75f22ef01cSRoman Divacky // Make sure the instructions have their operands in the reginfo lists. 76f22ef01cSRoman Divacky MachineRegisterInfo &RegInfo = MF.getRegInfo(); 77f22ef01cSRoman Divacky for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) 78f22ef01cSRoman Divacky I->AddRegOperandsToUseLists(RegInfo); 79f22ef01cSRoman Divacky 80f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 81f22ef01cSRoman Divacky } 82f22ef01cSRoman Divacky 83f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 84f22ef01cSRoman Divacky N->getParent()->removeFromMBBNumbering(N->Number); 85f22ef01cSRoman Divacky N->Number = -1; 86f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 87f22ef01cSRoman Divacky } 88f22ef01cSRoman Divacky 89f22ef01cSRoman Divacky 90f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block 91f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def 92f22ef01cSRoman Divacky /// lists if appropriate. 93f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 94f22ef01cSRoman Divacky assert(N->getParent() == 0 && "machine instruction already in a basic block"); 95f22ef01cSRoman Divacky N->setParent(Parent); 96f22ef01cSRoman Divacky 97f22ef01cSRoman Divacky // Add the instruction's register operands to their corresponding 98f22ef01cSRoman Divacky // use/def lists. 99f22ef01cSRoman Divacky MachineFunction *MF = Parent->getParent(); 100f22ef01cSRoman Divacky N->AddRegOperandsToUseLists(MF->getRegInfo()); 101f22ef01cSRoman Divacky 102f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 103f22ef01cSRoman Divacky } 104f22ef01cSRoman Divacky 105f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block 106f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def 107f22ef01cSRoman Divacky /// lists if appropriate. 108f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 109f22ef01cSRoman Divacky assert(N->getParent() != 0 && "machine instruction not in a basic block"); 110f22ef01cSRoman Divacky 111f22ef01cSRoman Divacky // Remove from the use/def lists. 112f22ef01cSRoman Divacky N->RemoveRegOperandsFromUseLists(); 113f22ef01cSRoman Divacky 114f22ef01cSRoman Divacky N->setParent(0); 115f22ef01cSRoman Divacky 116f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 117f22ef01cSRoman Divacky } 118f22ef01cSRoman Divacky 119f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one 120f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def 121f22ef01cSRoman Divacky /// lists. 122f22ef01cSRoman Divacky void ilist_traits<MachineInstr>:: 123f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList, 124f22ef01cSRoman Divacky MachineBasicBlock::iterator first, 125f22ef01cSRoman Divacky MachineBasicBlock::iterator last) { 126f22ef01cSRoman Divacky assert(Parent->getParent() == fromList.Parent->getParent() && 127f22ef01cSRoman Divacky "MachineInstr parent mismatch!"); 128f22ef01cSRoman Divacky 129f22ef01cSRoman Divacky // Splice within the same MBB -> no change. 130f22ef01cSRoman Divacky if (Parent == fromList.Parent) return; 131f22ef01cSRoman Divacky 132f22ef01cSRoman Divacky // If splicing between two blocks within the same function, just update the 133f22ef01cSRoman Divacky // parent pointers. 134f22ef01cSRoman Divacky for (; first != last; ++first) 135f22ef01cSRoman Divacky first->setParent(Parent); 136f22ef01cSRoman Divacky } 137f22ef01cSRoman Divacky 138f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 139f22ef01cSRoman Divacky assert(!MI->getParent() && "MI is still in a block!"); 140f22ef01cSRoman Divacky Parent->getParent()->DeleteMachineInstr(MI); 141f22ef01cSRoman Divacky } 142f22ef01cSRoman Divacky 143ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 144ffd1746dSEd Schouten iterator I = begin(); 145ffd1746dSEd Schouten while (I != end() && I->isPHI()) 146ffd1746dSEd Schouten ++I; 147ffd1746dSEd Schouten return I; 148ffd1746dSEd Schouten } 149ffd1746dSEd Schouten 1502754fe60SDimitry Andric MachineBasicBlock::iterator 1512754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 1522754fe60SDimitry Andric while (I != end() && (I->isPHI() || I->isLabel() || I->isDebugValue())) 1532754fe60SDimitry Andric ++I; 1542754fe60SDimitry Andric return I; 1552754fe60SDimitry Andric } 1562754fe60SDimitry Andric 157f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 158f22ef01cSRoman Divacky iterator I = end(); 1592754fe60SDimitry Andric while (I != begin() && ((--I)->getDesc().isTerminator() || I->isDebugValue())) 160f22ef01cSRoman Divacky ; /*noop */ 1612754fe60SDimitry Andric while (I != end() && !I->getDesc().isTerminator()) 1622754fe60SDimitry Andric ++I; 163f22ef01cSRoman Divacky return I; 164f22ef01cSRoman Divacky } 165f22ef01cSRoman Divacky 1662754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 1672754fe60SDimitry Andric iterator B = begin(), I = end(); 1682754fe60SDimitry Andric while (I != B) { 1692754fe60SDimitry Andric --I; 1702754fe60SDimitry Andric if (I->isDebugValue()) 1712754fe60SDimitry Andric continue; 1722754fe60SDimitry Andric return I; 1732754fe60SDimitry Andric } 1742754fe60SDimitry Andric // The block is all debug values. 1752754fe60SDimitry Andric return end(); 1762754fe60SDimitry Andric } 1772754fe60SDimitry Andric 1782754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 1792754fe60SDimitry Andric // A block with a landing pad successor only has one other successor. 1802754fe60SDimitry Andric if (succ_size() > 2) 1812754fe60SDimitry Andric return 0; 1822754fe60SDimitry Andric for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 1832754fe60SDimitry Andric if ((*I)->isLandingPad()) 1842754fe60SDimitry Andric return *I; 1852754fe60SDimitry Andric return 0; 1862754fe60SDimitry Andric } 1872754fe60SDimitry Andric 188f22ef01cSRoman Divacky void MachineBasicBlock::dump() const { 189f22ef01cSRoman Divacky print(dbgs()); 190f22ef01cSRoman Divacky } 191f22ef01cSRoman Divacky 192f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const { 193f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) 194f22ef01cSRoman Divacky return LBB->getName(); 195f22ef01cSRoman Divacky else 196f22ef01cSRoman Divacky return "(null)"; 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 1992754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 200f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 201f22ef01cSRoman Divacky if (!MF) { 202f22ef01cSRoman Divacky OS << "Can't print out MachineBasicBlock because parent MachineFunction" 203f22ef01cSRoman Divacky << " is null\n"; 204f22ef01cSRoman Divacky return; 205f22ef01cSRoman Divacky } 206f22ef01cSRoman Divacky 207f22ef01cSRoman Divacky if (Alignment) { OS << "Alignment " << Alignment << "\n"; } 208f22ef01cSRoman Divacky 2092754fe60SDimitry Andric if (Indexes) 2102754fe60SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 2112754fe60SDimitry Andric 212f22ef01cSRoman Divacky OS << "BB#" << getNumber() << ": "; 213f22ef01cSRoman Divacky 214f22ef01cSRoman Divacky const char *Comma = ""; 215f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) { 216f22ef01cSRoman Divacky OS << Comma << "derived from LLVM BB "; 217f22ef01cSRoman Divacky WriteAsOperand(OS, LBB, /*PrintType=*/false); 218f22ef01cSRoman Divacky Comma = ", "; 219f22ef01cSRoman Divacky } 220f22ef01cSRoman Divacky if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 221f22ef01cSRoman Divacky if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 222f22ef01cSRoman Divacky OS << '\n'; 223f22ef01cSRoman Divacky 224f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 225f22ef01cSRoman Divacky if (!livein_empty()) { 2262754fe60SDimitry Andric if (Indexes) OS << '\t'; 227f22ef01cSRoman Divacky OS << " Live Ins:"; 228f22ef01cSRoman Divacky for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 2292754fe60SDimitry Andric OS << ' ' << PrintReg(*I, TRI); 230f22ef01cSRoman Divacky OS << '\n'; 231f22ef01cSRoman Divacky } 232f22ef01cSRoman Divacky // Print the preds of this block according to the CFG. 233f22ef01cSRoman Divacky if (!pred_empty()) { 2342754fe60SDimitry Andric if (Indexes) OS << '\t'; 235f22ef01cSRoman Divacky OS << " Predecessors according to CFG:"; 236f22ef01cSRoman Divacky for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 237f22ef01cSRoman Divacky OS << " BB#" << (*PI)->getNumber(); 238f22ef01cSRoman Divacky OS << '\n'; 239f22ef01cSRoman Divacky } 240f22ef01cSRoman Divacky 241f22ef01cSRoman Divacky for (const_iterator I = begin(); I != end(); ++I) { 2422754fe60SDimitry Andric if (Indexes) { 2432754fe60SDimitry Andric if (Indexes->hasIndex(I)) 2442754fe60SDimitry Andric OS << Indexes->getInstructionIndex(I); 2452754fe60SDimitry Andric OS << '\t'; 2462754fe60SDimitry Andric } 247f22ef01cSRoman Divacky OS << '\t'; 248f22ef01cSRoman Divacky I->print(OS, &getParent()->getTarget()); 249f22ef01cSRoman Divacky } 250f22ef01cSRoman Divacky 251f22ef01cSRoman Divacky // Print the successors of this block according to the CFG. 252f22ef01cSRoman Divacky if (!succ_empty()) { 2532754fe60SDimitry Andric if (Indexes) OS << '\t'; 254f22ef01cSRoman Divacky OS << " Successors according to CFG:"; 255f22ef01cSRoman Divacky for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 256f22ef01cSRoman Divacky OS << " BB#" << (*SI)->getNumber(); 257f22ef01cSRoman Divacky OS << '\n'; 258f22ef01cSRoman Divacky } 259f22ef01cSRoman Divacky } 260f22ef01cSRoman Divacky 261f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) { 262f22ef01cSRoman Divacky std::vector<unsigned>::iterator I = 263f22ef01cSRoman Divacky std::find(LiveIns.begin(), LiveIns.end(), Reg); 264f22ef01cSRoman Divacky assert(I != LiveIns.end() && "Not a live in!"); 265f22ef01cSRoman Divacky LiveIns.erase(I); 266f22ef01cSRoman Divacky } 267f22ef01cSRoman Divacky 268f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 269f22ef01cSRoman Divacky livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 270f22ef01cSRoman Divacky return I != livein_end(); 271f22ef01cSRoman Divacky } 272f22ef01cSRoman Divacky 273f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 274f22ef01cSRoman Divacky getParent()->splice(NewAfter, this); 275f22ef01cSRoman Divacky } 276f22ef01cSRoman Divacky 277f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 278f22ef01cSRoman Divacky MachineFunction::iterator BBI = NewBefore; 279f22ef01cSRoman Divacky getParent()->splice(++BBI, this); 280f22ef01cSRoman Divacky } 281f22ef01cSRoman Divacky 282f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() { 283f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 284f22ef01cSRoman Divacky // A block with no successors has no concerns with fall-through edges. 285f22ef01cSRoman Divacky if (this->succ_empty()) return; 286f22ef01cSRoman Divacky 287f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 288f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 289ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 290f22ef01cSRoman Divacky bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 291f22ef01cSRoman Divacky (void) B; 292f22ef01cSRoman Divacky assert(!B && "UpdateTerminators requires analyzable predecessors!"); 293f22ef01cSRoman Divacky if (Cond.empty()) { 294f22ef01cSRoman Divacky if (TBB) { 295f22ef01cSRoman Divacky // The block has an unconditional branch. If its successor is now 296f22ef01cSRoman Divacky // its layout successor, delete the branch. 297f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) 298f22ef01cSRoman Divacky TII->RemoveBranch(*this); 299f22ef01cSRoman Divacky } else { 300f22ef01cSRoman Divacky // The block has an unconditional fallthrough. If its successor is not 301f22ef01cSRoman Divacky // its layout successor, insert a branch. 302f22ef01cSRoman Divacky TBB = *succ_begin(); 303f22ef01cSRoman Divacky if (!isLayoutSuccessor(TBB)) 304ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 305f22ef01cSRoman Divacky } 306f22ef01cSRoman Divacky } else { 307f22ef01cSRoman Divacky if (FBB) { 308f22ef01cSRoman Divacky // The block has a non-fallthrough conditional branch. If one of its 309f22ef01cSRoman Divacky // successors is its layout successor, rewrite it to a fallthrough 310f22ef01cSRoman Divacky // conditional branch. 311f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 312f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) 313f22ef01cSRoman Divacky return; 314f22ef01cSRoman Divacky TII->RemoveBranch(*this); 315ffd1746dSEd Schouten TII->InsertBranch(*this, FBB, 0, Cond, dl); 316f22ef01cSRoman Divacky } else if (isLayoutSuccessor(FBB)) { 317f22ef01cSRoman Divacky TII->RemoveBranch(*this); 318ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 319f22ef01cSRoman Divacky } 320f22ef01cSRoman Divacky } else { 321f22ef01cSRoman Divacky // The block has a fallthrough conditional branch. 322f22ef01cSRoman Divacky MachineBasicBlock *MBBA = *succ_begin(); 323f22ef01cSRoman Divacky MachineBasicBlock *MBBB = *llvm::next(succ_begin()); 324f22ef01cSRoman Divacky if (MBBA == TBB) std::swap(MBBB, MBBA); 325f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 326f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) { 327f22ef01cSRoman Divacky // We can't reverse the condition, add an unconditional branch. 328f22ef01cSRoman Divacky Cond.clear(); 329ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 330f22ef01cSRoman Divacky return; 331f22ef01cSRoman Divacky } 332f22ef01cSRoman Divacky TII->RemoveBranch(*this); 333ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 334f22ef01cSRoman Divacky } else if (!isLayoutSuccessor(MBBA)) { 335f22ef01cSRoman Divacky TII->RemoveBranch(*this); 336ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, MBBA, Cond, dl); 337f22ef01cSRoman Divacky } 338f22ef01cSRoman Divacky } 339f22ef01cSRoman Divacky } 340f22ef01cSRoman Divacky } 341f22ef01cSRoman Divacky 342f22ef01cSRoman Divacky void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 343f22ef01cSRoman Divacky Successors.push_back(succ); 344f22ef01cSRoman Divacky succ->addPredecessor(this); 345f22ef01cSRoman Divacky } 346f22ef01cSRoman Divacky 347f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 348f22ef01cSRoman Divacky succ->removePredecessor(this); 349f22ef01cSRoman Divacky succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 350f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 351f22ef01cSRoman Divacky Successors.erase(I); 352f22ef01cSRoman Divacky } 353f22ef01cSRoman Divacky 354f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator 355f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) { 356f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 357f22ef01cSRoman Divacky (*I)->removePredecessor(this); 358f22ef01cSRoman Divacky return Successors.erase(I); 359f22ef01cSRoman Divacky } 360f22ef01cSRoman Divacky 361f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 362f22ef01cSRoman Divacky Predecessors.push_back(pred); 363f22ef01cSRoman Divacky } 364f22ef01cSRoman Divacky 365f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 3663b0f4066SDimitry Andric pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 367f22ef01cSRoman Divacky assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 368f22ef01cSRoman Divacky Predecessors.erase(I); 369f22ef01cSRoman Divacky } 370f22ef01cSRoman Divacky 371f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 372f22ef01cSRoman Divacky if (this == fromMBB) 373f22ef01cSRoman Divacky return; 374f22ef01cSRoman Divacky 375ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 376ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 377ffd1746dSEd Schouten addSuccessor(Succ); 378ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 379ffd1746dSEd Schouten } 380ffd1746dSEd Schouten } 381f22ef01cSRoman Divacky 382ffd1746dSEd Schouten void 383ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 384ffd1746dSEd Schouten if (this == fromMBB) 385ffd1746dSEd Schouten return; 386ffd1746dSEd Schouten 387ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 388ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 389ffd1746dSEd Schouten addSuccessor(Succ); 390ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 391ffd1746dSEd Schouten 392ffd1746dSEd Schouten // Fix up any PHI nodes in the successor. 393ffd1746dSEd Schouten for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end(); 394ffd1746dSEd Schouten MI != ME && MI->isPHI(); ++MI) 395ffd1746dSEd Schouten for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 396ffd1746dSEd Schouten MachineOperand &MO = MI->getOperand(i); 397ffd1746dSEd Schouten if (MO.getMBB() == fromMBB) 398ffd1746dSEd Schouten MO.setMBB(this); 399ffd1746dSEd Schouten } 400ffd1746dSEd Schouten } 401f22ef01cSRoman Divacky } 402f22ef01cSRoman Divacky 403f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 4043b0f4066SDimitry Andric const_succ_iterator I = std::find(Successors.begin(), Successors.end(), MBB); 405f22ef01cSRoman Divacky return I != Successors.end(); 406f22ef01cSRoman Divacky } 407f22ef01cSRoman Divacky 408f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 409f22ef01cSRoman Divacky MachineFunction::const_iterator I(this); 410f22ef01cSRoman Divacky return llvm::next(I) == MachineFunction::const_iterator(MBB); 411f22ef01cSRoman Divacky } 412f22ef01cSRoman Divacky 413f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() { 414f22ef01cSRoman Divacky MachineFunction::iterator Fallthrough = this; 415f22ef01cSRoman Divacky ++Fallthrough; 416f22ef01cSRoman Divacky // If FallthroughBlock is off the end of the function, it can't fall through. 417f22ef01cSRoman Divacky if (Fallthrough == getParent()->end()) 418f22ef01cSRoman Divacky return false; 419f22ef01cSRoman Divacky 420f22ef01cSRoman Divacky // If FallthroughBlock isn't a successor, no fallthrough is possible. 421f22ef01cSRoman Divacky if (!isSuccessor(Fallthrough)) 422f22ef01cSRoman Divacky return false; 423f22ef01cSRoman Divacky 424f22ef01cSRoman Divacky // Analyze the branches, if any, at the end of the block. 425f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 426f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 427f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 428f22ef01cSRoman Divacky if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 429f22ef01cSRoman Divacky // If we couldn't analyze the branch, examine the last instruction. 430f22ef01cSRoman Divacky // If the block doesn't end in a known control barrier, assume fallthrough 431f22ef01cSRoman Divacky // is possible. The isPredicable check is needed because this code can be 432f22ef01cSRoman Divacky // called during IfConversion, where an instruction which is normally a 433f22ef01cSRoman Divacky // Barrier is predicated and thus no longer an actual control barrier. This 434f22ef01cSRoman Divacky // is over-conservative though, because if an instruction isn't actually 435f22ef01cSRoman Divacky // predicated we could still treat it like a barrier. 436f22ef01cSRoman Divacky return empty() || !back().getDesc().isBarrier() || 437f22ef01cSRoman Divacky back().getDesc().isPredicable(); 438f22ef01cSRoman Divacky } 439f22ef01cSRoman Divacky 440f22ef01cSRoman Divacky // If there is no branch, control always falls through. 441f22ef01cSRoman Divacky if (TBB == 0) return true; 442f22ef01cSRoman Divacky 443f22ef01cSRoman Divacky // If there is some explicit branch to the fallthrough block, it can obviously 444f22ef01cSRoman Divacky // reach, even though the branch should get folded to fall through implicitly. 445f22ef01cSRoman Divacky if (MachineFunction::iterator(TBB) == Fallthrough || 446f22ef01cSRoman Divacky MachineFunction::iterator(FBB) == Fallthrough) 447f22ef01cSRoman Divacky return true; 448f22ef01cSRoman Divacky 449f22ef01cSRoman Divacky // If it's an unconditional branch to some block not the fall through, it 450f22ef01cSRoman Divacky // doesn't fall through. 451f22ef01cSRoman Divacky if (Cond.empty()) return false; 452f22ef01cSRoman Divacky 453f22ef01cSRoman Divacky // Otherwise, if it is conditional and has no explicit false block, it falls 454f22ef01cSRoman Divacky // through. 455f22ef01cSRoman Divacky return FBB == 0; 456f22ef01cSRoman Divacky } 457f22ef01cSRoman Divacky 458ffd1746dSEd Schouten MachineBasicBlock * 459ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 460ffd1746dSEd Schouten MachineFunction *MF = getParent(); 461ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 462ffd1746dSEd Schouten 4632754fe60SDimitry Andric // We may need to update this's terminator, but we can't do that if 4642754fe60SDimitry Andric // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 465ffd1746dSEd Schouten const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 466ffd1746dSEd Schouten MachineBasicBlock *TBB = 0, *FBB = 0; 467ffd1746dSEd Schouten SmallVector<MachineOperand, 4> Cond; 468ffd1746dSEd Schouten if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 469ffd1746dSEd Schouten return NULL; 470ffd1746dSEd Schouten 4712754fe60SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 4722754fe60SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 4732754fe60SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 4742754fe60SDimitry Andric // code, just skip those edges. 4752754fe60SDimitry Andric if (TBB && TBB == FBB) { 4762754fe60SDimitry Andric DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 4772754fe60SDimitry Andric << getNumber() << '\n'); 4782754fe60SDimitry Andric return NULL; 4792754fe60SDimitry Andric } 4802754fe60SDimitry Andric 481ffd1746dSEd Schouten MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 482ffd1746dSEd Schouten MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 483e580952dSDimitry Andric DEBUG(dbgs() << "Splitting critical edge:" 484ffd1746dSEd Schouten " BB#" << getNumber() 485ffd1746dSEd Schouten << " -- BB#" << NMBB->getNumber() 486ffd1746dSEd Schouten << " -- BB#" << Succ->getNumber() << '\n'); 487ffd1746dSEd Schouten 488bd5abe19SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 489bd5abe19SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 490bd5abe19SDimitry Andric // terminators. 491bd5abe19SDimitry Andric LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 492bd5abe19SDimitry Andric 493bd5abe19SDimitry Andric // Collect a list of virtual registers killed by the terminators. 494bd5abe19SDimitry Andric SmallVector<unsigned, 4> KilledRegs; 495bd5abe19SDimitry Andric if (LV) 496bd5abe19SDimitry Andric for (iterator I = getFirstTerminator(), E = end(); I != E; ++I) { 497bd5abe19SDimitry Andric MachineInstr *MI = I; 498bd5abe19SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 499bd5abe19SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 500bd5abe19SDimitry Andric if (!OI->isReg() || !OI->isUse() || !OI->isKill() || OI->isUndef()) 501bd5abe19SDimitry Andric continue; 502bd5abe19SDimitry Andric unsigned Reg = OI->getReg(); 503bd5abe19SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Reg) && 504bd5abe19SDimitry Andric LV->getVarInfo(Reg).removeKill(MI)) { 505bd5abe19SDimitry Andric KilledRegs.push_back(Reg); 506bd5abe19SDimitry Andric DEBUG(dbgs() << "Removing terminator kill: " << *MI); 507bd5abe19SDimitry Andric OI->setIsKill(false); 508bd5abe19SDimitry Andric } 509bd5abe19SDimitry Andric } 510bd5abe19SDimitry Andric } 511bd5abe19SDimitry Andric 512ffd1746dSEd Schouten ReplaceUsesOfBlockWith(Succ, NMBB); 513ffd1746dSEd Schouten updateTerminator(); 514ffd1746dSEd Schouten 515ffd1746dSEd Schouten // Insert unconditional "jump Succ" instruction in NMBB if necessary. 516ffd1746dSEd Schouten NMBB->addSuccessor(Succ); 517ffd1746dSEd Schouten if (!NMBB->isLayoutSuccessor(Succ)) { 518ffd1746dSEd Schouten Cond.clear(); 519ffd1746dSEd Schouten MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 520ffd1746dSEd Schouten } 521ffd1746dSEd Schouten 522ffd1746dSEd Schouten // Fix PHI nodes in Succ so they refer to NMBB instead of this 523ffd1746dSEd Schouten for (MachineBasicBlock::iterator i = Succ->begin(), e = Succ->end(); 524ffd1746dSEd Schouten i != e && i->isPHI(); ++i) 525ffd1746dSEd Schouten for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 526ffd1746dSEd Schouten if (i->getOperand(ni+1).getMBB() == this) 527ffd1746dSEd Schouten i->getOperand(ni+1).setMBB(NMBB); 528ffd1746dSEd Schouten 529bd5abe19SDimitry Andric // Update LiveVariables. 530bd5abe19SDimitry Andric if (LV) { 531bd5abe19SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 532bd5abe19SDimitry Andric while (!KilledRegs.empty()) { 533bd5abe19SDimitry Andric unsigned Reg = KilledRegs.pop_back_val(); 534bd5abe19SDimitry Andric for (iterator I = end(), E = begin(); I != E;) { 535bd5abe19SDimitry Andric if (!(--I)->addRegisterKilled(Reg, NULL, /* addIfNotFound= */ false)) 536bd5abe19SDimitry Andric continue; 537bd5abe19SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(I); 538bd5abe19SDimitry Andric DEBUG(dbgs() << "Restored terminator kill: " << *I); 539bd5abe19SDimitry Andric break; 540bd5abe19SDimitry Andric } 541bd5abe19SDimitry Andric } 542bd5abe19SDimitry Andric // Update relevant live-through information. 543ffd1746dSEd Schouten LV->addNewBlock(NMBB, this, Succ); 544bd5abe19SDimitry Andric } 545ffd1746dSEd Schouten 546ffd1746dSEd Schouten if (MachineDominatorTree *MDT = 547e580952dSDimitry Andric P->getAnalysisIfAvailable<MachineDominatorTree>()) { 548e580952dSDimitry Andric // Update dominator information. 549e580952dSDimitry Andric MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 550ffd1746dSEd Schouten 551e580952dSDimitry Andric bool IsNewIDom = true; 552e580952dSDimitry Andric for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 553e580952dSDimitry Andric PI != E; ++PI) { 554e580952dSDimitry Andric MachineBasicBlock *PredBB = *PI; 555e580952dSDimitry Andric if (PredBB == NMBB) 556e580952dSDimitry Andric continue; 557e580952dSDimitry Andric if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 558e580952dSDimitry Andric IsNewIDom = false; 559e580952dSDimitry Andric break; 560e580952dSDimitry Andric } 561e580952dSDimitry Andric } 562e580952dSDimitry Andric 563e580952dSDimitry Andric // We know "this" dominates the newly created basic block. 564e580952dSDimitry Andric MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 565e580952dSDimitry Andric 566e580952dSDimitry Andric // If all the other predecessors of "Succ" are dominated by "Succ" itself 567e580952dSDimitry Andric // then the new block is the new immediate dominator of "Succ". Otherwise, 568e580952dSDimitry Andric // the new block doesn't dominate anything. 569e580952dSDimitry Andric if (IsNewIDom) 570e580952dSDimitry Andric MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 571e580952dSDimitry Andric } 572e580952dSDimitry Andric 573e580952dSDimitry Andric if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 574ffd1746dSEd Schouten if (MachineLoop *TIL = MLI->getLoopFor(this)) { 575ffd1746dSEd Schouten // If one or the other blocks were not in a loop, the new block is not 576ffd1746dSEd Schouten // either, and thus LI doesn't need to be updated. 577ffd1746dSEd Schouten if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 578ffd1746dSEd Schouten if (TIL == DestLoop) { 579ffd1746dSEd Schouten // Both in the same loop, the NMBB joins loop. 580ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 581ffd1746dSEd Schouten } else if (TIL->contains(DestLoop)) { 582ffd1746dSEd Schouten // Edge from an outer loop to an inner loop. Add to the outer loop. 583ffd1746dSEd Schouten TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 584ffd1746dSEd Schouten } else if (DestLoop->contains(TIL)) { 585ffd1746dSEd Schouten // Edge from an inner loop to an outer loop. Add to the outer loop. 586ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 587ffd1746dSEd Schouten } else { 588ffd1746dSEd Schouten // Edge from two loops with no containment relation. Because these 589ffd1746dSEd Schouten // are natural loops, we know that the destination block must be the 590ffd1746dSEd Schouten // header of its loop (adding a branch into a loop elsewhere would 591ffd1746dSEd Schouten // create an irreducible loop). 592ffd1746dSEd Schouten assert(DestLoop->getHeader() == Succ && 593ffd1746dSEd Schouten "Should not create irreducible loops!"); 594ffd1746dSEd Schouten if (MachineLoop *P = DestLoop->getParentLoop()) 595ffd1746dSEd Schouten P->addBasicBlockToLoop(NMBB, MLI->getBase()); 596ffd1746dSEd Schouten } 597ffd1746dSEd Schouten } 598ffd1746dSEd Schouten } 599ffd1746dSEd Schouten 600ffd1746dSEd Schouten return NMBB; 601ffd1746dSEd Schouten } 602ffd1746dSEd Schouten 603f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function, 604f22ef01cSRoman Divacky /// and returns it, but does not delete it. 605f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() { 606f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 607f22ef01cSRoman Divacky getParent()->remove(this); 608f22ef01cSRoman Divacky return this; 609f22ef01cSRoman Divacky } 610f22ef01cSRoman Divacky 611f22ef01cSRoman Divacky 612f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function, 613f22ef01cSRoman Divacky /// and deletes it. 614f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() { 615f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 616f22ef01cSRoman Divacky getParent()->erase(this); 617f22ef01cSRoman Divacky } 618f22ef01cSRoman Divacky 619f22ef01cSRoman Divacky 620f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 621f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead. 622f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 623f22ef01cSRoman Divacky MachineBasicBlock *New) { 624f22ef01cSRoman Divacky assert(Old != New && "Cannot replace self with self!"); 625f22ef01cSRoman Divacky 626f22ef01cSRoman Divacky MachineBasicBlock::iterator I = end(); 627f22ef01cSRoman Divacky while (I != begin()) { 628f22ef01cSRoman Divacky --I; 629f22ef01cSRoman Divacky if (!I->getDesc().isTerminator()) break; 630f22ef01cSRoman Divacky 631f22ef01cSRoman Divacky // Scan the operands of this machine instruction, replacing any uses of Old 632f22ef01cSRoman Divacky // with New. 633f22ef01cSRoman Divacky for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 634f22ef01cSRoman Divacky if (I->getOperand(i).isMBB() && 635f22ef01cSRoman Divacky I->getOperand(i).getMBB() == Old) 636f22ef01cSRoman Divacky I->getOperand(i).setMBB(New); 637f22ef01cSRoman Divacky } 638f22ef01cSRoman Divacky 639f22ef01cSRoman Divacky // Update the successor information. 640f22ef01cSRoman Divacky removeSuccessor(Old); 641f22ef01cSRoman Divacky addSuccessor(New); 642f22ef01cSRoman Divacky } 643f22ef01cSRoman Divacky 644f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 645f22ef01cSRoman Divacky /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 646f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 647f22ef01cSRoman Divacky /// null. 648f22ef01cSRoman Divacky /// 649f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads 650f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here). 651f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads. 652f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 653f22ef01cSRoman Divacky MachineBasicBlock *DestB, 654f22ef01cSRoman Divacky bool isCond) { 655f22ef01cSRoman Divacky // The values of DestA and DestB frequently come from a call to the 656f22ef01cSRoman Divacky // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 657f22ef01cSRoman Divacky // values from there. 658f22ef01cSRoman Divacky // 659f22ef01cSRoman Divacky // 1. If both DestA and DestB are null, then the block ends with no branches 660f22ef01cSRoman Divacky // (it falls through to its successor). 661f22ef01cSRoman Divacky // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 662f22ef01cSRoman Divacky // with only an unconditional branch. 663f22ef01cSRoman Divacky // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 664f22ef01cSRoman Divacky // with a conditional branch that falls through to a successor (DestB). 665f22ef01cSRoman Divacky // 4. If DestA and DestB is set and isCond is true, then the block ends with a 666f22ef01cSRoman Divacky // conditional branch followed by an unconditional branch. DestA is the 667f22ef01cSRoman Divacky // 'true' destination and DestB is the 'false' destination. 668f22ef01cSRoman Divacky 669f22ef01cSRoman Divacky bool Changed = false; 670f22ef01cSRoman Divacky 671f22ef01cSRoman Divacky MachineFunction::iterator FallThru = 672f22ef01cSRoman Divacky llvm::next(MachineFunction::iterator(this)); 673f22ef01cSRoman Divacky 674f22ef01cSRoman Divacky if (DestA == 0 && DestB == 0) { 675f22ef01cSRoman Divacky // Block falls through to successor. 676f22ef01cSRoman Divacky DestA = FallThru; 677f22ef01cSRoman Divacky DestB = FallThru; 678f22ef01cSRoman Divacky } else if (DestA != 0 && DestB == 0) { 679f22ef01cSRoman Divacky if (isCond) 680f22ef01cSRoman Divacky // Block ends in conditional jump that falls through to successor. 681f22ef01cSRoman Divacky DestB = FallThru; 682f22ef01cSRoman Divacky } else { 683f22ef01cSRoman Divacky assert(DestA && DestB && isCond && 684f22ef01cSRoman Divacky "CFG in a bad state. Cannot correct CFG edges"); 685f22ef01cSRoman Divacky } 686f22ef01cSRoman Divacky 687f22ef01cSRoman Divacky // Remove superfluous edges. I.e., those which aren't destinations of this 688f22ef01cSRoman Divacky // basic block, duplicate edges, or landing pads. 689f22ef01cSRoman Divacky SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 690f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator SI = succ_begin(); 691f22ef01cSRoman Divacky while (SI != succ_end()) { 692f22ef01cSRoman Divacky const MachineBasicBlock *MBB = *SI; 693f22ef01cSRoman Divacky if (!SeenMBBs.insert(MBB) || 694f22ef01cSRoman Divacky (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 695f22ef01cSRoman Divacky // This is a superfluous edge, remove it. 696f22ef01cSRoman Divacky SI = removeSuccessor(SI); 697f22ef01cSRoman Divacky Changed = true; 698f22ef01cSRoman Divacky } else { 699f22ef01cSRoman Divacky ++SI; 700f22ef01cSRoman Divacky } 701f22ef01cSRoman Divacky } 702f22ef01cSRoman Divacky 703f22ef01cSRoman Divacky return Changed; 704f22ef01cSRoman Divacky } 705f22ef01cSRoman Divacky 706f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 707f22ef01cSRoman Divacky /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 708f22ef01cSRoman Divacky DebugLoc 709f22ef01cSRoman Divacky MachineBasicBlock::findDebugLoc(MachineBasicBlock::iterator &MBBI) { 710f22ef01cSRoman Divacky DebugLoc DL; 711f22ef01cSRoman Divacky MachineBasicBlock::iterator E = end(); 712f22ef01cSRoman Divacky if (MBBI != E) { 713f22ef01cSRoman Divacky // Skip debug declarations, we don't want a DebugLoc from them. 714f22ef01cSRoman Divacky MachineBasicBlock::iterator MBBI2 = MBBI; 715f22ef01cSRoman Divacky while (MBBI2 != E && MBBI2->isDebugValue()) 716f22ef01cSRoman Divacky MBBI2++; 717f22ef01cSRoman Divacky if (MBBI2 != E) 718f22ef01cSRoman Divacky DL = MBBI2->getDebugLoc(); 719f22ef01cSRoman Divacky } 720f22ef01cSRoman Divacky return DL; 721f22ef01cSRoman Divacky } 722f22ef01cSRoman Divacky 723f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 724f22ef01cSRoman Divacky bool t) { 725f22ef01cSRoman Divacky OS << "BB#" << MBB->getNumber(); 726f22ef01cSRoman Divacky } 727f22ef01cSRoman Divacky 728