This patch (written by Reid) changes compressor to never throw an exception.
[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 //
11 // This file contains the declaration of the BasicBlock class.
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"
20
21 namespace llvm {
22
23 class TerminatorInst;
24 template <class Term, class BB> class SuccIterator;  // Successor Iterator
25 template <class Ptr, class USE_iterator> class PredIterator;
26
27 template<> struct ilist_traits<Instruction>
28   : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
29   // createSentinel is used to create a node that marks the end of the list...
30   static Instruction *createSentinel();
31   static void destroySentinel(Instruction *I) { delete I; }
32   static iplist<Instruction> &getList(BasicBlock *BB);
33 };
34
35 /// This represents a single basic block in LLVM. A basic block is simply a
36 /// container of instructions that execute sequentially. Basic blocks are Values
37 /// because they are referenced by instructions such as branches and switch
38 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
39 /// represents a label to which a branch can jump.
40 ///
41 /// A well formed basic block is formed of a list of non-terminating 
42 /// instructions followed by a single TerminatorInst instruction.  
43 /// TerminatorInst's may not occur in the middle of basic blocks, and must 
44 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
45 /// occur because it may be useful in the intermediate stage of constructing or
46 /// modifying a program. However, the verifier will ensure that basic blocks
47 /// are "well formed".
48 /// @brief LLVM Basic Block Representation
49 class BasicBlock : public Value {       // Basic blocks are data objects also
50 public:
51   typedef iplist<Instruction> InstListType;
52 private :
53   InstListType InstList;
54   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
55
56   void setParent(Function *parent);
57   void setNext(BasicBlock *N) { Next = N; }
58   void setPrev(BasicBlock *N) { Prev = N; }
59   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
60
61   BasicBlock(const BasicBlock &);     // Do not implement
62   void operator=(const BasicBlock &); // Do not implement
63
64 public:
65   /// Instruction iterators...
66   typedef InstListType::iterator                              iterator;
67   typedef InstListType::const_iterator                  const_iterator;
68
69   /// BasicBlock ctor - If the function parameter is specified, the basic block
70   /// is automatically inserted at either the end of the function (if
71   /// InsertBefore is null), or before the specified basic block.
72   ///
73   BasicBlock(const std::string &Name = "", Function *Parent = 0,
74              BasicBlock *InsertBefore = 0);
75   ~BasicBlock();
76
77   /// getParent - Return the enclosing method, or null if none
78   ///
79   const Function *getParent() const { return InstList.getParent(); }
80         Function *getParent()       { return InstList.getParent(); }
81
82   // getNext/Prev - Return the next or previous basic block in the list.
83         BasicBlock *getNext()       { return Next; }
84   const BasicBlock *getNext() const { return Next; }
85         BasicBlock *getPrev()       { return Prev; }
86   const BasicBlock *getPrev() const { return Prev; }
87
88   /// getTerminator() - If this is a well formed basic block, then this returns
89   /// a pointer to the terminator instruction.  If it is not, then you get a
90   /// null pointer back.
91   ///
92   TerminatorInst *getTerminator();
93   const TerminatorInst *const getTerminator() const;
94   
95   /// Returns a pointer to the first instructon in this block that is not a 
96   /// PHINode instruction. When adding instruction to the beginning of the
97   /// basic block, they should be added before the returned value, not before
98   /// the first instruction, which might be PHI.
99   /// Returns 0 is there's no non-PHI instruction.
100   Instruction* getFirstNonPHI();
101   
102   /// removeFromParent - This method unlinks 'this' from the containing
103   /// function, but does not delete it.
104   ///
105   void removeFromParent();
106
107   /// eraseFromParent - This method unlinks 'this' from the containing function
108   /// and deletes it.
109   ///
110   void eraseFromParent();
111   
112   /// moveBefore - Unlink this instruction from its current function and
113   /// insert it into the function that MovePos lives in, right before
114   /// MovePos.
115   void moveBefore(BasicBlock *MovePos);
116
117   /// getSinglePredecessor - If this basic block has a single predecessor block,
118   /// return the block, otherwise return a null pointer.
119   BasicBlock *getSinglePredecessor();
120   const BasicBlock *getSinglePredecessor() const {
121     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
122   }
123
124   //===--------------------------------------------------------------------===//
125   /// Instruction iterator methods
126   ///
127   inline iterator                begin()       { return InstList.begin(); }
128   inline const_iterator          begin() const { return InstList.begin(); }
129   inline iterator                end  ()       { return InstList.end();   }
130   inline const_iterator          end  () const { return InstList.end();   }
131
132   inline size_t                   size() const { return InstList.size();  }
133   inline bool                    empty() const { return InstList.empty(); }
134   inline const Instruction      &front() const { return InstList.front(); }
135   inline       Instruction      &front()       { return InstList.front(); }
136   inline const Instruction       &back() const { return InstList.back();  }
137   inline       Instruction       &back()       { return InstList.back();  }
138
139   /// getInstList() - Return the underlying instruction list container.  You
140   /// need to access it directly if you want to modify it currently.
141   ///
142   const InstListType &getInstList() const { return InstList; }
143         InstListType &getInstList()       { return InstList; }
144
145   virtual void print(std::ostream &OS) const { print(OS, 0); }
146   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
147
148   /// Methods for support type inquiry through isa, cast, and dyn_cast:
149   static inline bool classof(const BasicBlock *) { return true; }
150   static inline bool classof(const Value *V) {
151     return V->getValueType() == Value::BasicBlockVal;
152   }
153
154   /// dropAllReferences() - This function causes all the subinstructions to "let
155   /// go" of all references that they are maintaining.  This allows one to
156   /// 'delete' a whole class at a time, even though there may be circular
157   /// references... first all references are dropped, and all use counts go to
158   /// zero.  Then everything is delete'd for real.  Note that no operations are
159   /// valid on an object that has "dropped all references", except operator
160   /// delete.
161   ///
162   void dropAllReferences();
163
164   /// removePredecessor - This method is used to notify a BasicBlock that the
165   /// specified Predecessor of the block is no longer able to reach it.  This is
166   /// actually not used to update the Predecessor list, but is actually used to
167   /// update the PHI nodes that reside in the block.  Note that this should be
168   /// called while the predecessor still refers to this block.
169   ///
170   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
171
172   /// splitBasicBlock - This splits a basic block into two at the specified
173   /// instruction.  Note that all instructions BEFORE the specified iterator
174   /// stay as part of the original basic block, an unconditional branch is added
175   /// to the original BB, and the rest of the instructions in the BB are moved
176   /// to the new BB, including the old terminator.  The newly formed BasicBlock
177   /// is returned.  This function invalidates the specified iterator.
178   ///
179   /// Note that this only works on well formed basic blocks (must have a
180   /// terminator), and 'I' must not be the end of instruction list (which would
181   /// cause a degenerate basic block to be formed, having a terminator inside of
182   /// the basic block).
183   ///
184   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
185 };
186
187 } // End llvm namespace
188
189 #endif