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/TargetData.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/LeakDetector.h"
26 MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
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");
37 N
->Number
= Parent
->addToMBBNumbering(N
);
38 LeakDetector::removeGarbageObject(N
);
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
);
46 LeakDetector::addGarbageObject(N
);
50 MachineInstr
* ilist_traits
<MachineInstr
>::createSentinel() {
51 MachineInstr
* dummy
= new MachineInstr(0, 0);
52 LeakDetector::removeGarbageObject(dummy
);
56 void ilist_traits
<MachineInstr
>::addNodeToList(MachineInstr
* N
) {
57 assert(N
->parent
== 0 && "machine instruction already in a basic block");
59 LeakDetector::removeGarbageObject(N
);
62 void ilist_traits
<MachineInstr
>::removeNodeFromList(MachineInstr
* N
) {
63 assert(N
->parent
!= 0 && "machine instruction not in a basic block");
65 LeakDetector::addGarbageObject(N
);
68 void 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
;
77 MachineBasicBlock::iterator
MachineBasicBlock::getFirstTerminator() {
78 const TargetInstrInfo
& TII
= *getParent()->getTarget().getInstrInfo();
80 while (I
!= begin() && TII
.isTerminatorInstr((--I
)->getOpcode()));
81 if (I
!= end() && !TII
.isTerminatorInstr(I
->getOpcode())) ++I
;
85 void MachineBasicBlock::dump() const {
89 void MachineBasicBlock::print(std::ostream
&OS
) const {
91 OS
<< "Can't print out MachineBasicBlock because parent MachineFunction"
96 const BasicBlock
*LBB
= getBasicBlock();
98 OS
<< "\n" << LBB
->getName() << " (" << (const void*)this
99 << ", LLVM BB @" << (const void*) LBB
<< "):\n";
100 for (const_iterator I
= begin(); I
!= end(); ++I
) {
102 I
->print(OS
, &getParent()->getTarget());
105 // Print the successors of this block according to the CFG.
107 OS
<< " Successors according to CFG:";
108 for (const_succ_iterator SI
= succ_begin(), E
= succ_end(); SI
!= E
; ++SI
)
114 void MachineBasicBlock::addSuccessor(MachineBasicBlock
*succ
) {
115 Successors
.push_back(succ
);
116 succ
->addPredecessor(this);
119 void 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!");
126 void MachineBasicBlock::removeSuccessor(succ_iterator I
) {
127 assert(I
!= Successors
.end() && "Not a current successor!");
128 (*I
)->removePredecessor(this);
132 void MachineBasicBlock::addPredecessor(MachineBasicBlock
*pred
) {
133 Predecessors
.push_back(pred
);
136 void 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
);