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