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