add description how the ilist_traits<Instruction> works
[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 // This file contains the declaration of the BasicBlock class.
11 //
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.h"
20 #include "llvm/Support/DataTypes.h"
21
22 namespace llvm {
23
24 class TerminatorInst;
25
26 template<> struct ilist_traits<Instruction>
27   : public SymbolTableListTraits<Instruction, BasicBlock> {
28   // createSentinel is used to get hold of a node that marks the end of
29   // the list...
30   // The sentinel is relative to this instance, so we use a non-static
31   // method.
32   Instruction *createSentinel() const {
33     // since i(p)lists always publicly derive from the corresponding
34     // traits, placing a data member in this class will augment i(p)list.
35     // But since the NodeTy is expected to publicly derive from
36     // ilist_node<NodeTy>, there is a legal viable downcast from it
37     // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
38     // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
39     // forbidden (save the ilist_node<NodeTy>) so noone will ever notice
40     // the superposition.
41     return const_cast<Instruction*>(static_cast<const Instruction*>(&Sentinel));
42   }
43   static void destroySentinel(Instruction*) {}
44   static iplist<Instruction> &getList(BasicBlock *BB);
45   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
46   static int getListOffset();
47 private:
48   ilist_node<Instruction> Sentinel;
49 };
50
51 /// This represents a single basic block in LLVM. A basic block is simply a
52 /// container of instructions that execute sequentially. Basic blocks are Values
53 /// because they are referenced by instructions such as branches and switch
54 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
55 /// represents a label to which a branch can jump.
56 ///
57 /// A well formed basic block is formed of a list of non-terminating 
58 /// instructions followed by a single TerminatorInst instruction.  
59 /// TerminatorInst's may not occur in the middle of basic blocks, and must 
60 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
61 /// occur because it may be useful in the intermediate stage of constructing or
62 /// modifying a program. However, the verifier will ensure that basic blocks
63 /// are "well formed".
64 /// @brief LLVM Basic Block Representation
65 class BasicBlock : public Value, // Basic blocks are data objects also
66                    public ilist_node<BasicBlock> {
67
68 public:
69   typedef iplist<Instruction> InstListType;
70 private:
71   InstListType InstList;
72   Function *Parent;
73
74   void setParent(Function *parent);
75   friend class SymbolTableListTraits<BasicBlock, Function>;
76
77   BasicBlock(const BasicBlock &);     // Do not implement
78   void operator=(const BasicBlock &); // Do not implement
79
80   /// BasicBlock ctor - If the function parameter is specified, the basic block
81   /// is automatically inserted at either the end of the function (if
82   /// InsertBefore is null), or before the specified basic block.
83   ///
84   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
85                       BasicBlock *InsertBefore = 0);
86 public:
87   /// Instruction iterators...
88   typedef InstListType::iterator                              iterator;
89   typedef InstListType::const_iterator                  const_iterator;
90
91   /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
92   /// the basic block is automatically inserted at either the end of the
93   /// function (if InsertBefore is 0), or before the specified basic block.
94   static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
95                             BasicBlock *InsertBefore = 0) {
96     return new BasicBlock(Name, Parent, InsertBefore);
97   }
98   ~BasicBlock();
99
100   /// getParent - Return the enclosing method, or null if none
101   ///
102   const Function *getParent() const { return Parent; }
103         Function *getParent()       { return Parent; }
104
105   /// use_back - Specialize the methods defined in Value, as we know that an
106   /// BasicBlock can only be used by Instructions (specifically PHI and terms).
107   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
108   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
109   
110   /// getTerminator() - If this is a well formed basic block, then this returns
111   /// a pointer to the terminator instruction.  If it is not, then you get a
112   /// null pointer back.
113   ///
114   TerminatorInst *getTerminator();
115   const TerminatorInst *getTerminator() const;
116   
117   /// Returns a pointer to the first instructon in this block that is not a 
118   /// PHINode instruction. When adding instruction to the beginning of the
119   /// basic block, they should be added before the returned value, not before
120   /// the first instruction, which might be PHI.
121   /// Returns 0 is there's no non-PHI instruction.
122   Instruction* getFirstNonPHI();
123   const Instruction* getFirstNonPHI() const {
124     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
125   }
126   
127   /// removeFromParent - This method unlinks 'this' from the containing
128   /// function, but does not delete it.
129   ///
130   void removeFromParent();
131
132   /// eraseFromParent - This method unlinks 'this' from the containing function
133   /// and deletes it.
134   ///
135   void eraseFromParent();
136   
137   /// moveBefore - Unlink this basic block from its current function and
138   /// insert it into the function that MovePos lives in, right before MovePos.
139   void moveBefore(BasicBlock *MovePos);
140   
141   /// moveAfter - Unlink this basic block from its current function and
142   /// insert it into the function that MovePos lives in, right after MovePos.
143   void moveAfter(BasicBlock *MovePos);
144   
145
146   /// getSinglePredecessor - If this basic block has a single predecessor block,
147   /// return the block, otherwise return a null pointer.
148   BasicBlock *getSinglePredecessor();
149   const BasicBlock *getSinglePredecessor() const {
150     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
151   }
152
153   /// getUniquePredecessor - If this basic block has a unique predecessor block,
154   /// return the block, otherwise return a null pointer.
155   /// Note that unique predecessor doesn't mean single edge, there can be 
156   /// multiple edges from the unique predecessor to this block (for example 
157   /// a switch statement with multiple cases having the same destination).
158   BasicBlock *getUniquePredecessor();
159   const BasicBlock *getUniquePredecessor() const {
160     return const_cast<BasicBlock*>(this)->getUniquePredecessor();
161   }
162
163   //===--------------------------------------------------------------------===//
164   /// Instruction iterator methods
165   ///
166   inline iterator                begin()       { return InstList.begin(); }
167   inline const_iterator          begin() const { return InstList.begin(); }
168   inline iterator                end  ()       { return InstList.end();   }
169   inline const_iterator          end  () const { return InstList.end();   }
170
171   inline size_t                   size() const { return InstList.size();  }
172   inline bool                    empty() const { return InstList.empty(); }
173   inline const Instruction      &front() const { return InstList.front(); }
174   inline       Instruction      &front()       { return InstList.front(); }
175   inline const Instruction       &back() const { return InstList.back();  }
176   inline       Instruction       &back()       { return InstList.back();  }
177
178   /// getInstList() - Return the underlying instruction list container.  You
179   /// need to access it directly if you want to modify it currently.
180   ///
181   const InstListType &getInstList() const { return InstList; }
182         InstListType &getInstList()       { return InstList; }
183
184   /// Methods for support type inquiry through isa, cast, and dyn_cast:
185   static inline bool classof(const BasicBlock *) { return true; }
186   static inline bool classof(const Value *V) {
187     return V->getValueID() == Value::BasicBlockVal;
188   }
189
190   /// dropAllReferences() - This function causes all the subinstructions to "let
191   /// go" of all references that they are maintaining.  This allows one to
192   /// 'delete' a whole class at a time, even though there may be circular
193   /// references... first all references are dropped, and all use counts go to
194   /// zero.  Then everything is delete'd for real.  Note that no operations are
195   /// valid on an object that has "dropped all references", except operator
196   /// delete.
197   ///
198   void dropAllReferences();
199
200   /// removePredecessor - This method is used to notify a BasicBlock that the
201   /// specified Predecessor of the block is no longer able to reach it.  This is
202   /// actually not used to update the Predecessor list, but is actually used to
203   /// update the PHI nodes that reside in the block.  Note that this should be
204   /// called while the predecessor still refers to this block.
205   ///
206   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
207
208   /// splitBasicBlock - This splits a basic block into two at the specified
209   /// instruction.  Note that all instructions BEFORE the specified iterator
210   /// stay as part of the original basic block, an unconditional branch is added
211   /// to the original BB, and the rest of the instructions in the BB are moved
212   /// to the new BB, including the old terminator.  The newly formed BasicBlock
213   /// is returned.  This function invalidates the specified iterator.
214   ///
215   /// Note that this only works on well formed basic blocks (must have a
216   /// terminator), and 'I' must not be the end of instruction list (which would
217   /// cause a degenerate basic block to be formed, having a terminator inside of
218   /// the basic block).
219   ///
220   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
221   
222   
223   static unsigned getInstListOffset() {
224     BasicBlock *Obj = 0;
225     return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
226   }
227 };
228
229 inline int 
230 ilist_traits<Instruction>::getListOffset() {
231   return BasicBlock::getInstListOffset();
232 }
233
234 } // End llvm namespace
235
236 #endif