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"
25 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 typedef ilist<MachineInstr>::iterator iterator;
75 typedef ilist<MachineInstr>::const_iterator const_iterator;
76 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77 typedef std::reverse_iterator<iterator> reverse_iterator;
79 unsigned size() const { return Insts.size(); }
80 bool empty() const { return Insts.empty(); }
82 MachineInstr& front() { return Insts.front(); }
83 MachineInstr& back() { return Insts.back(); }
85 iterator begin() { return Insts.begin(); }
86 const_iterator begin() const { return Insts.begin(); }
87 iterator end() { return Insts.end(); }
88 const_iterator end() const { return Insts.end(); }
89 reverse_iterator rbegin() { return Insts.rbegin(); }
90 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
91 reverse_iterator rend () { return Insts.rend(); }
92 const_reverse_iterator rend () const { return Insts.rend(); }
94 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
96 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
97 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
99 // erase - Remove the specified element or range from the instruction list.
100 // These functions delete any instructions removed.
102 iterator erase(iterator I) { return Insts.erase(I); }
103 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
104 MachineInstr* remove(iterator &I) { return Insts.remove(I); }
106 // Debugging methods.
108 void print(std::ostream &OS) const;
110 private: // Methods used to maintain doubly linked list of blocks...
111 friend class ilist_traits<MachineBasicBlock>;
113 MachineBasicBlock *getPrev() const { return Prev; }
114 MachineBasicBlock *getNext() const { return Next; }
115 void setPrev(MachineBasicBlock *P) { Prev = P; }
116 void setNext(MachineBasicBlock *N) { Next = N; }
119 } // End llvm namespace