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"
26 MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
31 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
32 // gets the next available unique MBB number. If it is removed from a
33 // MachineFunction, it goes back to being #-1.
34 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
35 assert(N->Parent == 0 && "machine instruction already in a basic block");
37 N->Number = Parent->addToMBBNumbering(N);
38 LeakDetector::removeGarbageObject(N);
41 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
42 assert(N->Parent != 0 && "machine instruction not in a basic block");
43 N->Parent->removeFromMBBNumbering(N->Number);
46 LeakDetector::addGarbageObject(N);
50 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
51 MachineInstr* dummy = new MachineInstr(0, 0);
52 LeakDetector::removeGarbageObject(dummy);
56 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
57 assert(N->parent == 0 && "machine instruction already in a basic block");
59 LeakDetector::removeGarbageObject(N);
62 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
63 assert(N->parent != 0 && "machine instruction not in a basic block");
65 LeakDetector::addGarbageObject(N);
68 void ilist_traits<MachineInstr>::transferNodesFromList(
69 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
70 ilist_iterator<MachineInstr> first,
71 ilist_iterator<MachineInstr> last) {
72 if (parent != fromList.parent)
73 for (; first != last; ++first)
74 first->parent = parent;
77 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
78 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
80 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
81 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
85 void MachineBasicBlock::dump() const {
89 void MachineBasicBlock::print(std::ostream &OS) const {
91 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
96 const BasicBlock *LBB = getBasicBlock();
98 if (LBB) OS << LBB->getName();
99 OS << " (" << (const void*)this
100 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
101 // Print the preds of this block according to the CFG.
103 OS << " Predecessors according to CFG:";
104 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
109 for (const_iterator I = begin(); I != end(); ++I) {
111 I->print(OS, &getParent()->getTarget());
114 // Print the successors of this block according to the CFG.
116 OS << " Successors according to CFG:";
117 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
123 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
124 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
125 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
128 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
129 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
130 MachineFunction::iterator BBI = NewBefore;
131 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
135 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
136 Successors.push_back(succ);
137 succ->addPredecessor(this);
140 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
141 succ->removePredecessor(this);
142 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
143 assert(I != Successors.end() && "Not a current successor!");
147 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
148 assert(I != Successors.end() && "Not a current successor!");
149 (*I)->removePredecessor(this);
153 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
154 Predecessors.push_back(pred);
157 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
158 std::vector<MachineBasicBlock *>::iterator I =
159 std::find(Predecessors.begin(), Predecessors.end(), pred);
160 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
161 Predecessors.erase(I);