blob: bef2502089e5db5b89a8597d38559c01bafd7084 [file] [log] [blame]
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +00001//===-- 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"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000015#include "llvm/BasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng13d82852007-02-10 02:38:19 +000018#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000019#include "llvm/Target/TargetData.h"
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000020#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/LeakDetector.h"
Chris Lattner52c09d72004-10-26 15:43:42 +000023#include <algorithm>
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000024using namespace llvm;
25
Tanya Lattner17fb34b2004-05-24 07:14:35 +000026MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
28}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000029
Chris Lattner1ccc4682006-11-18 21:47:36 +000030std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
31 MBB.print(OS);
32 return OS;
33}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000034
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000035// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000036// gets the next available unique MBB number. If it is removed from a
37// MachineFunction, it goes back to being #-1.
Chris Lattnerca48eb92004-07-01 06:02:27 +000038void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
Tanya Lattner792699c2004-05-24 06:11:51 +000039 assert(N->Parent == 0 && "machine instruction already in a basic block");
Tanya Lattner17fb34b2004-05-24 07:14:35 +000040 N->Parent = Parent;
Chris Lattnerca48eb92004-07-01 06:02:27 +000041 N->Number = Parent->addToMBBNumbering(N);
Tanya Lattner792699c2004-05-24 06:11:51 +000042 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000043}
44
Chris Lattnerca48eb92004-07-01 06:02:27 +000045void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
Tanya Lattner792699c2004-05-24 06:11:51 +000046 assert(N->Parent != 0 && "machine instruction not in a basic block");
Chris Lattnerca48eb92004-07-01 06:02:27 +000047 N->Parent->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000048 N->Number = -1;
Chris Lattnerca48eb92004-07-01 06:02:27 +000049 N->Parent = 0;
Tanya Lattner792699c2004-05-24 06:11:51 +000050 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000051}
52
Chris Lattner5e61fa92004-02-19 16:13:54 +000053
Chris Lattnerbca81442005-01-30 00:09:23 +000054MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
Evan Chengc0f64ff2006-11-27 23:37:22 +000055 MachineInstr* dummy = new MachineInstr();
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000056 LeakDetector::removeGarbageObject(dummy);
57 return dummy;
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000058}
59
Chris Lattner52c09d72004-10-26 15:43:42 +000060void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000061 assert(N->parent == 0 && "machine instruction already in a basic block");
62 N->parent = parent;
63 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000064}
65
Chris Lattner52c09d72004-10-26 15:43:42 +000066void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000067 assert(N->parent != 0 && "machine instruction not in a basic block");
68 N->parent = 0;
69 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000070}
71
72void ilist_traits<MachineInstr>::transferNodesFromList(
Chris Lattner55046022006-10-06 01:12:44 +000073 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000074 ilist_iterator<MachineInstr> first,
Chris Lattner52c09d72004-10-26 15:43:42 +000075 ilist_iterator<MachineInstr> last) {
Chris Lattner55046022006-10-06 01:12:44 +000076 if (parent != fromList.parent)
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000077 for (; first != last; ++first)
Chris Lattner55046022006-10-06 01:12:44 +000078 first->parent = parent;
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000079}
80
Chris Lattner52c09d72004-10-26 15:43:42 +000081MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Chris Lattner9bcdcd12004-06-02 05:57:12 +000082 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000083 iterator I = end();
84 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
85 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
86 return I;
87}
88
Chris Lattner52c09d72004-10-26 15:43:42 +000089void MachineBasicBlock::dump() const {
Bill Wendlingbcd24982006-12-07 20:28:15 +000090 print(*cerr.stream());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000091}
92
Evan Cheng13d82852007-02-10 02:38:19 +000093static inline void OutputReg(std::ostream &os, unsigned RegNo,
94 const MRegisterInfo *MRI = 0) {
95 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
96 if (MRI)
97 os << " %" << MRI->get(RegNo).Name;
98 else
99 os << " %mreg(" << RegNo << ")";
100 } else
101 os << " %reg" << RegNo;
102}
103
Chris Lattner52c09d72004-10-26 15:43:42 +0000104void MachineBasicBlock::print(std::ostream &OS) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000105 const MachineFunction *MF = getParent();
106 if(!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000107 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
108 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000109 return;
110 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000111
112 const BasicBlock *LBB = getBasicBlock();
Chris Lattnerdb3ea672006-10-06 21:28:17 +0000113 OS << "\n";
114 if (LBB) OS << LBB->getName();
115 OS << " (" << (const void*)this
116 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
Evan Cheng13d82852007-02-10 02:38:19 +0000117
118 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo();
119 if (livein_begin() != livein_end()) {
120 OS << "Live Ins:";
Evan Chengb371f452007-02-19 21:49:54 +0000121 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Evan Cheng13d82852007-02-10 02:38:19 +0000122 OutputReg(OS, *I, MRI);
123 OS << "\n";
124 }
Chris Lattner681764b2006-09-26 03:41:59 +0000125 // Print the preds of this block according to the CFG.
126 if (!pred_empty()) {
127 OS << " Predecessors according to CFG:";
128 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
129 OS << " " << *PI;
130 OS << "\n";
131 }
132
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000133 for (const_iterator I = begin(); I != end(); ++I) {
134 OS << "\t";
135 I->print(OS, &getParent()->getTarget());
136 }
Chris Lattner380ae492005-04-01 06:48:38 +0000137
138 // Print the successors of this block according to the CFG.
139 if (!succ_empty()) {
140 OS << " Successors according to CFG:";
141 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
142 OS << " " << *SI;
143 OS << "\n";
144 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000145}
Chris Lattner52c09d72004-10-26 15:43:42 +0000146
Evan Chengb371f452007-02-19 21:49:54 +0000147void MachineBasicBlock::removeLiveIn(unsigned Reg) {
148 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
149 assert(I != livein_end() && "Not a live in!");
150 LiveIns.erase(I);
151}
152
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000153void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
154 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
155 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
156}
157
158void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
159 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
160 MachineFunction::iterator BBI = NewBefore;
161 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
162}
163
164
Chris Lattner52c09d72004-10-26 15:43:42 +0000165void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
166 Successors.push_back(succ);
167 succ->addPredecessor(this);
168}
169
170void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
171 succ->removePredecessor(this);
172 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
173 assert(I != Successors.end() && "Not a current successor!");
174 Successors.erase(I);
175}
176
177void MachineBasicBlock::removeSuccessor(succ_iterator I) {
178 assert(I != Successors.end() && "Not a current successor!");
179 (*I)->removePredecessor(this);
180 Successors.erase(I);
181}
182
183void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
184 Predecessors.push_back(pred);
185}
186
187void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000188 std::vector<MachineBasicBlock *>::iterator I =
Chris Lattner52c09d72004-10-26 15:43:42 +0000189 std::find(Predecessors.begin(), Predecessors.end(), pred);
190 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
191 Predecessors.erase(I);
192}