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 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
35 // gets the next available unique MBB number. If it is removed from a
36 // MachineFunction, it goes back to being #-1.
37 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
38 assert(N->Parent == 0 && "machine instruction already in a basic block");
40 N->Number = Parent->addToMBBNumbering(N);
41 LeakDetector::removeGarbageObject(N);
44 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
45 assert(N->Parent != 0 && "machine instruction not in a basic block");
46 N->Parent->removeFromMBBNumbering(N->Number);
49 LeakDetector::addGarbageObject(N);
53 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
54 MachineInstr* dummy = new MachineInstr();
55 LeakDetector::removeGarbageObject(dummy);
59 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
60 assert(N->parent == 0 && "machine instruction already in a basic block");
62 LeakDetector::removeGarbageObject(N);
65 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
66 assert(N->parent != 0 && "machine instruction not in a basic block");
68 LeakDetector::addGarbageObject(N);
71 void ilist_traits<MachineInstr>::transferNodesFromList(
72 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
73 ilist_iterator<MachineInstr> first,
74 ilist_iterator<MachineInstr> last) {
75 if (parent != fromList.parent)
76 for (; first != last; ++first)
77 first->parent = parent;
80 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
81 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
83 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
84 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
88 void MachineBasicBlock::dump() const {
89 print(*cerr.stream());
92 void MachineBasicBlock::print(std::ostream &OS) const {
94 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
99 const BasicBlock *LBB = getBasicBlock();
101 if (LBB) OS << LBB->getName();
102 OS << " (" << (const void*)this
103 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
104 // Print the preds of this block according to the CFG.
106 OS << " Predecessors according to CFG:";
107 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
112 for (const_iterator I = begin(); I != end(); ++I) {
114 I->print(OS, &getParent()->getTarget());
117 // Print the successors of this block according to the CFG.
119 OS << " Successors according to CFG:";
120 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
126 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
127 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
128 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
131 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
132 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
133 MachineFunction::iterator BBI = NewBefore;
134 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
138 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
139 Successors.push_back(succ);
140 succ->addPredecessor(this);
143 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
144 succ->removePredecessor(this);
145 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
146 assert(I != Successors.end() && "Not a current successor!");
150 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
151 assert(I != Successors.end() && "Not a current successor!");
152 (*I)->removePredecessor(this);
156 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
157 Predecessors.push_back(pred);
160 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
161 std::vector<MachineBasicBlock *>::iterator I =
162 std::find(Predecessors.begin(), Predecessors.end(), pred);
163 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
164 Predecessors.erase(I);