Move SLEB/ULEB size calculation routines from AsmPrinter to TargetAsmInfo. This makes...
[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 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 Value, // Basic blocks are data objects also
53                    public ilist_node<BasicBlock> {
54 public:
55   typedef iplist<Instruction> InstListType;
56 private :
57   InstListType InstList;
58   Function *Parent;
59
60   void setParent(Function *parent);
61   friend class SymbolTableListTraits<BasicBlock, Function>;
62
63   BasicBlock(const BasicBlock &);     // Do not implement
64   void operator=(const BasicBlock &); // Do not implement
65
66   /// BasicBlock ctor - If the function parameter is specified, the basic block
67   /// is automatically inserted at either the end of the function (if
68   /// InsertBefore is null), or before the specified basic block.
69   ///
70   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
71                       BasicBlock *InsertBefore = 0);
72 public:
73   /// Instruction iterators...
74   typedef InstListType::iterator                              iterator;
75   typedef InstListType::const_iterator                  const_iterator;
76
77   /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
78   /// the basic block is automatically inserted at either the end of the
79   /// function (if InsertBefore is 0), or before the specified basic block.
80   static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
81                             BasicBlock *InsertBefore = 0) {
82     return new BasicBlock(Name, Parent, InsertBefore);
83   }
84   ~BasicBlock();
85
86   /// getParent - Return the enclosing method, or null if none
87   ///
88   const Function *getParent() const { return Parent; }
89         Function *getParent()       { return Parent; }
90
91   /// use_back - Specialize the methods defined in Value, as we know that an
92   /// BasicBlock can only be used by Instructions (specifically PHI and terms).
93   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
94   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
95   
96   /// getTerminator() - If this is a well formed basic block, then this returns
97   /// a pointer to the terminator instruction.  If it is not, then you get a
98   /// null pointer back.
99   ///
100   TerminatorInst *getTerminator();
101   const TerminatorInst *getTerminator() const;
102   
103   /// Returns a pointer to the first instructon in this block that is not a 
104   /// PHINode instruction. When adding instruction to the beginning of the
105   /// basic block, they should be added before the returned value, not before
106   /// the first instruction, which might be PHI.
107   /// Returns 0 is there's no non-PHI instruction.
108   Instruction* getFirstNonPHI();
109   const Instruction* getFirstNonPHI() const {
110     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
111   }
112   
113   /// removeFromParent - This method unlinks 'this' from the containing
114   /// function, but does not delete it.
115   ///
116   void removeFromParent();
117
118   /// eraseFromParent - This method unlinks 'this' from the containing function
119   /// and deletes it.
120   ///
121   void eraseFromParent();
122   
123   /// moveBefore - Unlink this basic block from its current function and
124   /// insert it into the function that MovePos lives in, right before MovePos.
125   void moveBefore(BasicBlock *MovePos);
126   
127   /// moveAfter - Unlink this basic block from its current function and
128   /// insert it into the function that MovePos lives in, right after MovePos.
129   void moveAfter(BasicBlock *MovePos);
130   
131
132   /// getSinglePredecessor - If this basic block has a single predecessor block,
133   /// return the block, otherwise return a null pointer.
134   BasicBlock *getSinglePredecessor();
135   const BasicBlock *getSinglePredecessor() const {
136     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
137   }
138
139   //===--------------------------------------------------------------------===//
140   /// Instruction iterator methods
141   ///
142   inline iterator                begin()       { return InstList.begin(); }
143   inline const_iterator          begin() const { return InstList.begin(); }
144   inline iterator                end  ()       { return InstList.end();   }
145   inline const_iterator          end  () const { return InstList.end();   }
146
147   inline size_t                   size() const { return InstList.size();  }
148   inline bool                    empty() const { return InstList.empty(); }
149   inline const Instruction      &front() const { return InstList.front(); }
150   inline       Instruction      &front()       { return InstList.front(); }
151   inline const Instruction       &back() const { return InstList.back();  }
152   inline       Instruction       &back()       { return InstList.back();  }
153
154   /// getInstList() - Return the underlying instruction list container.  You
155   /// need to access it directly if you want to modify it currently.
156   ///
157   const InstListType &getInstList() const { return InstList; }
158         InstListType &getInstList()       { return InstList; }
159
160   virtual void print(std::ostream &OS) const { print(OS, 0); }
161   void print(std::ostream *OS) const { if (OS) print(*OS); }
162   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
163
164   /// Methods for support type inquiry through isa, cast, and dyn_cast:
165   static inline bool classof(const BasicBlock *) { return true; }
166   static inline bool classof(const Value *V) {
167     return V->getValueID() == Value::BasicBlockVal;
168   }
169
170   /// dropAllReferences() - This function causes all the subinstructions to "let
171   /// go" of all references that they are maintaining.  This allows one to
172   /// 'delete' a whole class at a time, even though there may be circular
173   /// references... first all references are dropped, and all use counts go to
174   /// zero.  Then everything is delete'd for real.  Note that no operations are
175   /// valid on an object that has "dropped all references", except operator
176   /// delete.
177   ///
178   void dropAllReferences();
179
180   /// removePredecessor - This method is used to notify a BasicBlock that the
181   /// specified Predecessor of the block is no longer able to reach it.  This is
182   /// actually not used to update the Predecessor list, but is actually used to
183   /// update the PHI nodes that reside in the block.  Note that this should be
184   /// called while the predecessor still refers to this block.
185   ///
186   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
187
188   /// splitBasicBlock - This splits a basic block into two at the specified
189   /// instruction.  Note that all instructions BEFORE the specified iterator
190   /// stay as part of the original basic block, an unconditional branch is added
191   /// to the original BB, and the rest of the instructions in the BB are moved
192   /// to the new BB, including the old terminator.  The newly formed BasicBlock
193   /// is returned.  This function invalidates the specified iterator.
194   ///
195   /// Note that this only works on well formed basic blocks (must have a
196   /// terminator), and 'I' must not be the end of instruction list (which would
197   /// cause a degenerate basic block to be formed, having a terminator inside of
198   /// the basic block).
199   ///
200   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
201   
202   
203   static unsigned getInstListOffset() {
204     BasicBlock *Obj = 0;
205     return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
206   }
207 };
208
209 inline int 
210 ilist_traits<Instruction>::getListOffset() {
211   return BasicBlock::getInstListOffset();
212 }
213
214 } // End llvm namespace
215
216 #endif