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