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