1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/LeakDetector.h"
25 MachineBasicBlock::~MachineBasicBlock() {
26 LeakDetector::removeGarbageObject(this);
30 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
31 // gets the next available unique MBB number. If it is removed from a
32 // MachineFunction, it goes back to being #-1.
33 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
34 assert(N->Parent == 0 && "machine instruction already in a basic block");
36 N->Number = Parent->addToMBBNumbering(N);
37 LeakDetector::removeGarbageObject(N);
40 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
41 assert(N->Parent != 0 && "machine instruction not in a basic block");
42 N->Parent->removeFromMBBNumbering(N->Number);
45 LeakDetector::addGarbageObject(N);
49 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
50 MachineInstr* dummy = new MachineInstr(0, 0);
51 LeakDetector::removeGarbageObject(dummy);
55 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
56 assert(N->parent == 0 && "machine instruction already in a basic block");
58 LeakDetector::removeGarbageObject(N);
61 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
62 assert(N->parent != 0 && "machine instruction not in a basic block");
64 LeakDetector::addGarbageObject(N);
67 void ilist_traits<MachineInstr>::transferNodesFromList(
68 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
69 ilist_iterator<MachineInstr> first,
70 ilist_iterator<MachineInstr> last) {
71 if (parent != toList.parent)
72 for (; first != last; ++first)
73 first->parent = toList.parent;
76 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
77 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
79 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
80 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
84 void MachineBasicBlock::dump() const {
88 void MachineBasicBlock::print(std::ostream &OS) const {
90 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
95 const BasicBlock *LBB = getBasicBlock();
97 OS << "\n" << LBB->getName() << " (" << (const void*)this
98 << ", LLVM BB @" << (const void*) LBB << "):\n";
99 for (const_iterator I = begin(); I != end(); ++I) {
101 I->print(OS, &getParent()->getTarget());
104 // Print the successors of this block according to the CFG.
106 OS << " Successors according to CFG:";
107 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
113 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
114 Successors.push_back(succ);
115 succ->addPredecessor(this);
118 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
119 succ->removePredecessor(this);
120 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
121 assert(I != Successors.end() && "Not a current successor!");
125 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
126 assert(I != Successors.end() && "Not a current successor!");
127 (*I)->removePredecessor(this);
131 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
132 Predecessors.push_back(pred);
135 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
136 std::vector<MachineBasicBlock *>::iterator I =
137 std::find(Predecessors.begin(), Predecessors.end(), pred);
138 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
139 Predecessors.erase(I);