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