Moved enum and command-line option in separate file. Also added function that returns...
[oota-llvm.git] / include / llvm / BasicBlock.h
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
2 ///
3 /// \class BasicBlock
4 ///
5 /// This file contains the declaration of the BasicBlock class, which represents
6 /// a single basic block in the VM.
7 ///
8 /// Note that basic blocks themselves are Value's, because they are referenced
9 /// by instructions like branches and can go in switch tables and stuff...
10 ///
11 ///===---------------------------------------------------------------------===//
12 ///
13 /// Note that well formed basic blocks are formed of a list of instructions 
14 /// followed by a single TerminatorInst instruction.  TerminatorInst's may not
15 /// occur in the middle of basic blocks, and must terminate the blocks.
16 ///
17 /// This code allows malformed basic blocks to occur, because it may be useful
18 /// in the intermediate stage modification to a program.
19 ///
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
24
25 #include "llvm/Instruction.h"
26 #include "llvm/SymbolTableListTraits.h"
27 #include "Support/ilist"
28
29 class TerminatorInst;
30 template <class _Term, class _BB> class SuccIterator;  // Successor Iterator
31 template <class _Ptr, class _USE_iterator> class PredIterator;
32
33 template<> struct ilist_traits<Instruction>
34   : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
35   // createNode is used to create a node that marks the end of the list...
36   static Instruction *createNode();
37   static iplist<Instruction> &getList(BasicBlock *BB);
38 };
39
40 struct BasicBlock : public Value {       // Basic blocks are data objects also
41   typedef iplist<Instruction> InstListType;
42 private :
43   InstListType InstList;
44   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
45
46   void setParent(Function *parent);
47   void setNext(BasicBlock *N) { Next = N; }
48   void setPrev(BasicBlock *N) { Prev = N; }
49   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
50
51   BasicBlock(const BasicBlock &);     // Do not implement
52   void operator=(const BasicBlock &); // Do not implement
53
54 public:
55   /// Instruction iterators...
56   typedef InstListType::iterator iterator;
57   typedef InstListType::const_iterator const_iterator;
58   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
59   typedef std::reverse_iterator<iterator>             reverse_iterator;
60
61   /// BasicBlock ctor - If the function parameter is specified, the basic block
62   /// is automatically inserted at the end of the function.
63   ///
64   BasicBlock(const std::string &Name = "", Function *Parent = 0);
65
66   /// BasicBlock ctor - If the InsertBefore parameter is specified, the basic
67   /// block is automatically inserted right before the specified block.
68   BasicBlock(const std::string &Name, BasicBlock *InsertBefore);
69   ~BasicBlock();
70
71   // Specialize setName to take care of symbol table majik
72   virtual void setName(const std::string &name, SymbolTable *ST = 0);
73
74   /// getParent - Return the enclosing method, or null if none
75   ///
76   const Function *getParent() const { return InstList.getParent(); }
77         Function *getParent()       { return InstList.getParent(); }
78
79   // getNext/Prev - Return the next or previous basic block in the list.
80         BasicBlock *getNext()       { return Next; }
81   const BasicBlock *getNext() const { return Next; }
82         BasicBlock *getPrev()       { return Prev; }
83   const BasicBlock *getPrev() const { return Prev; }
84
85   /// getTerminator() - If this is a well formed basic block, then this returns
86   /// a pointer to the terminator instruction.  If it is not, then you get a
87   /// null pointer back.
88   ///
89   TerminatorInst *getTerminator();
90   const TerminatorInst *const getTerminator() const;
91   
92   //===--------------------------------------------------------------------===//
93   /// Instruction iterator methods
94   ///
95   inline iterator                begin()       { return InstList.begin(); }
96   inline const_iterator          begin() const { return InstList.begin(); }
97   inline iterator                end  ()       { return InstList.end();   }
98   inline const_iterator          end  () const { return InstList.end();   }
99
100   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
101   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
102   inline reverse_iterator       rend  ()       { return InstList.rend();   }
103   inline const_reverse_iterator rend  () const { return InstList.rend();   }
104
105   inline unsigned                 size() const { return InstList.size(); }
106   inline bool                    empty() const { return InstList.empty(); }
107   inline const Instruction      &front() const { return InstList.front(); }
108   inline       Instruction      &front()       { return InstList.front(); }
109   inline const Instruction       &back()  const { return InstList.back(); }
110   inline       Instruction       &back()        { return InstList.back(); }
111
112   /// getInstList() - Return the underlying instruction list container.  You
113   /// need to access it directly if you want to modify it currently.
114   ///
115   const InstListType &getInstList() const { return InstList; }
116         InstListType &getInstList()       { return InstList; }
117
118   virtual void print(std::ostream &OS) const;
119
120   /// Methods for support type inquiry through isa, cast, and dyn_cast:
121   static inline bool classof(const BasicBlock *BB) { return true; }
122   static inline bool classof(const Value *V) {
123     return V->getValueType() == Value::BasicBlockVal;
124   }
125
126   /// hasConstantReferences() - This predicate is true if there is a 
127   /// reference to this basic block in the constant pool for this method.  For
128   /// example, if a block is reached through a switch table, that table resides
129   /// in the constant pool, and the basic block is reference from it.
130   ///
131   bool hasConstantReferences() const;
132
133   /// dropAllReferences() - This function causes all the subinstructions to "let
134   /// go" of all references that they are maintaining.  This allows one to
135   /// 'delete' a whole class at a time, even though there may be circular
136   /// references... first all references are dropped, and all use counts go to
137   /// zero.  Then everything is delete'd for real.  Note that no operations are
138   /// valid on an object that has "dropped all references", except operator 
139   /// delete.
140   ///
141   void dropAllReferences();
142
143   /// removePredecessor - This method is used to notify a BasicBlock that the
144   /// specified Predecessor of the block is no longer able to reach it.  This is
145   /// actually not used to update the Predecessor list, but is actually used to 
146   /// update the PHI nodes that reside in the block.  Note that this should be
147   /// called while the predecessor still refers to this block.
148   ///
149   void removePredecessor(BasicBlock *Pred);
150
151   /// splitBasicBlock - This splits a basic block into two at the specified
152   /// instruction.  Note that all instructions BEFORE the specified iterator
153   /// stay as part of the original basic block, an unconditional branch is added
154   /// to the new BB, and the rest of the instructions in the BB are moved to the
155   /// new BB, including the old terminator.  The newly formed BasicBlock is
156   /// returned.  This function invalidates the specified iterator.
157   ///
158   /// Note that this only works on well formed basic blocks (must have a 
159   /// terminator), and 'I' must not be the end of instruction list (which would
160   /// cause a degenerate basic block to be formed, having a terminator inside of
161   /// the basic block).
162   ///
163   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
164 };
165
166 #endif