blob: ebd492c7ac2291bd8c465f8fa549fd11eaea3d0c [file] [log] [blame]
Alkis Evlogimenos14f3fe82004-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 Evlogimenos14f3fe82004-02-16 07:17:43 +000015#include "llvm/BasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineInstr.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000018#include "llvm/Target/TargetData.h"
Alkis Evlogimenosaf2de482004-02-23 18:14:48 +000019#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000022#include <iostream>
Chris Lattner4336b872004-10-26 15:43:42 +000023#include <algorithm>
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000024using namespace llvm;
25
Tanya Lattner91fa3a92004-05-24 07:14:35 +000026MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
28}
Tanya Lattner91fa3a92004-05-24 07:14:35 +000029
30
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000031// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
Brian Gaekecb5d22a2004-05-12 21:35:22 +000032// gets the next available unique MBB number. If it is removed from a
33// MachineFunction, it goes back to being #-1.
Chris Lattner449af592004-07-01 06:02:27 +000034void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
Tanya Lattnera578cb72004-05-24 06:11:51 +000035 assert(N->Parent == 0 && "machine instruction already in a basic block");
Tanya Lattner91fa3a92004-05-24 07:14:35 +000036 N->Parent = Parent;
Chris Lattner449af592004-07-01 06:02:27 +000037 N->Number = Parent->addToMBBNumbering(N);
Tanya Lattnera578cb72004-05-24 06:11:51 +000038 LeakDetector::removeGarbageObject(N);
Brian Gaekecb5d22a2004-05-12 21:35:22 +000039}
40
Chris Lattner449af592004-07-01 06:02:27 +000041void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
Tanya Lattnera578cb72004-05-24 06:11:51 +000042 assert(N->Parent != 0 && "machine instruction not in a basic block");
Chris Lattner449af592004-07-01 06:02:27 +000043 N->Parent->removeFromMBBNumbering(N->Number);
Brian Gaekecb5d22a2004-05-12 21:35:22 +000044 N->Number = -1;
Chris Lattner449af592004-07-01 06:02:27 +000045 N->Parent = 0;
Tanya Lattnera578cb72004-05-24 06:11:51 +000046 LeakDetector::addGarbageObject(N);
Brian Gaekecb5d22a2004-05-12 21:35:22 +000047}
48
Chris Lattnerd23a8822004-02-19 16:13:54 +000049
Chris Lattnerf6c93e32005-01-30 00:09:23 +000050MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
Chris Lattner469647b2006-05-04 18:16:01 +000051 MachineInstr* dummy = new MachineInstr(0, 0);
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000052 LeakDetector::removeGarbageObject(dummy);
53 return dummy;
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000054}
55
Chris Lattner4336b872004-10-26 15:43:42 +000056void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000057 assert(N->parent == 0 && "machine instruction already in a basic block");
58 N->parent = parent;
59 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000060}
61
Chris Lattner4336b872004-10-26 15:43:42 +000062void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000063 assert(N->parent != 0 && "machine instruction not in a basic block");
64 N->parent = 0;
65 LeakDetector::addGarbageObject(N);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000066}
67
68void ilist_traits<MachineInstr>::transferNodesFromList(
Chris Lattner16ae43e2006-10-06 01:12:44 +000069 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000070 ilist_iterator<MachineInstr> first,
Chris Lattner4336b872004-10-26 15:43:42 +000071 ilist_iterator<MachineInstr> last) {
Chris Lattner16ae43e2006-10-06 01:12:44 +000072 if (parent != fromList.parent)
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000073 for (; first != last; ++first)
Chris Lattner16ae43e2006-10-06 01:12:44 +000074 first->parent = parent;
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000075}
76
Chris Lattner4336b872004-10-26 15:43:42 +000077MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Chris Lattner21505422004-06-02 05:57:12 +000078 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
Alkis Evlogimenosaf2de482004-02-23 18:14:48 +000079 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
Chris Lattner4336b872004-10-26 15:43:42 +000085void MachineBasicBlock::dump() const {
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000086 print(std::cerr);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000087}
88
Chris Lattner4336b872004-10-26 15:43:42 +000089void MachineBasicBlock::print(std::ostream &OS) const {
Tanya Lattnera578cb72004-05-24 06:11:51 +000090 if(!getParent()) {
Chris Lattner4336b872004-10-26 15:43:42 +000091 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
92 << " is null\n";
Tanya Lattnera578cb72004-05-24 06:11:51 +000093 return;
94 }
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +000095
96 const BasicBlock *LBB = getBasicBlock();
Chris Lattner4e107aa2006-10-06 21:28:17 +000097 OS << "\n";
98 if (LBB) OS << LBB->getName();
99 OS << " (" << (const void*)this
100 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n";
Chris Lattner9a1e91b2006-09-26 03:41:59 +0000101 // 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
Alkis Evlogimenosfcb3f512004-09-05 18:39:20 +0000109 for (const_iterator I = begin(); I != end(); ++I) {
110 OS << "\t";
111 I->print(OS, &getParent()->getTarget());
112 }
Chris Lattner329c14a2005-04-01 06:48:38 +0000113
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 }
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000121}
Chris Lattner4336b872004-10-26 15:43:42 +0000122
123void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
124 Successors.push_back(succ);
125 succ->addPredecessor(this);
126}
127
128void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
129 succ->removePredecessor(this);
130 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
131 assert(I != Successors.end() && "Not a current successor!");
132 Successors.erase(I);
133}
134
135void MachineBasicBlock::removeSuccessor(succ_iterator I) {
136 assert(I != Successors.end() && "Not a current successor!");
137 (*I)->removePredecessor(this);
138 Successors.erase(I);
139}
140
141void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
142 Predecessors.push_back(pred);
143}
144
145void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Misha Brukman835702a2005-04-21 22:36:52 +0000146 std::vector<MachineBasicBlock *>::iterator I =
Chris Lattner4336b872004-10-26 15:43:42 +0000147 std::find(Predecessors.begin(), Predecessors.end(), pred);
148 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
149 Predecessors.erase(I);
150}