Improve support for type-generic vector intrinsics by teaching TableGen how
[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 create a node that marks the end of the list...
29   static Instruction *createSentinel();
30   static void destroySentinel(Instruction *I) { delete I; }
31   static iplist<Instruction> &getList(BasicBlock *BB);
32   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
33   static int getListOffset();
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 ilist_node<BasicBlock> {
52 public:
53   typedef iplist<Instruction> InstListType;
54 private :
55   InstListType InstList;
56   Function *Parent;
57
58   void setParent(Function *parent);
59   friend class SymbolTableListTraits<BasicBlock, Function>;
60
61   BasicBlock(const BasicBlock &);     // Do not implement
62   void operator=(const BasicBlock &); // Do not implement
63
64   /// BasicBlock ctor - If the function parameter is specified, the basic block
65   /// is automatically inserted at either the end of the function (if
66   /// InsertBefore is null), or before the specified basic block.
67   ///
68   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
69                       BasicBlock *InsertBefore = 0);
70 public:
71   /// Instruction iterators...
72   typedef InstListType::iterator                              iterator;
73   typedef InstListType::const_iterator                  const_iterator;
74
75   /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
76   /// the basic block is automatically inserted at either the end of the
77   /// function (if InsertBefore is 0), or before the specified basic block.
78   static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
79                             BasicBlock *InsertBefore = 0) {
80     return new BasicBlock(Name, Parent, InsertBefore);
81   }
82   ~BasicBlock();
83
84   /// getParent - Return the enclosing method, or null if none
85   ///
86   const Function *getParent() const { return Parent; }
87         Function *getParent()       { return Parent; }
88
89   /// use_back - Specialize the methods defined in Value, as we know that an
90   /// BasicBlock can only be used by Instructions (specifically PHI and terms).
91   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
92   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
93   
94   /// getTerminator() - If this is a well formed basic block, then this returns
95   /// a pointer to the terminator instruction.  If it is not, then you get a
96   /// null pointer back.
97   ///
98   TerminatorInst *getTerminator();
99   const TerminatorInst *getTerminator() const;
100   
101   /// Returns a pointer to the first instructon in this block that is not a 
102   /// PHINode instruction. When adding instruction to the beginning of the
103   /// basic block, they should be added before the returned value, not before
104   /// the first instruction, which might be PHI.
105   /// Returns 0 is there's no non-PHI instruction.
106   Instruction* getFirstNonPHI();
107   const Instruction* getFirstNonPHI() const {
108     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
109   }
110   
111   /// removeFromParent - This method unlinks 'this' from the containing
112   /// function, but does not delete it.
113   ///
114   void removeFromParent();
115
116   /// eraseFromParent - This method unlinks 'this' from the containing function
117   /// and deletes it.
118   ///
119   void eraseFromParent();
120   
121   /// moveBefore - Unlink this basic block from its current function and
122   /// insert it into the function that MovePos lives in, right before MovePos.
123   void moveBefore(BasicBlock *MovePos);
124   
125   /// moveAfter - Unlink this basic block from its current function and
126   /// insert it into the function that MovePos lives in, right after MovePos.
127   void moveAfter(BasicBlock *MovePos);
128   
129
130   /// getSinglePredecessor - If this basic block has a single predecessor block,
131   /// return the block, otherwise return a null pointer.
132   BasicBlock *getSinglePredecessor();
133   const BasicBlock *getSinglePredecessor() const {
134     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
135   }
136
137   /// getUniquePredecessor - If this basic block has a unique predecessor block,
138   /// return the block, otherwise return a null pointer.
139   /// Note that unique predecessor doesn't mean single edge, there can be 
140   /// multiple edges from the unique predecessor to this block (for example 
141   /// a switch statement with multiple cases having the same destination).
142   BasicBlock *getUniquePredecessor();
143   const BasicBlock *getUniquePredecessor() const {
144     return const_cast<BasicBlock*>(this)->getUniquePredecessor();
145   }
146
147   //===--------------------------------------------------------------------===//
148   /// Instruction iterator methods
149   ///
150   inline iterator                begin()       { return InstList.begin(); }
151   inline const_iterator          begin() const { return InstList.begin(); }
152   inline iterator                end  ()       { return InstList.end();   }
153   inline const_iterator          end  () const { return InstList.end();   }
154
155   inline size_t                   size() const { return InstList.size();  }
156   inline bool                    empty() const { return InstList.empty(); }
157   inline const Instruction      &front() const { return InstList.front(); }
158   inline       Instruction      &front()       { return InstList.front(); }
159   inline const Instruction       &back() const { return InstList.back();  }
160   inline       Instruction       &back()       { return InstList.back();  }
161
162   /// getInstList() - Return the underlying instruction list container.  You
163   /// need to access it directly if you want to modify it currently.
164   ///
165   const InstListType &getInstList() const { return InstList; }
166         InstListType &getInstList()       { return InstList; }
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   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
191
192   /// splitBasicBlock - This splits a basic block into two at the specified
193   /// instruction.  Note that all instructions BEFORE the specified iterator
194   /// stay as part of the original basic block, an unconditional branch is added
195   /// to the original BB, and the rest of the instructions in the BB are moved
196   /// to the new BB, including the old terminator.  The newly formed BasicBlock
197   /// is returned.  This function invalidates the specified iterator.
198   ///
199   /// Note that this only works on well formed basic blocks (must have a
200   /// terminator), and 'I' must not be the end of instruction list (which would
201   /// cause a degenerate basic block to be formed, having a terminator inside of
202   /// the basic block).
203   ///
204   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
205   
206   
207   static unsigned getInstListOffset() {
208     BasicBlock *Obj = 0;
209     return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
210   }
211 };
212
213 inline int 
214 ilist_traits<Instruction>::getListOffset() {
215   return BasicBlock::getInstListOffset();
216 }
217
218 } // End llvm namespace
219
220 #endif