ooops, forgot to include the pointless-write eliminator in my previous checkin
[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 ilist_node<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 {
37     return static_cast<MachineInstr*>(&Sentinel);
38   }
39   void destroySentinel(MachineInstr *) const {}
40
41   MachineInstr *provideInitialHead() const { return createSentinel(); }
42   MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
43   static void noteHead(MachineInstr*, MachineInstr*) {}
44
45   void addNodeToList(MachineInstr* N);
46   void removeNodeFromList(MachineInstr* N);
47   void transferNodesFromList(ilist_traits &SrcTraits,
48                              ilist_iterator<MachineInstr> first,
49                              ilist_iterator<MachineInstr> last);
50   void deleteNode(MachineInstr *N);
51 private:
52   void createNode(const MachineInstr &);
53 };
54
55 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
56   typedef ilist<MachineInstr> Instructions;
57   Instructions Insts;
58   const BasicBlock *BB;
59   int Number;
60   MachineFunction *xParent;
61   
62   /// Predecessors/Successors - Keep track of the predecessor / successor
63   /// basicblocks.
64   std::vector<MachineBasicBlock *> Predecessors;
65   std::vector<MachineBasicBlock *> Successors;
66
67   /// LiveIns - Keep track of the physical registers that are livein of
68   /// the basicblock.
69   std::vector<unsigned> LiveIns;
70
71   /// Alignment - Alignment of the basic block. Zero if the basic block does
72   /// not need to be aligned.
73   unsigned Alignment;
74   
75   /// IsLandingPad - Indicate that this basic block is entered via an
76   /// exception handler.
77   bool IsLandingPad;
78
79   // Intrusive list support
80   friend struct ilist_sentinel_traits<MachineBasicBlock>;
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(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(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   void pop_front() { Insts.pop_front(); }
258   void pop_back() { Insts.pop_back(); }
259   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
260   template<typename IT>
261   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
262   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
263
264   // erase - Remove the specified element or range from the instruction list.
265   // These functions delete any instructions removed.
266   //
267   iterator erase(iterator I)             { return Insts.erase(I); }
268   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
269   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
270   void clear()                           { Insts.clear(); }
271
272   /// splice - Take an instruction from MBB 'Other' at the position From,
273   /// and insert it into this MBB right before 'where'.
274   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
275     Insts.splice(where, Other->Insts, From);
276   }
277
278   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
279   /// To), and insert them into this MBB right before 'where'.
280   void splice(iterator where, MachineBasicBlock *Other, iterator From,
281               iterator To) {
282     Insts.splice(where, Other->Insts, From, To);
283   }
284
285   /// removeFromParent - This method unlinks 'this' from the containing
286   /// function, and returns it, but does not delete it.
287   MachineBasicBlock *removeFromParent();
288   
289   /// eraseFromParent - This method unlinks 'this' from the containing
290   /// function and deletes it.
291   void eraseFromParent();
292
293   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
294   /// 'Old', change the code and CFG so that it branches to 'New' instead.
295   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
296
297   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
298   /// the CFG to be inserted.  If we have proven that MBB can only branch to
299   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
300   /// DestB can be null. Besides DestA and DestB, retain other edges leading
301   /// to LandingPads (currently there can be only one; we don't check or require
302   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
303   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
304                             MachineBasicBlock *DestB,
305                             bool isCond);
306
307   // Debugging methods.
308   void dump() const;
309   void print(std::ostream &OS) const;
310   void print(std::ostream *OS) const { if (OS) print(*OS); }
311
312   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
313   /// level, unless they're not in a MachineFunction yet, in which case this
314   /// will return -1.
315   ///
316   int getNumber() const { return Number; }
317   void setNumber(int N) { Number = N; }
318
319 private:   // Methods used to maintain doubly linked list of blocks...
320   friend struct ilist_traits<MachineBasicBlock>;
321
322   // Machine-CFG mutators
323
324   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
325   /// Don't do this unless you know what you're doing, because it doesn't
326   /// update pred's successors list. Use pred->addSuccessor instead.
327   ///
328   void addPredecessor(MachineBasicBlock *pred);
329
330   /// removePredecessor - Remove pred as a predecessor of this
331   /// MachineBasicBlock. Don't do this unless you know what you're
332   /// doing, because it doesn't update pred's successors list. Use
333   /// pred->removeSuccessor instead.
334   ///
335   void removePredecessor(MachineBasicBlock *pred);
336 };
337
338 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
339
340 //===--------------------------------------------------------------------===//
341 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
342 //===--------------------------------------------------------------------===//
343
344 // Provide specializations of GraphTraits to be able to treat a
345 // MachineFunction as a graph of MachineBasicBlocks...
346 //
347
348 template <> struct GraphTraits<MachineBasicBlock *> {
349   typedef MachineBasicBlock NodeType;
350   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
351
352   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
353   static inline ChildIteratorType child_begin(NodeType *N) {
354     return N->succ_begin();
355   }
356   static inline ChildIteratorType child_end(NodeType *N) {
357     return N->succ_end();
358   }
359 };
360
361 template <> struct GraphTraits<const MachineBasicBlock *> {
362   typedef const MachineBasicBlock NodeType;
363   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
364
365   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
366   static inline ChildIteratorType child_begin(NodeType *N) {
367     return N->succ_begin();
368   }
369   static inline ChildIteratorType child_end(NodeType *N) {
370     return N->succ_end();
371   }
372 };
373
374 // Provide specializations of GraphTraits to be able to treat a
375 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
376 // in inverse order.  Inverse order for a function is considered
377 // to be when traversing the predecessor edges of a MBB
378 // instead of the successor edges.
379 //
380 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
381   typedef MachineBasicBlock NodeType;
382   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
383   static NodeType *getEntryNode(Inverse<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 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
395   typedef const MachineBasicBlock NodeType;
396   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
397   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
398     return G.Graph;
399   }
400   static inline ChildIteratorType child_begin(NodeType *N) {
401     return N->pred_begin();
402   }
403   static inline ChildIteratorType child_end(NodeType *N) {
404     return N->pred_end();
405   }
406 };
407
408 } // End llvm namespace
409
410 #endif