Initial target-independent CodeGen support for BlockAddresses.
[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 raw_ostream;
25
26 template <>
27 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
28 private:
29   mutable ilist_half_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   /// AddressTaken - Indicate that this basic block is potentially the
80   /// target of an indirect branch.
81   bool AddressTaken;
82
83   // Intrusive list support
84   MachineBasicBlock() {}
85
86   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
87
88   ~MachineBasicBlock();
89
90   // MachineBasicBlocks are allocated and owned by MachineFunction.
91   friend class MachineFunction;
92
93 public:
94   /// getBasicBlock - Return the LLVM basic block that this instance
95   /// corresponded to originally.
96   ///
97   const BasicBlock *getBasicBlock() const { return BB; }
98
99   /// hasAddressTaken - Test whether this block is potentially the target
100   /// of an indirect branch.
101   bool hasAddressTaken() const { return AddressTaken; }
102
103   /// setHasAddressTaken - Set this block to reflect that it potentially
104   /// is the target of an indirect branch.
105   void setHasAddressTaken() { AddressTaken = true; }
106
107   /// getParent - Return the MachineFunction containing this basic block.
108   ///
109   const MachineFunction *getParent() const { return xParent; }
110   MachineFunction *getParent() { return xParent; }
111
112   typedef Instructions::iterator                              iterator;
113   typedef Instructions::const_iterator                  const_iterator;
114   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
115   typedef std::reverse_iterator<iterator>             reverse_iterator;
116
117   unsigned size() const { return (unsigned)Insts.size(); }
118   bool empty() const { return Insts.empty(); }
119
120   MachineInstr& front() { return Insts.front(); }
121   MachineInstr& back()  { return Insts.back(); }
122   const MachineInstr& front() const { return Insts.front(); }
123   const MachineInstr& back()  const { return Insts.back(); }
124
125   iterator                begin()       { return Insts.begin();  }
126   const_iterator          begin() const { return Insts.begin();  }
127   iterator                  end()       { return Insts.end();    }
128   const_iterator            end() const { return Insts.end();    }
129   reverse_iterator       rbegin()       { return Insts.rbegin(); }
130   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
131   reverse_iterator       rend  ()       { return Insts.rend();   }
132   const_reverse_iterator rend  () const { return Insts.rend();   }
133
134   // Machine-CFG iterators
135   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
136   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
137   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
138   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
139   typedef std::vector<MachineBasicBlock *>::reverse_iterator
140                                                          pred_reverse_iterator;
141   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
142                                                    const_pred_reverse_iterator;
143   typedef std::vector<MachineBasicBlock *>::reverse_iterator
144                                                          succ_reverse_iterator;
145   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
146                                                    const_succ_reverse_iterator;
147
148   pred_iterator        pred_begin()       { return Predecessors.begin(); }
149   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
150   pred_iterator        pred_end()         { return Predecessors.end();   }
151   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
152   pred_reverse_iterator        pred_rbegin()
153                                           { return Predecessors.rbegin();}
154   const_pred_reverse_iterator  pred_rbegin() const
155                                           { return Predecessors.rbegin();}
156   pred_reverse_iterator        pred_rend()
157                                           { return Predecessors.rend();  }
158   const_pred_reverse_iterator  pred_rend()   const
159                                           { return Predecessors.rend();  }
160   unsigned             pred_size()  const {
161     return (unsigned)Predecessors.size();
162   }
163   bool                 pred_empty() const { return Predecessors.empty(); }
164   succ_iterator        succ_begin()       { return Successors.begin();   }
165   const_succ_iterator  succ_begin() const { return Successors.begin();   }
166   succ_iterator        succ_end()         { return Successors.end();     }
167   const_succ_iterator  succ_end()   const { return Successors.end();     }
168   succ_reverse_iterator        succ_rbegin()
169                                           { return Successors.rbegin();  }
170   const_succ_reverse_iterator  succ_rbegin() const
171                                           { return Successors.rbegin();  }
172   succ_reverse_iterator        succ_rend()
173                                           { return Successors.rend();    }
174   const_succ_reverse_iterator  succ_rend()   const
175                                           { return Successors.rend();    }
176   unsigned             succ_size()  const {
177     return (unsigned)Successors.size();
178   }
179   bool                 succ_empty() const { return Successors.empty();   }
180
181   // LiveIn management methods.
182
183   /// addLiveIn - Add the specified register as a live in.  Note that it
184   /// is an error to add the same register to the same set more than once.
185   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
186
187   /// removeLiveIn - Remove the specified register from the live in set.
188   ///
189   void removeLiveIn(unsigned Reg);
190
191   /// isLiveIn - Return true if the specified register is in the live in set.
192   ///
193   bool isLiveIn(unsigned Reg) const;
194
195   // Iteration support for live in sets.  These sets are kept in sorted
196   // order by their register number.
197   typedef std::vector<unsigned>::iterator       livein_iterator;
198   typedef std::vector<unsigned>::const_iterator const_livein_iterator;
199   livein_iterator       livein_begin()       { return LiveIns.begin(); }
200   const_livein_iterator livein_begin() const { return LiveIns.begin(); }
201   livein_iterator       livein_end()         { return LiveIns.end(); }
202   const_livein_iterator livein_end()   const { return LiveIns.end(); }
203   bool            livein_empty() const { return LiveIns.empty(); }
204
205   /// getAlignment - Return alignment of the basic block.
206   ///
207   unsigned getAlignment() const { return Alignment; }
208
209   /// setAlignment - Set alignment of the basic block.
210   ///
211   void setAlignment(unsigned Align) { Alignment = Align; }
212
213   /// isLandingPad - Returns true if the block is a landing pad. That is
214   /// this basic block is entered via an exception handler.
215   bool isLandingPad() const { return IsLandingPad; }
216
217   /// setIsLandingPad - Indicates the block is a landing pad.  That is
218   /// this basic block is entered via an exception handler.
219   void setIsLandingPad() { IsLandingPad = true; }
220
221   // Code Layout methods.
222   
223   /// moveBefore/moveAfter - move 'this' block before or after the specified
224   /// block.  This only moves the block, it does not modify the CFG or adjust
225   /// potential fall-throughs at the end of the block.
226   void moveBefore(MachineBasicBlock *NewAfter);
227   void moveAfter(MachineBasicBlock *NewBefore);
228   
229   // Machine-CFG mutators
230   
231   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
232   /// The Predecessors list of succ is automatically updated.
233   ///
234   void addSuccessor(MachineBasicBlock *succ);
235
236   /// removeSuccessor - Remove successor from the successors list of this
237   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
238   ///
239   void removeSuccessor(MachineBasicBlock *succ);
240
241   /// removeSuccessor - Remove specified successor from the successors list of
242   /// this MachineBasicBlock. The Predecessors list of succ is automatically
243   /// updated.  Return the iterator to the element after the one removed.
244   ///
245   succ_iterator removeSuccessor(succ_iterator I);
246   
247   /// transferSuccessors - Transfers all the successors from MBB to this
248   /// machine basic block (i.e., copies all the successors fromMBB and
249   /// remove all the successors fromBB).
250   void transferSuccessors(MachineBasicBlock *fromMBB);
251   
252   /// isSuccessor - Return true if the specified MBB is a successor of this
253   /// block.
254   bool isSuccessor(const MachineBasicBlock *MBB) const;
255
256   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
257   /// immediately after this block, such that if this block exits by
258   /// falling through, control will transfer to the specified MBB. Note
259   /// that MBB need not be a successor at all, for example if this block
260   /// ends with an unconditional branch to some other block.
261   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
262
263   /// getFirstTerminator - returns an iterator to the first terminator
264   /// instruction of this basic block. If a terminator does not exist,
265   /// it returns end()
266   iterator getFirstTerminator();
267
268   /// isOnlyReachableViaFallthough - Return true if this basic block has
269   /// exactly one predecessor and the control transfer mechanism between
270   /// the predecessor and this block is a fall-through.
271   bool isOnlyReachableByFallthrough() const;
272
273   void pop_front() { Insts.pop_front(); }
274   void pop_back() { Insts.pop_back(); }
275   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
276   template<typename IT>
277   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
278   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
279
280   // erase - Remove the specified element or range from the instruction list.
281   // These functions delete any instructions removed.
282   //
283   iterator erase(iterator I)             { return Insts.erase(I); }
284   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
285   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
286   void clear()                           { Insts.clear(); }
287
288   /// splice - Take an instruction from MBB 'Other' at the position From,
289   /// and insert it into this MBB right before 'where'.
290   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
291     Insts.splice(where, Other->Insts, From);
292   }
293
294   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
295   /// To), and insert them into this MBB right before 'where'.
296   void splice(iterator where, MachineBasicBlock *Other, iterator From,
297               iterator To) {
298     Insts.splice(where, Other->Insts, From, To);
299   }
300
301   /// removeFromParent - This method unlinks 'this' from the containing
302   /// function, and returns it, but does not delete it.
303   MachineBasicBlock *removeFromParent();
304   
305   /// eraseFromParent - This method unlinks 'this' from the containing
306   /// function and deletes it.
307   void eraseFromParent();
308
309   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
310   /// 'Old', change the code and CFG so that it branches to 'New' instead.
311   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
312
313   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
314   /// the CFG to be inserted.  If we have proven that MBB can only branch to
315   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
316   /// DestB can be null. Besides DestA and DestB, retain other edges leading
317   /// to LandingPads (currently there can be only one; we don't check or require
318   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
319   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
320                             MachineBasicBlock *DestB,
321                             bool isCond);
322
323   // Debugging methods.
324   void dump() const;
325   void print(raw_ostream &OS) const;
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 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
354
355 //===--------------------------------------------------------------------===//
356 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
357 //===--------------------------------------------------------------------===//
358
359 // Provide specializations of GraphTraits to be able to treat a
360 // MachineFunction as a graph of MachineBasicBlocks...
361 //
362
363 template <> struct GraphTraits<MachineBasicBlock *> {
364   typedef MachineBasicBlock NodeType;
365   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
366
367   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
368   static inline ChildIteratorType child_begin(NodeType *N) {
369     return N->succ_begin();
370   }
371   static inline ChildIteratorType child_end(NodeType *N) {
372     return N->succ_end();
373   }
374 };
375
376 template <> struct GraphTraits<const MachineBasicBlock *> {
377   typedef const MachineBasicBlock NodeType;
378   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
379
380   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
381   static inline ChildIteratorType child_begin(NodeType *N) {
382     return N->succ_begin();
383   }
384   static inline ChildIteratorType child_end(NodeType *N) {
385     return N->succ_end();
386   }
387 };
388
389 // Provide specializations of GraphTraits to be able to treat a
390 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
391 // in inverse order.  Inverse order for a function is considered
392 // to be when traversing the predecessor edges of a MBB
393 // instead of the successor edges.
394 //
395 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
396   typedef MachineBasicBlock NodeType;
397   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
398   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
399     return G.Graph;
400   }
401   static inline ChildIteratorType child_begin(NodeType *N) {
402     return N->pred_begin();
403   }
404   static inline ChildIteratorType child_end(NodeType *N) {
405     return N->pred_end();
406   }
407 };
408
409 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
410   typedef const MachineBasicBlock NodeType;
411   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
412   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
413     return G.Graph;
414   }
415   static inline ChildIteratorType child_begin(NodeType *N) {
416     return N->pred_begin();
417   }
418   static inline ChildIteratorType child_end(NodeType *N) {
419     return N->pred_end();
420   }
421 };
422
423 } // End llvm namespace
424
425 #endif