MachineBasicBlock.h revision 8560af4f5fe589cf792bd44617c2308c4f087ba8
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    assert (std::find (Successors.begin (), Successors.end (), succ)
123            == Successors.end ()
124            && "Trying to addSuccessor a MBB which is already my successor");
125    Successors.push_back (succ);
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  /// getFirstTerminator - returns an iterator to the first terminator
142  /// instruction of this basic block. If a terminator does not exist,
143  /// it returns end()
144  iterator getFirstTerminator();
145
146  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
147  template<typename IT>
148  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
149  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
150
151  // erase - Remove the specified element or range from the instruction list.
152  // These functions delete any instructions removed.
153  //
154  iterator erase(iterator I)             { return Insts.erase(I); }
155  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
156  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
157  void clear()                           { Insts.clear(); }
158
159  // Debugging methods.
160  void dump() const;
161  void print(std::ostream &OS) const;
162
163private:   // Methods used to maintain doubly linked list of blocks...
164  friend class ilist_traits<MachineBasicBlock>;
165
166  MachineBasicBlock *getPrev() const { return Prev; }
167  MachineBasicBlock *getNext() const { return Next; }
168  void setPrev(MachineBasicBlock *P) { Prev = P; }
169  void setNext(MachineBasicBlock *N) { Next = N; }
170
171  // Machine-CFG mutators
172
173  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
174  /// Don't do this unless you know what you're doing, because it doesn't
175  /// update pred's successors list. Use pred->addSuccessor instead.
176  ///
177  void addPredecessor (MachineBasicBlock *pred) {
178    assert(std::find (Predecessors.begin (), Predecessors.end (), pred)
179           == Predecessors.end ()
180           && "Trying to addPredecessor a MBB which is already my predecessor");
181    Predecessors.push_back (pred);
182  }
183
184  /// removePredecessor - Remove pred as a predecessor of this
185  /// MachineBasicBlock. Don't do this unless you know what you're
186  /// doing, because it doesn't update pred's successors list. Use
187  /// pred->removeSuccessor instead.
188  ///
189  void removePredecessor (MachineBasicBlock *pred) {
190    std::vector<MachineBasicBlock *>::iterator goner =
191      std::find (Predecessors.begin(), Predecessors.end (), pred);
192    assert (goner != Predecessors.end ()
193            && "Trying to removePredecessor a MBB which isn't my predecessor");
194    Predecessors.erase (goner);
195  }
196};
197
198} // End llvm namespace
199
200#endif
201