MachineBasicBlock.cpp revision ca48eb9f5175058a55c1818cb1d5d06052f0092d
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"
21using namespace llvm;
22
23MachineBasicBlock::~MachineBasicBlock() {
24  LeakDetector::removeGarbageObject(this);
25}
26
27
28
29// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
30// gets the next available unique MBB number. If it is removed from a
31// MachineFunction, it goes back to being #-1.
32void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
33  assert(N->Parent == 0 && "machine instruction already in a basic block");
34  N->Parent = Parent;
35  N->Number = Parent->addToMBBNumbering(N);
36  LeakDetector::removeGarbageObject(N);
37}
38
39void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
40  assert(N->Parent != 0 && "machine instruction not in a basic block");
41  N->Parent->removeFromMBBNumbering(N->Number);
42  N->Number = -1;
43  N->Parent = 0;
44  LeakDetector::addGarbageObject(N);
45}
46
47
48MachineInstr* ilist_traits<MachineInstr>::createNode() {
49    MachineInstr* dummy = new MachineInstr(0, 0);
50    LeakDetector::removeGarbageObject(dummy);
51    return dummy;
52}
53
54void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
55{
56    assert(N->parent == 0 && "machine instruction already in a basic block");
57    N->parent = parent;
58    LeakDetector::removeGarbageObject(N);
59}
60
61void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
62{
63    assert(N->parent != 0 && "machine instruction not in a basic block");
64    N->parent = 0;
65    LeakDetector::addGarbageObject(N);
66}
67
68void ilist_traits<MachineInstr>::transferNodesFromList(
69    iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
70    ilist_iterator<MachineInstr> first,
71    ilist_iterator<MachineInstr> last)
72{
73    if (parent != toList.parent)
74        for (; first != last; ++first)
75            first->parent = toList.parent;
76}
77
78MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
79{
80  const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
81  iterator I = end();
82  while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
83  if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
84  return I;
85}
86
87void MachineBasicBlock::dump() const
88{
89    print(std::cerr);
90}
91
92void MachineBasicBlock::print(std::ostream &OS) const
93{
94  if(!getParent()) {
95    OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n";
96    return;
97  }
98    const BasicBlock *LBB = getBasicBlock();
99    if(LBB)
100      OS << "\n" << LBB->getName() << " (" << (const void*)this
101         << ", LLVM BB @" << (const void*) LBB << "):\n";
102    for (const_iterator I = begin(); I != end(); ++I) {
103        OS << "\t";
104        I->print(OS, &getParent()->getTarget());
105    }
106}
107