MachineBasicBlock.cpp revision bcd2498f4f1682dbdc41452add5b9bc72cbd6b3f
1//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// Collect the sequence of machine instructions for a basic block.
11//
12//===----------------------------------------------------------------------===//
13
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"
22#include <algorithm>
23using namespace llvm;
24
25MachineBasicBlock::~MachineBasicBlock() {
26  LeakDetector::removeGarbageObject(this);
27}
28
29std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
30  MBB.print(OS);
31  return OS;
32}
33
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.
37void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
38  assert(N->Parent == 0 && "machine instruction already in a basic block");
39  N->Parent = Parent;
40  N->Number = Parent->addToMBBNumbering(N);
41  LeakDetector::removeGarbageObject(N);
42}
43
44void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
45  assert(N->Parent != 0 && "machine instruction not in a basic block");
46  N->Parent->removeFromMBBNumbering(N->Number);
47  N->Number = -1;
48  N->Parent = 0;
49  LeakDetector::addGarbageObject(N);
50}
51
52
53MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
54  MachineInstr* dummy = new MachineInstr();
55  LeakDetector::removeGarbageObject(dummy);
56  return dummy;
57}
58
59void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
60  assert(N->parent == 0 && "machine instruction already in a basic block");
61  N->parent = parent;
62  LeakDetector::removeGarbageObject(N);
63}
64
65void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
66  assert(N->parent != 0 && "machine instruction not in a basic block");
67  N->parent = 0;
68  LeakDetector::addGarbageObject(N);
69}
70
71void 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;
78}
79
80MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
81  const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
82  iterator I = end();
83  while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
84  if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
85  return I;
86}
87
88void MachineBasicBlock::dump() const {
89  print(*cerr.stream());
90}
91
92void MachineBasicBlock::print(std::ostream &OS) const {
93  if(!getParent()) {
94    OS << "Can't print out MachineBasicBlock because parent MachineFunction"
95       << " is null\n";
96    return;
97  }
98
99  const BasicBlock *LBB = getBasicBlock();
100  OS << "\n";
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.
105  if (!pred_empty()) {
106    OS << "    Predecessors according to CFG:";
107    for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
108      OS << " " << *PI;
109    OS << "\n";
110  }
111
112  for (const_iterator I = begin(); I != end(); ++I) {
113    OS << "\t";
114    I->print(OS, &getParent()->getTarget());
115  }
116
117  // Print the successors of this block according to the CFG.
118  if (!succ_empty()) {
119    OS << "    Successors according to CFG:";
120    for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
121      OS << " " << *SI;
122    OS << "\n";
123  }
124}
125
126void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
127  MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
128  getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
129}
130
131void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
132  MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
133  MachineFunction::iterator BBI = NewBefore;
134  getParent()->getBasicBlockList().splice(++BBI, BBList, this);
135}
136
137
138void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
139  Successors.push_back(succ);
140  succ->addPredecessor(this);
141}
142
143void 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!");
147  Successors.erase(I);
148}
149
150void MachineBasicBlock::removeSuccessor(succ_iterator I) {
151  assert(I != Successors.end() && "Not a current successor!");
152  (*I)->removePredecessor(this);
153  Successors.erase(I);
154}
155
156void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
157  Predecessors.push_back(pred);
158}
159
160void 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);
165}
166