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), 40284c1978SDimitry Andric AddressTaken(false), CachedMCSymbol(NULL) { 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 { 51284c1978SDimitry Andric if (!CachedMCSymbol) { 52f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 53f22ef01cSRoman Divacky MCContext &Ctx = MF->getContext(); 54f22ef01cSRoman Divacky const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 55284c1978SDimitry Andric CachedMCSymbol = Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 56284c1978SDimitry Andric Twine(MF->getFunctionNumber()) + 57284c1978SDimitry Andric "_" + Twine(getNumber())); 58284c1978SDimitry Andric } 59284c1978SDimitry Andric 60284c1978SDimitry Andric return CachedMCSymbol; 61f22ef01cSRoman Divacky } 62f22ef01cSRoman Divacky 63f22ef01cSRoman Divacky 64f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 65f22ef01cSRoman Divacky MBB.print(OS); 66f22ef01cSRoman Divacky return OS; 67f22ef01cSRoman Divacky } 68f22ef01cSRoman Divacky 69f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 70f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the 71f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers. 72f22ef01cSRoman Divacky /// 73f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 74f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a 75f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1. 76f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 77f22ef01cSRoman Divacky MachineFunction &MF = *N->getParent(); 78f22ef01cSRoman Divacky N->Number = MF.addToMBBNumbering(N); 79f22ef01cSRoman Divacky 80f22ef01cSRoman Divacky // Make sure the instructions have their operands in the reginfo lists. 81f22ef01cSRoman Divacky MachineRegisterInfo &RegInfo = MF.getRegInfo(); 82dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator 83dff0c46cSDimitry Andric I = N->instr_begin(), E = N->instr_end(); I != E; ++I) 84f22ef01cSRoman Divacky I->AddRegOperandsToUseLists(RegInfo); 85f22ef01cSRoman Divacky 86f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 87f22ef01cSRoman Divacky } 88f22ef01cSRoman Divacky 89f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 90f22ef01cSRoman Divacky N->getParent()->removeFromMBBNumbering(N->Number); 91f22ef01cSRoman Divacky N->Number = -1; 92f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 93f22ef01cSRoman Divacky } 94f22ef01cSRoman Divacky 95f22ef01cSRoman Divacky 96f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block 97f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def 98f22ef01cSRoman Divacky /// lists if appropriate. 99f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 100f22ef01cSRoman Divacky assert(N->getParent() == 0 && "machine instruction already in a basic block"); 101f22ef01cSRoman Divacky N->setParent(Parent); 102f22ef01cSRoman Divacky 103f22ef01cSRoman Divacky // Add the instruction's register operands to their corresponding 104f22ef01cSRoman Divacky // use/def lists. 105f22ef01cSRoman Divacky MachineFunction *MF = Parent->getParent(); 106f22ef01cSRoman Divacky N->AddRegOperandsToUseLists(MF->getRegInfo()); 107f22ef01cSRoman Divacky 108f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 109f22ef01cSRoman Divacky } 110f22ef01cSRoman Divacky 111f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block 112f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def 113f22ef01cSRoman Divacky /// lists if appropriate. 114f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 115f22ef01cSRoman Divacky assert(N->getParent() != 0 && "machine instruction not in a basic block"); 116f22ef01cSRoman Divacky 117f22ef01cSRoman Divacky // Remove from the use/def lists. 1187ae0e2c9SDimitry Andric if (MachineFunction *MF = N->getParent()->getParent()) 1197ae0e2c9SDimitry Andric N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); 120f22ef01cSRoman Divacky 121f22ef01cSRoman Divacky N->setParent(0); 122f22ef01cSRoman Divacky 123f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 124f22ef01cSRoman Divacky } 125f22ef01cSRoman Divacky 126f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one 127f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def 128f22ef01cSRoman Divacky /// lists. 129f22ef01cSRoman Divacky void ilist_traits<MachineInstr>:: 130f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList, 131dff0c46cSDimitry Andric ilist_iterator<MachineInstr> first, 132dff0c46cSDimitry Andric ilist_iterator<MachineInstr> last) { 133f22ef01cSRoman Divacky assert(Parent->getParent() == fromList.Parent->getParent() && 134f22ef01cSRoman Divacky "MachineInstr parent mismatch!"); 135f22ef01cSRoman Divacky 136f22ef01cSRoman Divacky // Splice within the same MBB -> no change. 137f22ef01cSRoman Divacky if (Parent == fromList.Parent) return; 138f22ef01cSRoman Divacky 139f22ef01cSRoman Divacky // If splicing between two blocks within the same function, just update the 140f22ef01cSRoman Divacky // parent pointers. 141f22ef01cSRoman Divacky for (; first != last; ++first) 142f22ef01cSRoman Divacky first->setParent(Parent); 143f22ef01cSRoman Divacky } 144f22ef01cSRoman Divacky 145f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 146f22ef01cSRoman Divacky assert(!MI->getParent() && "MI is still in a block!"); 147f22ef01cSRoman Divacky Parent->getParent()->DeleteMachineInstr(MI); 148f22ef01cSRoman Divacky } 149f22ef01cSRoman Divacky 150ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 151dff0c46cSDimitry Andric instr_iterator I = instr_begin(), E = instr_end(); 152dff0c46cSDimitry Andric while (I != E && I->isPHI()) 153ffd1746dSEd Schouten ++I; 1543861d79fSDimitry Andric assert((I == E || !I->isInsideBundle()) && 1553861d79fSDimitry Andric "First non-phi MI cannot be inside a bundle!"); 156ffd1746dSEd Schouten return I; 157ffd1746dSEd Schouten } 158ffd1746dSEd Schouten 1592754fe60SDimitry Andric MachineBasicBlock::iterator 1602754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 161dff0c46cSDimitry Andric iterator E = end(); 162dff0c46cSDimitry Andric while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue())) 1632754fe60SDimitry Andric ++I; 164dff0c46cSDimitry Andric // FIXME: This needs to change if we wish to bundle labels / dbg_values 165dff0c46cSDimitry Andric // inside the bundle. 1663861d79fSDimitry Andric assert((I == E || !I->isInsideBundle()) && 167dff0c46cSDimitry Andric "First non-phi / non-label instruction is inside a bundle!"); 1682754fe60SDimitry Andric return I; 1692754fe60SDimitry Andric } 1702754fe60SDimitry Andric 171f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 172dff0c46cSDimitry Andric iterator B = begin(), E = end(), I = E; 173dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 174f22ef01cSRoman Divacky ; /*noop */ 175dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 176dff0c46cSDimitry Andric ++I; 177dff0c46cSDimitry Andric return I; 178dff0c46cSDimitry Andric } 179dff0c46cSDimitry Andric 180dff0c46cSDimitry Andric MachineBasicBlock::const_iterator 181dff0c46cSDimitry Andric MachineBasicBlock::getFirstTerminator() const { 182dff0c46cSDimitry Andric const_iterator B = begin(), E = end(), I = E; 183dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 184dff0c46cSDimitry Andric ; /*noop */ 185dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 186dff0c46cSDimitry Andric ++I; 187dff0c46cSDimitry Andric return I; 188dff0c46cSDimitry Andric } 189dff0c46cSDimitry Andric 190dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 191dff0c46cSDimitry Andric instr_iterator B = instr_begin(), E = instr_end(), I = E; 192dff0c46cSDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 193dff0c46cSDimitry Andric ; /*noop */ 194dff0c46cSDimitry Andric while (I != E && !I->isTerminator()) 1952754fe60SDimitry Andric ++I; 196f22ef01cSRoman Divacky return I; 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 1992754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 200dff0c46cSDimitry Andric // Skip over end-of-block dbg_value instructions. 201dff0c46cSDimitry Andric instr_iterator B = instr_begin(), I = instr_end(); 2022754fe60SDimitry Andric while (I != B) { 2032754fe60SDimitry Andric --I; 204dff0c46cSDimitry Andric // Return instruction that starts a bundle. 205dff0c46cSDimitry Andric if (I->isDebugValue() || I->isInsideBundle()) 206dff0c46cSDimitry Andric continue; 207dff0c46cSDimitry Andric return I; 208dff0c46cSDimitry Andric } 209dff0c46cSDimitry Andric // The block is all debug values. 210dff0c46cSDimitry Andric return end(); 211dff0c46cSDimitry Andric } 212dff0c46cSDimitry Andric 213dff0c46cSDimitry Andric MachineBasicBlock::const_iterator 214dff0c46cSDimitry Andric MachineBasicBlock::getLastNonDebugInstr() const { 215dff0c46cSDimitry Andric // Skip over end-of-block dbg_value instructions. 216dff0c46cSDimitry Andric const_instr_iterator B = instr_begin(), I = instr_end(); 217dff0c46cSDimitry Andric while (I != B) { 218dff0c46cSDimitry Andric --I; 219dff0c46cSDimitry Andric // Return instruction that starts a bundle. 220dff0c46cSDimitry Andric if (I->isDebugValue() || I->isInsideBundle()) 2212754fe60SDimitry Andric continue; 2222754fe60SDimitry Andric return I; 2232754fe60SDimitry Andric } 2242754fe60SDimitry Andric // The block is all debug values. 2252754fe60SDimitry Andric return end(); 2262754fe60SDimitry Andric } 2272754fe60SDimitry Andric 2282754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 2292754fe60SDimitry Andric // A block with a landing pad successor only has one other successor. 2302754fe60SDimitry Andric if (succ_size() > 2) 2312754fe60SDimitry Andric return 0; 2322754fe60SDimitry Andric for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 2332754fe60SDimitry Andric if ((*I)->isLandingPad()) 2342754fe60SDimitry Andric return *I; 2352754fe60SDimitry Andric return 0; 2362754fe60SDimitry Andric } 2372754fe60SDimitry Andric 2383861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 239f22ef01cSRoman Divacky void MachineBasicBlock::dump() const { 240f22ef01cSRoman Divacky print(dbgs()); 241f22ef01cSRoman Divacky } 2423861d79fSDimitry Andric #endif 243f22ef01cSRoman Divacky 244f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const { 245f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) 246f22ef01cSRoman Divacky return LBB->getName(); 247f22ef01cSRoman Divacky else 248f22ef01cSRoman Divacky return "(null)"; 249f22ef01cSRoman Divacky } 250f22ef01cSRoman Divacky 251dff0c46cSDimitry Andric /// Return a hopefully unique identifier for this block. 252dff0c46cSDimitry Andric std::string MachineBasicBlock::getFullName() const { 253dff0c46cSDimitry Andric std::string Name; 254dff0c46cSDimitry Andric if (getParent()) 2553861d79fSDimitry Andric Name = (getParent()->getName() + ":").str(); 256dff0c46cSDimitry Andric if (getBasicBlock()) 257dff0c46cSDimitry Andric Name += getBasicBlock()->getName(); 258dff0c46cSDimitry Andric else 259dff0c46cSDimitry Andric Name += (Twine("BB") + Twine(getNumber())).str(); 260dff0c46cSDimitry Andric return Name; 261dff0c46cSDimitry Andric } 262dff0c46cSDimitry Andric 2632754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 264f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 265f22ef01cSRoman Divacky if (!MF) { 266f22ef01cSRoman Divacky OS << "Can't print out MachineBasicBlock because parent MachineFunction" 267f22ef01cSRoman Divacky << " is null\n"; 268f22ef01cSRoman Divacky return; 269f22ef01cSRoman Divacky } 270f22ef01cSRoman Divacky 2712754fe60SDimitry Andric if (Indexes) 2722754fe60SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 2732754fe60SDimitry Andric 274f22ef01cSRoman Divacky OS << "BB#" << getNumber() << ": "; 275f22ef01cSRoman Divacky 276f22ef01cSRoman Divacky const char *Comma = ""; 277f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) { 278f22ef01cSRoman Divacky OS << Comma << "derived from LLVM BB "; 279f22ef01cSRoman Divacky WriteAsOperand(OS, LBB, /*PrintType=*/false); 280f22ef01cSRoman Divacky Comma = ", "; 281f22ef01cSRoman Divacky } 282f22ef01cSRoman Divacky if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 283f22ef01cSRoman Divacky if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 2847ae0e2c9SDimitry Andric if (Alignment) 285dff0c46cSDimitry Andric OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) 286dff0c46cSDimitry Andric << " bytes)"; 287dff0c46cSDimitry Andric 288f22ef01cSRoman Divacky OS << '\n'; 289f22ef01cSRoman Divacky 290f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 291f22ef01cSRoman Divacky if (!livein_empty()) { 2922754fe60SDimitry Andric if (Indexes) OS << '\t'; 293f22ef01cSRoman Divacky OS << " Live Ins:"; 294f22ef01cSRoman Divacky for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 2952754fe60SDimitry Andric OS << ' ' << PrintReg(*I, TRI); 296f22ef01cSRoman Divacky OS << '\n'; 297f22ef01cSRoman Divacky } 298f22ef01cSRoman Divacky // Print the preds of this block according to the CFG. 299f22ef01cSRoman Divacky if (!pred_empty()) { 3002754fe60SDimitry Andric if (Indexes) OS << '\t'; 301f22ef01cSRoman Divacky OS << " Predecessors according to CFG:"; 302f22ef01cSRoman Divacky for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 303f22ef01cSRoman Divacky OS << " BB#" << (*PI)->getNumber(); 304f22ef01cSRoman Divacky OS << '\n'; 305f22ef01cSRoman Divacky } 306f22ef01cSRoman Divacky 307dff0c46cSDimitry Andric for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { 3082754fe60SDimitry Andric if (Indexes) { 3092754fe60SDimitry Andric if (Indexes->hasIndex(I)) 3102754fe60SDimitry Andric OS << Indexes->getInstructionIndex(I); 3112754fe60SDimitry Andric OS << '\t'; 3122754fe60SDimitry Andric } 313f22ef01cSRoman Divacky OS << '\t'; 314dff0c46cSDimitry Andric if (I->isInsideBundle()) 315dff0c46cSDimitry Andric OS << " * "; 316f22ef01cSRoman Divacky I->print(OS, &getParent()->getTarget()); 317f22ef01cSRoman Divacky } 318f22ef01cSRoman Divacky 319f22ef01cSRoman Divacky // Print the successors of this block according to the CFG. 320f22ef01cSRoman Divacky if (!succ_empty()) { 3212754fe60SDimitry Andric if (Indexes) OS << '\t'; 322f22ef01cSRoman Divacky OS << " Successors according to CFG:"; 3237ae0e2c9SDimitry Andric for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { 324f22ef01cSRoman Divacky OS << " BB#" << (*SI)->getNumber(); 3257ae0e2c9SDimitry Andric if (!Weights.empty()) 3267ae0e2c9SDimitry Andric OS << '(' << *getWeightIterator(SI) << ')'; 3277ae0e2c9SDimitry Andric } 328f22ef01cSRoman Divacky OS << '\n'; 329f22ef01cSRoman Divacky } 330f22ef01cSRoman Divacky } 331f22ef01cSRoman Divacky 332f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) { 333f22ef01cSRoman Divacky std::vector<unsigned>::iterator I = 334f22ef01cSRoman Divacky std::find(LiveIns.begin(), LiveIns.end(), Reg); 335dff0c46cSDimitry Andric if (I != LiveIns.end()) 336f22ef01cSRoman Divacky LiveIns.erase(I); 337f22ef01cSRoman Divacky } 338f22ef01cSRoman Divacky 339f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 340f22ef01cSRoman Divacky livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 341f22ef01cSRoman Divacky return I != livein_end(); 342f22ef01cSRoman Divacky } 343f22ef01cSRoman Divacky 344f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 345f22ef01cSRoman Divacky getParent()->splice(NewAfter, this); 346f22ef01cSRoman Divacky } 347f22ef01cSRoman Divacky 348f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 349f22ef01cSRoman Divacky MachineFunction::iterator BBI = NewBefore; 350f22ef01cSRoman Divacky getParent()->splice(++BBI, this); 351f22ef01cSRoman Divacky } 352f22ef01cSRoman Divacky 353f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() { 354f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 355f22ef01cSRoman Divacky // A block with no successors has no concerns with fall-through edges. 356f22ef01cSRoman Divacky if (this->succ_empty()) return; 357f22ef01cSRoman Divacky 358f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 359f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 360ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 361f22ef01cSRoman Divacky bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 362f22ef01cSRoman Divacky (void) B; 363f22ef01cSRoman Divacky assert(!B && "UpdateTerminators requires analyzable predecessors!"); 364f22ef01cSRoman Divacky if (Cond.empty()) { 365f22ef01cSRoman Divacky if (TBB) { 366f22ef01cSRoman Divacky // The block has an unconditional branch. If its successor is now 367f22ef01cSRoman Divacky // its layout successor, delete the branch. 368f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) 369f22ef01cSRoman Divacky TII->RemoveBranch(*this); 370f22ef01cSRoman Divacky } else { 371f22ef01cSRoman Divacky // The block has an unconditional fallthrough. If its successor is not 372dff0c46cSDimitry Andric // its layout successor, insert a branch. First we have to locate the 373dff0c46cSDimitry Andric // only non-landing-pad successor, as that is the fallthrough block. 374dff0c46cSDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 375dff0c46cSDimitry Andric if ((*SI)->isLandingPad()) 376dff0c46cSDimitry Andric continue; 377dff0c46cSDimitry Andric assert(!TBB && "Found more than one non-landing-pad successor!"); 378dff0c46cSDimitry Andric TBB = *SI; 379dff0c46cSDimitry Andric } 380dff0c46cSDimitry Andric 381dff0c46cSDimitry Andric // If there is no non-landing-pad successor, the block has no 382dff0c46cSDimitry Andric // fall-through edges to be concerned with. 383dff0c46cSDimitry Andric if (!TBB) 384dff0c46cSDimitry Andric return; 385dff0c46cSDimitry Andric 386dff0c46cSDimitry Andric // Finally update the unconditional successor to be reached via a branch 387dff0c46cSDimitry Andric // if it would not be reached by fallthrough. 388f22ef01cSRoman Divacky if (!isLayoutSuccessor(TBB)) 389ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 390f22ef01cSRoman Divacky } 391f22ef01cSRoman Divacky } else { 392f22ef01cSRoman Divacky if (FBB) { 393f22ef01cSRoman Divacky // The block has a non-fallthrough conditional branch. If one of its 394f22ef01cSRoman Divacky // successors is its layout successor, rewrite it to a fallthrough 395f22ef01cSRoman Divacky // conditional branch. 396f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 397f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) 398f22ef01cSRoman Divacky return; 399f22ef01cSRoman Divacky TII->RemoveBranch(*this); 400ffd1746dSEd Schouten TII->InsertBranch(*this, FBB, 0, Cond, dl); 401f22ef01cSRoman Divacky } else if (isLayoutSuccessor(FBB)) { 402f22ef01cSRoman Divacky TII->RemoveBranch(*this); 403ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 404f22ef01cSRoman Divacky } 405f22ef01cSRoman Divacky } else { 406cb4dff85SDimitry Andric // Walk through the successors and find the successor which is not 407cb4dff85SDimitry Andric // a landing pad and is not the conditional branch destination (in TBB) 408cb4dff85SDimitry Andric // as the fallthrough successor. 409cb4dff85SDimitry Andric MachineBasicBlock *FallthroughBB = 0; 410cb4dff85SDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 411cb4dff85SDimitry Andric if ((*SI)->isLandingPad() || *SI == TBB) 412cb4dff85SDimitry Andric continue; 413cb4dff85SDimitry Andric assert(!FallthroughBB && "Found more than one fallthrough successor."); 414cb4dff85SDimitry Andric FallthroughBB = *SI; 415cb4dff85SDimitry Andric } 416cb4dff85SDimitry Andric if (!FallthroughBB && canFallThrough()) { 417cb4dff85SDimitry Andric // We fallthrough to the same basic block as the conditional jump 418cb4dff85SDimitry Andric // targets. Remove the conditional jump, leaving unconditional 419cb4dff85SDimitry Andric // fallthrough. 420cb4dff85SDimitry Andric // FIXME: This does not seem like a reasonable pattern to support, but it 421cb4dff85SDimitry Andric // has been seen in the wild coming out of degenerate ARM test cases. 422cb4dff85SDimitry Andric TII->RemoveBranch(*this); 423cb4dff85SDimitry Andric 424cb4dff85SDimitry Andric // Finally update the unconditional successor to be reached via a branch 425cb4dff85SDimitry Andric // if it would not be reached by fallthrough. 426cb4dff85SDimitry Andric if (!isLayoutSuccessor(TBB)) 427cb4dff85SDimitry Andric TII->InsertBranch(*this, TBB, 0, Cond, dl); 428cb4dff85SDimitry Andric return; 429cb4dff85SDimitry Andric } 430cb4dff85SDimitry Andric 431f22ef01cSRoman Divacky // The block has a fallthrough conditional branch. 432f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 433f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) { 434f22ef01cSRoman Divacky // We can't reverse the condition, add an unconditional branch. 435f22ef01cSRoman Divacky Cond.clear(); 436cb4dff85SDimitry Andric TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 437f22ef01cSRoman Divacky return; 438f22ef01cSRoman Divacky } 439f22ef01cSRoman Divacky TII->RemoveBranch(*this); 440cb4dff85SDimitry Andric TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 441cb4dff85SDimitry Andric } else if (!isLayoutSuccessor(FallthroughBB)) { 442f22ef01cSRoman Divacky TII->RemoveBranch(*this); 443cb4dff85SDimitry Andric TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); 444f22ef01cSRoman Divacky } 445f22ef01cSRoman Divacky } 446f22ef01cSRoman Divacky } 447f22ef01cSRoman Divacky } 448f22ef01cSRoman Divacky 44917a519f9SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { 45017a519f9SDimitry Andric 45117a519f9SDimitry Andric // If we see non-zero value for the first time it means we actually use Weight 45217a519f9SDimitry Andric // list, so we fill all Weights with 0's. 45317a519f9SDimitry Andric if (weight != 0 && Weights.empty()) 45417a519f9SDimitry Andric Weights.resize(Successors.size()); 45517a519f9SDimitry Andric 45617a519f9SDimitry Andric if (weight != 0 || !Weights.empty()) 45717a519f9SDimitry Andric Weights.push_back(weight); 45817a519f9SDimitry Andric 459f22ef01cSRoman Divacky Successors.push_back(succ); 460f22ef01cSRoman Divacky succ->addPredecessor(this); 461f22ef01cSRoman Divacky } 462f22ef01cSRoman Divacky 463f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 464f22ef01cSRoman Divacky succ->removePredecessor(this); 465f22ef01cSRoman Divacky succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 466f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 46717a519f9SDimitry Andric 46817a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 46917a519f9SDimitry Andric if (!Weights.empty()) { 47017a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 47117a519f9SDimitry Andric Weights.erase(WI); 47217a519f9SDimitry Andric } 47317a519f9SDimitry Andric 474f22ef01cSRoman Divacky Successors.erase(I); 475f22ef01cSRoman Divacky } 476f22ef01cSRoman Divacky 477f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator 478f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) { 479f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 48017a519f9SDimitry Andric 48117a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 48217a519f9SDimitry Andric if (!Weights.empty()) { 48317a519f9SDimitry Andric weight_iterator WI = getWeightIterator(I); 48417a519f9SDimitry Andric Weights.erase(WI); 48517a519f9SDimitry Andric } 48617a519f9SDimitry Andric 487f22ef01cSRoman Divacky (*I)->removePredecessor(this); 488f22ef01cSRoman Divacky return Successors.erase(I); 489f22ef01cSRoman Divacky } 490f22ef01cSRoman Divacky 49117a519f9SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 49217a519f9SDimitry Andric MachineBasicBlock *New) { 4937ae0e2c9SDimitry Andric if (Old == New) 4947ae0e2c9SDimitry Andric return; 49517a519f9SDimitry Andric 4967ae0e2c9SDimitry Andric succ_iterator E = succ_end(); 4977ae0e2c9SDimitry Andric succ_iterator NewI = E; 4987ae0e2c9SDimitry Andric succ_iterator OldI = E; 4997ae0e2c9SDimitry Andric for (succ_iterator I = succ_begin(); I != E; ++I) { 5007ae0e2c9SDimitry Andric if (*I == Old) { 5017ae0e2c9SDimitry Andric OldI = I; 5027ae0e2c9SDimitry Andric if (NewI != E) 5037ae0e2c9SDimitry Andric break; 5047ae0e2c9SDimitry Andric } 5057ae0e2c9SDimitry Andric if (*I == New) { 5067ae0e2c9SDimitry Andric NewI = I; 5077ae0e2c9SDimitry Andric if (OldI != E) 5087ae0e2c9SDimitry Andric break; 5097ae0e2c9SDimitry Andric } 5107ae0e2c9SDimitry Andric } 5117ae0e2c9SDimitry Andric assert(OldI != E && "Old is not a successor of this block"); 5127ae0e2c9SDimitry Andric Old->removePredecessor(this); 5137ae0e2c9SDimitry Andric 5147ae0e2c9SDimitry Andric // If New isn't already a successor, let it take Old's place. 5157ae0e2c9SDimitry Andric if (NewI == E) { 5167ae0e2c9SDimitry Andric New->addPredecessor(this); 5177ae0e2c9SDimitry Andric *OldI = New; 5187ae0e2c9SDimitry Andric return; 51917a519f9SDimitry Andric } 52017a519f9SDimitry Andric 5217ae0e2c9SDimitry Andric // New is already a successor. 5227ae0e2c9SDimitry Andric // Update its weight instead of adding a duplicate edge. 5237ae0e2c9SDimitry Andric if (!Weights.empty()) { 5247ae0e2c9SDimitry Andric weight_iterator OldWI = getWeightIterator(OldI); 5257ae0e2c9SDimitry Andric *getWeightIterator(NewI) += *OldWI; 5267ae0e2c9SDimitry Andric Weights.erase(OldWI); 5277ae0e2c9SDimitry Andric } 5287ae0e2c9SDimitry Andric Successors.erase(OldI); 52917a519f9SDimitry Andric } 53017a519f9SDimitry Andric 531f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 532f22ef01cSRoman Divacky Predecessors.push_back(pred); 533f22ef01cSRoman Divacky } 534f22ef01cSRoman Divacky 535f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 5363b0f4066SDimitry Andric pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 537f22ef01cSRoman Divacky assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 538f22ef01cSRoman Divacky Predecessors.erase(I); 539f22ef01cSRoman Divacky } 540f22ef01cSRoman Divacky 541f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 542f22ef01cSRoman Divacky if (this == fromMBB) 543f22ef01cSRoman Divacky return; 544f22ef01cSRoman Divacky 545ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 546ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 5477ae0e2c9SDimitry Andric uint32_t Weight = 0; 54817a519f9SDimitry Andric 54917a519f9SDimitry Andric // If Weight list is empty it means we don't use it (disabled optimization). 55017a519f9SDimitry Andric if (!fromMBB->Weights.empty()) 5517ae0e2c9SDimitry Andric Weight = *fromMBB->Weights.begin(); 55217a519f9SDimitry Andric 5537ae0e2c9SDimitry Andric addSuccessor(Succ, Weight); 554ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 555ffd1746dSEd Schouten } 556ffd1746dSEd Schouten } 557f22ef01cSRoman Divacky 558ffd1746dSEd Schouten void 559ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 560ffd1746dSEd Schouten if (this == fromMBB) 561ffd1746dSEd Schouten return; 562ffd1746dSEd Schouten 563ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 564ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 5657ae0e2c9SDimitry Andric uint32_t Weight = 0; 5667ae0e2c9SDimitry Andric if (!fromMBB->Weights.empty()) 5677ae0e2c9SDimitry Andric Weight = *fromMBB->Weights.begin(); 5687ae0e2c9SDimitry Andric addSuccessor(Succ, Weight); 569ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 570ffd1746dSEd Schouten 571ffd1746dSEd Schouten // Fix up any PHI nodes in the successor. 572dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), 573dff0c46cSDimitry Andric ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) 574ffd1746dSEd Schouten for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 575ffd1746dSEd Schouten MachineOperand &MO = MI->getOperand(i); 576ffd1746dSEd Schouten if (MO.getMBB() == fromMBB) 577ffd1746dSEd Schouten MO.setMBB(this); 578ffd1746dSEd Schouten } 579ffd1746dSEd Schouten } 580f22ef01cSRoman Divacky } 581f22ef01cSRoman Divacky 5827ae0e2c9SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 5837ae0e2c9SDimitry Andric return std::find(pred_begin(), pred_end(), MBB) != pred_end(); 5847ae0e2c9SDimitry Andric } 5857ae0e2c9SDimitry Andric 586f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 5877ae0e2c9SDimitry Andric return std::find(succ_begin(), succ_end(), MBB) != succ_end(); 588f22ef01cSRoman Divacky } 589f22ef01cSRoman Divacky 590f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 591f22ef01cSRoman Divacky MachineFunction::const_iterator I(this); 592f22ef01cSRoman Divacky return llvm::next(I) == MachineFunction::const_iterator(MBB); 593f22ef01cSRoman Divacky } 594f22ef01cSRoman Divacky 595f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() { 596f22ef01cSRoman Divacky MachineFunction::iterator Fallthrough = this; 597f22ef01cSRoman Divacky ++Fallthrough; 598f22ef01cSRoman Divacky // If FallthroughBlock is off the end of the function, it can't fall through. 599f22ef01cSRoman Divacky if (Fallthrough == getParent()->end()) 600f22ef01cSRoman Divacky return false; 601f22ef01cSRoman Divacky 602f22ef01cSRoman Divacky // If FallthroughBlock isn't a successor, no fallthrough is possible. 603f22ef01cSRoman Divacky if (!isSuccessor(Fallthrough)) 604f22ef01cSRoman Divacky return false; 605f22ef01cSRoman Divacky 606f22ef01cSRoman Divacky // Analyze the branches, if any, at the end of the block. 607f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 608f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 609f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 610f22ef01cSRoman Divacky if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 611f22ef01cSRoman Divacky // If we couldn't analyze the branch, examine the last instruction. 612f22ef01cSRoman Divacky // If the block doesn't end in a known control barrier, assume fallthrough 613dff0c46cSDimitry Andric // is possible. The isPredicated check is needed because this code can be 614f22ef01cSRoman Divacky // called during IfConversion, where an instruction which is normally a 615dff0c46cSDimitry Andric // Barrier is predicated and thus no longer an actual control barrier. 616dff0c46cSDimitry Andric return empty() || !back().isBarrier() || TII->isPredicated(&back()); 617f22ef01cSRoman Divacky } 618f22ef01cSRoman Divacky 619f22ef01cSRoman Divacky // If there is no branch, control always falls through. 620f22ef01cSRoman Divacky if (TBB == 0) return true; 621f22ef01cSRoman Divacky 622f22ef01cSRoman Divacky // If there is some explicit branch to the fallthrough block, it can obviously 623f22ef01cSRoman Divacky // reach, even though the branch should get folded to fall through implicitly. 624f22ef01cSRoman Divacky if (MachineFunction::iterator(TBB) == Fallthrough || 625f22ef01cSRoman Divacky MachineFunction::iterator(FBB) == Fallthrough) 626f22ef01cSRoman Divacky return true; 627f22ef01cSRoman Divacky 628f22ef01cSRoman Divacky // If it's an unconditional branch to some block not the fall through, it 629f22ef01cSRoman Divacky // doesn't fall through. 630f22ef01cSRoman Divacky if (Cond.empty()) return false; 631f22ef01cSRoman Divacky 632f22ef01cSRoman Divacky // Otherwise, if it is conditional and has no explicit false block, it falls 633f22ef01cSRoman Divacky // through. 634f22ef01cSRoman Divacky return FBB == 0; 635f22ef01cSRoman Divacky } 636f22ef01cSRoman Divacky 637ffd1746dSEd Schouten MachineBasicBlock * 638ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 6397ae0e2c9SDimitry Andric // Splitting the critical edge to a landing pad block is non-trivial. Don't do 6407ae0e2c9SDimitry Andric // it in this generic function. 6417ae0e2c9SDimitry Andric if (Succ->isLandingPad()) 6427ae0e2c9SDimitry Andric return NULL; 6437ae0e2c9SDimitry Andric 644ffd1746dSEd Schouten MachineFunction *MF = getParent(); 645ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 646ffd1746dSEd Schouten 6472754fe60SDimitry Andric // We may need to update this's terminator, but we can't do that if 6482754fe60SDimitry Andric // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 649ffd1746dSEd Schouten const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 650ffd1746dSEd Schouten MachineBasicBlock *TBB = 0, *FBB = 0; 651ffd1746dSEd Schouten SmallVector<MachineOperand, 4> Cond; 652ffd1746dSEd Schouten if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 653ffd1746dSEd Schouten return NULL; 654ffd1746dSEd Schouten 6552754fe60SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 6562754fe60SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 6572754fe60SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 6582754fe60SDimitry Andric // code, just skip those edges. 6592754fe60SDimitry Andric if (TBB && TBB == FBB) { 6602754fe60SDimitry Andric DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 6612754fe60SDimitry Andric << getNumber() << '\n'); 6622754fe60SDimitry Andric return NULL; 6632754fe60SDimitry Andric } 6642754fe60SDimitry Andric 665ffd1746dSEd Schouten MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 666ffd1746dSEd Schouten MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 667e580952dSDimitry Andric DEBUG(dbgs() << "Splitting critical edge:" 668ffd1746dSEd Schouten " BB#" << getNumber() 669ffd1746dSEd Schouten << " -- BB#" << NMBB->getNumber() 670ffd1746dSEd Schouten << " -- BB#" << Succ->getNumber() << '\n'); 671ffd1746dSEd Schouten 672139f7f9bSDimitry Andric LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>(); 673139f7f9bSDimitry Andric SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); 674139f7f9bSDimitry Andric if (LIS) 675139f7f9bSDimitry Andric LIS->insertMBBInMaps(NMBB); 676139f7f9bSDimitry Andric else if (Indexes) 677139f7f9bSDimitry Andric Indexes->insertMBBInMaps(NMBB); 678139f7f9bSDimitry Andric 679bd5abe19SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 680bd5abe19SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 681bd5abe19SDimitry Andric // terminators. 682bd5abe19SDimitry Andric LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 683bd5abe19SDimitry Andric 684bd5abe19SDimitry Andric // Collect a list of virtual registers killed by the terminators. 685bd5abe19SDimitry Andric SmallVector<unsigned, 4> KilledRegs; 686bd5abe19SDimitry Andric if (LV) 687dff0c46cSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 688dff0c46cSDimitry Andric I != E; ++I) { 689bd5abe19SDimitry Andric MachineInstr *MI = I; 690bd5abe19SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 691bd5abe19SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 692dff0c46cSDimitry Andric if (!OI->isReg() || OI->getReg() == 0 || 693dff0c46cSDimitry Andric !OI->isUse() || !OI->isKill() || OI->isUndef()) 694bd5abe19SDimitry Andric continue; 695bd5abe19SDimitry Andric unsigned Reg = OI->getReg(); 696dff0c46cSDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(Reg) || 697bd5abe19SDimitry Andric LV->getVarInfo(Reg).removeKill(MI)) { 698bd5abe19SDimitry Andric KilledRegs.push_back(Reg); 699bd5abe19SDimitry Andric DEBUG(dbgs() << "Removing terminator kill: " << *MI); 700bd5abe19SDimitry Andric OI->setIsKill(false); 701bd5abe19SDimitry Andric } 702bd5abe19SDimitry Andric } 703bd5abe19SDimitry Andric } 704bd5abe19SDimitry Andric 705139f7f9bSDimitry Andric SmallVector<unsigned, 4> UsedRegs; 706139f7f9bSDimitry Andric if (LIS) { 707139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 708139f7f9bSDimitry Andric I != E; ++I) { 709139f7f9bSDimitry Andric MachineInstr *MI = I; 710139f7f9bSDimitry Andric 711139f7f9bSDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 712139f7f9bSDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 713139f7f9bSDimitry Andric if (!OI->isReg() || OI->getReg() == 0) 714139f7f9bSDimitry Andric continue; 715139f7f9bSDimitry Andric 716139f7f9bSDimitry Andric unsigned Reg = OI->getReg(); 717139f7f9bSDimitry Andric if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end()) 718139f7f9bSDimitry Andric UsedRegs.push_back(Reg); 719139f7f9bSDimitry Andric } 720139f7f9bSDimitry Andric } 721139f7f9bSDimitry Andric } 722139f7f9bSDimitry Andric 723ffd1746dSEd Schouten ReplaceUsesOfBlockWith(Succ, NMBB); 724139f7f9bSDimitry Andric 725139f7f9bSDimitry Andric // If updateTerminator() removes instructions, we need to remove them from 726139f7f9bSDimitry Andric // SlotIndexes. 727139f7f9bSDimitry Andric SmallVector<MachineInstr*, 4> Terminators; 728139f7f9bSDimitry Andric if (Indexes) { 729139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 730139f7f9bSDimitry Andric I != E; ++I) 731139f7f9bSDimitry Andric Terminators.push_back(I); 732139f7f9bSDimitry Andric } 733139f7f9bSDimitry Andric 734ffd1746dSEd Schouten updateTerminator(); 735ffd1746dSEd Schouten 736139f7f9bSDimitry Andric if (Indexes) { 737139f7f9bSDimitry Andric SmallVector<MachineInstr*, 4> NewTerminators; 738139f7f9bSDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 739139f7f9bSDimitry Andric I != E; ++I) 740139f7f9bSDimitry Andric NewTerminators.push_back(I); 741139f7f9bSDimitry Andric 742139f7f9bSDimitry Andric for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), 743139f7f9bSDimitry Andric E = Terminators.end(); I != E; ++I) { 744139f7f9bSDimitry Andric if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == 745139f7f9bSDimitry Andric NewTerminators.end()) 746139f7f9bSDimitry Andric Indexes->removeMachineInstrFromMaps(*I); 747139f7f9bSDimitry Andric } 748139f7f9bSDimitry Andric } 749139f7f9bSDimitry Andric 750ffd1746dSEd Schouten // Insert unconditional "jump Succ" instruction in NMBB if necessary. 751ffd1746dSEd Schouten NMBB->addSuccessor(Succ); 752ffd1746dSEd Schouten if (!NMBB->isLayoutSuccessor(Succ)) { 753ffd1746dSEd Schouten Cond.clear(); 754ffd1746dSEd Schouten MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 755139f7f9bSDimitry Andric 756139f7f9bSDimitry Andric if (Indexes) { 757139f7f9bSDimitry Andric for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); 758139f7f9bSDimitry Andric I != E; ++I) { 759139f7f9bSDimitry Andric // Some instructions may have been moved to NMBB by updateTerminator(), 760139f7f9bSDimitry Andric // so we first remove any instruction that already has an index. 761139f7f9bSDimitry Andric if (Indexes->hasIndex(I)) 762139f7f9bSDimitry Andric Indexes->removeMachineInstrFromMaps(I); 763139f7f9bSDimitry Andric Indexes->insertMachineInstrInMaps(I); 764139f7f9bSDimitry Andric } 765139f7f9bSDimitry Andric } 766ffd1746dSEd Schouten } 767ffd1746dSEd Schouten 768ffd1746dSEd Schouten // Fix PHI nodes in Succ so they refer to NMBB instead of this 769dff0c46cSDimitry Andric for (MachineBasicBlock::instr_iterator 770dff0c46cSDimitry Andric i = Succ->instr_begin(),e = Succ->instr_end(); 771ffd1746dSEd Schouten i != e && i->isPHI(); ++i) 772ffd1746dSEd Schouten for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 773ffd1746dSEd Schouten if (i->getOperand(ni+1).getMBB() == this) 774ffd1746dSEd Schouten i->getOperand(ni+1).setMBB(NMBB); 775ffd1746dSEd Schouten 7766122f3e6SDimitry Andric // Inherit live-ins from the successor 7776122f3e6SDimitry Andric for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), 7786122f3e6SDimitry Andric E = Succ->livein_end(); I != E; ++I) 7796122f3e6SDimitry Andric NMBB->addLiveIn(*I); 7806122f3e6SDimitry Andric 781bd5abe19SDimitry Andric // Update LiveVariables. 782dff0c46cSDimitry Andric const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 783bd5abe19SDimitry Andric if (LV) { 784bd5abe19SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 785bd5abe19SDimitry Andric while (!KilledRegs.empty()) { 786bd5abe19SDimitry Andric unsigned Reg = KilledRegs.pop_back_val(); 787dff0c46cSDimitry Andric for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 788dff0c46cSDimitry Andric if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) 789bd5abe19SDimitry Andric continue; 790dff0c46cSDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Reg)) 791bd5abe19SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(I); 792bd5abe19SDimitry Andric DEBUG(dbgs() << "Restored terminator kill: " << *I); 793bd5abe19SDimitry Andric break; 794bd5abe19SDimitry Andric } 795bd5abe19SDimitry Andric } 796bd5abe19SDimitry Andric // Update relevant live-through information. 797ffd1746dSEd Schouten LV->addNewBlock(NMBB, this, Succ); 798bd5abe19SDimitry Andric } 799ffd1746dSEd Schouten 800139f7f9bSDimitry Andric if (LIS) { 801139f7f9bSDimitry Andric // After splitting the edge and updating SlotIndexes, live intervals may be 802139f7f9bSDimitry Andric // in one of two situations, depending on whether this block was the last in 803139f7f9bSDimitry Andric // the function. If the original block was the last in the function, all live 804139f7f9bSDimitry Andric // intervals will end prior to the beginning of the new split block. If the 805139f7f9bSDimitry Andric // original block was not at the end of the function, all live intervals will 806139f7f9bSDimitry Andric // extend to the end of the new split block. 807139f7f9bSDimitry Andric 808139f7f9bSDimitry Andric bool isLastMBB = 809139f7f9bSDimitry Andric llvm::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 810139f7f9bSDimitry Andric 811139f7f9bSDimitry Andric SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 812139f7f9bSDimitry Andric SlotIndex PrevIndex = StartIndex.getPrevSlot(); 813139f7f9bSDimitry Andric SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 814139f7f9bSDimitry Andric 815139f7f9bSDimitry Andric // Find the registers used from NMBB in PHIs in Succ. 816139f7f9bSDimitry Andric SmallSet<unsigned, 8> PHISrcRegs; 817139f7f9bSDimitry Andric for (MachineBasicBlock::instr_iterator 818139f7f9bSDimitry Andric I = Succ->instr_begin(), E = Succ->instr_end(); 819139f7f9bSDimitry Andric I != E && I->isPHI(); ++I) { 820139f7f9bSDimitry Andric for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 821139f7f9bSDimitry Andric if (I->getOperand(ni+1).getMBB() == NMBB) { 822139f7f9bSDimitry Andric MachineOperand &MO = I->getOperand(ni); 823139f7f9bSDimitry Andric unsigned Reg = MO.getReg(); 824139f7f9bSDimitry Andric PHISrcRegs.insert(Reg); 825139f7f9bSDimitry Andric if (MO.isUndef()) 826139f7f9bSDimitry Andric continue; 827139f7f9bSDimitry Andric 828139f7f9bSDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 829139f7f9bSDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 830139f7f9bSDimitry Andric assert(VNI && "PHI sources should be live out of their predecessors."); 831139f7f9bSDimitry Andric LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 832139f7f9bSDimitry Andric } 833139f7f9bSDimitry Andric } 834139f7f9bSDimitry Andric } 835139f7f9bSDimitry Andric 836139f7f9bSDimitry Andric MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 837139f7f9bSDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 838139f7f9bSDimitry Andric unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 839139f7f9bSDimitry Andric if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 840139f7f9bSDimitry Andric continue; 841139f7f9bSDimitry Andric 842139f7f9bSDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 843139f7f9bSDimitry Andric if (!LI.liveAt(PrevIndex)) 844139f7f9bSDimitry Andric continue; 845139f7f9bSDimitry Andric 846139f7f9bSDimitry Andric bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 847139f7f9bSDimitry Andric if (isLiveOut && isLastMBB) { 848139f7f9bSDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 849139f7f9bSDimitry Andric assert(VNI && "LiveInterval should have VNInfo where it is live."); 850139f7f9bSDimitry Andric LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 851139f7f9bSDimitry Andric } else if (!isLiveOut && !isLastMBB) { 852139f7f9bSDimitry Andric LI.removeRange(StartIndex, EndIndex); 853139f7f9bSDimitry Andric } 854139f7f9bSDimitry Andric } 855139f7f9bSDimitry Andric 856139f7f9bSDimitry Andric // Update all intervals for registers whose uses may have been modified by 857139f7f9bSDimitry Andric // updateTerminator(). 858139f7f9bSDimitry Andric LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 859139f7f9bSDimitry Andric } 860139f7f9bSDimitry Andric 861ffd1746dSEd Schouten if (MachineDominatorTree *MDT = 862e580952dSDimitry Andric P->getAnalysisIfAvailable<MachineDominatorTree>()) { 863e580952dSDimitry Andric // Update dominator information. 864e580952dSDimitry Andric MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 865ffd1746dSEd Schouten 866e580952dSDimitry Andric bool IsNewIDom = true; 867e580952dSDimitry Andric for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 868e580952dSDimitry Andric PI != E; ++PI) { 869e580952dSDimitry Andric MachineBasicBlock *PredBB = *PI; 870e580952dSDimitry Andric if (PredBB == NMBB) 871e580952dSDimitry Andric continue; 872e580952dSDimitry Andric if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 873e580952dSDimitry Andric IsNewIDom = false; 874e580952dSDimitry Andric break; 875e580952dSDimitry Andric } 876e580952dSDimitry Andric } 877e580952dSDimitry Andric 878e580952dSDimitry Andric // We know "this" dominates the newly created basic block. 879e580952dSDimitry Andric MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 880e580952dSDimitry Andric 881e580952dSDimitry Andric // If all the other predecessors of "Succ" are dominated by "Succ" itself 882e580952dSDimitry Andric // then the new block is the new immediate dominator of "Succ". Otherwise, 883e580952dSDimitry Andric // the new block doesn't dominate anything. 884e580952dSDimitry Andric if (IsNewIDom) 885e580952dSDimitry Andric MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 886e580952dSDimitry Andric } 887e580952dSDimitry Andric 888e580952dSDimitry Andric if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 889ffd1746dSEd Schouten if (MachineLoop *TIL = MLI->getLoopFor(this)) { 890ffd1746dSEd Schouten // If one or the other blocks were not in a loop, the new block is not 891ffd1746dSEd Schouten // either, and thus LI doesn't need to be updated. 892ffd1746dSEd Schouten if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 893ffd1746dSEd Schouten if (TIL == DestLoop) { 894ffd1746dSEd Schouten // Both in the same loop, the NMBB joins loop. 895ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 896ffd1746dSEd Schouten } else if (TIL->contains(DestLoop)) { 897ffd1746dSEd Schouten // Edge from an outer loop to an inner loop. Add to the outer loop. 898ffd1746dSEd Schouten TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 899ffd1746dSEd Schouten } else if (DestLoop->contains(TIL)) { 900ffd1746dSEd Schouten // Edge from an inner loop to an outer loop. Add to the outer loop. 901ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 902ffd1746dSEd Schouten } else { 903ffd1746dSEd Schouten // Edge from two loops with no containment relation. Because these 904ffd1746dSEd Schouten // are natural loops, we know that the destination block must be the 905ffd1746dSEd Schouten // header of its loop (adding a branch into a loop elsewhere would 906ffd1746dSEd Schouten // create an irreducible loop). 907ffd1746dSEd Schouten assert(DestLoop->getHeader() == Succ && 908ffd1746dSEd Schouten "Should not create irreducible loops!"); 909ffd1746dSEd Schouten if (MachineLoop *P = DestLoop->getParentLoop()) 910ffd1746dSEd Schouten P->addBasicBlockToLoop(NMBB, MLI->getBase()); 911ffd1746dSEd Schouten } 912ffd1746dSEd Schouten } 913ffd1746dSEd Schouten } 914ffd1746dSEd Schouten 915ffd1746dSEd Schouten return NMBB; 916ffd1746dSEd Schouten } 917ffd1746dSEd Schouten 918139f7f9bSDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 919139f7f9bSDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI. 920139f7f9bSDimitry Andric static void unbundleSingleMI(MachineInstr *MI) { 921139f7f9bSDimitry Andric // Removing the first instruction in a bundle. 922139f7f9bSDimitry Andric if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 923139f7f9bSDimitry Andric MI->unbundleFromSucc(); 924139f7f9bSDimitry Andric // Removing the last instruction in a bundle. 925139f7f9bSDimitry Andric if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 926139f7f9bSDimitry Andric MI->unbundleFromPred(); 927139f7f9bSDimitry Andric // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 928139f7f9bSDimitry Andric // are already fine. 929dff0c46cSDimitry Andric } 930dff0c46cSDimitry Andric 931139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator 932139f7f9bSDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 933139f7f9bSDimitry Andric unbundleSingleMI(I); 934139f7f9bSDimitry Andric return Insts.erase(I); 935dff0c46cSDimitry Andric } 936dff0c46cSDimitry Andric 937139f7f9bSDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 938139f7f9bSDimitry Andric unbundleSingleMI(MI); 939139f7f9bSDimitry Andric MI->clearFlag(MachineInstr::BundledPred); 940139f7f9bSDimitry Andric MI->clearFlag(MachineInstr::BundledSucc); 941139f7f9bSDimitry Andric return Insts.remove(MI); 942dff0c46cSDimitry Andric } 943dff0c46cSDimitry Andric 944139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator 945139f7f9bSDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 946139f7f9bSDimitry Andric assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 947139f7f9bSDimitry Andric "Cannot insert instruction with bundle flags"); 948139f7f9bSDimitry Andric // Set the bundle flags when inserting inside a bundle. 949139f7f9bSDimitry Andric if (I != instr_end() && I->isBundledWithPred()) { 950139f7f9bSDimitry Andric MI->setFlag(MachineInstr::BundledPred); 951139f7f9bSDimitry Andric MI->setFlag(MachineInstr::BundledSucc); 952dff0c46cSDimitry Andric } 953139f7f9bSDimitry Andric return Insts.insert(I, MI); 954dff0c46cSDimitry Andric } 955dff0c46cSDimitry Andric 956f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function, 957f22ef01cSRoman Divacky /// and returns it, but does not delete it. 958f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() { 959f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 960f22ef01cSRoman Divacky getParent()->remove(this); 961f22ef01cSRoman Divacky return this; 962f22ef01cSRoman Divacky } 963f22ef01cSRoman Divacky 964f22ef01cSRoman Divacky 965f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function, 966f22ef01cSRoman Divacky /// and deletes it. 967f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() { 968f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 969f22ef01cSRoman Divacky getParent()->erase(this); 970f22ef01cSRoman Divacky } 971f22ef01cSRoman Divacky 972f22ef01cSRoman Divacky 973f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 974f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead. 975f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 976f22ef01cSRoman Divacky MachineBasicBlock *New) { 977f22ef01cSRoman Divacky assert(Old != New && "Cannot replace self with self!"); 978f22ef01cSRoman Divacky 979dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator I = instr_end(); 980dff0c46cSDimitry Andric while (I != instr_begin()) { 981f22ef01cSRoman Divacky --I; 982dff0c46cSDimitry Andric if (!I->isTerminator()) break; 983f22ef01cSRoman Divacky 984f22ef01cSRoman Divacky // Scan the operands of this machine instruction, replacing any uses of Old 985f22ef01cSRoman Divacky // with New. 986f22ef01cSRoman Divacky for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 987f22ef01cSRoman Divacky if (I->getOperand(i).isMBB() && 988f22ef01cSRoman Divacky I->getOperand(i).getMBB() == Old) 989f22ef01cSRoman Divacky I->getOperand(i).setMBB(New); 990f22ef01cSRoman Divacky } 991f22ef01cSRoman Divacky 992f22ef01cSRoman Divacky // Update the successor information. 99317a519f9SDimitry Andric replaceSuccessor(Old, New); 994f22ef01cSRoman Divacky } 995f22ef01cSRoman Divacky 996f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 997f22ef01cSRoman Divacky /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 998f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 999f22ef01cSRoman Divacky /// null. 1000f22ef01cSRoman Divacky /// 1001f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads 1002f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here). 1003f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads. 1004f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 1005f22ef01cSRoman Divacky MachineBasicBlock *DestB, 1006f22ef01cSRoman Divacky bool isCond) { 1007f22ef01cSRoman Divacky // The values of DestA and DestB frequently come from a call to the 1008f22ef01cSRoman Divacky // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 1009f22ef01cSRoman Divacky // values from there. 1010f22ef01cSRoman Divacky // 1011f22ef01cSRoman Divacky // 1. If both DestA and DestB are null, then the block ends with no branches 1012f22ef01cSRoman Divacky // (it falls through to its successor). 1013f22ef01cSRoman Divacky // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 1014f22ef01cSRoman Divacky // with only an unconditional branch. 1015f22ef01cSRoman Divacky // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 1016f22ef01cSRoman Divacky // with a conditional branch that falls through to a successor (DestB). 1017f22ef01cSRoman Divacky // 4. If DestA and DestB is set and isCond is true, then the block ends with a 1018f22ef01cSRoman Divacky // conditional branch followed by an unconditional branch. DestA is the 1019f22ef01cSRoman Divacky // 'true' destination and DestB is the 'false' destination. 1020f22ef01cSRoman Divacky 1021f22ef01cSRoman Divacky bool Changed = false; 1022f22ef01cSRoman Divacky 1023f22ef01cSRoman Divacky MachineFunction::iterator FallThru = 1024f22ef01cSRoman Divacky llvm::next(MachineFunction::iterator(this)); 1025f22ef01cSRoman Divacky 1026f22ef01cSRoman Divacky if (DestA == 0 && DestB == 0) { 1027f22ef01cSRoman Divacky // Block falls through to successor. 1028f22ef01cSRoman Divacky DestA = FallThru; 1029f22ef01cSRoman Divacky DestB = FallThru; 1030f22ef01cSRoman Divacky } else if (DestA != 0 && DestB == 0) { 1031f22ef01cSRoman Divacky if (isCond) 1032f22ef01cSRoman Divacky // Block ends in conditional jump that falls through to successor. 1033f22ef01cSRoman Divacky DestB = FallThru; 1034f22ef01cSRoman Divacky } else { 1035f22ef01cSRoman Divacky assert(DestA && DestB && isCond && 1036f22ef01cSRoman Divacky "CFG in a bad state. Cannot correct CFG edges"); 1037f22ef01cSRoman Divacky } 1038f22ef01cSRoman Divacky 1039f22ef01cSRoman Divacky // Remove superfluous edges. I.e., those which aren't destinations of this 1040f22ef01cSRoman Divacky // basic block, duplicate edges, or landing pads. 1041f22ef01cSRoman Divacky SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 1042f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator SI = succ_begin(); 1043f22ef01cSRoman Divacky while (SI != succ_end()) { 1044f22ef01cSRoman Divacky const MachineBasicBlock *MBB = *SI; 1045f22ef01cSRoman Divacky if (!SeenMBBs.insert(MBB) || 1046f22ef01cSRoman Divacky (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 1047f22ef01cSRoman Divacky // This is a superfluous edge, remove it. 1048f22ef01cSRoman Divacky SI = removeSuccessor(SI); 1049f22ef01cSRoman Divacky Changed = true; 1050f22ef01cSRoman Divacky } else { 1051f22ef01cSRoman Divacky ++SI; 1052f22ef01cSRoman Divacky } 1053f22ef01cSRoman Divacky } 1054f22ef01cSRoman Divacky 1055f22ef01cSRoman Divacky return Changed; 1056f22ef01cSRoman Divacky } 1057f22ef01cSRoman Divacky 1058f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 1059f22ef01cSRoman Divacky /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 1060f22ef01cSRoman Divacky DebugLoc 1061dff0c46cSDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 1062f22ef01cSRoman Divacky DebugLoc DL; 1063dff0c46cSDimitry Andric instr_iterator E = instr_end(); 1064dff0c46cSDimitry Andric if (MBBI == E) 1065dff0c46cSDimitry Andric return DL; 1066dff0c46cSDimitry Andric 1067f22ef01cSRoman Divacky // Skip debug declarations, we don't want a DebugLoc from them. 1068dff0c46cSDimitry Andric while (MBBI != E && MBBI->isDebugValue()) 1069dff0c46cSDimitry Andric MBBI++; 1070dff0c46cSDimitry Andric if (MBBI != E) 1071dff0c46cSDimitry Andric DL = MBBI->getDebugLoc(); 1072f22ef01cSRoman Divacky return DL; 1073f22ef01cSRoman Divacky } 1074f22ef01cSRoman Divacky 107517a519f9SDimitry Andric /// getSuccWeight - Return weight of the edge from this block to MBB. 107617a519f9SDimitry Andric /// 10773861d79fSDimitry Andric uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { 107817a519f9SDimitry Andric if (Weights.empty()) 107917a519f9SDimitry Andric return 0; 108017a519f9SDimitry Andric 10813861d79fSDimitry Andric return *getWeightIterator(Succ); 108217a519f9SDimitry Andric } 108317a519f9SDimitry Andric 108417a519f9SDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor 108517a519f9SDimitry Andric /// iterator 108617a519f9SDimitry Andric MachineBasicBlock::weight_iterator MachineBasicBlock:: 108717a519f9SDimitry Andric getWeightIterator(MachineBasicBlock::succ_iterator I) { 108817a519f9SDimitry Andric assert(Weights.size() == Successors.size() && "Async weight list!"); 108917a519f9SDimitry Andric size_t index = std::distance(Successors.begin(), I); 109017a519f9SDimitry Andric assert(index < Weights.size() && "Not a current successor!"); 109117a519f9SDimitry Andric return Weights.begin() + index; 109217a519f9SDimitry Andric } 109317a519f9SDimitry Andric 1094dff0c46cSDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor 1095dff0c46cSDimitry Andric /// iterator 1096dff0c46cSDimitry Andric MachineBasicBlock::const_weight_iterator MachineBasicBlock:: 1097dff0c46cSDimitry Andric getWeightIterator(MachineBasicBlock::const_succ_iterator I) const { 1098dff0c46cSDimitry Andric assert(Weights.size() == Successors.size() && "Async weight list!"); 1099dff0c46cSDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1100dff0c46cSDimitry Andric assert(index < Weights.size() && "Not a current successor!"); 1101dff0c46cSDimitry Andric return Weights.begin() + index; 1102dff0c46cSDimitry Andric } 1103dff0c46cSDimitry Andric 11043861d79fSDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 11053861d79fSDimitry Andric /// as of just before "MI". 11063861d79fSDimitry Andric /// 11073861d79fSDimitry Andric /// Search is localised to a neighborhood of 11083861d79fSDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N 11093861d79fSDimitry Andric /// instructions after (searching just for defs) MI. 11103861d79fSDimitry Andric MachineBasicBlock::LivenessQueryResult 11113861d79fSDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 11123861d79fSDimitry Andric unsigned Reg, MachineInstr *MI, 11133861d79fSDimitry Andric unsigned Neighborhood) { 11143861d79fSDimitry Andric unsigned N = Neighborhood; 11153861d79fSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 11163861d79fSDimitry Andric 11173861d79fSDimitry Andric // Start by searching backwards from MI, looking for kills, reads or defs. 11183861d79fSDimitry Andric 11193861d79fSDimitry Andric MachineBasicBlock::iterator I(MI); 11203861d79fSDimitry Andric // If this is the first insn in the block, don't search backwards. 11213861d79fSDimitry Andric if (I != MBB->begin()) { 11223861d79fSDimitry Andric do { 11233861d79fSDimitry Andric --I; 11243861d79fSDimitry Andric 11253861d79fSDimitry Andric MachineOperandIteratorBase::PhysRegInfo Analysis = 11263861d79fSDimitry Andric MIOperands(I).analyzePhysReg(Reg, TRI); 11273861d79fSDimitry Andric 1128139f7f9bSDimitry Andric if (Analysis.Defines) 1129139f7f9bSDimitry Andric // Outputs happen after inputs so they take precedence if both are 1130139f7f9bSDimitry Andric // present. 1131139f7f9bSDimitry Andric return Analysis.DefinesDead ? LQR_Dead : LQR_Live; 1132139f7f9bSDimitry Andric 1133139f7f9bSDimitry Andric if (Analysis.Kills || Analysis.Clobbers) 11343861d79fSDimitry Andric // Register killed, so isn't live. 11353861d79fSDimitry Andric return LQR_Dead; 11363861d79fSDimitry Andric 1137139f7f9bSDimitry Andric else if (Analysis.ReadsOverlap) 11383861d79fSDimitry Andric // Defined or read without a previous kill - live. 1139139f7f9bSDimitry Andric return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; 11403861d79fSDimitry Andric 11413861d79fSDimitry Andric } while (I != MBB->begin() && --N > 0); 11423861d79fSDimitry Andric } 11433861d79fSDimitry Andric 11443861d79fSDimitry Andric // Did we get to the start of the block? 11453861d79fSDimitry Andric if (I == MBB->begin()) { 11463861d79fSDimitry Andric // If so, the register's state is definitely defined by the live-in state. 11473861d79fSDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); 11483861d79fSDimitry Andric RAI.isValid(); ++RAI) { 11493861d79fSDimitry Andric if (MBB->isLiveIn(*RAI)) 11503861d79fSDimitry Andric return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; 11513861d79fSDimitry Andric } 11523861d79fSDimitry Andric 11533861d79fSDimitry Andric return LQR_Dead; 11543861d79fSDimitry Andric } 11553861d79fSDimitry Andric 11563861d79fSDimitry Andric N = Neighborhood; 11573861d79fSDimitry Andric 11583861d79fSDimitry Andric // Try searching forwards from MI, looking for reads or defs. 11593861d79fSDimitry Andric I = MachineBasicBlock::iterator(MI); 11603861d79fSDimitry Andric // If this is the last insn in the block, don't search forwards. 11613861d79fSDimitry Andric if (I != MBB->end()) { 11623861d79fSDimitry Andric for (++I; I != MBB->end() && N > 0; ++I, --N) { 11633861d79fSDimitry Andric MachineOperandIteratorBase::PhysRegInfo Analysis = 11643861d79fSDimitry Andric MIOperands(I).analyzePhysReg(Reg, TRI); 11653861d79fSDimitry Andric 11663861d79fSDimitry Andric if (Analysis.ReadsOverlap) 11673861d79fSDimitry Andric // Used, therefore must have been live. 11683861d79fSDimitry Andric return (Analysis.Reads) ? 11693861d79fSDimitry Andric LQR_Live : LQR_OverlappingLive; 11703861d79fSDimitry Andric 1171139f7f9bSDimitry Andric else if (Analysis.Clobbers || Analysis.Defines) 11723861d79fSDimitry Andric // Defined (but not read) therefore cannot have been live. 11733861d79fSDimitry Andric return LQR_Dead; 11743861d79fSDimitry Andric } 11753861d79fSDimitry Andric } 11763861d79fSDimitry Andric 11773861d79fSDimitry Andric // At this point we have no idea of the liveness of the register. 11783861d79fSDimitry Andric return LQR_Unknown; 11793861d79fSDimitry Andric } 11803861d79fSDimitry Andric 1181f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 1182f22ef01cSRoman Divacky bool t) { 1183f22ef01cSRoman Divacky OS << "BB#" << MBB->getNumber(); 1184f22ef01cSRoman Divacky } 1185f22ef01cSRoman Divacky 1186