Tidy up #includes.
[oota-llvm.git] / include / llvm / CodeGen / MachineBasicBlock.h
1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "llvm/ADT/GraphTraits.h"
19
20 namespace llvm {
21
22 class BasicBlock;
23 class MachineFunction;
24
25 template <>
26 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
27 private:
28   mutable ilist_node<MachineInstr> Sentinel;
29
30   // this is only set by the MachineBasicBlock owning the LiveList
31   friend class MachineBasicBlock;
32   MachineBasicBlock* Parent;
33
34 public:
35   MachineInstr *createSentinel() const {
36     return static_cast<MachineInstr*>(&Sentinel);
37   }
38   void destroySentinel(MachineInstr *) const {}
39
40   MachineInstr *provideInitialHead() const { return createSentinel(); }
41   MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
42   static void noteHead(MachineInstr*, MachineInstr*) {}
43
44   void addNodeToList(MachineInstr* N);
45   void removeNodeFromList(MachineInstr* N);
46   void transferNodesFromList(ilist_traits &SrcTraits,
47                              ilist_iterator<MachineInstr> first,
48                              ilist_iterator<MachineInstr> last);
49   void deleteNode(MachineInstr *N);
50 private:
51   void createNode(const MachineInstr &);
52 };
53
54 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
55   typedef ilist<MachineInstr> Instructions;
56   Instructions Insts;
57   const BasicBlock *BB;
58   int Number;
59   MachineFunction *xParent;
60   
61   /// Predecessors/Successors - Keep track of the predecessor / successor
62   /// basicblocks.
63   std::vector<MachineBasicBlock *> Predecessors;
64   std::vector<MachineBasicBlock *> Successors;
65
66   /// LiveIns - Keep track of the physical registers that are livein of
67   /// the basicblock.
68   std::vector<unsigned> LiveIns;
69
70   /// Alignment - Alignment of the basic block. Zero if the basic block does
71   /// not need to be aligned.
72   unsigned Alignment;
73   
74   /// IsLandingPad - Indicate that this basic block is entered via an
75   /// exception handler.
76   bool IsLandingPad;
77
78   // Intrusive list support
79   MachineBasicBlock() {}
80
81   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
82
83   ~MachineBasicBlock();
84
85   // MachineBasicBlocks are allocated and owned by MachineFunction.
86   friend class MachineFunction;
87
88 public:
89   /// getBasicBlock - Return the LLVM basic block that this instance
90   /// corresponded to originally.
91   ///
92   const BasicBlock *getBasicBlock() const { return BB; }
93
94   /// getParent - Return the MachineFunction containing this basic block.
95   ///
96   const MachineFunction *getParent() const { return xParent; }
97   MachineFunction *getParent() { return xParent; }
98
99   typedef Instructions::iterator                              iterator;
100   typedef Instructions::const_iterator                  const_iterator;
101   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
102   typedef std::reverse_iterator<iterator>             reverse_iterator;
103
104   unsigned size() const { return (unsigned)Insts.size(); }
105   bool empty() const { return Insts.empty(); }
106
107   MachineInstr& front() { return Insts.front(); }
108   MachineInstr& back()  { return Insts.back(); }
109   const MachineInstr& front() const { return Insts.front(); }
110   const MachineInstr& back()  const { return Insts.back(); }
111
112   iterator                begin()       { return Insts.begin();  }
113   const_iterator          begin() const { return Insts.begin();  }
114   iterator                  end()       { return Insts.end();    }
115   const_iterator            end() const { return Insts.end();    }
116   reverse_iterator       rbegin()       { return Insts.rbegin(); }
117   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
118   reverse_iterator       rend  ()       { return Insts.rend();   }
119   const_reverse_iterator rend  () const { return Insts.rend();   }
120
121   // Machine-CFG iterators
122   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
123   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
124   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
125   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
126   typedef std::vector<MachineBasicBlock *>::reverse_iterator
127                                                          pred_reverse_iterator;
128   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
129                                                    const_pred_reverse_iterator;
130   typedef std::vector<MachineBasicBlock *>::reverse_iterator
131                                                          succ_reverse_iterator;
132   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
133                                                    const_succ_reverse_iterator;
134
135   pred_iterator        pred_begin()       { return Predecessors.begin(); }
136   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
137   pred_iterator        pred_end()         { return Predecessors.end();   }
138   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
139   pred_reverse_iterator        pred_rbegin()
140                                           { return Predecessors.rbegin();}
141   const_pred_reverse_iterator  pred_rbegin() const
142                                           { return Predecessors.rbegin();}
143   pred_reverse_iterator        pred_rend()
144                                           { return Predecessors.rend();  }
145   const_pred_reverse_iterator  pred_rend()   const
146                                           { return Predecessors.rend();  }
147   unsigned             pred_size()  const {
148     return (unsigned)Predecessors.size();
149   }
150   bool                 pred_empty() const { return Predecessors.empty(); }
151   succ_iterator        succ_begin()       { return Successors.begin();   }
152   const_succ_iterator  succ_begin() const { return Successors.begin();   }
153   succ_iterator        succ_end()         { return Successors.end();     }
154   const_succ_iterator  succ_end()   const { return Successors.end();     }
155   succ_reverse_iterator        succ_rbegin()
156                                           { return Successors.rbegin();  }
157   const_succ_reverse_iterator  succ_rbegin() const
158                                           { return Successors.rbegin();  }
159   succ_reverse_iterator        succ_rend()
160                                           { return Successors.rend();    }
161   const_succ_reverse_iterator  succ_rend()   const
162                                           { return Successors.rend();    }
163   unsigned             succ_size()  const {
164     return (unsigned)Successors.size();
165   }
166   bool                 succ_empty() const { return Successors.empty();   }
167
168   // LiveIn management methods.
169
170   /// addLiveIn - Add the specified register as a live in.  Note that it
171   /// is an error to add the same register to the same set more than once.
172   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
173
174   /// removeLiveIn - Remove the specified register from the live in set.
175   ///
176   void removeLiveIn(unsigned Reg);
177
178   /// isLiveIn - Return true if the specified register is in the live in set.
179   ///
180   bool isLiveIn(unsigned Reg) const;
181
182   // Iteration support for live in sets.  These sets are kept in sorted
183   // order by their register number.
184   typedef std::vector<unsigned>::iterator       livein_iterator;
185   typedef std::vector<unsigned>::const_iterator const_livein_iterator;
186   livein_iterator       livein_begin()       { return LiveIns.begin(); }
187   const_livein_iterator livein_begin() const { return LiveIns.begin(); }
188   livein_iterator       livein_end()         { return LiveIns.end(); }
189   const_livein_iterator livein_end()   const { return LiveIns.end(); }
190   bool            livein_empty() const { return LiveIns.empty(); }
191
192   /// getAlignment - Return alignment of the basic block.
193   ///
194   unsigned getAlignment() const { return Alignment; }
195
196   /// setAlignment - Set alignment of the basic block.
197   ///
198   void setAlignment(unsigned Align) { Alignment = Align; }
199
200   /// isLandingPad - Returns true if the block is a landing pad. That is
201   /// this basic block is entered via an exception handler.
202   bool isLandingPad() const { return IsLandingPad; }
203
204   /// setIsLandingPad - Indicates the block is a landing pad.  That is
205   /// this basic block is entered via an exception handler.
206   void setIsLandingPad() { IsLandingPad = true; }
207
208   // Code Layout methods.
209   
210   /// moveBefore/moveAfter - move 'this' block before or after the specified
211   /// block.  This only moves the block, it does not modify the CFG or adjust
212   /// potential fall-throughs at the end of the block.
213   void moveBefore(MachineBasicBlock *NewAfter);
214   void moveAfter(MachineBasicBlock *NewBefore);
215   
216   // Machine-CFG mutators
217   
218   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
219   /// The Predecessors list of succ is automatically updated.
220   ///
221   void addSuccessor(MachineBasicBlock *succ);
222
223   /// removeSuccessor - Remove successor from the successors list of this
224   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
225   ///
226   void removeSuccessor(MachineBasicBlock *succ);
227
228   /// removeSuccessor - Remove specified successor from the successors list of
229   /// this MachineBasicBlock. The Predecessors list of succ is automatically
230   /// updated.  Return the iterator to the element after the one removed.
231   ///
232   succ_iterator removeSuccessor(succ_iterator I);
233   
234   /// transferSuccessors - Transfers all the successors from MBB to this
235   /// machine basic block (i.e., copies all the successors fromMBB and
236   /// remove all the successors fromBB).
237   void transferSuccessors(MachineBasicBlock *fromMBB);
238   
239   /// isSuccessor - Return true if the specified MBB is a successor of this
240   /// block.
241   bool isSuccessor(const MachineBasicBlock *MBB) const;
242
243   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
244   /// immediately after this block, such that if this block exits by
245   /// falling through, control will transfer to the specified MBB. Note
246   /// that MBB need not be a successor at all, for example if this block
247   /// ends with an unconditional branch to some other block.
248   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
249
250   /// getFirstTerminator - returns an iterator to the first terminator
251   /// instruction of this basic block. If a terminator does not exist,
252   /// it returns end()
253   iterator getFirstTerminator();
254
255   /// isOnlyReachableViaFallthough - Return true if this basic block has
256   /// exactly one predecessor and the control transfer mechanism between
257   /// the predecessor and this block is a fall-through.
258   bool isOnlyReachableByFallthrough() const;
259
260   void pop_front() { Insts.pop_front(); }
261   void pop_back() { Insts.pop_back(); }
262   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
263   template<typename IT>
264   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
265   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
266
267   // erase - Remove the specified element or range from the instruction list.
268   // These functions delete any instructions removed.
269   //
270   iterator erase(iterator I)             { return Insts.erase(I); }
271   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
272   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
273   void clear()                           { Insts.clear(); }
274
275   /// splice - Take an instruction from MBB 'Other' at the position From,
276   /// and insert it into this MBB right before 'where'.
277   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
278     Insts.splice(where, Other->Insts, From);
279   }
280
281   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
282   /// To), and insert them into this MBB right before 'where'.
283   void splice(iterator where, MachineBasicBlock *Other, iterator From,
284               iterator To) {
285     Insts.splice(where, Other->Insts, From, To);
286   }
287
288   /// removeFromParent - This method unlinks 'this' from the containing
289   /// function, and returns it, but does not delete it.
290   MachineBasicBlock *removeFromParent();
291   
292   /// eraseFromParent - This method unlinks 'this' from the containing
293   /// function and deletes it.
294   void eraseFromParent();
295
296   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
297   /// 'Old', change the code and CFG so that it branches to 'New' instead.
298   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
299
300   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
301   /// the CFG to be inserted.  If we have proven that MBB can only branch to
302   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
303   /// DestB can be null. Besides DestA and DestB, retain other edges leading
304   /// to LandingPads (currently there can be only one; we don't check or require
305   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
306   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
307                             MachineBasicBlock *DestB,
308                             bool isCond);
309
310   // Debugging methods.
311   void dump() const;
312   void print(std::ostream &OS) const;
313   void print(std::ostream *OS) const { if (OS) print(*OS); }
314
315   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
316   /// level, unless they're not in a MachineFunction yet, in which case this
317   /// will return -1.
318   ///
319   int getNumber() const { return Number; }
320   void setNumber(int N) { Number = N; }
321
322 private:   // Methods used to maintain doubly linked list of blocks...
323   friend struct ilist_traits<MachineBasicBlock>;
324
325   // Machine-CFG mutators
326
327   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
328   /// Don't do this unless you know what you're doing, because it doesn't
329   /// update pred's successors list. Use pred->addSuccessor instead.
330   ///
331   void addPredecessor(MachineBasicBlock *pred);
332
333   /// removePredecessor - Remove pred as a predecessor of this
334   /// MachineBasicBlock. Don't do this unless you know what you're
335   /// doing, because it doesn't update pred's successors list. Use
336   /// pred->removeSuccessor instead.
337   ///
338   void removePredecessor(MachineBasicBlock *pred);
339 };
340
341 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
342
343 //===--------------------------------------------------------------------===//
344 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
345 //===--------------------------------------------------------------------===//
346
347 // Provide specializations of GraphTraits to be able to treat a
348 // MachineFunction as a graph of MachineBasicBlocks...
349 //
350
351 template <> struct GraphTraits<MachineBasicBlock *> {
352   typedef MachineBasicBlock NodeType;
353   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
354
355   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
356   static inline ChildIteratorType child_begin(NodeType *N) {
357     return N->succ_begin();
358   }
359   static inline ChildIteratorType child_end(NodeType *N) {
360     return N->succ_end();
361   }
362 };
363
364 template <> struct GraphTraits<const MachineBasicBlock *> {
365   typedef const MachineBasicBlock NodeType;
366   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
367
368   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
369   static inline ChildIteratorType child_begin(NodeType *N) {
370     return N->succ_begin();
371   }
372   static inline ChildIteratorType child_end(NodeType *N) {
373     return N->succ_end();
374   }
375 };
376
377 // Provide specializations of GraphTraits to be able to treat a
378 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
379 // in inverse order.  Inverse order for a function is considered
380 // to be when traversing the predecessor edges of a MBB
381 // instead of the successor edges.
382 //
383 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
384   typedef MachineBasicBlock NodeType;
385   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
386   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
387     return G.Graph;
388   }
389   static inline ChildIteratorType child_begin(NodeType *N) {
390     return N->pred_begin();
391   }
392   static inline ChildIteratorType child_end(NodeType *N) {
393     return N->pred_end();
394   }
395 };
396
397 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
398   typedef const MachineBasicBlock NodeType;
399   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
400   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
401     return G.Graph;
402   }
403   static inline ChildIteratorType child_begin(NodeType *N) {
404     return N->pred_begin();
405   }
406   static inline ChildIteratorType child_end(NodeType *N) {
407     return N->pred_end();
408   }
409 };
410
411 } // End llvm namespace
412
413 #endif