enhance MachineFunction to have a MMI pointer.
[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>::iterator       livein_iterator;
205   typedef std::vector<unsigned>::const_iterator const_livein_iterator;
206   livein_iterator       livein_begin()       { return LiveIns.begin(); }
207   const_livein_iterator livein_begin() const { return LiveIns.begin(); }
208   livein_iterator       livein_end()         { return LiveIns.end(); }
209   const_livein_iterator livein_end()   const { return LiveIns.end(); }
210   bool            livein_empty() const { return LiveIns.empty(); }
211
212   /// getAlignment - Return alignment of the basic block.
213   ///
214   unsigned getAlignment() const { return Alignment; }
215
216   /// setAlignment - Set alignment of the basic block.
217   ///
218   void setAlignment(unsigned Align) { Alignment = Align; }
219
220   /// isLandingPad - Returns true if the block is a landing pad. That is
221   /// this basic block is entered via an exception handler.
222   bool isLandingPad() const { return IsLandingPad; }
223
224   /// setIsLandingPad - Indicates the block is a landing pad.  That is
225   /// this basic block is entered via an exception handler.
226   void setIsLandingPad() { IsLandingPad = true; }
227
228   // Code Layout methods.
229   
230   /// moveBefore/moveAfter - move 'this' block before or after the specified
231   /// block.  This only moves the block, it does not modify the CFG or adjust
232   /// potential fall-throughs at the end of the block.
233   void moveBefore(MachineBasicBlock *NewAfter);
234   void moveAfter(MachineBasicBlock *NewBefore);
235
236   /// updateTerminator - Update the terminator instructions in block to account
237   /// for changes to the layout. If the block previously used a fallthrough,
238   /// it may now need a branch, and if it previously used branching it may now
239   /// be able to use a fallthrough.
240   void updateTerminator();
241
242   // Machine-CFG mutators
243   
244   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
245   /// The Predecessors list of succ is automatically updated.
246   ///
247   void addSuccessor(MachineBasicBlock *succ);
248
249   /// removeSuccessor - Remove successor from the successors list of this
250   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
251   ///
252   void removeSuccessor(MachineBasicBlock *succ);
253
254   /// removeSuccessor - Remove specified successor from the successors list of
255   /// this MachineBasicBlock. The Predecessors list of succ is automatically
256   /// updated.  Return the iterator to the element after the one removed.
257   ///
258   succ_iterator removeSuccessor(succ_iterator I);
259   
260   /// transferSuccessors - Transfers all the successors from MBB to this
261   /// machine basic block (i.e., copies all the successors fromMBB and
262   /// remove all the successors from fromMBB).
263   void transferSuccessors(MachineBasicBlock *fromMBB);
264   
265   /// isSuccessor - Return true if the specified MBB is a successor of this
266   /// block.
267   bool isSuccessor(const MachineBasicBlock *MBB) const;
268
269   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
270   /// immediately after this block, such that if this block exits by
271   /// falling through, control will transfer to the specified MBB. Note
272   /// that MBB need not be a successor at all, for example if this block
273   /// ends with an unconditional branch to some other block.
274   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
275
276   /// canFallThrough - Return true if the block can implicitly transfer
277   /// control to the block after it by falling off the end of it.  This should
278   /// return false if it can reach the block after it, but it uses an explicit
279   /// branch to do so (e.g., a table jump).  True is a conservative answer.
280   bool canFallThrough();
281
282   /// getFirstTerminator - returns an iterator to the first terminator
283   /// instruction of this basic block. If a terminator does not exist,
284   /// it returns end()
285   iterator getFirstTerminator();
286
287   void pop_front() { Insts.pop_front(); }
288   void pop_back() { Insts.pop_back(); }
289   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
290   template<typename IT>
291   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
292   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
293
294   // erase - Remove the specified element or range from the instruction list.
295   // These functions delete any instructions removed.
296   //
297   iterator erase(iterator I)             { return Insts.erase(I); }
298   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
299   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
300   void clear()                           { Insts.clear(); }
301
302   /// splice - Take an instruction from MBB 'Other' at the position From,
303   /// and insert it into this MBB right before 'where'.
304   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
305     Insts.splice(where, Other->Insts, From);
306   }
307
308   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
309   /// To), and insert them into this MBB right before 'where'.
310   void splice(iterator where, MachineBasicBlock *Other, iterator From,
311               iterator To) {
312     Insts.splice(where, Other->Insts, From, To);
313   }
314
315   /// removeFromParent - This method unlinks 'this' from the containing
316   /// function, and returns it, but does not delete it.
317   MachineBasicBlock *removeFromParent();
318   
319   /// eraseFromParent - This method unlinks 'this' from the containing
320   /// function and deletes it.
321   void eraseFromParent();
322
323   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
324   /// 'Old', change the code and CFG so that it branches to 'New' instead.
325   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
326
327   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
328   /// the CFG to be inserted.  If we have proven that MBB can only branch to
329   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
330   /// DestB can be null. Besides DestA and DestB, retain other edges leading
331   /// to LandingPads (currently there can be only one; we don't check or require
332   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
333   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
334                             MachineBasicBlock *DestB,
335                             bool isCond);
336
337   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
338   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
339   DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
340
341   // Debugging methods.
342   void dump() const;
343   void print(raw_ostream &OS) const;
344
345   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
346   /// level, unless they're not in a MachineFunction yet, in which case this
347   /// will return -1.
348   ///
349   int getNumber() const { return Number; }
350   void setNumber(int N) { Number = N; }
351
352   /// getSymbol - Return the MCSymbol for this basic block.
353   ///
354   MCSymbol *getSymbol() const;
355   
356 private:   // Methods used to maintain doubly linked list of blocks...
357   friend struct ilist_traits<MachineBasicBlock>;
358
359   // Machine-CFG mutators
360
361   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
362   /// Don't do this unless you know what you're doing, because it doesn't
363   /// update pred's successors list. Use pred->addSuccessor instead.
364   ///
365   void addPredecessor(MachineBasicBlock *pred);
366
367   /// removePredecessor - Remove pred as a predecessor of this
368   /// MachineBasicBlock. Don't do this unless you know what you're
369   /// doing, because it doesn't update pred's successors list. Use
370   /// pred->removeSuccessor instead.
371   ///
372   void removePredecessor(MachineBasicBlock *pred);
373 };
374
375 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
376
377 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
378
379 //===--------------------------------------------------------------------===//
380 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
381 //===--------------------------------------------------------------------===//
382
383 // Provide specializations of GraphTraits to be able to treat a
384 // MachineFunction as a graph of MachineBasicBlocks...
385 //
386
387 template <> struct GraphTraits<MachineBasicBlock *> {
388   typedef MachineBasicBlock NodeType;
389   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
390
391   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
392   static inline ChildIteratorType child_begin(NodeType *N) {
393     return N->succ_begin();
394   }
395   static inline ChildIteratorType child_end(NodeType *N) {
396     return N->succ_end();
397   }
398 };
399
400 template <> struct GraphTraits<const MachineBasicBlock *> {
401   typedef const MachineBasicBlock NodeType;
402   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
403
404   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
405   static inline ChildIteratorType child_begin(NodeType *N) {
406     return N->succ_begin();
407   }
408   static inline ChildIteratorType child_end(NodeType *N) {
409     return N->succ_end();
410   }
411 };
412
413 // Provide specializations of GraphTraits to be able to treat a
414 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
415 // in inverse order.  Inverse order for a function is considered
416 // to be when traversing the predecessor edges of a MBB
417 // instead of the successor edges.
418 //
419 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
420   typedef MachineBasicBlock NodeType;
421   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
422   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
423     return G.Graph;
424   }
425   static inline ChildIteratorType child_begin(NodeType *N) {
426     return N->pred_begin();
427   }
428   static inline ChildIteratorType child_end(NodeType *N) {
429     return N->pred_end();
430   }
431 };
432
433 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
434   typedef const MachineBasicBlock NodeType;
435   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
436   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
437     return G.Graph;
438   }
439   static inline ChildIteratorType child_begin(NodeType *N) {
440     return N->pred_begin();
441   }
442   static inline ChildIteratorType child_end(NodeType *N) {
443     return N->pred_end();
444   }
445 };
446
447 } // End llvm namespace
448
449 #endif