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/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "Support/LeakDetector.h"
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());
29 MachineFunction *MachineBasicBlock::getParent() {
30 // Get the parent by getting the Function parent of the basic block, and
31 // getting the MachineFunction from it.
32 return &MachineFunction::get(getBasicBlock()->getParent());
35 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
36 // gets the next available unique MBB number. If it is removed from a
37 // MachineFunction, it goes back to being #-1.
38 void ilist_traits<MachineBasicBlock>::addNodeToList (MachineBasicBlock* N)
40 N->Number = N->getParent ()->getNextMBBNumber ();
43 void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N)
49 MachineInstr* ilist_traits<MachineInstr>::createNode()
51 MachineInstr* dummy = new MachineInstr(0, 0);
52 LeakDetector::removeGarbageObject(dummy);
56 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
58 assert(N->parent == 0 && "machine instruction already in a basic block");
60 LeakDetector::removeGarbageObject(N);
63 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
65 assert(N->parent != 0 && "machine instruction not in a basic block");
67 LeakDetector::addGarbageObject(N);
70 void ilist_traits<MachineInstr>::transferNodesFromList(
71 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
72 ilist_iterator<MachineInstr> first,
73 ilist_iterator<MachineInstr> last)
75 if (parent != toList.parent)
76 for (; first != last; ++first)
77 first->parent = toList.parent;
80 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
82 const TargetInstrInfo& TII = getParent()->getTarget().getInstrInfo();
84 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
85 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
89 void MachineBasicBlock::dump() const
94 void MachineBasicBlock::print(std::ostream &OS) const
96 const BasicBlock *LBB = getBasicBlock();
97 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
98 for (const_iterator I = begin(); I != end(); ++I) {
100 I->print(OS, getParent()->getTarget());