Urg, forgot to check this in.
[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/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "Support/LeakDetector.h"
21 using namespace llvm;
22
23 const MachineFunction *MachineBasicBlock::getParent() const {
24   // Get the parent by getting the Function parent of the basic block, and
25   // getting the MachineFunction from it.
26   return &MachineFunction::get(getBasicBlock()->getParent());
27 }
28
29
30 MachineInstr* ilist_traits<MachineInstr>::createNode()
31 {
32     MachineInstr* dummy = new MachineInstr(0, 0);
33     LeakDetector::removeGarbageObject(dummy);
34     return dummy;
35 }
36
37 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
38 {
39     assert(N->parent == 0 && "machine instruction already in a basic block");
40     N->parent = parent;
41     LeakDetector::removeGarbageObject(N);
42 }
43
44 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
45 {
46     assert(N->parent != 0 && "machine instruction not in a basic block");
47     N->parent = 0;
48     LeakDetector::addGarbageObject(N);
49 }
50
51 void ilist_traits<MachineInstr>::transferNodesFromList(
52     iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
53     ilist_iterator<MachineInstr> first,
54     ilist_iterator<MachineInstr> last)
55 {
56     if (parent != toList.parent)
57         for (; first != last; ++first)
58             first->parent = toList.parent;
59 }
60
61 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
62 {
63   const TargetInstrInfo& TII = getParent()->getTarget().getInstrInfo();
64   iterator I = end();
65   while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
66   if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
67   return I;
68 }
69
70 void MachineBasicBlock::dump() const
71 {
72     print(std::cerr);
73 }
74
75 void MachineBasicBlock::print(std::ostream &OS) const
76 {
77     const BasicBlock *LBB = getBasicBlock();
78     OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
79     for (const_iterator I = begin(); I != end(); ++I) {
80         OS << "\t";
81         I->print(OS, MachineFunction::get(LBB->getParent()).getTarget());
82     }
83 }