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