1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- 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 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "Support/ilist"
22 class MachineFunction;
26 class ilist_traits<MachineInstr> {
27 // this is only set by the MachineBasicBlock owning the ilist
28 friend class MachineBasicBlock;
29 MachineBasicBlock* parent;
32 ilist_traits<MachineInstr>() : parent(0) { }
34 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
35 static MachineInstr* getNext(MachineInstr* N) { return N->next; }
37 static const MachineInstr*
38 getPrev(const MachineInstr* N) { return N->prev; }
40 static const MachineInstr*
41 getNext(const MachineInstr* N) { return N->next; }
43 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
44 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
46 static MachineInstr* createNode();
47 void addNodeToList(MachineInstr* N);
48 void removeNodeFromList(MachineInstr* N);
49 void transferNodesFromList(
50 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
51 ilist_iterator<MachineInstr> first,
52 ilist_iterator<MachineInstr> last);
57 class MachineBasicBlock {
59 typedef ilist<MachineInstr> Instructions;
61 MachineBasicBlock *Prev, *Next;
64 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) {
67 ~MachineBasicBlock() {}
69 /// getBasicBlock - Return the LLVM basic block that this instance
70 /// corresponded to originally.
72 const BasicBlock *getBasicBlock() const { return BB; }
74 /// getParent - Return the MachineFunction containing this basic block.
76 const MachineFunction *getParent() const;
78 typedef ilist<MachineInstr>::iterator iterator;
79 typedef ilist<MachineInstr>::const_iterator const_iterator;
80 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
81 typedef std::reverse_iterator<iterator> reverse_iterator;
83 unsigned size() const { return Insts.size(); }
84 bool empty() const { return Insts.empty(); }
86 MachineInstr& front() { return Insts.front(); }
87 MachineInstr& back() { return Insts.back(); }
89 iterator begin() { return Insts.begin(); }
90 const_iterator begin() const { return Insts.begin(); }
91 iterator end() { return Insts.end(); }
92 const_iterator end() const { return Insts.end(); }
93 reverse_iterator rbegin() { return Insts.rbegin(); }
94 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
95 reverse_iterator rend () { return Insts.rend(); }
96 const_reverse_iterator rend () const { return Insts.rend(); }
98 /// getFirstTerminator - returns an iterator to the first terminator
99 /// instruction of this basic block. If a terminator does not exist,
101 iterator getFirstTerminator();
103 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
104 template<typename IT>
105 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
106 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
108 // erase - Remove the specified element or range from the instruction list.
109 // These functions delete any instructions removed.
111 iterator erase(iterator I) { return Insts.erase(I); }
112 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
113 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
115 // Debugging methods.
117 void print(std::ostream &OS) const;
119 private: // Methods used to maintain doubly linked list of blocks...
120 friend class ilist_traits<MachineBasicBlock>;
122 MachineBasicBlock *getPrev() const { return Prev; }
123 MachineBasicBlock *getNext() const { return Next; }
124 void setPrev(MachineBasicBlock *P) { Prev = P; }
125 void setNext(MachineBasicBlock *N) { Next = N; }
128 } // End llvm namespace