Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 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" |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineInstr.h" |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetInstrInfo.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 20 | #include "Support/LeakDetector.h" |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | 5e61fa9 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 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 | |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 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()); |
| 33 | } |
| 34 | |
| 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) |
| 39 | { |
| 40 | N->Number = N->getParent ()->getNextMBBNumber (); |
| 41 | } |
| 42 | |
| 43 | void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N) |
| 44 | { |
| 45 | N->Number = -1; |
| 46 | } |
| 47 | |
Chris Lattner | 5e61fa9 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 48 | |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 49 | MachineInstr* ilist_traits<MachineInstr>::createNode() |
| 50 | { |
| 51 | MachineInstr* dummy = new MachineInstr(0, 0); |
| 52 | LeakDetector::removeGarbageObject(dummy); |
| 53 | return dummy; |
| 54 | } |
| 55 | |
| 56 | void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) |
| 57 | { |
| 58 | assert(N->parent == 0 && "machine instruction already in a basic block"); |
| 59 | N->parent = parent; |
| 60 | LeakDetector::removeGarbageObject(N); |
| 61 | } |
| 62 | |
| 63 | void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) |
| 64 | { |
| 65 | assert(N->parent != 0 && "machine instruction not in a basic block"); |
| 66 | N->parent = 0; |
| 67 | LeakDetector::addGarbageObject(N); |
| 68 | } |
| 69 | |
| 70 | void ilist_traits<MachineInstr>::transferNodesFromList( |
| 71 | iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, |
| 72 | ilist_iterator<MachineInstr> first, |
| 73 | ilist_iterator<MachineInstr> last) |
| 74 | { |
| 75 | if (parent != toList.parent) |
| 76 | for (; first != last; ++first) |
| 77 | first->parent = toList.parent; |
| 78 | } |
| 79 | |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 80 | MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() |
| 81 | { |
Alkis Evlogimenos | e699b16 | 2004-02-23 18:36:38 +0000 | [diff] [blame] | 82 | const TargetInstrInfo& TII = getParent()->getTarget().getInstrInfo(); |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 83 | iterator I = end(); |
| 84 | while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); |
| 85 | if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; |
| 86 | return I; |
| 87 | } |
| 88 | |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 89 | void MachineBasicBlock::dump() const |
| 90 | { |
| 91 | print(std::cerr); |
| 92 | } |
| 93 | |
| 94 | void MachineBasicBlock::print(std::ostream &OS) const |
| 95 | { |
| 96 | const BasicBlock *LBB = getBasicBlock(); |
| 97 | OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n"; |
| 98 | for (const_iterator I = begin(); I != end(); ++I) { |
| 99 | OS << "\t"; |
Chris Lattner | 5bf3ce2 | 2004-05-24 03:44:52 +0000 | [diff] [blame^] | 100 | I->print(OS, getParent()->getTarget()); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 101 | } |
| 102 | } |