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" 15139f7f9bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 16139f7f9bSDimitry Andric #include "llvm/ADT/SmallString.h" 17139f7f9bSDimitry Andric #include "llvm/Assembly/Writer.h" 18139f7f9bSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 19ffd1746dSEd Schouten #include "llvm/CodeGen/LiveVariables.h" 20ffd1746dSEd Schouten #include "llvm/CodeGen/MachineDominators.h" 21f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h" 22ffd1746dSEd Schouten #include "llvm/CodeGen/MachineLoopInfo.h" 23139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 242754fe60SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 25139f7f9bSDimitry Andric #include "llvm/IR/BasicBlock.h" 26139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 27f22ef01cSRoman Divacky #include "llvm/MC/MCAsmInfo.h" 28f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 29f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 30f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h" 31f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 32139f7f9bSDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 33139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h" 34139f7f9bSDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 35f22ef01cSRoman Divacky #include <algorithm> 36f22ef01cSRoman Divacky using namespace llvm; 37f22ef01cSRoman Divacky 38f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 39f22ef01cSRoman Divacky : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 40f22ef01cSRoman Divacky AddressTaken(false) { 41f22ef01cSRoman Divacky Insts.Parent = this; 42f22ef01cSRoman Divacky } 43f22ef01cSRoman Divacky 44f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() { 45f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(this); 46f22ef01cSRoman Divacky } 47f22ef01cSRoman Divacky 48f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block. 49f22ef01cSRoman Divacky /// 50f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const { 51f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 52f22ef01cSRoman Divacky MCContext &Ctx = MF->getContext(); 53f22ef01cSRoman Divacky const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 54f22ef01cSRoman Divacky return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 55f22ef01cSRoman Divacky Twine(MF->getFunctionNumber()) + "_" + 56f22ef01cSRoman Divacky Twine(getNumber())); 57f22ef01cSRoman Divacky } 58f22ef01cSRoman Divacky 59f22ef01cSRoman Divacky 60f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 61f22ef01cSRoman Divacky MBB.print(OS); 62f22ef01cSRoman Divacky return OS; 63f22ef01cSRoman Divacky } 64f22ef01cSRoman Divacky 65f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 66f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the 67f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers. 68f22ef01cSRoman Divacky /// 69f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 70f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a 71f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1. 72f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 73f22ef01cSRoman Divacky MachineFunction &MF = *N->getParent(); 74f22ef01cSRoman Divacky N->Number = MF.addToMBBNumbering(N); 75f22ef01cSRoman Divacky 76f22ef01cSRoman Divacky // Make sure the instructions have their operands in the reginfo lists. 77f22ef01cSRoman Divacky MachineRegisterInfo &RegInfo = MF.getRegInfo(); 78dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator 79dff0c46cSDimitry Andric I = N->instr_begin(), E = N->instr_end(); I != E; ++I) 80f22ef01cSRoman Divacky I->AddRegOperandsToUseLists(RegInfo); 81f22ef01cSRoman Divacky 82f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 83f22ef01cSRoman Divacky } 84f22ef01cSRoman Divacky 85f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 86f22ef01cSRoman Divacky N->getParent()->removeFromMBBNumbering(N->Number); 87f22ef01cSRoman Divacky N->Number = -1; 88f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 89f22ef01cSRoman Divacky } 90f22ef01cSRoman Divacky 91f22ef01cSRoman Divacky 92f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block 93f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def 94f22ef01cSRoman Divacky /// lists if appropriate. 95f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 96f22ef01cSRoman Divacky assert(N->getParent() == 0 && "machine instruction already in a basic block"); 97f22ef01cSRoman Divacky N->setParent(Parent); 98f22ef01cSRoman Divacky 99f22ef01cSRoman Divacky // Add the instruction's register operands to their corresponding 100f22ef01cSRoman Divacky // use/def lists. 101f22ef01cSRoman Divacky MachineFunction *MF = Parent->getParent(); 102f22ef01cSRoman Divacky N->AddRegOperandsToUseLists(MF->getRegInfo()); 103f22ef01cSRoman Divacky 104f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 105f22ef01cSRoman Divacky } 106f22ef01cSRoman Divacky 107f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block 108f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def 109f22ef01cSRoman Divacky /// lists if appropriate. 110f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 111f22ef01cSRoman Divacky assert(N->getParent() != 0 && "machine instruction not in a basic block"); 112f22ef01cSRoman Divacky 113f22ef01cSRoman Divacky // Remove from the use/def lists. 1147ae0e2c9SDimitry Andric if (MachineFunction *MF = N->getParent()->getParent()) 1157ae0e2c9SDimitry Andric N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); 116f22ef01cSRoman Divacky 117f22ef01cSRoman Divacky N->setParent(0); 118f22ef01cSRoman Divacky 119f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 120f22ef01cSRoman Divacky } 121f22ef01cSRoman Divacky 122f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one 123f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def 124f22ef01cSRoman Divacky /// lists. 125f22ef01cSRoman Divacky void ilist_traits<MachineInstr>:: 126f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList, 127dff0c46cSDimitry Andric ilist_iterator<MachineInstr> first, 128dff0c46cSDimitry Andric ilist_iterator<MachineInstr> last) { 129f22ef01cSRoman Divacky assert(Parent->getParent() == fromList.Parent->getParent() && 130f22ef01cSRoman Divacky "MachineInstr parent mismatch!"); 131f22ef01cSRoman Divacky 132f22ef01cSRoman Divacky // Splice within the same MBB -> no change. 133f22ef01cSRoman Divacky if (Parent == fromList.Parent) return; 134f22ef01cSRoman Divacky 135f22ef01cSRoman Divacky // If splicing between two blocks within the same function, just update the 136f22ef01cSRoman Divacky // parent pointers. 137f22ef01cSRoman Divacky for (; first != last; ++first) 138f22ef01cSRoman Divacky first->setParent(Parent); 139f22ef01cSRoman Divacky } 140f22ef01cSRoman Divacky 141f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 142f22ef01cSRoman Divacky assert(!MI->getParent() && "MI is still in a block!"); 143f22ef01cSRoman Divacky Parent->getParent()->DeleteMachineInstr(MI); 144f22ef01cSRoman Divacky } 145f22ef01cSRoman Divacky 146ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 147dff0c46cSDimitry Andric instr_iterator I = instr_begin(), E = instr_end(); 148dff0c46cSDimitry Andric while (I != E && I->isPHI()) 149ffd1746dSEd Schouten ++I; 1503861d79fSDimitry Andric assert((I == E || !I->isInsideBundle()) && 1513861d79fSDimitry Andric "First non-phi MI cannot be inside a bundle!"); 152ffd1746dSEd Schouten return I; 153ffd1746dSEd Schouten } 154ffd1746dSEd Schouten 1552754fe60SDimitry Andric MachineBasicBlock::iterator 1562754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 157dff0c46cSDimitry Andric iterator E = end(); 158dff0c46cSDimitry Andric while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue())) 1592754fe60SDimitry Andric ++I; 160dff0c46cSDimitry Andric // FIXME: This needs to change if we wish to bundle labels / dbg_values 161dff0c46cSDimitry Andric // inside the bundle. 1623861d79fSDimitry Andric assert((I == E || !I->isInsideBundle()) && 163dff0c46cSDimitry Andric "First non-phi / non-label instruction is inside a bundle!"); 1642754fe60SDimitry Andric return I; 1652754fe60SDimitry Andric } 1662754fe60SDimitry Andric 167f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 168dff0c46cSDimitry Andric iterator B = begin(), E = end(), I = E; 169dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 170f22ef01cSRoman Divacky ; /*noop */ 171dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 172dff0c46cSDimitry Andric ++I; 173dff0c46cSDimitry Andric return I; 174dff0c46cSDimitry Andric } 175dff0c46cSDimitry Andric 176dff0c46cSDimitry Andric MachineBasicBlock::const_iterator 177dff0c46cSDimitry Andric MachineBasicBlock::getFirstTerminator() const { 178dff0c46cSDimitry Andric const_iterator B = begin(), E = end(), I = E; 179dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 180dff0c46cSDimitry Andric ; /*noop */ 181dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 182dff0c46cSDimitry Andric ++I; 183dff0c46cSDimitry Andric return I; 184dff0c46cSDimitry Andric } 185dff0c46cSDimitry Andric 186dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 187dff0c46cSDimitry Andric instr_iterator B = instr_begin(), E = instr_end(), I = E; 188dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 189dff0c46cSDimitry Andric ; /*noop */ 190dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 1912754fe60SDimitry Andric ++I; 192f22ef01cSRoman Divacky return I; 193f22ef01cSRoman Divacky } 194f22ef01cSRoman Divacky 1952754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 196dff0c46cSDimitry Andric // Skip over end-of-block dbg_value instructions. 197dff0c46cSDimitry Andric instr_iterator B = instr_begin(), I = instr_end(); 1982754fe60SDimitry Andric while (I != B) { 1992754fe60SDimitry Andric --I; 200dff0c46cSDimitry Andric // Return instruction that starts a bundle. 201dff0c46cSDimitry Andric if (I->isDebugValue() || I->isInsideBundle()) 202dff0c46cSDimitry Andric continue; 203dff0c46cSDimitry Andric return I; 204dff0c46cSDimitry Andric } 205dff0c46cSDimitry Andric // The block is all debug values. 206dff0c46cSDimitry Andric return end(); 207dff0c46cSDimitry Andric } 208dff0c46cSDimitry Andric 209dff0c46cSDimitry Andric MachineBasicBlock::const_iterator 210dff0c46cSDimitry Andric MachineBasicBlock::getLastNonDebugInstr() const { 211dff0c46cSDimitry Andric // Skip over end-of-block dbg_value instructions. 212dff0c46cSDimitry Andric const_instr_iterator B = instr_begin(), I = instr_end(); 213dff0c46cSDimitry Andric while (I != B) { 214dff0c46cSDimitry Andric --I; 215dff0c46cSDimitry Andric // Return instruction that starts a bundle. 216dff0c46cSDimitry Andric if (I->isDebugValue() || I->isInsideBundle()) 2172754fe60SDimitry Andric continue; 2182754fe60SDimitry Andric return I; 2192754fe60SDimitry Andric } 2202754fe60SDimitry Andric // The block is all debug values. 2212754fe60SDimitry Andric return end(); 2222754fe60SDimitry Andric } 2232754fe60SDimitry Andric 2242754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 2252754fe60SDimitry Andric // A block with a landing pad successor only has one other successor. 2262754fe60SDimitry Andric if (succ_size() > 2) 2272754fe60SDimitry Andric return 0; 2282754fe60SDimitry Andric for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 2292754fe60SDimitry Andric if ((*I)->isLandingPad()) 2302754fe60SDimitry Andric return *I; 2312754fe60SDimitry Andric return 0; 2322754fe60SDimitry Andric } 2332754fe60SDimitry Andric 2343861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 235f22ef01cSRoman Divacky void MachineBasicBlock::dump() const { 236f22ef01cSRoman Divacky print(dbgs()); 237f22ef01cSRoman Divacky } 2383861d79fSDimitry Andric #endif 239f22ef01cSRoman Divacky 240f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const { 241f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) 242f22ef01cSRoman Divacky return LBB->getName(); 243f22ef01cSRoman Divacky else 244f22ef01cSRoman Divacky return "(null)"; 245f22ef01cSRoman Divacky } 246f22ef01cSRoman Divacky 247dff0c46cSDimitry Andric /// Return a hopefully unique identifier for this block. 248dff0c46cSDimitry Andric std::string MachineBasicBlock::getFullName() const { 249dff0c46cSDimitry Andric std::string Name; 250dff0c46cSDimitry Andric if (getParent()) 2513861d79fSDimitry Andric Name = (getParent()->getName() + ":").str(); 252dff0c46cSDimitry Andric if (getBasicBlock()) 253dff0c46cSDimitry Andric Name += getBasicBlock()->getName(); 254dff0c46cSDimitry Andric else 255dff0c46cSDimitry Andric Name += (Twine("BB") + Twine(getNumber())).str(); 256dff0c46cSDimitry Andric return Name; 257dff0c46cSDimitry Andric } 258dff0c46cSDimitry Andric 2592754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 260f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 261f22ef01cSRoman Divacky if (!MF) { 262f22ef01cSRoman Divacky OS << "Can't print out MachineBasicBlock because parent MachineFunction" 263f22ef01cSRoman Divacky << " is null\n"; 264f22ef01cSRoman Divacky return; 265f22ef01cSRoman Divacky } 266f22ef01cSRoman Divacky 2672754fe60SDimitry Andric if (Indexes) 2682754fe60SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 2692754fe60SDimitry Andric 270f22ef01cSRoman Divacky OS << "BB#" << getNumber() << ": "; 271f22ef01cSRoman Divacky 272f22ef01cSRoman Divacky const char *Comma = ""; 273f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) { 274f22ef01cSRoman Divacky OS << Comma << "derived from LLVM BB "; 275f22ef01cSRoman Divacky WriteAsOperand(OS, LBB, /*PrintType=*/false); 276f22ef01cSRoman Divacky Comma = ", "; 277f22ef01cSRoman Divacky } 278f22ef01cSRoman Divacky if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 279f22ef01cSRoman Divacky if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 2807ae0e2c9SDimitry Andric if (Alignment) 281dff0c46cSDimitry Andric OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) 282dff0c46cSDimitry Andric << " bytes)"; 283dff0c46cSDimitry Andric 284f22ef01cSRoman Divacky OS << '\n'; 285f22ef01cSRoman Divacky 286f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 287f22ef01cSRoman Divacky if (!livein_empty()) { 2882754fe60SDimitry Andric if (Indexes) OS << '\t'; 289f22ef01cSRoman Divacky OS << " Live Ins:"; 290f22ef01cSRoman Divacky for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 2912754fe60SDimitry Andric OS << ' ' << PrintReg(*I, TRI); 292f22ef01cSRoman Divacky OS << '\n'; 293f22ef01cSRoman Divacky } 294f22ef01cSRoman Divacky // Print the preds of this block according to the CFG. 295f22ef01cSRoman Divacky if (!pred_empty()) { 2962754fe60SDimitry Andric if (Indexes) OS << '\t'; 297f22ef01cSRoman Divacky OS << " Predecessors according to CFG:"; 298f22ef01cSRoman Divacky for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 299f22ef01cSRoman Divacky OS << " BB#" << (*PI)->getNumber(); 300f22ef01cSRoman Divacky OS << '\n'; 301f22ef01cSRoman Divacky } 302f22ef01cSRoman Divacky 303dff0c46cSDimitry Andric for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { 3042754fe60SDimitry Andric if (Indexes) { 3052754fe60SDimitry Andric if (Indexes->hasIndex(I)) 3062754fe60SDimitry Andric OS << Indexes->getInstructionIndex(I); 3072754fe60SDimitry Andric OS << '\t'; 3082754fe60SDimitry Andric } 309f22ef01cSRoman Divacky OS << '\t'; 310dff0c46cSDimitry Andric if (I->isInsideBundle()) 311dff0c46cSDimitry Andric OS << " * "; 312f22ef01cSRoman Divacky I->print(OS, &getParent()->getTarget()); 313f22ef01cSRoman Divacky } 314f22ef01cSRoman Divacky 315f22ef01cSRoman Divacky // Print the successors of this block according to the CFG. 316f22ef01cSRoman Divacky if (!succ_empty()) { 3172754fe60SDimitry Andric if (Indexes) OS << '\t'; 318f22ef01cSRoman Divacky OS << " Successors according to CFG:"; 3197ae0e2c9SDimitry Andric for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { 320f22ef01cSRoman Divacky OS << " BB#" << (*SI)->getNumber(); 3217ae0e2c9SDimitry Andric if (!Weights.empty()) 3227ae0e2c9SDimitry Andric OS << '(' << *getWeightIterator(SI) << ')'; 3237ae0e2c9SDimitry Andric } 324f22ef01cSRoman Divacky OS << '\n'; 325f22ef01cSRoman Divacky } 326f22ef01cSRoman Divacky } 327f22ef01cSRoman Divacky 328f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) { 329f22ef01cSRoman Divacky std::vector<unsigned>::iterator I = 330f22ef01cSRoman Divacky std::find(LiveIns.begin(), LiveIns.end(), Reg); 331dff0c46cSDimitry Andric if (I != LiveIns.end()) 332f22ef01cSRoman Divacky LiveIns.erase(I); 333f22ef01cSRoman Divacky } 334f22ef01cSRoman Divacky 335f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 336f22ef01cSRoman Divacky livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 337f22ef01cSRoman Divacky return I != livein_end(); 338f22ef01cSRoman Divacky } 339f22ef01cSRoman Divacky 340f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 341f22ef01cSRoman Divacky getParent()->splice(NewAfter, this); 342f22ef01cSRoman Divacky } 343f22ef01cSRoman Divacky 344f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 345f22ef01cSRoman Divacky MachineFunction::iterator BBI = NewBefore; 346f22ef01cSRoman Divacky getParent()->splice(++BBI, this); 347f22ef01cSRoman Divacky } 348f22ef01cSRoman Divacky 349f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() { 350f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 351f22ef01cSRoman Divacky // A block with no successors has no concerns with fall-through edges. 352f22ef01cSRoman Divacky if (this->succ_empty()) return; 353f22ef01cSRoman Divacky 354f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 355f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 356ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 357f22ef01cSRoman Divacky bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 358f22ef01cSRoman Divacky (void) B; 359f22ef01cSRoman Divacky assert(!B && "UpdateTerminators requires analyzable predecessors!"); 360f22ef01cSRoman Divacky if (Cond.empty()) { 361f22ef01cSRoman Divacky if (TBB) { 362f22ef01cSRoman Divacky // The block has an unconditional branch. If its successor is now 363f22ef01cSRoman Divacky // its layout successor, delete the branch. 364f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) 365f22ef01cSRoman Divacky TII->RemoveBranch(*this); 366f22ef01cSRoman Divacky } else { 367f22ef01cSRoman Divacky // The block has an unconditional fallthrough. If its successor is not 368dff0c46cSDimitry Andric // its layout successor, insert a branch. First we have to locate the 369dff0c46cSDimitry Andric // only non-landing-pad successor, as that is the fallthrough block. 370dff0c46cSDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 371dff0c46cSDimitry Andric if ((*SI)->isLandingPad()) 372dff0c46cSDimitry Andric continue; 373dff0c46cSDimitry Andric assert(!TBB && "Found more than one non-landing-pad successor!"); 374dff0c46cSDimitry Andric TBB = *SI; 375dff0c46cSDimitry Andric } 376dff0c46cSDimitry Andric 377dff0c46cSDimitry Andric // If there is no non-landing-pad successor, the block has no 378dff0c46cSDimitry Andric // fall-through edges to be concerned with. 379dff0c46cSDimitry Andric if (!TBB) 380dff0c46cSDimitry Andric return; 381dff0c46cSDimitry Andric 382dff0c46cSDimitry Andric // Finally update the unconditional successor to be reached via a branch 383dff0c46cSDimitry Andric // if it would not be reached by fallthrough. 384f22ef01cSRoman Divacky if (!isLayoutSuccessor(TBB)) 385ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 386f22ef01cSRoman Divacky } 387f22ef01cSRoman Divacky } else { 388f22ef01cSRoman Divacky if (FBB) { 389f22ef01cSRoman Divacky // The block has a non-fallthrough conditional branch. If one of its 390f22ef01cSRoman Divacky // successors is its layout successor, rewrite it to a fallthrough 391f22ef01cSRoman Divacky // conditional branch. 392f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 393f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) 394f22ef01cSRoman Divacky return; 395f22ef01cSRoman Divacky TII->RemoveBranch(*this); 396ffd1746dSEd Schouten TII->InsertBranch(*this, FBB, 0, Cond, dl); 397f22ef01cSRoman Divacky } else if (isLayoutSuccessor(FBB)) { 398f22ef01cSRoman Divacky TII->RemoveBranch(*this); 399ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 400f22ef01cSRoman Divacky } 401f22ef01cSRoman Divacky } else { 402cb4dff85SDimitry Andric // Walk through the successors and find the successor which is not 403cb4dff85SDimitry Andric // a landing pad and is not the conditional branch destination (in TBB) 404cb4dff85SDimitry Andric // as the fallthrough successor. 405cb4dff85SDimitry Andric MachineBasicBlock *FallthroughBB = 0; 406cb4dff85SDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 407cb4dff85SDimitry Andric if ((*SI)->isLandingPad() || *SI == TBB) 408cb4dff85SDimitry Andric continue; 409cb4dff85SDimitry Andric assert(!FallthroughBB && "Found more than one fallthrough successor."); 410cb4dff85SDimitry Andric FallthroughBB = *SI; 411cb4dff85SDimitry Andric } 412cb4dff85SDimitry Andric if (!FallthroughBB && canFallThrough()) { 413cb4dff85SDimitry Andric // We fallthrough to the same basic block as the conditional jump 414cb4dff85SDimitry Andric // targets. Remove the conditional jump, leaving unconditional 415cb4dff85SDimitry Andric // fallthrough. 416cb4dff85SDimitry Andric // FIXME: This does not seem like a reasonable pattern to support, but it 417cb4dff85SDimitry Andric // has been seen in the wild coming out of degenerate ARM test cases. 418cb4dff85SDimitry Andric TII->RemoveBranch(*this); 419cb4dff85SDimitry Andric 420cb4dff85SDimitry Andric // Finally update the unconditional successor to be reached via a branch 421cb4dff85SDimitry Andric // if it would not be reached by fallthrough. 422cb4dff85SDimitry Andric if (!isLayoutSuccessor(TBB)) 423cb4dff85SDimitry Andric TII->InsertBranch(*this, TBB, 0, Cond, dl); 424cb4dff85SDimitry Andric return; 425cb4dff85SDimitry Andric } 426cb4dff85SDimitry Andric 427f22ef01cSRoman Divacky // The block has a fallthrough conditional branch. 428f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 429f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) { 430f22ef01cSRoman Divacky // We can't reverse the condition, add an unconditional branch. 431f22ef01cSRoman Divacky Cond.clear(); 432cb4dff85SDimitry Andric TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 433f22ef01cSRoman Divacky return; 434f22ef01cSRoman Divacky } 435f22ef01cSRoman Divacky TII->RemoveBranch(*this); 436cb4dff85SDimitry Andric TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 437cb4dff85SDimitry Andric } else if (!isLayoutSuccessor(FallthroughBB)) { 438f22ef01cSRoman Divacky TII->RemoveBranch(*this); 439cb4dff85SDimitry Andric TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); 440f22ef01cSRoman Divacky } 441f22ef01cSRoman Divacky } 442f22ef01cSRoman Divacky } 443f22ef01cSRoman Divacky } 444f22ef01cSRoman Divacky 44517a519f9SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { 44617a519f9SDimitry Andric 44717a519f9SDimitry Andric // If we see non-zero value for the first time it means we actually use Weight 44817a519f9SDimitry Andric // list, so we fill all Weights with 0's. 44917a519f9SDimitry Andric if (weight != 0 && Weights.empty()) 45017a519f9SDimitry Andric Weights.resize(Successors.size()); 45117a519f9SDimitry Andric 45217a519f9SDimitry Andric if (weight != 0 || !Weights.empty()) 45317a519f9SDimitry Andric Weights.push_back(weight); 45417a519f9SDimitry Andric 455f22ef01cSRoman Divacky Successors.push_back(succ); 456f22ef01cSRoman Divacky succ->addPredecessor(this); 457f22ef01cSRoman Divacky } 458f22ef01cSRoman Divacky 459f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 460f22ef01cSRoman Divacky succ->removePredecessor(this); 461f22ef01cSRoman Divacky succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 462f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 46317a519f9SDimitry Andric 46417a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 46517a519f9SDimitry Andric if (!Weights.empty()) { 46617a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 46717a519f9SDimitry Andric Weights.erase(WI); 46817a519f9SDimitry Andric } 46917a519f9SDimitry Andric 470f22ef01cSRoman Divacky Successors.erase(I); 471f22ef01cSRoman Divacky } 472f22ef01cSRoman Divacky 473f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator 474f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) { 475f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 47617a519f9SDimitry Andric 47717a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 47817a519f9SDimitry Andric if (!Weights.empty()) { 47917a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 48017a519f9SDimitry Andric Weights.erase(WI); 48117a519f9SDimitry Andric } 48217a519f9SDimitry Andric 483f22ef01cSRoman Divacky (*I)->removePredecessor(this); 484f22ef01cSRoman Divacky return Successors.erase(I); 485f22ef01cSRoman Divacky } 486f22ef01cSRoman Divacky 48717a519f9SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 48817a519f9SDimitry Andric MachineBasicBlock *New) { 4897ae0e2c9SDimitry Andric if (Old == New) 4907ae0e2c9SDimitry Andric return; 49117a519f9SDimitry Andric 4927ae0e2c9SDimitry Andric succ_iterator E = succ_end(); 4937ae0e2c9SDimitry Andric succ_iterator NewI = E; 4947ae0e2c9SDimitry Andric succ_iterator OldI = E; 4957ae0e2c9SDimitry Andric for (succ_iterator I = succ_begin(); I != E; ++I) { 4967ae0e2c9SDimitry Andric if (*I == Old) { 4977ae0e2c9SDimitry Andric OldI = I; 4987ae0e2c9SDimitry Andric if (NewI != E) 4997ae0e2c9SDimitry Andric break; 5007ae0e2c9SDimitry Andric } 5017ae0e2c9SDimitry Andric if (*I == New) { 5027ae0e2c9SDimitry Andric NewI = I; 5037ae0e2c9SDimitry Andric if (OldI != E) 5047ae0e2c9SDimitry Andric break; 5057ae0e2c9SDimitry Andric } 5067ae0e2c9SDimitry Andric } 5077ae0e2c9SDimitry Andric assert(OldI != E && "Old is not a successor of this block"); 5087ae0e2c9SDimitry Andric Old->removePredecessor(this); 5097ae0e2c9SDimitry Andric 5107ae0e2c9SDimitry Andric // If New isn't already a successor, let it take Old's place. 5117ae0e2c9SDimitry Andric if (NewI == E) { 5127ae0e2c9SDimitry Andric New->addPredecessor(this); 5137ae0e2c9SDimitry Andric *OldI = New; 5147ae0e2c9SDimitry Andric return; 51517a519f9SDimitry Andric } 51617a519f9SDimitry Andric 5177ae0e2c9SDimitry Andric // New is already a successor. 5187ae0e2c9SDimitry Andric // Update its weight instead of adding a duplicate edge. 5197ae0e2c9SDimitry Andric if (!Weights.empty()) { 5207ae0e2c9SDimitry Andric weight_iterator OldWI = getWeightIterator(OldI); 5217ae0e2c9SDimitry Andric *getWeightIterator(NewI) += *OldWI; 5227ae0e2c9SDimitry Andric Weights.erase(OldWI); 5237ae0e2c9SDimitry Andric } 5247ae0e2c9SDimitry Andric Successors.erase(OldI); 52517a519f9SDimitry Andric } 52617a519f9SDimitry Andric 527f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 528f22ef01cSRoman Divacky Predecessors.push_back(pred); 529f22ef01cSRoman Divacky } 530f22ef01cSRoman Divacky 531f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 5323b0f4066SDimitry Andric pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 533f22ef01cSRoman Divacky assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 534f22ef01cSRoman Divacky Predecessors.erase(I); 535f22ef01cSRoman Divacky } 536f22ef01cSRoman Divacky 537f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 538f22ef01cSRoman Divacky if (this == fromMBB) 539f22ef01cSRoman Divacky return; 540f22ef01cSRoman Divacky 541ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 542ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 5437ae0e2c9SDimitry Andric uint32_t Weight = 0; 54417a519f9SDimitry Andric 54517a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 54617a519f9SDimitry Andric if (!fromMBB->Weights.empty()) 5477ae0e2c9SDimitry Andric Weight = *fromMBB->Weights.begin(); 54817a519f9SDimitry Andric 5497ae0e2c9SDimitry Andric addSuccessor(Succ, Weight); 550ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 551ffd1746dSEd Schouten } 552ffd1746dSEd Schouten } 553f22ef01cSRoman Divacky 554ffd1746dSEd Schouten void 555ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 556ffd1746dSEd Schouten if (this == fromMBB) 557ffd1746dSEd Schouten return; 558ffd1746dSEd Schouten 559ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 560ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 5617ae0e2c9SDimitry Andric uint32_t Weight = 0; 5627ae0e2c9SDimitry Andric if (!fromMBB->Weights.empty()) 5637ae0e2c9SDimitry Andric Weight = *fromMBB->Weights.begin(); 5647ae0e2c9SDimitry Andric addSuccessor(Succ, Weight); 565ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 566ffd1746dSEd Schouten 567ffd1746dSEd Schouten // Fix up any PHI nodes in the successor. 568dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), 569dff0c46cSDimitry Andric ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) 570ffd1746dSEd Schouten for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 571ffd1746dSEd Schouten MachineOperand &MO = MI->getOperand(i); 572ffd1746dSEd Schouten if (MO.getMBB() == fromMBB) 573ffd1746dSEd Schouten MO.setMBB(this); 574ffd1746dSEd Schouten } 575ffd1746dSEd Schouten } 576f22ef01cSRoman Divacky } 577f22ef01cSRoman Divacky 5787ae0e2c9SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 5797ae0e2c9SDimitry Andric return std::find(pred_begin(), pred_end(), MBB) != pred_end(); 5807ae0e2c9SDimitry Andric } 5817ae0e2c9SDimitry Andric 582f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 5837ae0e2c9SDimitry Andric return std::find(succ_begin(), succ_end(), MBB) != succ_end(); 584f22ef01cSRoman Divacky } 585f22ef01cSRoman Divacky 586f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 587f22ef01cSRoman Divacky MachineFunction::const_iterator I(this); 588f22ef01cSRoman Divacky return llvm::next(I) == MachineFunction::const_iterator(MBB); 589f22ef01cSRoman Divacky } 590f22ef01cSRoman Divacky 591f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() { 592f22ef01cSRoman Divacky MachineFunction::iterator Fallthrough = this; 593f22ef01cSRoman Divacky ++Fallthrough; 594f22ef01cSRoman Divacky // If FallthroughBlock is off the end of the function, it can't fall through. 595f22ef01cSRoman Divacky if (Fallthrough == getParent()->end()) 596f22ef01cSRoman Divacky return false; 597f22ef01cSRoman Divacky 598f22ef01cSRoman Divacky // If FallthroughBlock isn't a successor, no fallthrough is possible. 599f22ef01cSRoman Divacky if (!isSuccessor(Fallthrough)) 600f22ef01cSRoman Divacky return false; 601f22ef01cSRoman Divacky 602f22ef01cSRoman Divacky // Analyze the branches, if any, at the end of the block. 603f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 604f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 605f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 606f22ef01cSRoman Divacky if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 607f22ef01cSRoman Divacky // If we couldn't analyze the branch, examine the last instruction. 608f22ef01cSRoman Divacky // If the block doesn't end in a known control barrier, assume fallthrough 609dff0c46cSDimitry Andric // is possible. The isPredicated check is needed because this code can be 610f22ef01cSRoman Divacky // called during IfConversion, where an instruction which is normally a 611dff0c46cSDimitry Andric // Barrier is predicated and thus no longer an actual control barrier. 612dff0c46cSDimitry Andric return empty() || !back().isBarrier() || TII->isPredicated(&back()); 613f22ef01cSRoman Divacky } 614f22ef01cSRoman Divacky 615f22ef01cSRoman Divacky // If there is no branch, control always falls through. 616f22ef01cSRoman Divacky if (TBB == 0) return true; 617f22ef01cSRoman Divacky 618f22ef01cSRoman Divacky // If there is some explicit branch to the fallthrough block, it can obviously 619f22ef01cSRoman Divacky // reach, even though the branch should get folded to fall through implicitly. 620f22ef01cSRoman Divacky if (MachineFunction::iterator(TBB) == Fallthrough || 621f22ef01cSRoman Divacky MachineFunction::iterator(FBB) == Fallthrough) 622f22ef01cSRoman Divacky return true; 623f22ef01cSRoman Divacky 624f22ef01cSRoman Divacky // If it's an unconditional branch to some block not the fall through, it 625f22ef01cSRoman Divacky // doesn't fall through. 626f22ef01cSRoman Divacky if (Cond.empty()) return false; 627f22ef01cSRoman Divacky 628f22ef01cSRoman Divacky // Otherwise, if it is conditional and has no explicit false block, it falls 629f22ef01cSRoman Divacky // through. 630f22ef01cSRoman Divacky return FBB == 0; 631f22ef01cSRoman Divacky } 632f22ef01cSRoman Divacky 633ffd1746dSEd Schouten MachineBasicBlock * 634ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 6357ae0e2c9SDimitry Andric // Splitting the critical edge to a landing pad block is non-trivial. Don't do 6367ae0e2c9SDimitry Andric // it in this generic function. 6377ae0e2c9SDimitry Andric if (Succ->isLandingPad()) 6387ae0e2c9SDimitry Andric return NULL; 6397ae0e2c9SDimitry Andric 640ffd1746dSEd Schouten MachineFunction *MF = getParent(); 641ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 642ffd1746dSEd Schouten 6432754fe60SDimitry Andric // We may need to update this's terminator, but we can't do that if 6442754fe60SDimitry Andric // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 645ffd1746dSEd Schouten const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 646ffd1746dSEd Schouten MachineBasicBlock *TBB = 0, *FBB = 0; 647ffd1746dSEd Schouten SmallVector<MachineOperand, 4> Cond; 648ffd1746dSEd Schouten if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 649ffd1746dSEd Schouten return NULL; 650ffd1746dSEd Schouten 6512754fe60SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 6522754fe60SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 6532754fe60SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 6542754fe60SDimitry Andric // code, just skip those edges. 6552754fe60SDimitry Andric if (TBB && TBB == FBB) { 6562754fe60SDimitry Andric DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 6572754fe60SDimitry Andric << getNumber() << '\n'); 6582754fe60SDimitry Andric return NULL; 6592754fe60SDimitry Andric } 6602754fe60SDimitry Andric 661ffd1746dSEd Schouten MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 662ffd1746dSEd Schouten MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 663e580952dSDimitry Andric DEBUG(dbgs() << "Splitting critical edge:" 664ffd1746dSEd Schouten " BB#" << getNumber() 665ffd1746dSEd Schouten << " -- BB#" << NMBB->getNumber() 666ffd1746dSEd Schouten << " -- BB#" << Succ->getNumber() << '\n'); 667ffd1746dSEd Schouten 668139f7f9bSDimitry Andric LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>(); 669139f7f9bSDimitry Andric SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); 670139f7f9bSDimitry Andric if (LIS) 671139f7f9bSDimitry Andric LIS->insertMBBInMaps(NMBB); 672139f7f9bSDimitry Andric else if (Indexes) 673139f7f9bSDimitry Andric Indexes->insertMBBInMaps(NMBB); 674139f7f9bSDimitry Andric 675bd5abe19SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 676bd5abe19SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 677bd5abe19SDimitry Andric // terminators. 678bd5abe19SDimitry Andric LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 679bd5abe19SDimitry Andric 680bd5abe19SDimitry Andric // Collect a list of virtual registers killed by the terminators. 681bd5abe19SDimitry Andric SmallVector<unsigned, 4> KilledRegs; 682bd5abe19SDimitry Andric if (LV) 683dff0c46cSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 684dff0c46cSDimitry Andric I != E; ++I) { 685bd5abe19SDimitry Andric MachineInstr *MI = I; 686bd5abe19SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 687bd5abe19SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 688dff0c46cSDimitry Andric if (!OI->isReg() || OI->getReg() == 0 || 689dff0c46cSDimitry Andric !OI->isUse() || !OI->isKill() || OI->isUndef()) 690bd5abe19SDimitry Andric continue; 691bd5abe19SDimitry Andric unsigned Reg = OI->getReg(); 692dff0c46cSDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(Reg) || 693bd5abe19SDimitry Andric LV->getVarInfo(Reg).removeKill(MI)) { 694bd5abe19SDimitry Andric KilledRegs.push_back(Reg); 695bd5abe19SDimitry Andric DEBUG(dbgs() << "Removing terminator kill: " << *MI); 696bd5abe19SDimitry Andric OI->setIsKill(false); 697bd5abe19SDimitry Andric } 698bd5abe19SDimitry Andric } 699bd5abe19SDimitry Andric } 700bd5abe19SDimitry Andric 701139f7f9bSDimitry Andric SmallVector<unsigned, 4> UsedRegs; 702139f7f9bSDimitry Andric if (LIS) { 703139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 704139f7f9bSDimitry Andric I != E; ++I) { 705139f7f9bSDimitry Andric MachineInstr *MI = I; 706139f7f9bSDimitry Andric 707139f7f9bSDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 708139f7f9bSDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 709139f7f9bSDimitry Andric if (!OI->isReg() || OI->getReg() == 0) 710139f7f9bSDimitry Andric continue; 711139f7f9bSDimitry Andric 712139f7f9bSDimitry Andric unsigned Reg = OI->getReg(); 713139f7f9bSDimitry Andric if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end()) 714139f7f9bSDimitry Andric UsedRegs.push_back(Reg); 715139f7f9bSDimitry Andric } 716139f7f9bSDimitry Andric } 717139f7f9bSDimitry Andric } 718139f7f9bSDimitry Andric 719ffd1746dSEd Schouten ReplaceUsesOfBlockWith(Succ, NMBB); 720139f7f9bSDimitry Andric 721139f7f9bSDimitry Andric // If updateTerminator() removes instructions, we need to remove them from 722139f7f9bSDimitry Andric // SlotIndexes. 723139f7f9bSDimitry Andric SmallVector<MachineInstr*, 4> Terminators; 724139f7f9bSDimitry Andric if (Indexes) { 725139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 726139f7f9bSDimitry Andric I != E; ++I) 727139f7f9bSDimitry Andric Terminators.push_back(I); 728139f7f9bSDimitry Andric } 729139f7f9bSDimitry Andric 730ffd1746dSEd Schouten updateTerminator(); 731ffd1746dSEd Schouten 732139f7f9bSDimitry Andric if (Indexes) { 733139f7f9bSDimitry Andric SmallVector<MachineInstr*, 4> NewTerminators; 734139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 735139f7f9bSDimitry Andric I != E; ++I) 736139f7f9bSDimitry Andric NewTerminators.push_back(I); 737139f7f9bSDimitry Andric 738139f7f9bSDimitry Andric for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), 739139f7f9bSDimitry Andric E = Terminators.end(); I != E; ++I) { 740139f7f9bSDimitry Andric if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == 741139f7f9bSDimitry Andric NewTerminators.end()) 742139f7f9bSDimitry Andric Indexes->removeMachineInstrFromMaps(*I); 743139f7f9bSDimitry Andric } 744139f7f9bSDimitry Andric } 745139f7f9bSDimitry Andric 746ffd1746dSEd Schouten // Insert unconditional "jump Succ" instruction in NMBB if necessary. 747ffd1746dSEd Schouten NMBB->addSuccessor(Succ); 748ffd1746dSEd Schouten if (!NMBB->isLayoutSuccessor(Succ)) { 749ffd1746dSEd Schouten Cond.clear(); 750ffd1746dSEd Schouten MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 751139f7f9bSDimitry Andric 752139f7f9bSDimitry Andric if (Indexes) { 753139f7f9bSDimitry Andric for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); 754139f7f9bSDimitry Andric I != E; ++I) { 755139f7f9bSDimitry Andric // Some instructions may have been moved to NMBB by updateTerminator(), 756139f7f9bSDimitry Andric // so we first remove any instruction that already has an index. 757139f7f9bSDimitry Andric if (Indexes->hasIndex(I)) 758139f7f9bSDimitry Andric Indexes->removeMachineInstrFromMaps(I); 759139f7f9bSDimitry Andric Indexes->insertMachineInstrInMaps(I); 760139f7f9bSDimitry Andric } 761139f7f9bSDimitry Andric } 762ffd1746dSEd Schouten } 763ffd1746dSEd Schouten 764ffd1746dSEd Schouten // Fix PHI nodes in Succ so they refer to NMBB instead of this 765dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator 766dff0c46cSDimitry Andric i = Succ->instr_begin(),e = Succ->instr_end(); 767ffd1746dSEd Schouten i != e && i->isPHI(); ++i) 768ffd1746dSEd Schouten for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 769ffd1746dSEd Schouten if (i->getOperand(ni+1).getMBB() == this) 770ffd1746dSEd Schouten i->getOperand(ni+1).setMBB(NMBB); 771ffd1746dSEd Schouten 7726122f3e6SDimitry Andric // Inherit live-ins from the successor 7736122f3e6SDimitry Andric for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), 7746122f3e6SDimitry Andric E = Succ->livein_end(); I != E; ++I) 7756122f3e6SDimitry Andric NMBB->addLiveIn(*I); 7766122f3e6SDimitry Andric 777bd5abe19SDimitry Andric // Update LiveVariables. 778dff0c46cSDimitry Andric const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 779bd5abe19SDimitry Andric if (LV) { 780bd5abe19SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 781bd5abe19SDimitry Andric while (!KilledRegs.empty()) { 782bd5abe19SDimitry Andric unsigned Reg = KilledRegs.pop_back_val(); 783dff0c46cSDimitry Andric for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 784dff0c46cSDimitry Andric if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) 785bd5abe19SDimitry Andric continue; 786dff0c46cSDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Reg)) 787bd5abe19SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(I); 788bd5abe19SDimitry Andric DEBUG(dbgs() << "Restored terminator kill: " << *I); 789bd5abe19SDimitry Andric break; 790bd5abe19SDimitry Andric } 791bd5abe19SDimitry Andric } 792bd5abe19SDimitry Andric // Update relevant live-through information. 793ffd1746dSEd Schouten LV->addNewBlock(NMBB, this, Succ); 794bd5abe19SDimitry Andric } 795ffd1746dSEd Schouten 796139f7f9bSDimitry Andric if (LIS) { 797139f7f9bSDimitry Andric // After splitting the edge and updating SlotIndexes, live intervals may be 798139f7f9bSDimitry Andric // in one of two situations, depending on whether this block was the last in 799139f7f9bSDimitry Andric // the function. If the original block was the last in the function, all live 800139f7f9bSDimitry Andric // intervals will end prior to the beginning of the new split block. If the 801139f7f9bSDimitry Andric // original block was not at the end of the function, all live intervals will 802139f7f9bSDimitry Andric // extend to the end of the new split block. 803139f7f9bSDimitry Andric 804139f7f9bSDimitry Andric bool isLastMBB = 805139f7f9bSDimitry Andric llvm::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 806139f7f9bSDimitry Andric 807139f7f9bSDimitry Andric SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 808139f7f9bSDimitry Andric SlotIndex PrevIndex = StartIndex.getPrevSlot(); 809139f7f9bSDimitry Andric SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 810139f7f9bSDimitry Andric 811139f7f9bSDimitry Andric // Find the registers used from NMBB in PHIs in Succ. 812139f7f9bSDimitry Andric SmallSet<unsigned, 8> PHISrcRegs; 813139f7f9bSDimitry Andric for (MachineBasicBlock::instr_iterator 814139f7f9bSDimitry Andric I = Succ->instr_begin(), E = Succ->instr_end(); 815139f7f9bSDimitry Andric I != E && I->isPHI(); ++I) { 816139f7f9bSDimitry Andric for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 817139f7f9bSDimitry Andric if (I->getOperand(ni+1).getMBB() == NMBB) { 818139f7f9bSDimitry Andric MachineOperand &MO = I->getOperand(ni); 819139f7f9bSDimitry Andric unsigned Reg = MO.getReg(); 820139f7f9bSDimitry Andric PHISrcRegs.insert(Reg); 821139f7f9bSDimitry Andric if (MO.isUndef()) 822139f7f9bSDimitry Andric continue; 823139f7f9bSDimitry Andric 824139f7f9bSDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 825139f7f9bSDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 826139f7f9bSDimitry Andric assert(VNI && "PHI sources should be live out of their predecessors."); 827139f7f9bSDimitry Andric LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 828139f7f9bSDimitry Andric } 829139f7f9bSDimitry Andric } 830139f7f9bSDimitry Andric } 831139f7f9bSDimitry Andric 832139f7f9bSDimitry Andric MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 833139f7f9bSDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 834139f7f9bSDimitry Andric unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 835139f7f9bSDimitry Andric if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 836139f7f9bSDimitry Andric continue; 837139f7f9bSDimitry Andric 838139f7f9bSDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 839139f7f9bSDimitry Andric if (!LI.liveAt(PrevIndex)) 840139f7f9bSDimitry Andric continue; 841139f7f9bSDimitry Andric 842139f7f9bSDimitry Andric bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 843139f7f9bSDimitry Andric if (isLiveOut && isLastMBB) { 844139f7f9bSDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 845139f7f9bSDimitry Andric assert(VNI && "LiveInterval should have VNInfo where it is live."); 846139f7f9bSDimitry Andric LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 847139f7f9bSDimitry Andric } else if (!isLiveOut && !isLastMBB) { 848139f7f9bSDimitry Andric LI.removeRange(StartIndex, EndIndex); 849139f7f9bSDimitry Andric } 850139f7f9bSDimitry Andric } 851139f7f9bSDimitry Andric 852139f7f9bSDimitry Andric // Update all intervals for registers whose uses may have been modified by 853139f7f9bSDimitry Andric // updateTerminator(). 854139f7f9bSDimitry Andric LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 855139f7f9bSDimitry Andric } 856139f7f9bSDimitry Andric 857ffd1746dSEd Schouten if (MachineDominatorTree *MDT = 858e580952dSDimitry Andric P->getAnalysisIfAvailable<MachineDominatorTree>()) { 859e580952dSDimitry Andric // Update dominator information. 860e580952dSDimitry Andric MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 861ffd1746dSEd Schouten 862e580952dSDimitry Andric bool IsNewIDom = true; 863e580952dSDimitry Andric for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 864e580952dSDimitry Andric PI != E; ++PI) { 865e580952dSDimitry Andric MachineBasicBlock *PredBB = *PI; 866e580952dSDimitry Andric if (PredBB == NMBB) 867e580952dSDimitry Andric continue; 868e580952dSDimitry Andric if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 869e580952dSDimitry Andric IsNewIDom = false; 870e580952dSDimitry Andric break; 871e580952dSDimitry Andric } 872e580952dSDimitry Andric } 873e580952dSDimitry Andric 874e580952dSDimitry Andric // We know "this" dominates the newly created basic block. 875e580952dSDimitry Andric MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 876e580952dSDimitry Andric 877e580952dSDimitry Andric // If all the other predecessors of "Succ" are dominated by "Succ" itself 878e580952dSDimitry Andric // then the new block is the new immediate dominator of "Succ". Otherwise, 879e580952dSDimitry Andric // the new block doesn't dominate anything. 880e580952dSDimitry Andric if (IsNewIDom) 881e580952dSDimitry Andric MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 882e580952dSDimitry Andric } 883e580952dSDimitry Andric 884e580952dSDimitry Andric if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 885ffd1746dSEd Schouten if (MachineLoop *TIL = MLI->getLoopFor(this)) { 886ffd1746dSEd Schouten // If one or the other blocks were not in a loop, the new block is not 887ffd1746dSEd Schouten // either, and thus LI doesn't need to be updated. 888ffd1746dSEd Schouten if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 889ffd1746dSEd Schouten if (TIL == DestLoop) { 890ffd1746dSEd Schouten // Both in the same loop, the NMBB joins loop. 891ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 892ffd1746dSEd Schouten } else if (TIL->contains(DestLoop)) { 893ffd1746dSEd Schouten // Edge from an outer loop to an inner loop. Add to the outer loop. 894ffd1746dSEd Schouten TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 895ffd1746dSEd Schouten } else if (DestLoop->contains(TIL)) { 896ffd1746dSEd Schouten // Edge from an inner loop to an outer loop. Add to the outer loop. 897ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 898ffd1746dSEd Schouten } else { 899ffd1746dSEd Schouten // Edge from two loops with no containment relation. Because these 900ffd1746dSEd Schouten // are natural loops, we know that the destination block must be the 901ffd1746dSEd Schouten // header of its loop (adding a branch into a loop elsewhere would 902ffd1746dSEd Schouten // create an irreducible loop). 903ffd1746dSEd Schouten assert(DestLoop->getHeader() == Succ && 904ffd1746dSEd Schouten "Should not create irreducible loops!"); 905ffd1746dSEd Schouten if (MachineLoop *P = DestLoop->getParentLoop()) 906ffd1746dSEd Schouten P->addBasicBlockToLoop(NMBB, MLI->getBase()); 907ffd1746dSEd Schouten } 908ffd1746dSEd Schouten } 909ffd1746dSEd Schouten } 910ffd1746dSEd Schouten 911ffd1746dSEd Schouten return NMBB; 912ffd1746dSEd Schouten } 913ffd1746dSEd Schouten 914139f7f9bSDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 915139f7f9bSDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI. 916139f7f9bSDimitry Andric static void unbundleSingleMI(MachineInstr *MI) { 917139f7f9bSDimitry Andric // Removing the first instruction in a bundle. 918139f7f9bSDimitry Andric if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 919139f7f9bSDimitry Andric MI->unbundleFromSucc(); 920139f7f9bSDimitry Andric // Removing the last instruction in a bundle. 921139f7f9bSDimitry Andric if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 922139f7f9bSDimitry Andric MI->unbundleFromPred(); 923139f7f9bSDimitry Andric // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 924139f7f9bSDimitry Andric // are already fine. 925dff0c46cSDimitry Andric } 926dff0c46cSDimitry Andric 927139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator 928139f7f9bSDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 929139f7f9bSDimitry Andric unbundleSingleMI(I); 930139f7f9bSDimitry Andric return Insts.erase(I); 931dff0c46cSDimitry Andric } 932dff0c46cSDimitry Andric 933139f7f9bSDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 934139f7f9bSDimitry Andric unbundleSingleMI(MI); 935139f7f9bSDimitry Andric MI->clearFlag(MachineInstr::BundledPred); 936139f7f9bSDimitry Andric MI->clearFlag(MachineInstr::BundledSucc); 937139f7f9bSDimitry Andric return Insts.remove(MI); 938dff0c46cSDimitry Andric } 939dff0c46cSDimitry Andric 940139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator 941139f7f9bSDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 942139f7f9bSDimitry Andric assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 943139f7f9bSDimitry Andric "Cannot insert instruction with bundle flags"); 944139f7f9bSDimitry Andric // Set the bundle flags when inserting inside a bundle. 945139f7f9bSDimitry Andric if (I != instr_end() && I->isBundledWithPred()) { 946139f7f9bSDimitry Andric MI->setFlag(MachineInstr::BundledPred); 947139f7f9bSDimitry Andric MI->setFlag(MachineInstr::BundledSucc); 948dff0c46cSDimitry Andric } 949139f7f9bSDimitry Andric return Insts.insert(I, MI); 950dff0c46cSDimitry Andric } 951dff0c46cSDimitry Andric 952f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function, 953f22ef01cSRoman Divacky /// and returns it, but does not delete it. 954f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() { 955f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 956f22ef01cSRoman Divacky getParent()->remove(this); 957f22ef01cSRoman Divacky return this; 958f22ef01cSRoman Divacky } 959f22ef01cSRoman Divacky 960f22ef01cSRoman Divacky 961f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function, 962f22ef01cSRoman Divacky /// and deletes it. 963f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() { 964f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 965f22ef01cSRoman Divacky getParent()->erase(this); 966f22ef01cSRoman Divacky } 967f22ef01cSRoman Divacky 968f22ef01cSRoman Divacky 969f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 970f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead. 971f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 972f22ef01cSRoman Divacky MachineBasicBlock *New) { 973f22ef01cSRoman Divacky assert(Old != New && "Cannot replace self with self!"); 974f22ef01cSRoman Divacky 975dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator I = instr_end(); 976dff0c46cSDimitry Andric while (I != instr_begin()) { 977f22ef01cSRoman Divacky --I; 978dff0c46cSDimitry Andric if (!I->isTerminator()) break; 979f22ef01cSRoman Divacky 980f22ef01cSRoman Divacky // Scan the operands of this machine instruction, replacing any uses of Old 981f22ef01cSRoman Divacky // with New. 982f22ef01cSRoman Divacky for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 983f22ef01cSRoman Divacky if (I->getOperand(i).isMBB() && 984f22ef01cSRoman Divacky I->getOperand(i).getMBB() == Old) 985f22ef01cSRoman Divacky I->getOperand(i).setMBB(New); 986f22ef01cSRoman Divacky } 987f22ef01cSRoman Divacky 988f22ef01cSRoman Divacky // Update the successor information. 98917a519f9SDimitry Andric replaceSuccessor(Old, New); 990f22ef01cSRoman Divacky } 991f22ef01cSRoman Divacky 992f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 993f22ef01cSRoman Divacky /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 994f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 995f22ef01cSRoman Divacky /// null. 996f22ef01cSRoman Divacky /// 997f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads 998f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here). 999f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads. 1000f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 1001f22ef01cSRoman Divacky MachineBasicBlock *DestB, 1002f22ef01cSRoman Divacky bool isCond) { 1003f22ef01cSRoman Divacky // The values of DestA and DestB frequently come from a call to the 1004f22ef01cSRoman Divacky // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 1005f22ef01cSRoman Divacky // values from there. 1006f22ef01cSRoman Divacky // 1007f22ef01cSRoman Divacky // 1. If both DestA and DestB are null, then the block ends with no branches 1008f22ef01cSRoman Divacky // (it falls through to its successor). 1009f22ef01cSRoman Divacky // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 1010f22ef01cSRoman Divacky // with only an unconditional branch. 1011f22ef01cSRoman Divacky // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 1012f22ef01cSRoman Divacky // with a conditional branch that falls through to a successor (DestB). 1013f22ef01cSRoman Divacky // 4. If DestA and DestB is set and isCond is true, then the block ends with a 1014f22ef01cSRoman Divacky // conditional branch followed by an unconditional branch. DestA is the 1015f22ef01cSRoman Divacky // 'true' destination and DestB is the 'false' destination. 1016f22ef01cSRoman Divacky 1017f22ef01cSRoman Divacky bool Changed = false; 1018f22ef01cSRoman Divacky 1019f22ef01cSRoman Divacky MachineFunction::iterator FallThru = 1020f22ef01cSRoman Divacky llvm::next(MachineFunction::iterator(this)); 1021f22ef01cSRoman Divacky 1022f22ef01cSRoman Divacky if (DestA == 0 && DestB == 0) { 1023f22ef01cSRoman Divacky // Block falls through to successor. 1024f22ef01cSRoman Divacky DestA = FallThru; 1025f22ef01cSRoman Divacky DestB = FallThru; 1026f22ef01cSRoman Divacky } else if (DestA != 0 && DestB == 0) { 1027f22ef01cSRoman Divacky if (isCond) 1028f22ef01cSRoman Divacky // Block ends in conditional jump that falls through to successor. 1029f22ef01cSRoman Divacky DestB = FallThru; 1030f22ef01cSRoman Divacky } else { 1031f22ef01cSRoman Divacky assert(DestA && DestB && isCond && 1032f22ef01cSRoman Divacky "CFG in a bad state. Cannot correct CFG edges"); 1033f22ef01cSRoman Divacky } 1034f22ef01cSRoman Divacky 1035f22ef01cSRoman Divacky // Remove superfluous edges. I.e., those which aren't destinations of this 1036f22ef01cSRoman Divacky // basic block, duplicate edges, or landing pads. 1037f22ef01cSRoman Divacky SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 1038f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator SI = succ_begin(); 1039f22ef01cSRoman Divacky while (SI != succ_end()) { 1040f22ef01cSRoman Divacky const MachineBasicBlock *MBB = *SI; 1041f22ef01cSRoman Divacky if (!SeenMBBs.insert(MBB) || 1042f22ef01cSRoman Divacky (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 1043f22ef01cSRoman Divacky // This is a superfluous edge, remove it. 1044f22ef01cSRoman Divacky SI = removeSuccessor(SI); 1045f22ef01cSRoman Divacky Changed = true; 1046f22ef01cSRoman Divacky } else { 1047f22ef01cSRoman Divacky ++SI; 1048f22ef01cSRoman Divacky } 1049f22ef01cSRoman Divacky } 1050f22ef01cSRoman Divacky 1051f22ef01cSRoman Divacky return Changed; 1052f22ef01cSRoman Divacky } 1053f22ef01cSRoman Divacky 1054f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 1055f22ef01cSRoman Divacky /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 1056f22ef01cSRoman Divacky DebugLoc 1057dff0c46cSDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 1058f22ef01cSRoman Divacky DebugLoc DL; 1059dff0c46cSDimitry Andric instr_iterator E = instr_end(); 1060dff0c46cSDimitry Andric if (MBBI == E) 1061dff0c46cSDimitry Andric return DL; 1062dff0c46cSDimitry Andric 1063f22ef01cSRoman Divacky // Skip debug declarations, we don't want a DebugLoc from them. 1064dff0c46cSDimitry Andric while (MBBI != E && MBBI->isDebugValue()) 1065dff0c46cSDimitry Andric MBBI++; 1066dff0c46cSDimitry Andric if (MBBI != E) 1067dff0c46cSDimitry Andric DL = MBBI->getDebugLoc(); 1068f22ef01cSRoman Divacky return DL; 1069f22ef01cSRoman Divacky } 1070f22ef01cSRoman Divacky 107117a519f9SDimitry Andric /// getSuccWeight - Return weight of the edge from this block to MBB. 107217a519f9SDimitry Andric /// 10733861d79fSDimitry Andric uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { 107417a519f9SDimitry Andric if (Weights.empty()) 107517a519f9SDimitry Andric return 0; 107617a519f9SDimitry Andric 10773861d79fSDimitry Andric return *getWeightIterator(Succ); 107817a519f9SDimitry Andric } 107917a519f9SDimitry Andric 108017a519f9SDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor 108117a519f9SDimitry Andric /// iterator 108217a519f9SDimitry Andric MachineBasicBlock::weight_iterator MachineBasicBlock:: 108317a519f9SDimitry Andric getWeightIterator(MachineBasicBlock::succ_iterator I) { 108417a519f9SDimitry Andric assert(Weights.size() == Successors.size() && "Async weight list!"); 108517a519f9SDimitry Andric size_t index = std::distance(Successors.begin(), I); 108617a519f9SDimitry Andric assert(index < Weights.size() && "Not a current successor!"); 108717a519f9SDimitry Andric return Weights.begin() + index; 108817a519f9SDimitry Andric } 108917a519f9SDimitry Andric 1090dff0c46cSDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor 1091dff0c46cSDimitry Andric /// iterator 1092dff0c46cSDimitry Andric MachineBasicBlock::const_weight_iterator MachineBasicBlock:: 1093dff0c46cSDimitry Andric getWeightIterator(MachineBasicBlock::const_succ_iterator I) const { 1094dff0c46cSDimitry Andric assert(Weights.size() == Successors.size() && "Async weight list!"); 1095dff0c46cSDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1096dff0c46cSDimitry Andric assert(index < Weights.size() && "Not a current successor!"); 1097dff0c46cSDimitry Andric return Weights.begin() + index; 1098dff0c46cSDimitry Andric } 1099dff0c46cSDimitry Andric 11003861d79fSDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 11013861d79fSDimitry Andric /// as of just before "MI". 11023861d79fSDimitry Andric /// 11033861d79fSDimitry Andric /// Search is localised to a neighborhood of 11043861d79fSDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N 11053861d79fSDimitry Andric /// instructions after (searching just for defs) MI. 11063861d79fSDimitry Andric MachineBasicBlock::LivenessQueryResult 11073861d79fSDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 11083861d79fSDimitry Andric unsigned Reg, MachineInstr *MI, 11093861d79fSDimitry Andric unsigned Neighborhood) { 11103861d79fSDimitry Andric unsigned N = Neighborhood; 11113861d79fSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 11123861d79fSDimitry Andric 11133861d79fSDimitry Andric // Start by searching backwards from MI, looking for kills, reads or defs. 11143861d79fSDimitry Andric 11153861d79fSDimitry Andric MachineBasicBlock::iterator I(MI); 11163861d79fSDimitry Andric // If this is the first insn in the block, don't search backwards. 11173861d79fSDimitry Andric if (I != MBB->begin()) { 11183861d79fSDimitry Andric do { 11193861d79fSDimitry Andric --I; 11203861d79fSDimitry Andric 11213861d79fSDimitry Andric MachineOperandIteratorBase::PhysRegInfo Analysis = 11223861d79fSDimitry Andric MIOperands(I).analyzePhysReg(Reg, TRI); 11233861d79fSDimitry Andric 1124139f7f9bSDimitry Andric if (Analysis.Defines) 1125139f7f9bSDimitry Andric // Outputs happen after inputs so they take precedence if both are 1126139f7f9bSDimitry Andric // present. 1127139f7f9bSDimitry Andric return Analysis.DefinesDead ? LQR_Dead : LQR_Live; 1128139f7f9bSDimitry Andric 1129139f7f9bSDimitry Andric if (Analysis.Kills || Analysis.Clobbers) 11303861d79fSDimitry Andric // Register killed, so isn't live. 11313861d79fSDimitry Andric return LQR_Dead; 11323861d79fSDimitry Andric 1133139f7f9bSDimitry Andric else if (Analysis.ReadsOverlap) 11343861d79fSDimitry Andric // Defined or read without a previous kill - live. 1135139f7f9bSDimitry Andric return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; 11363861d79fSDimitry Andric 11373861d79fSDimitry Andric } while (I != MBB->begin() && --N > 0); 11383861d79fSDimitry Andric } 11393861d79fSDimitry Andric 11403861d79fSDimitry Andric // Did we get to the start of the block? 11413861d79fSDimitry Andric if (I == MBB->begin()) { 11423861d79fSDimitry Andric // If so, the register's state is definitely defined by the live-in state. 11433861d79fSDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); 11443861d79fSDimitry Andric RAI.isValid(); ++RAI) { 11453861d79fSDimitry Andric if (MBB->isLiveIn(*RAI)) 11463861d79fSDimitry Andric return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; 11473861d79fSDimitry Andric } 11483861d79fSDimitry Andric 11493861d79fSDimitry Andric return LQR_Dead; 11503861d79fSDimitry Andric } 11513861d79fSDimitry Andric 11523861d79fSDimitry Andric N = Neighborhood; 11533861d79fSDimitry Andric 11543861d79fSDimitry Andric // Try searching forwards from MI, looking for reads or defs. 11553861d79fSDimitry Andric I = MachineBasicBlock::iterator(MI); 11563861d79fSDimitry Andric // If this is the last insn in the block, don't search forwards. 11573861d79fSDimitry Andric if (I != MBB->end()) { 11583861d79fSDimitry Andric for (++I; I != MBB->end() && N > 0; ++I, --N) { 11593861d79fSDimitry Andric MachineOperandIteratorBase::PhysRegInfo Analysis = 11603861d79fSDimitry Andric MIOperands(I).analyzePhysReg(Reg, TRI); 11613861d79fSDimitry Andric 11623861d79fSDimitry Andric if (Analysis.ReadsOverlap) 11633861d79fSDimitry Andric // Used, therefore must have been live. 11643861d79fSDimitry Andric return (Analysis.Reads) ? 11653861d79fSDimitry Andric LQR_Live : LQR_OverlappingLive; 11663861d79fSDimitry Andric 1167139f7f9bSDimitry Andric else if (Analysis.Clobbers || Analysis.Defines) 11683861d79fSDimitry Andric // Defined (but not read) therefore cannot have been live. 11693861d79fSDimitry Andric return LQR_Dead; 11703861d79fSDimitry Andric } 11713861d79fSDimitry Andric } 11723861d79fSDimitry Andric 11733861d79fSDimitry Andric // At this point we have no idea of the liveness of the register. 11743861d79fSDimitry Andric return LQR_Unknown; 11753861d79fSDimitry Andric } 11763861d79fSDimitry Andric 1177f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 1178f22ef01cSRoman Divacky bool t) { 1179f22ef01cSRoman Divacky OS << "BB#" << MBB->getNumber(); 1180f22ef01cSRoman Divacky } 1181f22ef01cSRoman Divacky 1182