MachineBasicBlock.cpp revision 9d5d7598db72c00a0fb89dc77198e4f6ebc5294d
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/TargetInstrInfo.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Support/LeakDetector.h"
21#include <iostream>
22#include <algorithm>
23using namespace llvm;
24
25MachineBasicBlock::~MachineBasicBlock() {
26  LeakDetector::removeGarbageObject(this);
27}
28
29
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.
33void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
34  assert(N->Parent == 0 && "machine instruction already in a basic block");
35  N->Parent = Parent;
36  N->Number = Parent->addToMBBNumbering(N);
37  LeakDetector::removeGarbageObject(N);
38}
39
40void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
41  assert(N->Parent != 0 && "machine instruction not in a basic block");
42  N->Parent->removeFromMBBNumbering(N->Number);
43  N->Number = -1;
44  N->Parent = 0;
45  LeakDetector::addGarbageObject(N);
46}
47
48
49MachineInstr* ilist_traits<MachineInstr>::createSentinal() {
50  MachineInstr* dummy = new MachineInstr(0, 0);
51  LeakDetector::removeGarbageObject(dummy);
52  return dummy;
53}
54
55void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
56  assert(N->parent == 0 && "machine instruction already in a basic block");
57  N->parent = parent;
58  LeakDetector::removeGarbageObject(N);
59}
60
61void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
62  assert(N->parent != 0 && "machine instruction not in a basic block");
63  N->parent = 0;
64  LeakDetector::addGarbageObject(N);
65}
66
67void 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;
74}
75
76MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
77  const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
78  iterator I = end();
79  while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
80  if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
81  return I;
82}
83
84void MachineBasicBlock::dump() const {
85  print(std::cerr);
86}
87
88void MachineBasicBlock::print(std::ostream &OS) const {
89  if(!getParent()) {
90    OS << "Can't print out MachineBasicBlock because parent MachineFunction"
91       << " is null\n";
92    return;
93  }
94
95  const BasicBlock *LBB = getBasicBlock();
96  if (LBB)
97    OS << "\n" << LBB->getName() << " (" << (const void*)this
98       << ", LLVM BB @" << (const void*) LBB << "):\n";
99  for (const_iterator I = begin(); I != end(); ++I) {
100    OS << "\t";
101    I->print(OS, &getParent()->getTarget());
102  }
103}
104
105void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
106  Successors.push_back(succ);
107  succ->addPredecessor(this);
108}
109
110void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
111  succ->removePredecessor(this);
112  succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
113  assert(I != Successors.end() && "Not a current successor!");
114  Successors.erase(I);
115}
116
117void MachineBasicBlock::removeSuccessor(succ_iterator I) {
118  assert(I != Successors.end() && "Not a current successor!");
119  (*I)->removePredecessor(this);
120  Successors.erase(I);
121}
122
123void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
124  Predecessors.push_back(pred);
125}
126
127void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
128  std::vector<MachineBasicBlock *>::iterator I =
129    std::find(Predecessors.begin(), Predecessors.end(), pred);
130  assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
131  Predecessors.erase(I);
132}
133