LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Collect the sequence of machine instructions for a basic block. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/MachineBasicBlock.h" 00015 #include "llvm/BasicBlock.h" 00016 #include "llvm/CodeGen/MachineFunction.h" 00017 #include "llvm/CodeGen/MachineInstr.h" 00018 #include "llvm/Target/TargetInstrInfo.h" 00019 #include "llvm/Target/TargetMachine.h" 00020 #include "llvm/Support/LeakDetector.h" 00021 #include <iostream> 00022 #include <algorithm> 00023 using namespace llvm; 00024 00025 MachineBasicBlock::~MachineBasicBlock() { 00026 LeakDetector::removeGarbageObject(this); 00027 } 00028 00029 00030 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it 00031 // gets the next available unique MBB number. If it is removed from a 00032 // MachineFunction, it goes back to being #-1. 00033 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 00034 assert(N->Parent == 0 && "machine instruction already in a basic block"); 00035 N->Parent = Parent; 00036 N->Number = Parent->addToMBBNumbering(N); 00037 LeakDetector::removeGarbageObject(N); 00038 } 00039 00040 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 00041 assert(N->Parent != 0 && "machine instruction not in a basic block"); 00042 N->Parent->removeFromMBBNumbering(N->Number); 00043 N->Number = -1; 00044 N->Parent = 0; 00045 LeakDetector::addGarbageObject(N); 00046 } 00047 00048 00049 MachineInstr* ilist_traits<MachineInstr>::createSentinel() { 00050 MachineInstr* dummy = new MachineInstr(0, 0); 00051 LeakDetector::removeGarbageObject(dummy); 00052 return dummy; 00053 } 00054 00055 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { 00056 assert(N->parent == 0 && "machine instruction already in a basic block"); 00057 N->parent = parent; 00058 LeakDetector::removeGarbageObject(N); 00059 } 00060 00061 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { 00062 assert(N->parent != 0 && "machine instruction not in a basic block"); 00063 N->parent = 0; 00064 LeakDetector::addGarbageObject(N); 00065 } 00066 00067 void ilist_traits<MachineInstr>::transferNodesFromList( 00068 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 00069 ilist_iterator<MachineInstr> first, 00070 ilist_iterator<MachineInstr> last) { 00071 if (parent != toList.parent) 00072 for (; first != last; ++first) 00073 first->parent = toList.parent; 00074 } 00075 00076 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 00077 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 00078 iterator I = end(); 00079 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 00080 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 00081 return I; 00082 } 00083 00084 void MachineBasicBlock::dump() const { 00085 print(std::cerr); 00086 } 00087 00088 void MachineBasicBlock::print(std::ostream &OS) const { 00089 if(!getParent()) { 00090 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 00091 << " is null\n"; 00092 return; 00093 } 00094 00095 const BasicBlock *LBB = getBasicBlock(); 00096 if (LBB) 00097 OS << "\n" << LBB->getName() << " (" << (const void*)this 00098 << ", LLVM BB @" << (const void*) LBB << "):\n"; 00099 for (const_iterator I = begin(); I != end(); ++I) { 00100 OS << "\t"; 00101 I->print(OS, &getParent()->getTarget()); 00102 } 00103 00104 // Print the successors of this block according to the CFG. 00105 if (!succ_empty()) { 00106 OS << " Successors according to CFG:"; 00107 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 00108 OS << " " << *SI; 00109 OS << "\n"; 00110 } 00111 } 00112 00113 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 00114 Successors.push_back(succ); 00115 succ->addPredecessor(this); 00116 } 00117 00118 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 00119 succ->removePredecessor(this); 00120 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 00121 assert(I != Successors.end() && "Not a current successor!"); 00122 Successors.erase(I); 00123 } 00124 00125 void MachineBasicBlock::removeSuccessor(succ_iterator I) { 00126 assert(I != Successors.end() && "Not a current successor!"); 00127 (*I)->removePredecessor(this); 00128 Successors.erase(I); 00129 } 00130 00131 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 00132 Predecessors.push_back(pred); 00133 } 00134 00135 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 00136 std::vector<MachineBasicBlock *>::iterator I = 00137 std::find(Predecessors.begin(), Predecessors.end(), pred); 00138 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 00139 Predecessors.erase(I); 00140 }