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/MRegisterInfo.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/LeakDetector.h"
26 MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
30 std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
35 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
36 // gets the next available unique MBB number. If it is removed from a
37 // MachineFunction, it goes back to being #-1.
38 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
39 assert(N->Parent == 0 && "machine instruction already in a basic block");
41 N->Number = Parent->addToMBBNumbering(N);
42 LeakDetector::removeGarbageObject(N);
45 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
46 assert(N->Parent != 0 && "machine instruction not in a basic block");
47 N->Parent->removeFromMBBNumbering(N->Number);
50 LeakDetector::addGarbageObject(N);
54 MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
55 MachineInstr* dummy = new MachineInstr();
56 LeakDetector::removeGarbageObject(dummy);
60 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
61 assert(N->parent == 0 && "machine instruction already in a basic block");
63 LeakDetector::removeGarbageObject(N);
66 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
67 assert(N->parent != 0 && "machine instruction not in a basic block");
69 LeakDetector::addGarbageObject(N);
72 void ilist_traits<MachineInstr>::transferNodesFromList(
73 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
74 ilist_iterator<MachineInstr> first,
75 ilist_iterator<MachineInstr> last) {
76 if (parent != fromList.parent)
77 for (; first != last; ++first)
78 first->parent = parent;
81 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
82 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
84 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
85 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
89 void MachineBasicBlock::dump() const {
90 print(*cerr.stream());
93 static inline void OutputReg(std::ostream &os, unsigned RegNo,
94 const MRegisterInfo *MRI = 0) {
95 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
97 os << " %" << MRI->get(RegNo).Name;
99 os << " %mreg(" << RegNo << ")";
101 os << " %reg" << RegNo;
104 void MachineBasicBlock::print(std::ostream &OS) const {
105 const MachineFunction *MF = getParent();
107 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
112 const BasicBlock *LBB = getBasicBlock();
114 if (LBB) OS << LBB->getName();
115 OS << " (" << (const void*)this
116 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
118 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo();
119 if (livein_begin() != livein_end()) {
121 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
122 OutputReg(OS, *I, MRI);
125 // Print the preds of this block according to the CFG.
127 OS << " Predecessors according to CFG:";
128 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
133 for (const_iterator I = begin(); I != end(); ++I) {
135 I->print(OS, &getParent()->getTarget());
138 // Print the successors of this block according to the CFG.
140 OS << " Successors according to CFG:";
141 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
147 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
148 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
149 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
152 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
153 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
154 MachineFunction::iterator BBI = NewBefore;
155 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
159 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
160 Successors.push_back(succ);
161 succ->addPredecessor(this);
164 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
165 succ->removePredecessor(this);
166 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
167 assert(I != Successors.end() && "Not a current successor!");
171 void MachineBasicBlock::removeSuccessor(succ_iterator I) {
172 assert(I != Successors.end() && "Not a current successor!");
173 (*I)->removePredecessor(this);
177 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
178 Predecessors.push_back(pred);
181 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
182 std::vector<MachineBasicBlock *>::iterator I =
183 std::find(Predecessors.begin(), Predecessors.end(), pred);
184 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
185 Predecessors.erase(I);