MachineBasicBlock.h revision 76456bc40c79fcae4da52d34f96c079d9759257c
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  class MachineFunction;
23
24// ilist_traits
25template <>
26class ilist_traits<MachineInstr> {
27  // this is only set by the MachineBasicBlock owning the ilist
28  friend class MachineBasicBlock;
29  MachineBasicBlock* parent;
30
31public:
32  ilist_traits<MachineInstr>() : parent(0) { }
33
34  static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
35  static MachineInstr* getNext(MachineInstr* N) { return N->next; }
36
37  static const MachineInstr*
38  getPrev(const MachineInstr* N) { return N->prev; }
39
40  static const MachineInstr*
41  getNext(const MachineInstr* N) { return N->next; }
42
43  static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
44  static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
45
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);
53};
54
55class BasicBlock;
56
57class MachineBasicBlock {
58public:
59  typedef ilist<MachineInstr> Instructions;
60  Instructions Insts;
61  MachineBasicBlock *Prev, *Next;
62  const BasicBlock *BB;
63  std::vector<MachineBasicBlock *> Predecessors;
64  std::vector<MachineBasicBlock *> Successors;
65
66public:
67  MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) {
68    Insts.parent = this;
69  }
70  ~MachineBasicBlock() {}
71
72  /// getBasicBlock - Return the LLVM basic block that this instance
73  /// corresponded to originally.
74  ///
75  const BasicBlock *getBasicBlock() const { return BB; }
76
77  /// getParent - Return the MachineFunction containing this basic block.
78  ///
79  const MachineFunction *getParent() const;
80
81  typedef ilist<MachineInstr>::iterator                       iterator;
82  typedef ilist<MachineInstr>::const_iterator           const_iterator;
83  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
84  typedef std::reverse_iterator<iterator>             reverse_iterator;
85
86  unsigned size() const { return Insts.size(); }
87  bool empty() const { return Insts.empty(); }
88
89  MachineInstr& front() { return Insts.front(); }
90  MachineInstr& back()  { return Insts.back(); }
91
92  iterator                begin()       { return Insts.begin();  }
93  const_iterator          begin() const { return Insts.begin();  }
94  iterator                  end()       { return Insts.end();    }
95  const_iterator            end() const { return Insts.end();    }
96  reverse_iterator       rbegin()       { return Insts.rbegin(); }
97  const_reverse_iterator rbegin() const { return Insts.rbegin(); }
98  reverse_iterator       rend  ()       { return Insts.rend();   }
99  const_reverse_iterator rend  () const { return Insts.rend();   }
100
101  // Machine-CFG iterators
102  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
103  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
104  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
105  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
106
107  pred_iterator        pred_begin()       { return Predecessors.begin (); }
108  const_pred_iterator  pred_begin() const { return Predecessors.begin (); }
109  pred_iterator        pred_end()         { return Predecessors.end ();   }
110  const_pred_iterator  pred_end()   const { return Predecessors.end ();   }
111  succ_iterator        succ_begin()       { return Successors.begin ();   }
112  const_succ_iterator  succ_begin() const { return Successors.begin ();   }
113  succ_iterator        succ_end()         { return Successors.end ();     }
114  const_succ_iterator  succ_end()   const { return Successors.end ();     }
115
116  // Machine-CFG mutators
117
118  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
119  /// The Predecessors list of succ is automatically updated.
120  ///
121  void addSuccessor (MachineBasicBlock *succ) {
122    Successors.push_back (succ);
123    assert (std::find (Successors.begin (), Successors.end (), succ)
124            == Successors.end ()
125            && "Trying to addSuccessor a MBB which is already my successor");
126    succ->addPredecessor (this);
127  }
128
129  /// removeSuccessor - Remove succ from the successors list of this
130  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
131  ///
132  void removeSuccessor (MachineBasicBlock *succ) {
133    succ->removePredecessor (this);
134    std::vector<MachineBasicBlock *>::iterator goner =
135      std::find (Successors.begin(), Successors.end (), succ);
136    assert (goner != Successors.end ()
137            && "Trying to removeSuccessor a MBB which isn't my successor");
138    Successors.erase (goner);
139  }
140
141  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
142  /// Don't do this unless you know what you're doing, because it doesn't
143  /// update pred's successors list. Use pred->addSuccessor instead.
144  ///
145  void addPredecessor (MachineBasicBlock *pred) {
146    Predecessors.push_back (pred);
147    assert(std::find (Predecessors.begin (), Predecessors.end (), pred)
148           == Predecessors.end ()
149           && "Trying to addPredecessor a MBB which is already my predecessor");
150  }
151
152  /// removePredecessor - Remove pred as a predecessor of this
153  /// MachineBasicBlock. Don't do this unless you know what you're
154  /// doing, because it doesn't update pred's successors list. Use
155  /// pred->removeSuccessor instead.
156  ///
157  void removePredecessor (MachineBasicBlock *pred) {
158    std::vector<MachineBasicBlock *>::iterator goner =
159      std::find (Predecessors.begin(), Predecessors.end (), pred);
160    assert (goner != Predecessors.end ()
161            && "Trying to removePredecessor a MBB which isn't my predecessor");
162    Predecessors.erase (goner);
163  }
164
165  /// getFirstTerminator - returns an iterator to the first terminator
166  /// instruction of this basic block. If a terminator does not exist,
167  /// it returns end()
168  iterator getFirstTerminator();
169
170  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
171  template<typename IT>
172  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
173  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
174
175  // erase - Remove the specified element or range from the instruction list.
176  // These functions delete any instructions removed.
177  //
178  iterator erase(iterator I)             { return Insts.erase(I); }
179  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
180  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
181  void clear()                           { Insts.clear(); }
182
183  // Debugging methods.
184  void dump() const;
185  void print(std::ostream &OS) const;
186
187private:   // Methods used to maintain doubly linked list of blocks...
188  friend class ilist_traits<MachineBasicBlock>;
189
190  MachineBasicBlock *getPrev() const { return Prev; }
191  MachineBasicBlock *getNext() const { return Next; }
192  void setPrev(MachineBasicBlock *P) { Prev = P; }
193  void setNext(MachineBasicBlock *N) { Next = N; }
194};
195
196} // End llvm namespace
197
198#endif
199