LinearScanner hotspot.
[oota-llvm.git] / lib / CodeGen / MachineBasicBlock.cpp
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>
24 using namespace llvm;
25
26 MachineBasicBlock::~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.
34 void 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
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);
44   N->Number = -1;
45   N->Parent = 0;
46   LeakDetector::addGarbageObject(N);
47 }
48
49
50 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
51   MachineInstr* dummy = new MachineInstr(0, 0);
52   LeakDetector::removeGarbageObject(dummy);
53   return dummy;
54 }
55
56 void 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
62 void 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
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;
75 }
76
77 MachineBasicBlock::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
85 void MachineBasicBlock::dump() const {
86   print(std::cerr);
87 }
88
89 void 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   OS << "\n";
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.
102   if (!pred_empty()) {
103     OS << "    Predecessors according to CFG:";
104     for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
105       OS << " " << *PI;
106     OS << "\n";
107   }
108   
109   for (const_iterator I = begin(); I != end(); ++I) {
110     OS << "\t";
111     I->print(OS, &getParent()->getTarget());
112   }
113
114   // Print the successors of this block according to the CFG.
115   if (!succ_empty()) {
116     OS << "    Successors according to CFG:";
117     for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
118       OS << " " << *SI;
119     OS << "\n";
120   }
121 }
122
123 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
124   MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
125   getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
126 }
127
128 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
129   MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
130   MachineFunction::iterator BBI = NewBefore;
131   getParent()->getBasicBlockList().splice(++BBI, BBList, this);
132 }
133
134
135 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
136   Successors.push_back(succ);
137   succ->addPredecessor(this);
138 }
139
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!");
144   Successors.erase(I);
145 }
146
147 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
148   assert(I != Successors.end() && "Not a current successor!");
149   (*I)->removePredecessor(this);
150   Successors.erase(I);
151 }
152
153 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
154   Predecessors.push_back(pred);
155 }
156
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);
162 }