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