MachineBasicBlock.h revision 903b22cd51d82ba1c1c5ee5d37b7eb7911c755ee
1//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- 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#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 15#define LLVM_CODEGEN_MACHINEBASICBLOCK_H 16 17#include "llvm/CodeGen/MachineInstr.h" 18#include "Support/ilist" 19#include <iosfwd> 20 21namespace llvm { 22 23// ilist_traits 24template <> 25class ilist_traits<MachineInstr> 26{ 27 typedef ilist_traits<MachineInstr> self; 28 29 // this is only set by the MachineBasicBlock owning the ilist 30 friend class MachineBasicBlock; 31 MachineBasicBlock* parent; 32 33public: 34 ilist_traits<MachineInstr>() : parent(0) { } 35 36 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; } 37 static MachineInstr* getNext(MachineInstr* N) { return N->next; } 38 39 static const MachineInstr* 40 getPrev(const MachineInstr* N) { return N->prev; } 41 42 static const MachineInstr* 43 getNext(const MachineInstr* N) { return N->next; } 44 45 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; } 46 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; } 47 48 static MachineInstr* createNode() { return new MachineInstr(0, 0); } 49 50 void addNodeToList(MachineInstr* N) { 51 assert(N->parent == 0 && "machine instruction already in a basic block"); 52 N->parent = parent; 53 } 54 55 void removeNodeFromList(MachineInstr* N) { 56 assert(N->parent != 0 && "machine instruction not in a basic block"); 57 N->parent = 0; 58 } 59 60 void transferNodesFromList(iplist<MachineInstr, self>& toList, 61 ilist_iterator<MachineInstr> first, 62 ilist_iterator<MachineInstr> last) { 63 if (parent != toList.parent) 64 for (; first != last; ++first) 65 first->parent = toList.parent; 66 } 67}; 68 69class BasicBlock; 70 71class MachineBasicBlock { 72public: 73 typedef ilist<MachineInstr> Instructions; 74 Instructions Insts; 75 MachineBasicBlock *Prev, *Next; 76 const BasicBlock *BB; 77public: 78 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) { 79 Insts.parent = this; 80 } 81 ~MachineBasicBlock() {} 82 83 /// getBasicBlock - Return the LLVM basic block that this instance 84 /// corresponded to originally. 85 /// 86 const BasicBlock *getBasicBlock() const { return BB; } 87 88 typedef ilist<MachineInstr>::iterator iterator; 89 typedef ilist<MachineInstr>::const_iterator const_iterator; 90 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 91 typedef std::reverse_iterator<iterator> reverse_iterator; 92 93 unsigned size() const { return Insts.size(); } 94 bool empty() const { return Insts.empty(); } 95 96 // This is a really inefficient way of accessing a basic 97 // block. These methods will be removed when all of their uses are 98 // eliminated. 99 inline const MachineInstr& operator[](unsigned i) const DEPRECATED; 100 inline MachineInstr& operator[](unsigned i) DEPRECATED; 101 102 MachineInstr& front() { return Insts.front(); } 103 MachineInstr& back() { return Insts.back(); } 104 105 iterator begin() { return Insts.begin(); } 106 const_iterator begin() const { return Insts.begin(); } 107 iterator end() { return Insts.end(); } 108 const_iterator end() const { return Insts.end(); } 109 reverse_iterator rbegin() { return Insts.rbegin(); } 110 const_reverse_iterator rbegin() const { return Insts.rbegin(); } 111 reverse_iterator rend () { return Insts.rend(); } 112 const_reverse_iterator rend () const { return Insts.rend(); } 113 114 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 115 template<typename IT> 116 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 117 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 118 119 // erase - Remove the specified element or range from the instruction list. 120 // These functions delete any instructions removed. 121 // 122 iterator erase(iterator I) { return Insts.erase(I); } 123 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 124 MachineInstr* remove(iterator &I) { return Insts.remove(I); } 125 126 // Debugging methods. 127 void dump() const; 128 void print(std::ostream &OS) const; 129 130private: // Methods used to maintain doubly linked list of blocks... 131 friend class ilist_traits<MachineBasicBlock>; 132 133 MachineBasicBlock *getPrev() const { return Prev; } 134 MachineBasicBlock *getNext() const { return Next; } 135 void setPrev(MachineBasicBlock *P) { Prev = P; } 136 void setNext(MachineBasicBlock *N) { Next = N; } 137}; 138 139const MachineInstr& MachineBasicBlock::operator[](unsigned i) const 140{ 141 const_iterator it = Insts.begin(); 142 std::advance(it, i); 143 return *it; 144} 145 146MachineInstr& MachineBasicBlock::operator[](unsigned i) 147{ 148 iterator it = Insts.begin(); 149 std::advance(it, i); 150 return *it; 151} 152 153 154} // End llvm namespace 155 156#endif 157