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