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/TargetData.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/LeakDetector.h"
25 MachineBasicBlock::~MachineBasicBlock() {
26 LeakDetector::removeGarbageObject(this);
29 std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
34 OStream& llvm::operator<<(OStream &OS, const MachineBasicBlock &MBB) {
35 if (OS.stream()) *OS.stream() << MBB;
39 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
40 // gets the next available unique MBB number. If it is removed from a
41 // MachineFunction, it goes back to being #-1.
42 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
43 assert(N->Parent == 0 && "machine instruction already in a basic block");
45 N->Number = Parent->addToMBBNumbering(N);
46 LeakDetector::removeGarbageObject(N);
49 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
50 assert(N->Parent != 0 && "machine instruction not in a basic block");
51 N->Parent->removeFromMBBNumbering(N->Number);
54 LeakDetector::addGarbageObject(N);
58 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
59 MachineInstr* dummy = new MachineInstr();
60 LeakDetector::removeGarbageObject(dummy);
64 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
65 assert(N->parent == 0 && "machine instruction already in a basic block");
67 LeakDetector::removeGarbageObject(N);
70 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
71 assert(N->parent != 0 && "machine instruction not in a basic block");
73 LeakDetector::addGarbageObject(N);
76 void ilist_traits<MachineInstr>::transferNodesFromList(
77 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
78 ilist_iterator<MachineInstr> first,
79 ilist_iterator<MachineInstr> last) {
80 if (parent != fromList.parent)
81 for (; first != last; ++first)
82 first->parent = parent;
85 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
86 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
88 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
89 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
93 void MachineBasicBlock::dump() const {
94 print(*cerr.stream());
97 void MachineBasicBlock::print(std::ostream &OS) const {
99 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
104 const BasicBlock *LBB = getBasicBlock();
106 if (LBB) OS << LBB->getName();
107 OS << " (" << (const void*)this
108 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
109 // Print the preds of this block according to the CFG.
111 OS << " Predecessors according to CFG:";
112 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
117 for (const_iterator I = begin(); I != end(); ++I) {
119 I->print(OS, &getParent()->getTarget());
122 // Print the successors of this block according to the CFG.
124 OS << " Successors according to CFG:";
125 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
131 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
132 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
133 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
136 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
137 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
138 MachineFunction::iterator BBI = NewBefore;
139 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
143 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
144 Successors.push_back(succ);
145 succ->addPredecessor(this);
148 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
149 succ->removePredecessor(this);
150 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
151 assert(I != Successors.end() && "Not a current successor!");
155 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
156 assert(I != Successors.end() && "Not a current successor!");
157 (*I)->removePredecessor(this);
161 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
162 Predecessors.push_back(pred);
165 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
166 std::vector<MachineBasicBlock *>::iterator I =
167 std::find(Predecessors.begin(), Predecessors.end(), pred);
168 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
169 Predecessors.erase(I);