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/TargetInstrInfo.h" 26f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h" 27f22ef01cSRoman Divacky #include "llvm/Assembly/Writer.h" 28f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 29f22ef01cSRoman Divacky #include "llvm/ADT/SmallPtrSet.h" 30f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 31f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h" 32f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 33f22ef01cSRoman Divacky #include <algorithm> 34f22ef01cSRoman Divacky using namespace llvm; 35f22ef01cSRoman Divacky 36f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 37f22ef01cSRoman Divacky : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 38f22ef01cSRoman Divacky AddressTaken(false) { 39f22ef01cSRoman Divacky Insts.Parent = this; 40f22ef01cSRoman Divacky } 41f22ef01cSRoman Divacky 42f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() { 43f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(this); 44f22ef01cSRoman Divacky } 45f22ef01cSRoman Divacky 46f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block. 47f22ef01cSRoman Divacky /// 48f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const { 49f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 50f22ef01cSRoman Divacky MCContext &Ctx = MF->getContext(); 51f22ef01cSRoman Divacky const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 52f22ef01cSRoman Divacky return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 53f22ef01cSRoman Divacky Twine(MF->getFunctionNumber()) + "_" + 54f22ef01cSRoman Divacky Twine(getNumber())); 55f22ef01cSRoman Divacky } 56f22ef01cSRoman Divacky 57f22ef01cSRoman Divacky 58f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 59f22ef01cSRoman Divacky MBB.print(OS); 60f22ef01cSRoman Divacky return OS; 61f22ef01cSRoman Divacky } 62f22ef01cSRoman Divacky 63f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 64f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the 65f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers. 66f22ef01cSRoman Divacky /// 67f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 68f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a 69f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1. 70f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 71f22ef01cSRoman Divacky MachineFunction &MF = *N->getParent(); 72f22ef01cSRoman Divacky N->Number = MF.addToMBBNumbering(N); 73f22ef01cSRoman Divacky 74f22ef01cSRoman Divacky // Make sure the instructions have their operands in the reginfo lists. 75f22ef01cSRoman Divacky MachineRegisterInfo &RegInfo = MF.getRegInfo(); 76f22ef01cSRoman Divacky for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) 77f22ef01cSRoman Divacky I->AddRegOperandsToUseLists(RegInfo); 78f22ef01cSRoman Divacky 79f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 80f22ef01cSRoman Divacky } 81f22ef01cSRoman Divacky 82f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 83f22ef01cSRoman Divacky N->getParent()->removeFromMBBNumbering(N->Number); 84f22ef01cSRoman Divacky N->Number = -1; 85f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 86f22ef01cSRoman Divacky } 87f22ef01cSRoman Divacky 88f22ef01cSRoman Divacky 89f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block 90f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def 91f22ef01cSRoman Divacky /// lists if appropriate. 92f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 93f22ef01cSRoman Divacky assert(N->getParent() == 0 && "machine instruction already in a basic block"); 94f22ef01cSRoman Divacky N->setParent(Parent); 95f22ef01cSRoman Divacky 96f22ef01cSRoman Divacky // Add the instruction's register operands to their corresponding 97f22ef01cSRoman Divacky // use/def lists. 98f22ef01cSRoman Divacky MachineFunction *MF = Parent->getParent(); 99f22ef01cSRoman Divacky N->AddRegOperandsToUseLists(MF->getRegInfo()); 100f22ef01cSRoman Divacky 101f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 102f22ef01cSRoman Divacky } 103f22ef01cSRoman Divacky 104f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block 105f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def 106f22ef01cSRoman Divacky /// lists if appropriate. 107f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 108f22ef01cSRoman Divacky assert(N->getParent() != 0 && "machine instruction not in a basic block"); 109f22ef01cSRoman Divacky 110f22ef01cSRoman Divacky // Remove from the use/def lists. 111f22ef01cSRoman Divacky N->RemoveRegOperandsFromUseLists(); 112f22ef01cSRoman Divacky 113f22ef01cSRoman Divacky N->setParent(0); 114f22ef01cSRoman Divacky 115f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 116f22ef01cSRoman Divacky } 117f22ef01cSRoman Divacky 118f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one 119f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def 120f22ef01cSRoman Divacky /// lists. 121f22ef01cSRoman Divacky void ilist_traits<MachineInstr>:: 122f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList, 123f22ef01cSRoman Divacky MachineBasicBlock::iterator first, 124f22ef01cSRoman Divacky MachineBasicBlock::iterator last) { 125f22ef01cSRoman Divacky assert(Parent->getParent() == fromList.Parent->getParent() && 126f22ef01cSRoman Divacky "MachineInstr parent mismatch!"); 127f22ef01cSRoman Divacky 128f22ef01cSRoman Divacky // Splice within the same MBB -> no change. 129f22ef01cSRoman Divacky if (Parent == fromList.Parent) return; 130f22ef01cSRoman Divacky 131f22ef01cSRoman Divacky // If splicing between two blocks within the same function, just update the 132f22ef01cSRoman Divacky // parent pointers. 133f22ef01cSRoman Divacky for (; first != last; ++first) 134f22ef01cSRoman Divacky first->setParent(Parent); 135f22ef01cSRoman Divacky } 136f22ef01cSRoman Divacky 137f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 138f22ef01cSRoman Divacky assert(!MI->getParent() && "MI is still in a block!"); 139f22ef01cSRoman Divacky Parent->getParent()->DeleteMachineInstr(MI); 140f22ef01cSRoman Divacky } 141f22ef01cSRoman Divacky 142ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 143ffd1746dSEd Schouten iterator I = begin(); 144ffd1746dSEd Schouten while (I != end() && I->isPHI()) 145ffd1746dSEd Schouten ++I; 146ffd1746dSEd Schouten return I; 147ffd1746dSEd Schouten } 148ffd1746dSEd Schouten 1492754fe60SDimitry Andric MachineBasicBlock::iterator 1502754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 1512754fe60SDimitry Andric while (I != end() && (I->isPHI() || I->isLabel() || I->isDebugValue())) 1522754fe60SDimitry Andric ++I; 1532754fe60SDimitry Andric return I; 1542754fe60SDimitry Andric } 1552754fe60SDimitry Andric 156f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 157f22ef01cSRoman Divacky iterator I = end(); 1582754fe60SDimitry Andric while (I != begin() && ((--I)->getDesc().isTerminator() || I->isDebugValue())) 159f22ef01cSRoman Divacky ; /*noop */ 1602754fe60SDimitry Andric while (I != end() && !I->getDesc().isTerminator()) 1612754fe60SDimitry Andric ++I; 162f22ef01cSRoman Divacky return I; 163f22ef01cSRoman Divacky } 164f22ef01cSRoman Divacky 1652754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 1662754fe60SDimitry Andric iterator B = begin(), I = end(); 1672754fe60SDimitry Andric while (I != B) { 1682754fe60SDimitry Andric --I; 1692754fe60SDimitry Andric if (I->isDebugValue()) 1702754fe60SDimitry Andric continue; 1712754fe60SDimitry Andric return I; 1722754fe60SDimitry Andric } 1732754fe60SDimitry Andric // The block is all debug values. 1742754fe60SDimitry Andric return end(); 1752754fe60SDimitry Andric } 1762754fe60SDimitry Andric 1772754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 1782754fe60SDimitry Andric // A block with a landing pad successor only has one other successor. 1792754fe60SDimitry Andric if (succ_size() > 2) 1802754fe60SDimitry Andric return 0; 1812754fe60SDimitry Andric for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 1822754fe60SDimitry Andric if ((*I)->isLandingPad()) 1832754fe60SDimitry Andric return *I; 1842754fe60SDimitry Andric return 0; 1852754fe60SDimitry Andric } 1862754fe60SDimitry Andric 187f22ef01cSRoman Divacky void MachineBasicBlock::dump() const { 188f22ef01cSRoman Divacky print(dbgs()); 189f22ef01cSRoman Divacky } 190f22ef01cSRoman Divacky 191f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const { 192f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) 193f22ef01cSRoman Divacky return LBB->getName(); 194f22ef01cSRoman Divacky else 195f22ef01cSRoman Divacky return "(null)"; 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky 1982754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 199f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 200f22ef01cSRoman Divacky if (!MF) { 201f22ef01cSRoman Divacky OS << "Can't print out MachineBasicBlock because parent MachineFunction" 202f22ef01cSRoman Divacky << " is null\n"; 203f22ef01cSRoman Divacky return; 204f22ef01cSRoman Divacky } 205f22ef01cSRoman Divacky 206f22ef01cSRoman Divacky if (Alignment) { OS << "Alignment " << Alignment << "\n"; } 207f22ef01cSRoman Divacky 2082754fe60SDimitry Andric if (Indexes) 2092754fe60SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 2102754fe60SDimitry Andric 211f22ef01cSRoman Divacky OS << "BB#" << getNumber() << ": "; 212f22ef01cSRoman Divacky 213f22ef01cSRoman Divacky const char *Comma = ""; 214f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) { 215f22ef01cSRoman Divacky OS << Comma << "derived from LLVM BB "; 216f22ef01cSRoman Divacky WriteAsOperand(OS, LBB, /*PrintType=*/false); 217f22ef01cSRoman Divacky Comma = ", "; 218f22ef01cSRoman Divacky } 219f22ef01cSRoman Divacky if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 220f22ef01cSRoman Divacky if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 221f22ef01cSRoman Divacky OS << '\n'; 222f22ef01cSRoman Divacky 223f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 224f22ef01cSRoman Divacky if (!livein_empty()) { 2252754fe60SDimitry Andric if (Indexes) OS << '\t'; 226f22ef01cSRoman Divacky OS << " Live Ins:"; 227f22ef01cSRoman Divacky for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 2282754fe60SDimitry Andric OS << ' ' << PrintReg(*I, TRI); 229f22ef01cSRoman Divacky OS << '\n'; 230f22ef01cSRoman Divacky } 231f22ef01cSRoman Divacky // Print the preds of this block according to the CFG. 232f22ef01cSRoman Divacky if (!pred_empty()) { 2332754fe60SDimitry Andric if (Indexes) OS << '\t'; 234f22ef01cSRoman Divacky OS << " Predecessors according to CFG:"; 235f22ef01cSRoman Divacky for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 236f22ef01cSRoman Divacky OS << " BB#" << (*PI)->getNumber(); 237f22ef01cSRoman Divacky OS << '\n'; 238f22ef01cSRoman Divacky } 239f22ef01cSRoman Divacky 240f22ef01cSRoman Divacky for (const_iterator I = begin(); I != end(); ++I) { 2412754fe60SDimitry Andric if (Indexes) { 2422754fe60SDimitry Andric if (Indexes->hasIndex(I)) 2432754fe60SDimitry Andric OS << Indexes->getInstructionIndex(I); 2442754fe60SDimitry Andric OS << '\t'; 2452754fe60SDimitry Andric } 246f22ef01cSRoman Divacky OS << '\t'; 247f22ef01cSRoman Divacky I->print(OS, &getParent()->getTarget()); 248f22ef01cSRoman Divacky } 249f22ef01cSRoman Divacky 250f22ef01cSRoman Divacky // Print the successors of this block according to the CFG. 251f22ef01cSRoman Divacky if (!succ_empty()) { 2522754fe60SDimitry Andric if (Indexes) OS << '\t'; 253f22ef01cSRoman Divacky OS << " Successors according to CFG:"; 254f22ef01cSRoman Divacky for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 255f22ef01cSRoman Divacky OS << " BB#" << (*SI)->getNumber(); 256f22ef01cSRoman Divacky OS << '\n'; 257f22ef01cSRoman Divacky } 258f22ef01cSRoman Divacky } 259f22ef01cSRoman Divacky 260f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) { 261f22ef01cSRoman Divacky std::vector<unsigned>::iterator I = 262f22ef01cSRoman Divacky std::find(LiveIns.begin(), LiveIns.end(), Reg); 263f22ef01cSRoman Divacky assert(I != LiveIns.end() && "Not a live in!"); 264f22ef01cSRoman Divacky LiveIns.erase(I); 265f22ef01cSRoman Divacky } 266f22ef01cSRoman Divacky 267f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 268f22ef01cSRoman Divacky livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 269f22ef01cSRoman Divacky return I != livein_end(); 270f22ef01cSRoman Divacky } 271f22ef01cSRoman Divacky 272f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 273f22ef01cSRoman Divacky getParent()->splice(NewAfter, this); 274f22ef01cSRoman Divacky } 275f22ef01cSRoman Divacky 276f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 277f22ef01cSRoman Divacky MachineFunction::iterator BBI = NewBefore; 278f22ef01cSRoman Divacky getParent()->splice(++BBI, this); 279f22ef01cSRoman Divacky } 280f22ef01cSRoman Divacky 281f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() { 282f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 283f22ef01cSRoman Divacky // A block with no successors has no concerns with fall-through edges. 284f22ef01cSRoman Divacky if (this->succ_empty()) return; 285f22ef01cSRoman Divacky 286f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 287f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 288ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 289f22ef01cSRoman Divacky bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 290f22ef01cSRoman Divacky (void) B; 291f22ef01cSRoman Divacky assert(!B && "UpdateTerminators requires analyzable predecessors!"); 292f22ef01cSRoman Divacky if (Cond.empty()) { 293f22ef01cSRoman Divacky if (TBB) { 294f22ef01cSRoman Divacky // The block has an unconditional branch. If its successor is now 295f22ef01cSRoman Divacky // its layout successor, delete the branch. 296f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) 297f22ef01cSRoman Divacky TII->RemoveBranch(*this); 298f22ef01cSRoman Divacky } else { 299f22ef01cSRoman Divacky // The block has an unconditional fallthrough. If its successor is not 300f22ef01cSRoman Divacky // its layout successor, insert a branch. 301f22ef01cSRoman Divacky TBB = *succ_begin(); 302f22ef01cSRoman Divacky if (!isLayoutSuccessor(TBB)) 303ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 304f22ef01cSRoman Divacky } 305f22ef01cSRoman Divacky } else { 306f22ef01cSRoman Divacky if (FBB) { 307f22ef01cSRoman Divacky // The block has a non-fallthrough conditional branch. If one of its 308f22ef01cSRoman Divacky // successors is its layout successor, rewrite it to a fallthrough 309f22ef01cSRoman Divacky // conditional branch. 310f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 311f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) 312f22ef01cSRoman Divacky return; 313f22ef01cSRoman Divacky TII->RemoveBranch(*this); 314ffd1746dSEd Schouten TII->InsertBranch(*this, FBB, 0, Cond, dl); 315f22ef01cSRoman Divacky } else if (isLayoutSuccessor(FBB)) { 316f22ef01cSRoman Divacky TII->RemoveBranch(*this); 317ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 318f22ef01cSRoman Divacky } 319f22ef01cSRoman Divacky } else { 320f22ef01cSRoman Divacky // The block has a fallthrough conditional branch. 321f22ef01cSRoman Divacky MachineBasicBlock *MBBA = *succ_begin(); 322f22ef01cSRoman Divacky MachineBasicBlock *MBBB = *llvm::next(succ_begin()); 323f22ef01cSRoman Divacky if (MBBA == TBB) std::swap(MBBB, MBBA); 324f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 325f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) { 326f22ef01cSRoman Divacky // We can't reverse the condition, add an unconditional branch. 327f22ef01cSRoman Divacky Cond.clear(); 328ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 329f22ef01cSRoman Divacky return; 330f22ef01cSRoman Divacky } 331f22ef01cSRoman Divacky TII->RemoveBranch(*this); 332ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 333f22ef01cSRoman Divacky } else if (!isLayoutSuccessor(MBBA)) { 334f22ef01cSRoman Divacky TII->RemoveBranch(*this); 335ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, MBBA, Cond, dl); 336f22ef01cSRoman Divacky } 337f22ef01cSRoman Divacky } 338f22ef01cSRoman Divacky } 339f22ef01cSRoman Divacky } 340f22ef01cSRoman Divacky 34117a519f9SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { 34217a519f9SDimitry Andric 34317a519f9SDimitry Andric // If we see non-zero value for the first time it means we actually use Weight 34417a519f9SDimitry Andric // list, so we fill all Weights with 0's. 34517a519f9SDimitry Andric if (weight != 0 && Weights.empty()) 34617a519f9SDimitry Andric Weights.resize(Successors.size()); 34717a519f9SDimitry Andric 34817a519f9SDimitry Andric if (weight != 0 || !Weights.empty()) 34917a519f9SDimitry Andric Weights.push_back(weight); 35017a519f9SDimitry Andric 351f22ef01cSRoman Divacky Successors.push_back(succ); 352f22ef01cSRoman Divacky succ->addPredecessor(this); 353f22ef01cSRoman Divacky } 354f22ef01cSRoman Divacky 355f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 356f22ef01cSRoman Divacky succ->removePredecessor(this); 357f22ef01cSRoman Divacky succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 358f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 35917a519f9SDimitry Andric 36017a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 36117a519f9SDimitry Andric if (!Weights.empty()) { 36217a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 36317a519f9SDimitry Andric Weights.erase(WI); 36417a519f9SDimitry Andric } 36517a519f9SDimitry Andric 366f22ef01cSRoman Divacky Successors.erase(I); 367f22ef01cSRoman Divacky } 368f22ef01cSRoman Divacky 369f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator 370f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) { 371f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 37217a519f9SDimitry Andric 37317a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 37417a519f9SDimitry Andric if (!Weights.empty()) { 37517a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 37617a519f9SDimitry Andric Weights.erase(WI); 37717a519f9SDimitry Andric } 37817a519f9SDimitry Andric 379f22ef01cSRoman Divacky (*I)->removePredecessor(this); 380f22ef01cSRoman Divacky return Successors.erase(I); 381f22ef01cSRoman Divacky } 382f22ef01cSRoman Divacky 38317a519f9SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 38417a519f9SDimitry Andric MachineBasicBlock *New) { 38517a519f9SDimitry Andric uint32_t weight = 0; 38617a519f9SDimitry Andric succ_iterator SI = std::find(Successors.begin(), Successors.end(), Old); 38717a519f9SDimitry Andric 38817a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 38917a519f9SDimitry Andric if (!Weights.empty()) { 39017a519f9SDimitry Andric weight_iterator WI = getWeightIterator(SI); 39117a519f9SDimitry Andric weight = *WI; 39217a519f9SDimitry Andric } 39317a519f9SDimitry Andric 39417a519f9SDimitry Andric // Update the successor information. 39517a519f9SDimitry Andric removeSuccessor(SI); 39617a519f9SDimitry Andric addSuccessor(New, weight); 39717a519f9SDimitry Andric } 39817a519f9SDimitry Andric 399f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 400f22ef01cSRoman Divacky Predecessors.push_back(pred); 401f22ef01cSRoman Divacky } 402f22ef01cSRoman Divacky 403f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 4043b0f4066SDimitry Andric pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 405f22ef01cSRoman Divacky assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 406f22ef01cSRoman Divacky Predecessors.erase(I); 407f22ef01cSRoman Divacky } 408f22ef01cSRoman Divacky 409f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 410f22ef01cSRoman Divacky if (this == fromMBB) 411f22ef01cSRoman Divacky return; 412f22ef01cSRoman Divacky 413ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 414ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 41517a519f9SDimitry Andric uint32_t weight = 0; 41617a519f9SDimitry Andric 41717a519f9SDimitry Andric 41817a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 41917a519f9SDimitry Andric if (!fromMBB->Weights.empty()) 42017a519f9SDimitry Andric weight = *fromMBB->Weights.begin(); 42117a519f9SDimitry Andric 42217a519f9SDimitry Andric addSuccessor(Succ, weight); 423ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 424ffd1746dSEd Schouten } 425ffd1746dSEd Schouten } 426f22ef01cSRoman Divacky 427ffd1746dSEd Schouten void 428ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 429ffd1746dSEd Schouten if (this == fromMBB) 430ffd1746dSEd Schouten return; 431ffd1746dSEd Schouten 432ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 433ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 434ffd1746dSEd Schouten addSuccessor(Succ); 435ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 436ffd1746dSEd Schouten 437ffd1746dSEd Schouten // Fix up any PHI nodes in the successor. 438ffd1746dSEd Schouten for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end(); 439ffd1746dSEd Schouten MI != ME && MI->isPHI(); ++MI) 440ffd1746dSEd Schouten for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 441ffd1746dSEd Schouten MachineOperand &MO = MI->getOperand(i); 442ffd1746dSEd Schouten if (MO.getMBB() == fromMBB) 443ffd1746dSEd Schouten MO.setMBB(this); 444ffd1746dSEd Schouten } 445ffd1746dSEd Schouten } 446f22ef01cSRoman Divacky } 447f22ef01cSRoman Divacky 448f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 4493b0f4066SDimitry Andric const_succ_iterator I = std::find(Successors.begin(), Successors.end(), MBB); 450f22ef01cSRoman Divacky return I != Successors.end(); 451f22ef01cSRoman Divacky } 452f22ef01cSRoman Divacky 453f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 454f22ef01cSRoman Divacky MachineFunction::const_iterator I(this); 455f22ef01cSRoman Divacky return llvm::next(I) == MachineFunction::const_iterator(MBB); 456f22ef01cSRoman Divacky } 457f22ef01cSRoman Divacky 458f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() { 459f22ef01cSRoman Divacky MachineFunction::iterator Fallthrough = this; 460f22ef01cSRoman Divacky ++Fallthrough; 461f22ef01cSRoman Divacky // If FallthroughBlock is off the end of the function, it can't fall through. 462f22ef01cSRoman Divacky if (Fallthrough == getParent()->end()) 463f22ef01cSRoman Divacky return false; 464f22ef01cSRoman Divacky 465f22ef01cSRoman Divacky // If FallthroughBlock isn't a successor, no fallthrough is possible. 466f22ef01cSRoman Divacky if (!isSuccessor(Fallthrough)) 467f22ef01cSRoman Divacky return false; 468f22ef01cSRoman Divacky 469f22ef01cSRoman Divacky // Analyze the branches, if any, at the end of the block. 470f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 471f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 472f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 473f22ef01cSRoman Divacky if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 474f22ef01cSRoman Divacky // If we couldn't analyze the branch, examine the last instruction. 475f22ef01cSRoman Divacky // If the block doesn't end in a known control barrier, assume fallthrough 476f22ef01cSRoman Divacky // is possible. The isPredicable check is needed because this code can be 477f22ef01cSRoman Divacky // called during IfConversion, where an instruction which is normally a 478f22ef01cSRoman Divacky // Barrier is predicated and thus no longer an actual control barrier. This 479f22ef01cSRoman Divacky // is over-conservative though, because if an instruction isn't actually 480f22ef01cSRoman Divacky // predicated we could still treat it like a barrier. 481f22ef01cSRoman Divacky return empty() || !back().getDesc().isBarrier() || 482f22ef01cSRoman Divacky back().getDesc().isPredicable(); 483f22ef01cSRoman Divacky } 484f22ef01cSRoman Divacky 485f22ef01cSRoman Divacky // If there is no branch, control always falls through. 486f22ef01cSRoman Divacky if (TBB == 0) return true; 487f22ef01cSRoman Divacky 488f22ef01cSRoman Divacky // If there is some explicit branch to the fallthrough block, it can obviously 489f22ef01cSRoman Divacky // reach, even though the branch should get folded to fall through implicitly. 490f22ef01cSRoman Divacky if (MachineFunction::iterator(TBB) == Fallthrough || 491f22ef01cSRoman Divacky MachineFunction::iterator(FBB) == Fallthrough) 492f22ef01cSRoman Divacky return true; 493f22ef01cSRoman Divacky 494f22ef01cSRoman Divacky // If it's an unconditional branch to some block not the fall through, it 495f22ef01cSRoman Divacky // doesn't fall through. 496f22ef01cSRoman Divacky if (Cond.empty()) return false; 497f22ef01cSRoman Divacky 498f22ef01cSRoman Divacky // Otherwise, if it is conditional and has no explicit false block, it falls 499f22ef01cSRoman Divacky // through. 500f22ef01cSRoman Divacky return FBB == 0; 501f22ef01cSRoman Divacky } 502f22ef01cSRoman Divacky 503ffd1746dSEd Schouten MachineBasicBlock * 504ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 505ffd1746dSEd Schouten MachineFunction *MF = getParent(); 506ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 507ffd1746dSEd Schouten 5082754fe60SDimitry Andric // We may need to update this's terminator, but we can't do that if 5092754fe60SDimitry Andric // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 510ffd1746dSEd Schouten const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 511ffd1746dSEd Schouten MachineBasicBlock *TBB = 0, *FBB = 0; 512ffd1746dSEd Schouten SmallVector<MachineOperand, 4> Cond; 513ffd1746dSEd Schouten if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 514ffd1746dSEd Schouten return NULL; 515ffd1746dSEd Schouten 5162754fe60SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 5172754fe60SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 5182754fe60SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 5192754fe60SDimitry Andric // code, just skip those edges. 5202754fe60SDimitry Andric if (TBB && TBB == FBB) { 5212754fe60SDimitry Andric DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 5222754fe60SDimitry Andric << getNumber() << '\n'); 5232754fe60SDimitry Andric return NULL; 5242754fe60SDimitry Andric } 5252754fe60SDimitry Andric 526ffd1746dSEd Schouten MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 527ffd1746dSEd Schouten MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 528e580952dSDimitry Andric DEBUG(dbgs() << "Splitting critical edge:" 529ffd1746dSEd Schouten " BB#" << getNumber() 530ffd1746dSEd Schouten << " -- BB#" << NMBB->getNumber() 531ffd1746dSEd Schouten << " -- BB#" << Succ->getNumber() << '\n'); 532ffd1746dSEd Schouten 533bd5abe19SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 534bd5abe19SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 535bd5abe19SDimitry Andric // terminators. 536bd5abe19SDimitry Andric LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 537bd5abe19SDimitry Andric 538bd5abe19SDimitry Andric // Collect a list of virtual registers killed by the terminators. 539bd5abe19SDimitry Andric SmallVector<unsigned, 4> KilledRegs; 540bd5abe19SDimitry Andric if (LV) 541bd5abe19SDimitry Andric for (iterator I = getFirstTerminator(), E = end(); I != E; ++I) { 542bd5abe19SDimitry Andric MachineInstr *MI = I; 543bd5abe19SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 544bd5abe19SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 545bd5abe19SDimitry Andric if (!OI->isReg() || !OI->isUse() || !OI->isKill() || OI->isUndef()) 546bd5abe19SDimitry Andric continue; 547bd5abe19SDimitry Andric unsigned Reg = OI->getReg(); 548bd5abe19SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Reg) && 549bd5abe19SDimitry Andric LV->getVarInfo(Reg).removeKill(MI)) { 550bd5abe19SDimitry Andric KilledRegs.push_back(Reg); 551bd5abe19SDimitry Andric DEBUG(dbgs() << "Removing terminator kill: " << *MI); 552bd5abe19SDimitry Andric OI->setIsKill(false); 553bd5abe19SDimitry Andric } 554bd5abe19SDimitry Andric } 555bd5abe19SDimitry Andric } 556bd5abe19SDimitry Andric 557ffd1746dSEd Schouten ReplaceUsesOfBlockWith(Succ, NMBB); 558ffd1746dSEd Schouten updateTerminator(); 559ffd1746dSEd Schouten 560ffd1746dSEd Schouten // Insert unconditional "jump Succ" instruction in NMBB if necessary. 561ffd1746dSEd Schouten NMBB->addSuccessor(Succ); 562ffd1746dSEd Schouten if (!NMBB->isLayoutSuccessor(Succ)) { 563ffd1746dSEd Schouten Cond.clear(); 564ffd1746dSEd Schouten MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 565ffd1746dSEd Schouten } 566ffd1746dSEd Schouten 567ffd1746dSEd Schouten // Fix PHI nodes in Succ so they refer to NMBB instead of this 568ffd1746dSEd Schouten for (MachineBasicBlock::iterator i = Succ->begin(), e = Succ->end(); 569ffd1746dSEd Schouten i != e && i->isPHI(); ++i) 570ffd1746dSEd Schouten for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 571ffd1746dSEd Schouten if (i->getOperand(ni+1).getMBB() == this) 572ffd1746dSEd Schouten i->getOperand(ni+1).setMBB(NMBB); 573ffd1746dSEd Schouten 5746122f3e6SDimitry Andric // Inherit live-ins from the successor 5756122f3e6SDimitry Andric for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), 5766122f3e6SDimitry Andric E = Succ->livein_end(); I != E; ++I) 5776122f3e6SDimitry Andric NMBB->addLiveIn(*I); 5786122f3e6SDimitry Andric 579bd5abe19SDimitry Andric // Update LiveVariables. 580bd5abe19SDimitry Andric if (LV) { 581bd5abe19SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 582bd5abe19SDimitry Andric while (!KilledRegs.empty()) { 583bd5abe19SDimitry Andric unsigned Reg = KilledRegs.pop_back_val(); 584bd5abe19SDimitry Andric for (iterator I = end(), E = begin(); I != E;) { 585bd5abe19SDimitry Andric if (!(--I)->addRegisterKilled(Reg, NULL, /* addIfNotFound= */ false)) 586bd5abe19SDimitry Andric continue; 587bd5abe19SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(I); 588bd5abe19SDimitry Andric DEBUG(dbgs() << "Restored terminator kill: " << *I); 589bd5abe19SDimitry Andric break; 590bd5abe19SDimitry Andric } 591bd5abe19SDimitry Andric } 592bd5abe19SDimitry Andric // Update relevant live-through information. 593ffd1746dSEd Schouten LV->addNewBlock(NMBB, this, Succ); 594bd5abe19SDimitry Andric } 595ffd1746dSEd Schouten 596ffd1746dSEd Schouten if (MachineDominatorTree *MDT = 597e580952dSDimitry Andric P->getAnalysisIfAvailable<MachineDominatorTree>()) { 598e580952dSDimitry Andric // Update dominator information. 599e580952dSDimitry Andric MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 600ffd1746dSEd Schouten 601e580952dSDimitry Andric bool IsNewIDom = true; 602e580952dSDimitry Andric for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 603e580952dSDimitry Andric PI != E; ++PI) { 604e580952dSDimitry Andric MachineBasicBlock *PredBB = *PI; 605e580952dSDimitry Andric if (PredBB == NMBB) 606e580952dSDimitry Andric continue; 607e580952dSDimitry Andric if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 608e580952dSDimitry Andric IsNewIDom = false; 609e580952dSDimitry Andric break; 610e580952dSDimitry Andric } 611e580952dSDimitry Andric } 612e580952dSDimitry Andric 613e580952dSDimitry Andric // We know "this" dominates the newly created basic block. 614e580952dSDimitry Andric MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 615e580952dSDimitry Andric 616e580952dSDimitry Andric // If all the other predecessors of "Succ" are dominated by "Succ" itself 617e580952dSDimitry Andric // then the new block is the new immediate dominator of "Succ". Otherwise, 618e580952dSDimitry Andric // the new block doesn't dominate anything. 619e580952dSDimitry Andric if (IsNewIDom) 620e580952dSDimitry Andric MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 621e580952dSDimitry Andric } 622e580952dSDimitry Andric 623e580952dSDimitry Andric if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 624ffd1746dSEd Schouten if (MachineLoop *TIL = MLI->getLoopFor(this)) { 625ffd1746dSEd Schouten // If one or the other blocks were not in a loop, the new block is not 626ffd1746dSEd Schouten // either, and thus LI doesn't need to be updated. 627ffd1746dSEd Schouten if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 628ffd1746dSEd Schouten if (TIL == DestLoop) { 629ffd1746dSEd Schouten // Both in the same loop, the NMBB joins loop. 630ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 631ffd1746dSEd Schouten } else if (TIL->contains(DestLoop)) { 632ffd1746dSEd Schouten // Edge from an outer loop to an inner loop. Add to the outer loop. 633ffd1746dSEd Schouten TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 634ffd1746dSEd Schouten } else if (DestLoop->contains(TIL)) { 635ffd1746dSEd Schouten // Edge from an inner loop to an outer loop. Add to the outer loop. 636ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 637ffd1746dSEd Schouten } else { 638ffd1746dSEd Schouten // Edge from two loops with no containment relation. Because these 639ffd1746dSEd Schouten // are natural loops, we know that the destination block must be the 640ffd1746dSEd Schouten // header of its loop (adding a branch into a loop elsewhere would 641ffd1746dSEd Schouten // create an irreducible loop). 642ffd1746dSEd Schouten assert(DestLoop->getHeader() == Succ && 643ffd1746dSEd Schouten "Should not create irreducible loops!"); 644ffd1746dSEd Schouten if (MachineLoop *P = DestLoop->getParentLoop()) 645ffd1746dSEd Schouten P->addBasicBlockToLoop(NMBB, MLI->getBase()); 646ffd1746dSEd Schouten } 647ffd1746dSEd Schouten } 648ffd1746dSEd Schouten } 649ffd1746dSEd Schouten 650ffd1746dSEd Schouten return NMBB; 651ffd1746dSEd Schouten } 652ffd1746dSEd Schouten 653f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function, 654f22ef01cSRoman Divacky /// and returns it, but does not delete it. 655f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() { 656f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 657f22ef01cSRoman Divacky getParent()->remove(this); 658f22ef01cSRoman Divacky return this; 659f22ef01cSRoman Divacky } 660f22ef01cSRoman Divacky 661f22ef01cSRoman Divacky 662f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function, 663f22ef01cSRoman Divacky /// and deletes it. 664f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() { 665f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 666f22ef01cSRoman Divacky getParent()->erase(this); 667f22ef01cSRoman Divacky } 668f22ef01cSRoman Divacky 669f22ef01cSRoman Divacky 670f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 671f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead. 672f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 673f22ef01cSRoman Divacky MachineBasicBlock *New) { 674f22ef01cSRoman Divacky assert(Old != New && "Cannot replace self with self!"); 675f22ef01cSRoman Divacky 676f22ef01cSRoman Divacky MachineBasicBlock::iterator I = end(); 677f22ef01cSRoman Divacky while (I != begin()) { 678f22ef01cSRoman Divacky --I; 679f22ef01cSRoman Divacky if (!I->getDesc().isTerminator()) break; 680f22ef01cSRoman Divacky 681f22ef01cSRoman Divacky // Scan the operands of this machine instruction, replacing any uses of Old 682f22ef01cSRoman Divacky // with New. 683f22ef01cSRoman Divacky for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 684f22ef01cSRoman Divacky if (I->getOperand(i).isMBB() && 685f22ef01cSRoman Divacky I->getOperand(i).getMBB() == Old) 686f22ef01cSRoman Divacky I->getOperand(i).setMBB(New); 687f22ef01cSRoman Divacky } 688f22ef01cSRoman Divacky 689f22ef01cSRoman Divacky // Update the successor information. 69017a519f9SDimitry Andric replaceSuccessor(Old, New); 691f22ef01cSRoman Divacky } 692f22ef01cSRoman Divacky 693f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 694f22ef01cSRoman Divacky /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 695f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 696f22ef01cSRoman Divacky /// null. 697f22ef01cSRoman Divacky /// 698f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads 699f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here). 700f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads. 701f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 702f22ef01cSRoman Divacky MachineBasicBlock *DestB, 703f22ef01cSRoman Divacky bool isCond) { 704f22ef01cSRoman Divacky // The values of DestA and DestB frequently come from a call to the 705f22ef01cSRoman Divacky // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 706f22ef01cSRoman Divacky // values from there. 707f22ef01cSRoman Divacky // 708f22ef01cSRoman Divacky // 1. If both DestA and DestB are null, then the block ends with no branches 709f22ef01cSRoman Divacky // (it falls through to its successor). 710f22ef01cSRoman Divacky // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 711f22ef01cSRoman Divacky // with only an unconditional branch. 712f22ef01cSRoman Divacky // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 713f22ef01cSRoman Divacky // with a conditional branch that falls through to a successor (DestB). 714f22ef01cSRoman Divacky // 4. If DestA and DestB is set and isCond is true, then the block ends with a 715f22ef01cSRoman Divacky // conditional branch followed by an unconditional branch. DestA is the 716f22ef01cSRoman Divacky // 'true' destination and DestB is the 'false' destination. 717f22ef01cSRoman Divacky 718f22ef01cSRoman Divacky bool Changed = false; 719f22ef01cSRoman Divacky 720f22ef01cSRoman Divacky MachineFunction::iterator FallThru = 721f22ef01cSRoman Divacky llvm::next(MachineFunction::iterator(this)); 722f22ef01cSRoman Divacky 723f22ef01cSRoman Divacky if (DestA == 0 && DestB == 0) { 724f22ef01cSRoman Divacky // Block falls through to successor. 725f22ef01cSRoman Divacky DestA = FallThru; 726f22ef01cSRoman Divacky DestB = FallThru; 727f22ef01cSRoman Divacky } else if (DestA != 0 && DestB == 0) { 728f22ef01cSRoman Divacky if (isCond) 729f22ef01cSRoman Divacky // Block ends in conditional jump that falls through to successor. 730f22ef01cSRoman Divacky DestB = FallThru; 731f22ef01cSRoman Divacky } else { 732f22ef01cSRoman Divacky assert(DestA && DestB && isCond && 733f22ef01cSRoman Divacky "CFG in a bad state. Cannot correct CFG edges"); 734f22ef01cSRoman Divacky } 735f22ef01cSRoman Divacky 736f22ef01cSRoman Divacky // Remove superfluous edges. I.e., those which aren't destinations of this 737f22ef01cSRoman Divacky // basic block, duplicate edges, or landing pads. 738f22ef01cSRoman Divacky SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 739f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator SI = succ_begin(); 740f22ef01cSRoman Divacky while (SI != succ_end()) { 741f22ef01cSRoman Divacky const MachineBasicBlock *MBB = *SI; 742f22ef01cSRoman Divacky if (!SeenMBBs.insert(MBB) || 743f22ef01cSRoman Divacky (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 744f22ef01cSRoman Divacky // This is a superfluous edge, remove it. 745f22ef01cSRoman Divacky SI = removeSuccessor(SI); 746f22ef01cSRoman Divacky Changed = true; 747f22ef01cSRoman Divacky } else { 748f22ef01cSRoman Divacky ++SI; 749f22ef01cSRoman Divacky } 750f22ef01cSRoman Divacky } 751f22ef01cSRoman Divacky 752f22ef01cSRoman Divacky return Changed; 753f22ef01cSRoman Divacky } 754f22ef01cSRoman Divacky 755f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 756f22ef01cSRoman Divacky /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 757f22ef01cSRoman Divacky DebugLoc 758f22ef01cSRoman Divacky MachineBasicBlock::findDebugLoc(MachineBasicBlock::iterator &MBBI) { 759f22ef01cSRoman Divacky DebugLoc DL; 760f22ef01cSRoman Divacky MachineBasicBlock::iterator E = end(); 761f22ef01cSRoman Divacky if (MBBI != E) { 762f22ef01cSRoman Divacky // Skip debug declarations, we don't want a DebugLoc from them. 763f22ef01cSRoman Divacky MachineBasicBlock::iterator MBBI2 = MBBI; 764f22ef01cSRoman Divacky while (MBBI2 != E && MBBI2->isDebugValue()) 765f22ef01cSRoman Divacky MBBI2++; 766f22ef01cSRoman Divacky if (MBBI2 != E) 767f22ef01cSRoman Divacky DL = MBBI2->getDebugLoc(); 768f22ef01cSRoman Divacky } 769f22ef01cSRoman Divacky return DL; 770f22ef01cSRoman Divacky } 771f22ef01cSRoman Divacky 77217a519f9SDimitry Andric /// getSuccWeight - Return weight of the edge from this block to MBB. 77317a519f9SDimitry Andric /// 77417a519f9SDimitry Andric uint32_t MachineBasicBlock::getSuccWeight(MachineBasicBlock *succ) { 77517a519f9SDimitry Andric if (Weights.empty()) 77617a519f9SDimitry Andric return 0; 77717a519f9SDimitry Andric 77817a519f9SDimitry Andric succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 77917a519f9SDimitry Andric return *getWeightIterator(I); 78017a519f9SDimitry Andric } 78117a519f9SDimitry Andric 78217a519f9SDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor 78317a519f9SDimitry Andric /// iterator 78417a519f9SDimitry Andric MachineBasicBlock::weight_iterator MachineBasicBlock:: 78517a519f9SDimitry Andric getWeightIterator(MachineBasicBlock::succ_iterator I) { 78617a519f9SDimitry Andric assert(Weights.size() == Successors.size() && "Async weight list!"); 78717a519f9SDimitry Andric size_t index = std::distance(Successors.begin(), I); 78817a519f9SDimitry Andric assert(index < Weights.size() && "Not a current successor!"); 78917a519f9SDimitry Andric return Weights.begin() + index; 79017a519f9SDimitry Andric } 79117a519f9SDimitry Andric 792f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 793f22ef01cSRoman Divacky bool t) { 794f22ef01cSRoman Divacky OS << "BB#" << MBB->getNumber(); 795f22ef01cSRoman Divacky } 796f22ef01cSRoman Divacky 797