Add assertions
[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 /// \class BasicBlock
11 ///
12 /// This file contains the declaration of the BasicBlock class, which represents
13 /// a single basic block in the VM.
14 ///
15 /// Note that basic blocks themselves are Value's, because they are referenced
16 /// by instructions like branches and can go in switch tables and stuff...
17 ///
18 ///===---------------------------------------------------------------------===//
19 ///
20 /// Note that well formed basic blocks are formed of a list of instructions 
21 /// followed by a single TerminatorInst instruction.  TerminatorInst's may not
22 /// occur in the middle of basic blocks, and must terminate the blocks.
23 ///
24 /// This code allows malformed basic blocks to occur, because it may be useful
25 /// in the intermediate stage modification to a program.
26 ///
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_BASICBLOCK_H
30 #define LLVM_BASICBLOCK_H
31
32 #include "llvm/Instruction.h"
33 #include "llvm/SymbolTableListTraits.h"
34 #include "Support/ilist"
35
36 class TerminatorInst;
37 template <class _Term, class _BB> class SuccIterator;  // Successor Iterator
38 template <class _Ptr, class _USE_iterator> class PredIterator;
39
40 template<> struct ilist_traits<Instruction>
41   : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
42   // createNode is used to create a node that marks the end of the list...
43   static Instruction *createNode();
44   static iplist<Instruction> &getList(BasicBlock *BB);
45 };
46
47 struct BasicBlock : public Value {       // Basic blocks are data objects also
48   typedef iplist<Instruction> InstListType;
49 private :
50   InstListType InstList;
51   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
52
53   void setParent(Function *parent);
54   void setNext(BasicBlock *N) { Next = N; }
55   void setPrev(BasicBlock *N) { Prev = N; }
56   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
57
58   BasicBlock(const BasicBlock &);     // Do not implement
59   void operator=(const BasicBlock &); // Do not implement
60
61 public:
62   /// Instruction iterators...
63   typedef InstListType::iterator iterator;
64   typedef InstListType::const_iterator const_iterator;
65   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
66   typedef std::reverse_iterator<iterator>             reverse_iterator;
67
68   /// BasicBlock ctor - If the function parameter is specified, the basic block
69   /// is automatically inserted at the end of the function.
70   ///
71   BasicBlock(const std::string &Name = "", Function *Parent = 0);
72
73   /// BasicBlock ctor - If the InsertBefore parameter is specified, the basic
74   /// block is automatically inserted right before the specified block.
75   BasicBlock(const std::string &Name, BasicBlock *InsertBefore);
76   ~BasicBlock();
77
78   // Specialize setName to take care of symbol table majik
79   virtual void setName(const std::string &name, SymbolTable *ST = 0);
80
81   /// getParent - Return the enclosing method, or null if none
82   ///
83   const Function *getParent() const { return InstList.getParent(); }
84         Function *getParent()       { return InstList.getParent(); }
85
86   // getNext/Prev - Return the next or previous basic block in the list.
87         BasicBlock *getNext()       { return Next; }
88   const BasicBlock *getNext() const { return Next; }
89         BasicBlock *getPrev()       { return Prev; }
90   const BasicBlock *getPrev() const { return Prev; }
91
92   /// getTerminator() - If this is a well formed basic block, then this returns
93   /// a pointer to the terminator instruction.  If it is not, then you get a
94   /// null pointer back.
95   ///
96   TerminatorInst *getTerminator();
97   const TerminatorInst *const getTerminator() const;
98   
99   //===--------------------------------------------------------------------===//
100   /// Instruction iterator methods
101   ///
102   inline iterator                begin()       { return InstList.begin(); }
103   inline const_iterator          begin() const { return InstList.begin(); }
104   inline iterator                end  ()       { return InstList.end();   }
105   inline const_iterator          end  () const { return InstList.end();   }
106
107   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
108   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
109   inline reverse_iterator       rend  ()       { return InstList.rend();   }
110   inline const_reverse_iterator rend  () const { return InstList.rend();   }
111
112   inline unsigned                 size() const { return InstList.size(); }
113   inline bool                    empty() const { return InstList.empty(); }
114   inline const Instruction      &front() const { return InstList.front(); }
115   inline       Instruction      &front()       { return InstList.front(); }
116   inline const Instruction       &back()  const { return InstList.back(); }
117   inline       Instruction       &back()        { return InstList.back(); }
118
119   /// getInstList() - Return the underlying instruction list container.  You
120   /// need to access it directly if you want to modify it currently.
121   ///
122   const InstListType &getInstList() const { return InstList; }
123         InstListType &getInstList()       { return InstList; }
124
125   virtual void print(std::ostream &OS) const;
126
127   /// Methods for support type inquiry through isa, cast, and dyn_cast:
128   static inline bool classof(const BasicBlock *BB) { return true; }
129   static inline bool classof(const Value *V) {
130     return V->getValueType() == Value::BasicBlockVal;
131   }
132
133   /// hasConstantReferences() - This predicate is true if there is a 
134   /// reference to this basic block in the constant pool for this method.  For
135   /// example, if a block is reached through a switch table, that table resides
136   /// in the constant pool, and the basic block is reference from it.
137   ///
138   bool hasConstantReferences() const;
139
140   /// dropAllReferences() - This function causes all the subinstructions to "let
141   /// go" of all references that they are maintaining.  This allows one to
142   /// 'delete' a whole class at a time, even though there may be circular
143   /// references... first all references are dropped, and all use counts go to
144   /// zero.  Then everything is delete'd for real.  Note that no operations are
145   /// valid on an object that has "dropped all references", except operator 
146   /// delete.
147   ///
148   void dropAllReferences();
149
150   /// removePredecessor - This method is used to notify a BasicBlock that the
151   /// specified Predecessor of the block is no longer able to reach it.  This is
152   /// actually not used to update the Predecessor list, but is actually used to 
153   /// update the PHI nodes that reside in the block.  Note that this should be
154   /// called while the predecessor still refers to this block.
155   ///
156   void removePredecessor(BasicBlock *Pred);
157
158   /// splitBasicBlock - This splits a basic block into two at the specified
159   /// instruction.  Note that all instructions BEFORE the specified iterator
160   /// stay as part of the original basic block, an unconditional branch is added
161   /// to the new BB, and the rest of the instructions in the BB are moved to the
162   /// new BB, including the old terminator.  The newly formed BasicBlock is
163   /// returned.  This function invalidates the specified iterator.
164   ///
165   /// Note that this only works on well formed basic blocks (must have a 
166   /// terminator), and 'I' must not be the end of instruction list (which would
167   /// cause a degenerate basic block to be formed, having a terminator inside of
168   /// the basic block).
169   ///
170   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
171 };
172
173 #endif