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