1f22ef01cSRoman Divacky //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // Collect the sequence of machine instructions for a basic block. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineBasicBlock.h" 15f22ef01cSRoman Divacky #include "llvm/BasicBlock.h" 16ffd1746dSEd Schouten #include "llvm/CodeGen/LiveVariables.h" 17ffd1746dSEd Schouten #include "llvm/CodeGen/MachineDominators.h" 18f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h" 19ffd1746dSEd Schouten #include "llvm/CodeGen/MachineLoopInfo.h" 20f22ef01cSRoman Divacky #include "llvm/MC/MCAsmInfo.h" 21f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 22f22ef01cSRoman Divacky #include "llvm/Target/TargetRegisterInfo.h" 23f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 24f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrDesc.h" 25f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrInfo.h" 26f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h" 27f22ef01cSRoman Divacky #include "llvm/Assembly/Writer.h" 28f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 29f22ef01cSRoman Divacky #include "llvm/ADT/SmallPtrSet.h" 30f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 31f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h" 32f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 33f22ef01cSRoman Divacky #include <algorithm> 34f22ef01cSRoman Divacky using namespace llvm; 35f22ef01cSRoman Divacky 36f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 37f22ef01cSRoman Divacky : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 38f22ef01cSRoman Divacky AddressTaken(false) { 39f22ef01cSRoman Divacky Insts.Parent = this; 40f22ef01cSRoman Divacky } 41f22ef01cSRoman Divacky 42f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() { 43f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(this); 44f22ef01cSRoman Divacky } 45f22ef01cSRoman Divacky 46f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block. 47f22ef01cSRoman Divacky /// 48f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const { 49f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 50f22ef01cSRoman Divacky MCContext &Ctx = MF->getContext(); 51f22ef01cSRoman Divacky const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 52f22ef01cSRoman Divacky return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 53f22ef01cSRoman Divacky Twine(MF->getFunctionNumber()) + "_" + 54f22ef01cSRoman Divacky Twine(getNumber())); 55f22ef01cSRoman Divacky } 56f22ef01cSRoman Divacky 57f22ef01cSRoman Divacky 58f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 59f22ef01cSRoman Divacky MBB.print(OS); 60f22ef01cSRoman Divacky return OS; 61f22ef01cSRoman Divacky } 62f22ef01cSRoman Divacky 63f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 64f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the 65f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers. 66f22ef01cSRoman Divacky /// 67f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 68f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a 69f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1. 70f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 71f22ef01cSRoman Divacky MachineFunction &MF = *N->getParent(); 72f22ef01cSRoman Divacky N->Number = MF.addToMBBNumbering(N); 73f22ef01cSRoman Divacky 74f22ef01cSRoman Divacky // Make sure the instructions have their operands in the reginfo lists. 75f22ef01cSRoman Divacky MachineRegisterInfo &RegInfo = MF.getRegInfo(); 76f22ef01cSRoman Divacky for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) 77f22ef01cSRoman Divacky I->AddRegOperandsToUseLists(RegInfo); 78f22ef01cSRoman Divacky 79f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 80f22ef01cSRoman Divacky } 81f22ef01cSRoman Divacky 82f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 83f22ef01cSRoman Divacky N->getParent()->removeFromMBBNumbering(N->Number); 84f22ef01cSRoman Divacky N->Number = -1; 85f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 86f22ef01cSRoman Divacky } 87f22ef01cSRoman Divacky 88f22ef01cSRoman Divacky 89f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block 90f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def 91f22ef01cSRoman Divacky /// lists if appropriate. 92f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 93f22ef01cSRoman Divacky assert(N->getParent() == 0 && "machine instruction already in a basic block"); 94f22ef01cSRoman Divacky N->setParent(Parent); 95f22ef01cSRoman Divacky 96f22ef01cSRoman Divacky // Add the instruction's register operands to their corresponding 97f22ef01cSRoman Divacky // use/def lists. 98f22ef01cSRoman Divacky MachineFunction *MF = Parent->getParent(); 99f22ef01cSRoman Divacky N->AddRegOperandsToUseLists(MF->getRegInfo()); 100f22ef01cSRoman Divacky 101f22ef01cSRoman Divacky LeakDetector::removeGarbageObject(N); 102f22ef01cSRoman Divacky } 103f22ef01cSRoman Divacky 104f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block 105f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def 106f22ef01cSRoman Divacky /// lists if appropriate. 107f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 108f22ef01cSRoman Divacky assert(N->getParent() != 0 && "machine instruction not in a basic block"); 109f22ef01cSRoman Divacky 110f22ef01cSRoman Divacky // Remove from the use/def lists. 111f22ef01cSRoman Divacky N->RemoveRegOperandsFromUseLists(); 112f22ef01cSRoman Divacky 113f22ef01cSRoman Divacky N->setParent(0); 114f22ef01cSRoman Divacky 115f22ef01cSRoman Divacky LeakDetector::addGarbageObject(N); 116f22ef01cSRoman Divacky } 117f22ef01cSRoman Divacky 118f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one 119f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def 120f22ef01cSRoman Divacky /// lists. 121f22ef01cSRoman Divacky void ilist_traits<MachineInstr>:: 122f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList, 123f22ef01cSRoman Divacky MachineBasicBlock::iterator first, 124f22ef01cSRoman Divacky MachineBasicBlock::iterator last) { 125f22ef01cSRoman Divacky assert(Parent->getParent() == fromList.Parent->getParent() && 126f22ef01cSRoman Divacky "MachineInstr parent mismatch!"); 127f22ef01cSRoman Divacky 128f22ef01cSRoman Divacky // Splice within the same MBB -> no change. 129f22ef01cSRoman Divacky if (Parent == fromList.Parent) return; 130f22ef01cSRoman Divacky 131f22ef01cSRoman Divacky // If splicing between two blocks within the same function, just update the 132f22ef01cSRoman Divacky // parent pointers. 133f22ef01cSRoman Divacky for (; first != last; ++first) 134f22ef01cSRoman Divacky first->setParent(Parent); 135f22ef01cSRoman Divacky } 136f22ef01cSRoman Divacky 137f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 138f22ef01cSRoman Divacky assert(!MI->getParent() && "MI is still in a block!"); 139f22ef01cSRoman Divacky Parent->getParent()->DeleteMachineInstr(MI); 140f22ef01cSRoman Divacky } 141f22ef01cSRoman Divacky 142ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 143ffd1746dSEd Schouten iterator I = begin(); 144ffd1746dSEd Schouten while (I != end() && I->isPHI()) 145ffd1746dSEd Schouten ++I; 146ffd1746dSEd Schouten return I; 147ffd1746dSEd Schouten } 148ffd1746dSEd Schouten 149f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 150f22ef01cSRoman Divacky iterator I = end(); 151f22ef01cSRoman Divacky while (I != begin() && (--I)->getDesc().isTerminator()) 152f22ef01cSRoman Divacky ; /*noop */ 153f22ef01cSRoman Divacky if (I != end() && !I->getDesc().isTerminator()) ++I; 154f22ef01cSRoman Divacky return I; 155f22ef01cSRoman Divacky } 156f22ef01cSRoman Divacky 157f22ef01cSRoman Divacky void MachineBasicBlock::dump() const { 158f22ef01cSRoman Divacky print(dbgs()); 159f22ef01cSRoman Divacky } 160f22ef01cSRoman Divacky 161f22ef01cSRoman Divacky static inline void OutputReg(raw_ostream &os, unsigned RegNo, 162f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = 0) { 163f22ef01cSRoman Divacky if (RegNo != 0 && TargetRegisterInfo::isPhysicalRegister(RegNo)) { 164f22ef01cSRoman Divacky if (TRI) 165f22ef01cSRoman Divacky os << " %" << TRI->get(RegNo).Name; 166f22ef01cSRoman Divacky else 167f22ef01cSRoman Divacky os << " %physreg" << RegNo; 168f22ef01cSRoman Divacky } else 169f22ef01cSRoman Divacky os << " %reg" << RegNo; 170f22ef01cSRoman Divacky } 171f22ef01cSRoman Divacky 172f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const { 173f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) 174f22ef01cSRoman Divacky return LBB->getName(); 175f22ef01cSRoman Divacky else 176f22ef01cSRoman Divacky return "(null)"; 177f22ef01cSRoman Divacky } 178f22ef01cSRoman Divacky 179f22ef01cSRoman Divacky void MachineBasicBlock::print(raw_ostream &OS) const { 180f22ef01cSRoman Divacky const MachineFunction *MF = getParent(); 181f22ef01cSRoman Divacky if (!MF) { 182f22ef01cSRoman Divacky OS << "Can't print out MachineBasicBlock because parent MachineFunction" 183f22ef01cSRoman Divacky << " is null\n"; 184f22ef01cSRoman Divacky return; 185f22ef01cSRoman Divacky } 186f22ef01cSRoman Divacky 187f22ef01cSRoman Divacky if (Alignment) { OS << "Alignment " << Alignment << "\n"; } 188f22ef01cSRoman Divacky 189f22ef01cSRoman Divacky OS << "BB#" << getNumber() << ": "; 190f22ef01cSRoman Divacky 191f22ef01cSRoman Divacky const char *Comma = ""; 192f22ef01cSRoman Divacky if (const BasicBlock *LBB = getBasicBlock()) { 193f22ef01cSRoman Divacky OS << Comma << "derived from LLVM BB "; 194f22ef01cSRoman Divacky WriteAsOperand(OS, LBB, /*PrintType=*/false); 195f22ef01cSRoman Divacky Comma = ", "; 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 198f22ef01cSRoman Divacky if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 199f22ef01cSRoman Divacky OS << '\n'; 200f22ef01cSRoman Divacky 201f22ef01cSRoman Divacky const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 202f22ef01cSRoman Divacky if (!livein_empty()) { 203f22ef01cSRoman Divacky OS << " Live Ins:"; 204f22ef01cSRoman Divacky for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 205f22ef01cSRoman Divacky OutputReg(OS, *I, TRI); 206f22ef01cSRoman Divacky OS << '\n'; 207f22ef01cSRoman Divacky } 208f22ef01cSRoman Divacky // Print the preds of this block according to the CFG. 209f22ef01cSRoman Divacky if (!pred_empty()) { 210f22ef01cSRoman Divacky OS << " Predecessors according to CFG:"; 211f22ef01cSRoman Divacky for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 212f22ef01cSRoman Divacky OS << " BB#" << (*PI)->getNumber(); 213f22ef01cSRoman Divacky OS << '\n'; 214f22ef01cSRoman Divacky } 215f22ef01cSRoman Divacky 216f22ef01cSRoman Divacky for (const_iterator I = begin(); I != end(); ++I) { 217f22ef01cSRoman Divacky OS << '\t'; 218f22ef01cSRoman Divacky I->print(OS, &getParent()->getTarget()); 219f22ef01cSRoman Divacky } 220f22ef01cSRoman Divacky 221f22ef01cSRoman Divacky // Print the successors of this block according to the CFG. 222f22ef01cSRoman Divacky if (!succ_empty()) { 223f22ef01cSRoman Divacky OS << " Successors according to CFG:"; 224f22ef01cSRoman Divacky for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 225f22ef01cSRoman Divacky OS << " BB#" << (*SI)->getNumber(); 226f22ef01cSRoman Divacky OS << '\n'; 227f22ef01cSRoman Divacky } 228f22ef01cSRoman Divacky } 229f22ef01cSRoman Divacky 230f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) { 231f22ef01cSRoman Divacky std::vector<unsigned>::iterator I = 232f22ef01cSRoman Divacky std::find(LiveIns.begin(), LiveIns.end(), Reg); 233f22ef01cSRoman Divacky assert(I != LiveIns.end() && "Not a live in!"); 234f22ef01cSRoman Divacky LiveIns.erase(I); 235f22ef01cSRoman Divacky } 236f22ef01cSRoman Divacky 237f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 238f22ef01cSRoman Divacky livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 239f22ef01cSRoman Divacky return I != livein_end(); 240f22ef01cSRoman Divacky } 241f22ef01cSRoman Divacky 242f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 243f22ef01cSRoman Divacky getParent()->splice(NewAfter, this); 244f22ef01cSRoman Divacky } 245f22ef01cSRoman Divacky 246f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 247f22ef01cSRoman Divacky MachineFunction::iterator BBI = NewBefore; 248f22ef01cSRoman Divacky getParent()->splice(++BBI, this); 249f22ef01cSRoman Divacky } 250f22ef01cSRoman Divacky 251f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() { 252f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 253f22ef01cSRoman Divacky // A block with no successors has no concerns with fall-through edges. 254f22ef01cSRoman Divacky if (this->succ_empty()) return; 255f22ef01cSRoman Divacky 256f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 257f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 258ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 259f22ef01cSRoman Divacky bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 260f22ef01cSRoman Divacky (void) B; 261f22ef01cSRoman Divacky assert(!B && "UpdateTerminators requires analyzable predecessors!"); 262f22ef01cSRoman Divacky if (Cond.empty()) { 263f22ef01cSRoman Divacky if (TBB) { 264f22ef01cSRoman Divacky // The block has an unconditional branch. If its successor is now 265f22ef01cSRoman Divacky // its layout successor, delete the branch. 266f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) 267f22ef01cSRoman Divacky TII->RemoveBranch(*this); 268f22ef01cSRoman Divacky } else { 269f22ef01cSRoman Divacky // The block has an unconditional fallthrough. If its successor is not 270f22ef01cSRoman Divacky // its layout successor, insert a branch. 271f22ef01cSRoman Divacky TBB = *succ_begin(); 272f22ef01cSRoman Divacky if (!isLayoutSuccessor(TBB)) 273ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 274f22ef01cSRoman Divacky } 275f22ef01cSRoman Divacky } else { 276f22ef01cSRoman Divacky if (FBB) { 277f22ef01cSRoman Divacky // The block has a non-fallthrough conditional branch. If one of its 278f22ef01cSRoman Divacky // successors is its layout successor, rewrite it to a fallthrough 279f22ef01cSRoman Divacky // conditional branch. 280f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 281f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) 282f22ef01cSRoman Divacky return; 283f22ef01cSRoman Divacky TII->RemoveBranch(*this); 284ffd1746dSEd Schouten TII->InsertBranch(*this, FBB, 0, Cond, dl); 285f22ef01cSRoman Divacky } else if (isLayoutSuccessor(FBB)) { 286f22ef01cSRoman Divacky TII->RemoveBranch(*this); 287ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, 0, Cond, dl); 288f22ef01cSRoman Divacky } 289f22ef01cSRoman Divacky } else { 290f22ef01cSRoman Divacky // The block has a fallthrough conditional branch. 291f22ef01cSRoman Divacky MachineBasicBlock *MBBA = *succ_begin(); 292f22ef01cSRoman Divacky MachineBasicBlock *MBBB = *llvm::next(succ_begin()); 293f22ef01cSRoman Divacky if (MBBA == TBB) std::swap(MBBB, MBBA); 294f22ef01cSRoman Divacky if (isLayoutSuccessor(TBB)) { 295f22ef01cSRoman Divacky if (TII->ReverseBranchCondition(Cond)) { 296f22ef01cSRoman Divacky // We can't reverse the condition, add an unconditional branch. 297f22ef01cSRoman Divacky Cond.clear(); 298ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 299f22ef01cSRoman Divacky return; 300f22ef01cSRoman Divacky } 301f22ef01cSRoman Divacky TII->RemoveBranch(*this); 302ffd1746dSEd Schouten TII->InsertBranch(*this, MBBA, 0, Cond, dl); 303f22ef01cSRoman Divacky } else if (!isLayoutSuccessor(MBBA)) { 304f22ef01cSRoman Divacky TII->RemoveBranch(*this); 305ffd1746dSEd Schouten TII->InsertBranch(*this, TBB, MBBA, Cond, dl); 306f22ef01cSRoman Divacky } 307f22ef01cSRoman Divacky } 308f22ef01cSRoman Divacky } 309f22ef01cSRoman Divacky } 310f22ef01cSRoman Divacky 311f22ef01cSRoman Divacky void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 312f22ef01cSRoman Divacky Successors.push_back(succ); 313f22ef01cSRoman Divacky succ->addPredecessor(this); 314f22ef01cSRoman Divacky } 315f22ef01cSRoman Divacky 316f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 317f22ef01cSRoman Divacky succ->removePredecessor(this); 318f22ef01cSRoman Divacky succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 319f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 320f22ef01cSRoman Divacky Successors.erase(I); 321f22ef01cSRoman Divacky } 322f22ef01cSRoman Divacky 323f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator 324f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) { 325f22ef01cSRoman Divacky assert(I != Successors.end() && "Not a current successor!"); 326f22ef01cSRoman Divacky (*I)->removePredecessor(this); 327f22ef01cSRoman Divacky return Successors.erase(I); 328f22ef01cSRoman Divacky } 329f22ef01cSRoman Divacky 330f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 331f22ef01cSRoman Divacky Predecessors.push_back(pred); 332f22ef01cSRoman Divacky } 333f22ef01cSRoman Divacky 334f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 335f22ef01cSRoman Divacky std::vector<MachineBasicBlock *>::iterator I = 336f22ef01cSRoman Divacky std::find(Predecessors.begin(), Predecessors.end(), pred); 337f22ef01cSRoman Divacky assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 338f22ef01cSRoman Divacky Predecessors.erase(I); 339f22ef01cSRoman Divacky } 340f22ef01cSRoman Divacky 341f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 342f22ef01cSRoman Divacky if (this == fromMBB) 343f22ef01cSRoman Divacky return; 344f22ef01cSRoman Divacky 345ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 346ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 347ffd1746dSEd Schouten addSuccessor(Succ); 348ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 349ffd1746dSEd Schouten } 350ffd1746dSEd Schouten } 351f22ef01cSRoman Divacky 352ffd1746dSEd Schouten void 353ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 354ffd1746dSEd Schouten if (this == fromMBB) 355ffd1746dSEd Schouten return; 356ffd1746dSEd Schouten 357ffd1746dSEd Schouten while (!fromMBB->succ_empty()) { 358ffd1746dSEd Schouten MachineBasicBlock *Succ = *fromMBB->succ_begin(); 359ffd1746dSEd Schouten addSuccessor(Succ); 360ffd1746dSEd Schouten fromMBB->removeSuccessor(Succ); 361ffd1746dSEd Schouten 362ffd1746dSEd Schouten // Fix up any PHI nodes in the successor. 363ffd1746dSEd Schouten for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end(); 364ffd1746dSEd Schouten MI != ME && MI->isPHI(); ++MI) 365ffd1746dSEd Schouten for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 366ffd1746dSEd Schouten MachineOperand &MO = MI->getOperand(i); 367ffd1746dSEd Schouten if (MO.getMBB() == fromMBB) 368ffd1746dSEd Schouten MO.setMBB(this); 369ffd1746dSEd Schouten } 370ffd1746dSEd Schouten } 371f22ef01cSRoman Divacky } 372f22ef01cSRoman Divacky 373f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 374f22ef01cSRoman Divacky std::vector<MachineBasicBlock *>::const_iterator I = 375f22ef01cSRoman Divacky std::find(Successors.begin(), Successors.end(), MBB); 376f22ef01cSRoman Divacky return I != Successors.end(); 377f22ef01cSRoman Divacky } 378f22ef01cSRoman Divacky 379f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 380f22ef01cSRoman Divacky MachineFunction::const_iterator I(this); 381f22ef01cSRoman Divacky return llvm::next(I) == MachineFunction::const_iterator(MBB); 382f22ef01cSRoman Divacky } 383f22ef01cSRoman Divacky 384f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() { 385f22ef01cSRoman Divacky MachineFunction::iterator Fallthrough = this; 386f22ef01cSRoman Divacky ++Fallthrough; 387f22ef01cSRoman Divacky // If FallthroughBlock is off the end of the function, it can't fall through. 388f22ef01cSRoman Divacky if (Fallthrough == getParent()->end()) 389f22ef01cSRoman Divacky return false; 390f22ef01cSRoman Divacky 391f22ef01cSRoman Divacky // If FallthroughBlock isn't a successor, no fallthrough is possible. 392f22ef01cSRoman Divacky if (!isSuccessor(Fallthrough)) 393f22ef01cSRoman Divacky return false; 394f22ef01cSRoman Divacky 395f22ef01cSRoman Divacky // Analyze the branches, if any, at the end of the block. 396f22ef01cSRoman Divacky MachineBasicBlock *TBB = 0, *FBB = 0; 397f22ef01cSRoman Divacky SmallVector<MachineOperand, 4> Cond; 398f22ef01cSRoman Divacky const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 399f22ef01cSRoman Divacky if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 400f22ef01cSRoman Divacky // If we couldn't analyze the branch, examine the last instruction. 401f22ef01cSRoman Divacky // If the block doesn't end in a known control barrier, assume fallthrough 402f22ef01cSRoman Divacky // is possible. The isPredicable check is needed because this code can be 403f22ef01cSRoman Divacky // called during IfConversion, where an instruction which is normally a 404f22ef01cSRoman Divacky // Barrier is predicated and thus no longer an actual control barrier. This 405f22ef01cSRoman Divacky // is over-conservative though, because if an instruction isn't actually 406f22ef01cSRoman Divacky // predicated we could still treat it like a barrier. 407f22ef01cSRoman Divacky return empty() || !back().getDesc().isBarrier() || 408f22ef01cSRoman Divacky back().getDesc().isPredicable(); 409f22ef01cSRoman Divacky } 410f22ef01cSRoman Divacky 411f22ef01cSRoman Divacky // If there is no branch, control always falls through. 412f22ef01cSRoman Divacky if (TBB == 0) return true; 413f22ef01cSRoman Divacky 414f22ef01cSRoman Divacky // If there is some explicit branch to the fallthrough block, it can obviously 415f22ef01cSRoman Divacky // reach, even though the branch should get folded to fall through implicitly. 416f22ef01cSRoman Divacky if (MachineFunction::iterator(TBB) == Fallthrough || 417f22ef01cSRoman Divacky MachineFunction::iterator(FBB) == Fallthrough) 418f22ef01cSRoman Divacky return true; 419f22ef01cSRoman Divacky 420f22ef01cSRoman Divacky // If it's an unconditional branch to some block not the fall through, it 421f22ef01cSRoman Divacky // doesn't fall through. 422f22ef01cSRoman Divacky if (Cond.empty()) return false; 423f22ef01cSRoman Divacky 424f22ef01cSRoman Divacky // Otherwise, if it is conditional and has no explicit false block, it falls 425f22ef01cSRoman Divacky // through. 426f22ef01cSRoman Divacky return FBB == 0; 427f22ef01cSRoman Divacky } 428f22ef01cSRoman Divacky 429ffd1746dSEd Schouten MachineBasicBlock * 430ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 431ffd1746dSEd Schouten MachineFunction *MF = getParent(); 432ffd1746dSEd Schouten DebugLoc dl; // FIXME: this is nowhere 433ffd1746dSEd Schouten 434ffd1746dSEd Schouten // We may need to update this's terminator, but we can't do that if AnalyzeBranch 435ffd1746dSEd Schouten // fails. If this uses a jump table, we won't touch it. 436ffd1746dSEd Schouten const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 437ffd1746dSEd Schouten MachineBasicBlock *TBB = 0, *FBB = 0; 438ffd1746dSEd Schouten SmallVector<MachineOperand, 4> Cond; 439ffd1746dSEd Schouten if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 440ffd1746dSEd Schouten return NULL; 441ffd1746dSEd Schouten 442ffd1746dSEd Schouten MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 443ffd1746dSEd Schouten MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 444ffd1746dSEd Schouten DEBUG(dbgs() << "PHIElimination splitting critical edge:" 445ffd1746dSEd Schouten " BB#" << getNumber() 446ffd1746dSEd Schouten << " -- BB#" << NMBB->getNumber() 447ffd1746dSEd Schouten << " -- BB#" << Succ->getNumber() << '\n'); 448ffd1746dSEd Schouten 449ffd1746dSEd Schouten ReplaceUsesOfBlockWith(Succ, NMBB); 450ffd1746dSEd Schouten updateTerminator(); 451ffd1746dSEd Schouten 452ffd1746dSEd Schouten // Insert unconditional "jump Succ" instruction in NMBB if necessary. 453ffd1746dSEd Schouten NMBB->addSuccessor(Succ); 454ffd1746dSEd Schouten if (!NMBB->isLayoutSuccessor(Succ)) { 455ffd1746dSEd Schouten Cond.clear(); 456ffd1746dSEd Schouten MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 457ffd1746dSEd Schouten } 458ffd1746dSEd Schouten 459ffd1746dSEd Schouten // Fix PHI nodes in Succ so they refer to NMBB instead of this 460ffd1746dSEd Schouten for (MachineBasicBlock::iterator i = Succ->begin(), e = Succ->end(); 461ffd1746dSEd Schouten i != e && i->isPHI(); ++i) 462ffd1746dSEd Schouten for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 463ffd1746dSEd Schouten if (i->getOperand(ni+1).getMBB() == this) 464ffd1746dSEd Schouten i->getOperand(ni+1).setMBB(NMBB); 465ffd1746dSEd Schouten 466ffd1746dSEd Schouten if (LiveVariables *LV = 467ffd1746dSEd Schouten P->getAnalysisIfAvailable<LiveVariables>()) 468ffd1746dSEd Schouten LV->addNewBlock(NMBB, this, Succ); 469ffd1746dSEd Schouten 470ffd1746dSEd Schouten if (MachineDominatorTree *MDT = 471ffd1746dSEd Schouten P->getAnalysisIfAvailable<MachineDominatorTree>()) 472ffd1746dSEd Schouten MDT->addNewBlock(NMBB, this); 473ffd1746dSEd Schouten 474ffd1746dSEd Schouten if (MachineLoopInfo *MLI = 475ffd1746dSEd Schouten P->getAnalysisIfAvailable<MachineLoopInfo>()) 476ffd1746dSEd Schouten if (MachineLoop *TIL = MLI->getLoopFor(this)) { 477ffd1746dSEd Schouten // If one or the other blocks were not in a loop, the new block is not 478ffd1746dSEd Schouten // either, and thus LI doesn't need to be updated. 479ffd1746dSEd Schouten if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 480ffd1746dSEd Schouten if (TIL == DestLoop) { 481ffd1746dSEd Schouten // Both in the same loop, the NMBB joins loop. 482ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 483ffd1746dSEd Schouten } else if (TIL->contains(DestLoop)) { 484ffd1746dSEd Schouten // Edge from an outer loop to an inner loop. Add to the outer loop. 485ffd1746dSEd Schouten TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 486ffd1746dSEd Schouten } else if (DestLoop->contains(TIL)) { 487ffd1746dSEd Schouten // Edge from an inner loop to an outer loop. Add to the outer loop. 488ffd1746dSEd Schouten DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 489ffd1746dSEd Schouten } else { 490ffd1746dSEd Schouten // Edge from two loops with no containment relation. Because these 491ffd1746dSEd Schouten // are natural loops, we know that the destination block must be the 492ffd1746dSEd Schouten // header of its loop (adding a branch into a loop elsewhere would 493ffd1746dSEd Schouten // create an irreducible loop). 494ffd1746dSEd Schouten assert(DestLoop->getHeader() == Succ && 495ffd1746dSEd Schouten "Should not create irreducible loops!"); 496ffd1746dSEd Schouten if (MachineLoop *P = DestLoop->getParentLoop()) 497ffd1746dSEd Schouten P->addBasicBlockToLoop(NMBB, MLI->getBase()); 498ffd1746dSEd Schouten } 499ffd1746dSEd Schouten } 500ffd1746dSEd Schouten } 501ffd1746dSEd Schouten 502ffd1746dSEd Schouten return NMBB; 503ffd1746dSEd Schouten } 504ffd1746dSEd Schouten 505f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function, 506f22ef01cSRoman Divacky /// and returns it, but does not delete it. 507f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() { 508f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 509f22ef01cSRoman Divacky getParent()->remove(this); 510f22ef01cSRoman Divacky return this; 511f22ef01cSRoman Divacky } 512f22ef01cSRoman Divacky 513f22ef01cSRoman Divacky 514f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function, 515f22ef01cSRoman Divacky /// and deletes it. 516f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() { 517f22ef01cSRoman Divacky assert(getParent() && "Not embedded in a function!"); 518f22ef01cSRoman Divacky getParent()->erase(this); 519f22ef01cSRoman Divacky } 520f22ef01cSRoman Divacky 521f22ef01cSRoman Divacky 522f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 523f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead. 524f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 525f22ef01cSRoman Divacky MachineBasicBlock *New) { 526f22ef01cSRoman Divacky assert(Old != New && "Cannot replace self with self!"); 527f22ef01cSRoman Divacky 528f22ef01cSRoman Divacky MachineBasicBlock::iterator I = end(); 529f22ef01cSRoman Divacky while (I != begin()) { 530f22ef01cSRoman Divacky --I; 531f22ef01cSRoman Divacky if (!I->getDesc().isTerminator()) break; 532f22ef01cSRoman Divacky 533f22ef01cSRoman Divacky // Scan the operands of this machine instruction, replacing any uses of Old 534f22ef01cSRoman Divacky // with New. 535f22ef01cSRoman Divacky for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 536f22ef01cSRoman Divacky if (I->getOperand(i).isMBB() && 537f22ef01cSRoman Divacky I->getOperand(i).getMBB() == Old) 538f22ef01cSRoman Divacky I->getOperand(i).setMBB(New); 539f22ef01cSRoman Divacky } 540f22ef01cSRoman Divacky 541f22ef01cSRoman Divacky // Update the successor information. 542f22ef01cSRoman Divacky removeSuccessor(Old); 543f22ef01cSRoman Divacky addSuccessor(New); 544f22ef01cSRoman Divacky } 545f22ef01cSRoman Divacky 546f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 547f22ef01cSRoman Divacky /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 548f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 549f22ef01cSRoman Divacky /// null. 550f22ef01cSRoman Divacky /// 551f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads 552f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here). 553f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads. 554f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 555f22ef01cSRoman Divacky MachineBasicBlock *DestB, 556f22ef01cSRoman Divacky bool isCond) { 557f22ef01cSRoman Divacky // The values of DestA and DestB frequently come from a call to the 558f22ef01cSRoman Divacky // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 559f22ef01cSRoman Divacky // values from there. 560f22ef01cSRoman Divacky // 561f22ef01cSRoman Divacky // 1. If both DestA and DestB are null, then the block ends with no branches 562f22ef01cSRoman Divacky // (it falls through to its successor). 563f22ef01cSRoman Divacky // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 564f22ef01cSRoman Divacky // with only an unconditional branch. 565f22ef01cSRoman Divacky // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 566f22ef01cSRoman Divacky // with a conditional branch that falls through to a successor (DestB). 567f22ef01cSRoman Divacky // 4. If DestA and DestB is set and isCond is true, then the block ends with a 568f22ef01cSRoman Divacky // conditional branch followed by an unconditional branch. DestA is the 569f22ef01cSRoman Divacky // 'true' destination and DestB is the 'false' destination. 570f22ef01cSRoman Divacky 571f22ef01cSRoman Divacky bool Changed = false; 572f22ef01cSRoman Divacky 573f22ef01cSRoman Divacky MachineFunction::iterator FallThru = 574f22ef01cSRoman Divacky llvm::next(MachineFunction::iterator(this)); 575f22ef01cSRoman Divacky 576f22ef01cSRoman Divacky if (DestA == 0 && DestB == 0) { 577f22ef01cSRoman Divacky // Block falls through to successor. 578f22ef01cSRoman Divacky DestA = FallThru; 579f22ef01cSRoman Divacky DestB = FallThru; 580f22ef01cSRoman Divacky } else if (DestA != 0 && DestB == 0) { 581f22ef01cSRoman Divacky if (isCond) 582f22ef01cSRoman Divacky // Block ends in conditional jump that falls through to successor. 583f22ef01cSRoman Divacky DestB = FallThru; 584f22ef01cSRoman Divacky } else { 585f22ef01cSRoman Divacky assert(DestA && DestB && isCond && 586f22ef01cSRoman Divacky "CFG in a bad state. Cannot correct CFG edges"); 587f22ef01cSRoman Divacky } 588f22ef01cSRoman Divacky 589f22ef01cSRoman Divacky // Remove superfluous edges. I.e., those which aren't destinations of this 590f22ef01cSRoman Divacky // basic block, duplicate edges, or landing pads. 591f22ef01cSRoman Divacky SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 592f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator SI = succ_begin(); 593f22ef01cSRoman Divacky while (SI != succ_end()) { 594f22ef01cSRoman Divacky const MachineBasicBlock *MBB = *SI; 595f22ef01cSRoman Divacky if (!SeenMBBs.insert(MBB) || 596f22ef01cSRoman Divacky (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 597f22ef01cSRoman Divacky // This is a superfluous edge, remove it. 598f22ef01cSRoman Divacky SI = removeSuccessor(SI); 599f22ef01cSRoman Divacky Changed = true; 600f22ef01cSRoman Divacky } else { 601f22ef01cSRoman Divacky ++SI; 602f22ef01cSRoman Divacky } 603f22ef01cSRoman Divacky } 604f22ef01cSRoman Divacky 605f22ef01cSRoman Divacky return Changed; 606f22ef01cSRoman Divacky } 607f22ef01cSRoman Divacky 608f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 609f22ef01cSRoman Divacky /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 610f22ef01cSRoman Divacky DebugLoc 611f22ef01cSRoman Divacky MachineBasicBlock::findDebugLoc(MachineBasicBlock::iterator &MBBI) { 612f22ef01cSRoman Divacky DebugLoc DL; 613f22ef01cSRoman Divacky MachineBasicBlock::iterator E = end(); 614f22ef01cSRoman Divacky if (MBBI != E) { 615f22ef01cSRoman Divacky // Skip debug declarations, we don't want a DebugLoc from them. 616f22ef01cSRoman Divacky MachineBasicBlock::iterator MBBI2 = MBBI; 617f22ef01cSRoman Divacky while (MBBI2 != E && MBBI2->isDebugValue()) 618f22ef01cSRoman Divacky MBBI2++; 619f22ef01cSRoman Divacky if (MBBI2 != E) 620f22ef01cSRoman Divacky DL = MBBI2->getDebugLoc(); 621f22ef01cSRoman Divacky } 622f22ef01cSRoman Divacky return DL; 623f22ef01cSRoman Divacky } 624f22ef01cSRoman Divacky 625f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 626f22ef01cSRoman Divacky bool t) { 627f22ef01cSRoman Divacky OS << "BB#" << MBB->getNumber(); 628f22ef01cSRoman Divacky } 629f22ef01cSRoman Divacky 630