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