API changes for class Use size reduction, wave 1.
[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 is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/Support/DataTypes.h"
21
22 namespace llvm {
23
24 class TerminatorInst;
25 template <class Term, class BB> class SuccIterator;  // Successor Iterator
26 template <class Ptr, class USE_iterator> class PredIterator;
27
28 template<> struct ilist_traits<Instruction>
29   : public SymbolTableListTraits<Instruction, BasicBlock> {
30   // createSentinel is used to create a node that marks the end of the list...
31   static Instruction *createSentinel();
32   static void destroySentinel(Instruction *I) { delete I; }
33   static iplist<Instruction> &getList(BasicBlock *BB);
34   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
35   static int getListOffset();
36 };
37
38 /// This represents a single basic block in LLVM. A basic block is simply a
39 /// container of instructions that execute sequentially. Basic blocks are Values
40 /// because they are referenced by instructions such as branches and switch
41 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
42 /// represents a label to which a branch can jump.
43 ///
44 /// A well formed basic block is formed of a list of non-terminating 
45 /// instructions followed by a single TerminatorInst instruction.  
46 /// TerminatorInst's may not occur in the middle of basic blocks, and must 
47 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
48 /// occur because it may be useful in the intermediate stage of constructing or
49 /// modifying a program. However, the verifier will ensure that basic blocks
50 /// are "well formed".
51 /// @brief LLVM Basic Block Representation
52 class BasicBlock : public User {       // Basic blocks are data objects also
53 public:
54   typedef iplist<Instruction> InstListType;
55 private :
56   InstListType InstList;
57   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
58   Function *Parent;
59   Use unwindDest;
60
61   void setParent(Function *parent);
62   void setNext(BasicBlock *N) { Next = N; }
63   void setPrev(BasicBlock *N) { Prev = N; }
64   friend class SymbolTableListTraits<BasicBlock, Function>;
65
66   BasicBlock(const BasicBlock &);     // Do not implement
67   void operator=(const BasicBlock &); // Do not implement
68
69   /// BasicBlock ctor - If the function parameter is specified, the basic block
70   /// is automatically inserted at either the end of the function (if
71   /// InsertBefore is null), or before the specified basic block.
72   ///
73   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
74                       BasicBlock *InsertBefore = 0, BasicBlock *UnwindDest = 0);
75 public:
76   /// Instruction iterators...
77   typedef InstListType::iterator                              iterator;
78   typedef InstListType::const_iterator                  const_iterator;
79
80   // allocate space for exactly zero operands
81   static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
82                             BasicBlock *InsertBefore = 0, BasicBlock *UnwindDest = 0) {
83     return new(!!UnwindDest) BasicBlock(Name, Parent, InsertBefore, UnwindDest);
84   }
85   ~BasicBlock();
86
87   /// getUnwindDest - Returns the BasicBlock that flow will enter if an unwind
88   /// instruction occurs in this block. May be null, in which case unwinding
89   /// is undefined in this block.
90   const BasicBlock *getUnwindDest() const;
91   BasicBlock *getUnwindDest();
92
93   /// setUnwindDest - Set which BasicBlock flow will enter if an unwind is
94   /// executed within this block. It may be set to null if unwinding is not
95   /// permitted in this block.
96   void setUnwindDest(BasicBlock *unwindDest);
97
98   /// getParent - Return the enclosing method, or null if none
99   ///
100   const Function *getParent() const { return Parent; }
101         Function *getParent()       { return Parent; }
102
103   /// getTerminator() - If this is a well formed basic block, then this returns
104   /// a pointer to the terminator instruction.  If it is not, then you get a
105   /// null pointer back.
106   ///
107   TerminatorInst *getTerminator();
108   const TerminatorInst *getTerminator() const;
109   
110   /// Returns a pointer to the first instructon in this block that is not a 
111   /// PHINode instruction. When adding instruction to the beginning of the
112   /// basic block, they should be added before the returned value, not before
113   /// the first instruction, which might be PHI.
114   /// Returns 0 is there's no non-PHI instruction.
115   Instruction* getFirstNonPHI();
116   
117   /// removeFromParent - This method unlinks 'this' from the containing
118   /// function, but does not delete it.
119   ///
120   void removeFromParent();
121
122   /// eraseFromParent - This method unlinks 'this' from the containing function
123   /// and deletes it.
124   ///
125   void eraseFromParent();
126   
127   /// moveBefore - Unlink this basic block from its current function and
128   /// insert it into the function that MovePos lives in, right before MovePos.
129   void moveBefore(BasicBlock *MovePos);
130   
131   /// moveAfter - Unlink this basic block from its current function and
132   /// insert it into the function that MovePos lives in, right after MovePos.
133   void moveAfter(BasicBlock *MovePos);
134   
135
136   /// getSinglePredecessor - If this basic block has a single predecessor block,
137   /// return the block, otherwise return a null pointer.
138   BasicBlock *getSinglePredecessor();
139   const BasicBlock *getSinglePredecessor() const {
140     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
141   }
142
143   //===--------------------------------------------------------------------===//
144   /// Instruction iterator methods
145   ///
146   inline iterator                begin()       { return InstList.begin(); }
147   inline const_iterator          begin() const { return InstList.begin(); }
148   inline iterator                end  ()       { return InstList.end();   }
149   inline const_iterator          end  () const { return InstList.end();   }
150
151   inline size_t                   size() const { return InstList.size();  }
152   inline bool                    empty() const { return InstList.empty(); }
153   inline const Instruction      &front() const { return InstList.front(); }
154   inline       Instruction      &front()       { return InstList.front(); }
155   inline const Instruction       &back() const { return InstList.back();  }
156   inline       Instruction       &back()       { return InstList.back();  }
157
158   /// getInstList() - Return the underlying instruction list container.  You
159   /// need to access it directly if you want to modify it currently.
160   ///
161   const InstListType &getInstList() const { return InstList; }
162         InstListType &getInstList()       { return InstList; }
163
164   virtual void print(std::ostream &OS) const { print(OS, 0); }
165   void print(std::ostream *OS) const { if (OS) print(*OS); }
166   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
167
168   /// Methods for support type inquiry through isa, cast, and dyn_cast:
169   static inline bool classof(const BasicBlock *) { return true; }
170   static inline bool classof(const Value *V) {
171     return V->getValueID() == Value::BasicBlockVal;
172   }
173
174   /// dropAllReferences() - This function causes all the subinstructions to "let
175   /// go" of all references that they are maintaining.  This allows one to
176   /// 'delete' a whole class at a time, even though there may be circular
177   /// references... first all references are dropped, and all use counts go to
178   /// zero.  Then everything is delete'd for real.  Note that no operations are
179   /// valid on an object that has "dropped all references", except operator
180   /// delete.
181   ///
182   void dropAllReferences();
183
184   /// removePredecessor - This method is used to notify a BasicBlock that the
185   /// specified Predecessor of the block is no longer able to reach it.  This is
186   /// actually not used to update the Predecessor list, but is actually used to
187   /// update the PHI nodes that reside in the block.  Note that this should be
188   /// called while the predecessor still refers to this block.
189   ///
190   /// DontDeleteUselessPHIs will keep PHIs that have one value or the same 
191   /// value for all entries.
192   ///
193   /// OnlyDeleteOne will only remove one entry from a PHI, in case there were
194   /// duplicate entries for the Pred.
195   ///
196   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false,
197                          bool OnlyDeleteOne = false);
198
199   /// splitBasicBlock - This splits a basic block into two at the specified
200   /// instruction.  Note that all instructions BEFORE the specified iterator
201   /// stay as part of the original basic block, an unconditional branch is added
202   /// to the original BB, and the rest of the instructions in the BB are moved
203   /// to the new BB, including the old terminator.  The newly formed BasicBlock
204   /// is returned.  This function invalidates the specified iterator.
205   ///
206   /// Note that this only works on well formed basic blocks (must have a
207   /// terminator), and 'I' must not be the end of instruction list (which would
208   /// cause a degenerate basic block to be formed, having a terminator inside of
209   /// the basic block).
210   ///
211   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
212   
213   
214   static unsigned getInstListOffset() {
215     BasicBlock *Obj = 0;
216     return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
217   }
218
219 private:
220   // getNext/Prev - Return the next or previous basic block in the list.  Access
221   // these with Function::iterator.
222   BasicBlock *getNext()       { return Next; }
223   const BasicBlock *getNext() const { return Next; }
224   BasicBlock *getPrev()       { return Prev; }
225   const BasicBlock *getPrev() const { return Prev; }
226 };
227
228 inline int 
229 ilist_traits<Instruction>::getListOffset() {
230   return BasicBlock::getInstListOffset();
231 }
232
233 } // End llvm namespace
234
235 #endif