Refactor SymbolTableListTraits to only have a single pointer in it, instead
[oota-llvm.git] / include / llvm / BasicBlock.h
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //
11 // This file contains the declaration of the BasicBlock class.
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_BASICBLOCK_H
15 #define LLVM_BASICBLOCK_H
16
17 #include "llvm/Instruction.h"
18 #include "llvm/SymbolTableListTraits.h"
19 #include "llvm/ADT/ilist"
20
21 namespace llvm {
22
23 class TerminatorInst;
24 template <class Term, class BB> class SuccIterator;  // Successor Iterator
25 template <class Ptr, class USE_iterator> class PredIterator;
26
27 template<> struct ilist_traits<Instruction>
28   : public SymbolTableListTraits<Instruction, BasicBlock> {
29   // createSentinel is used to create a node that marks the end of the list...
30   static Instruction *createSentinel();
31   static void destroySentinel(Instruction *I) { delete I; }
32   static iplist<Instruction> &getList(BasicBlock *BB);
33   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
34 };
35
36 /// This represents a single basic block in LLVM. A basic block is simply a
37 /// container of instructions that execute sequentially. Basic blocks are Values
38 /// because they are referenced by instructions such as branches and switch
39 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
40 /// represents a label to which a branch can jump.
41 ///
42 /// A well formed basic block is formed of a list of non-terminating 
43 /// instructions followed by a single TerminatorInst instruction.  
44 /// TerminatorInst's may not occur in the middle of basic blocks, and must 
45 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
46 /// occur because it may be useful in the intermediate stage of constructing or
47 /// modifying a program. However, the verifier will ensure that basic blocks
48 /// are "well formed".
49 /// @brief LLVM Basic Block Representation
50 class BasicBlock : public Value {       // Basic blocks are data objects also
51 public:
52   typedef iplist<Instruction> InstListType;
53 private :
54   InstListType InstList;
55   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
56   Function *Parent;
57
58   void setParent(Function *parent);
59   void setNext(BasicBlock *N) { Next = N; }
60   void setPrev(BasicBlock *N) { Prev = N; }
61   friend class SymbolTableListTraits<BasicBlock, Function>;
62
63   BasicBlock(const BasicBlock &);     // Do not implement
64   void operator=(const BasicBlock &); // Do not implement
65
66 public:
67   /// Instruction iterators...
68   typedef InstListType::iterator                              iterator;
69   typedef InstListType::const_iterator                  const_iterator;
70
71   /// BasicBlock ctor - If the function parameter is specified, the basic block
72   /// is automatically inserted at either the end of the function (if
73   /// InsertBefore is null), or before the specified basic block.
74   ///
75   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
76                       BasicBlock *InsertBefore = 0);
77   ~BasicBlock();
78
79   /// getParent - Return the enclosing method, or null if none
80   ///
81   const Function *getParent() const { return Parent; }
82         Function *getParent()       { return Parent; }
83
84   // getNext/Prev - Return the next or previous basic block in the list.
85         BasicBlock *getNext()       { return Next; }
86   const BasicBlock *getNext() const { return Next; }
87         BasicBlock *getPrev()       { return Prev; }
88   const BasicBlock *getPrev() const { return Prev; }
89
90   /// use_back - Specialize the methods defined in Value, as we know that an
91   /// BasicBlock can only be used by Instructions (specifically PHI and terms).
92   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
93   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
94   
95   /// getTerminator() - If this is a well formed basic block, then this returns
96   /// a pointer to the terminator instruction.  If it is not, then you get a
97   /// null pointer back.
98   ///
99   TerminatorInst *getTerminator();
100   const TerminatorInst *const getTerminator() const;
101   
102   /// Returns a pointer to the first instructon in this block that is not a 
103   /// PHINode instruction. When adding instruction to the beginning of the
104   /// basic block, they should be added before the returned value, not before
105   /// the first instruction, which might be PHI.
106   /// Returns 0 is there's no non-PHI instruction.
107   Instruction* getFirstNonPHI();
108   
109   /// removeFromParent - This method unlinks 'this' from the containing
110   /// function, but does not delete it.
111   ///
112   void removeFromParent();
113
114   /// eraseFromParent - This method unlinks 'this' from the containing function
115   /// and deletes it.
116   ///
117   void eraseFromParent();
118   
119   /// moveBefore - Unlink this basic block from its current function and
120   /// insert it into the function that MovePos lives in, right before MovePos.
121   void moveBefore(BasicBlock *MovePos);
122   
123   /// moveAfter - Unlink this basic block from its current function and
124   /// insert it into the function that MovePos lives in, right after MovePos.
125   void moveAfter(BasicBlock *MovePos);
126   
127
128   /// getSinglePredecessor - If this basic block has a single predecessor block,
129   /// return the block, otherwise return a null pointer.
130   BasicBlock *getSinglePredecessor();
131   const BasicBlock *getSinglePredecessor() const {
132     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
133   }
134
135   //===--------------------------------------------------------------------===//
136   /// Instruction iterator methods
137   ///
138   inline iterator                begin()       { return InstList.begin(); }
139   inline const_iterator          begin() const { return InstList.begin(); }
140   inline iterator                end  ()       { return InstList.end();   }
141   inline const_iterator          end  () const { return InstList.end();   }
142
143   inline size_t                   size() const { return InstList.size();  }
144   inline bool                    empty() const { return InstList.empty(); }
145   inline const Instruction      &front() const { return InstList.front(); }
146   inline       Instruction      &front()       { return InstList.front(); }
147   inline const Instruction       &back() const { return InstList.back();  }
148   inline       Instruction       &back()       { return InstList.back();  }
149
150   /// getInstList() - Return the underlying instruction list container.  You
151   /// need to access it directly if you want to modify it currently.
152   ///
153   const InstListType &getInstList() const { return InstList; }
154         InstListType &getInstList()       { return InstList; }
155
156   virtual void print(std::ostream &OS) const { print(OS, 0); }
157   void print(std::ostream *OS) const { if (OS) print(*OS); }
158   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
159
160   /// Methods for support type inquiry through isa, cast, and dyn_cast:
161   static inline bool classof(const BasicBlock *) { return true; }
162   static inline bool classof(const Value *V) {
163     return V->getValueID() == Value::BasicBlockVal;
164   }
165
166   /// dropAllReferences() - This function causes all the subinstructions to "let
167   /// go" of all references that they are maintaining.  This allows one to
168   /// 'delete' a whole class at a time, even though there may be circular
169   /// references... first all references are dropped, and all use counts go to
170   /// zero.  Then everything is delete'd for real.  Note that no operations are
171   /// valid on an object that has "dropped all references", except operator
172   /// delete.
173   ///
174   void dropAllReferences();
175
176   /// removePredecessor - This method is used to notify a BasicBlock that the
177   /// specified Predecessor of the block is no longer able to reach it.  This is
178   /// actually not used to update the Predecessor list, but is actually used to
179   /// update the PHI nodes that reside in the block.  Note that this should be
180   /// called while the predecessor still refers to this block.
181   ///
182   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
183
184   /// splitBasicBlock - This splits a basic block into two at the specified
185   /// instruction.  Note that all instructions BEFORE the specified iterator
186   /// stay as part of the original basic block, an unconditional branch is added
187   /// to the original BB, and the rest of the instructions in the BB are moved
188   /// to the new BB, including the old terminator.  The newly formed BasicBlock
189   /// is returned.  This function invalidates the specified iterator.
190   ///
191   /// Note that this only works on well formed basic blocks (must have a
192   /// terminator), and 'I' must not be the end of instruction list (which would
193   /// cause a degenerate basic block to be formed, having a terminator inside of
194   /// the basic block).
195   ///
196   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
197 };
198
199 } // End llvm namespace
200
201 #endif