Added vector of machine instructions for the basic block.
[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 Def's, because they are referenced
7 // by instructions like branches and can go in switch tables and stuff...
8 //
9 // This may see wierd at first, but it's really pretty cool.  :)
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 of analysis or modification of a program.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
24
25 #include "llvm/Value.h"               // Get the definition of Value
26 #include "llvm/ValueHolder.h"
27 #include "llvm/CFGdecls.h"
28
29 class Instruction;
30 class Method;
31 class TerminatorInst;
32 class MachineCodeForBasicBlock;
33
34 class BasicBlock : public Value {       // Basic blocks are data objects also
35 public:
36   typedef ValueHolder<Instruction, BasicBlock, Method> InstListType;
37 private :
38   InstListType InstList;
39   MachineCodeForBasicBlock* machineInstrVec;
40
41   friend class ValueHolder<BasicBlock,Method,Method>;
42   void setParent(Method *parent);
43
44 public:
45   // Instruction iterators...
46   typedef InstListType::iterator iterator;
47   typedef InstListType::const_iterator const_iterator;
48   typedef reverse_iterator<const_iterator> const_reverse_iterator;
49   typedef reverse_iterator<iterator>             reverse_iterator;
50
51   typedef cfg::succ_iterator succ_iterator;   // Include CFG.h to use these
52   typedef cfg::pred_iterator pred_iterator;
53   typedef cfg::succ_const_iterator succ_const_iterator;
54   typedef cfg::pred_const_iterator pred_const_iterator;
55
56   BasicBlock(const string &Name = "", Method *Parent = 0);
57   ~BasicBlock();
58
59   // Specialize setName to take care of symbol table majik
60   virtual void setName(const string &name);
61
62   const Method *getParent() const { return InstList.getParent(); }
63         Method *getParent()       { return InstList.getParent(); }
64
65   // getTerminator() - If this is a well formed basic block, then this returns
66   // a pointer to the terminator instruction.  If it is not, then you get a null
67   // pointer back.
68   //
69   TerminatorInst *getTerminator();
70   const TerminatorInst *const getTerminator() const;
71
72   
73   // Machine code accessor...
74   inline MachineCodeForBasicBlock& getMachineInstrVec() const {
75     return *machineInstrVec; 
76   }
77   
78   //===--------------------------------------------------------------------===//
79   // Instruction iterator methods
80   inline iterator                begin()       { return InstList.begin(); }
81   inline const_iterator          begin() const { return InstList.begin(); }
82   inline iterator                end  ()       { return InstList.end();   }
83   inline const_iterator          end  () const { return InstList.end();   }
84
85   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
86   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
87   inline reverse_iterator       rend  ()       { return InstList.rend();   }
88   inline const_reverse_iterator rend  () const { return InstList.rend();   }
89
90   inline unsigned                 size() const { return InstList.size(); }
91   inline bool                    empty() const { return InstList.empty(); }
92   inline const Instruction      *front() const { return InstList.front(); }
93   inline       Instruction      *front()       { return InstList.front(); }
94   inline const Instruction       *back()  const { return InstList.back(); }
95   inline       Instruction       *back()        { return InstList.back(); }
96
97   // getInstList() - Return the underlying instruction list container.  You need
98   // to access it directly if you want to modify it currently.
99   //
100   const InstListType &getInstList() const { return InstList; }
101         InstListType &getInstList()       { return InstList; }
102
103   // hasConstantPoolReferences() - This predicate is true if there is a 
104   // reference to this basic block in the constant pool for this method.  For
105   // example, if a block is reached through a switch table, that table resides
106   // in the constant pool, and the basic block is reference from it.
107   //
108   bool hasConstantPoolReferences() const;
109
110   // dropAllReferences() - This function causes all the subinstructions to "let
111   // go" of all references that they are maintaining.  This allows one to
112   // 'delete' a whole class at a time, even though there may be circular
113   // references... first all references are dropped, and all use counts go to
114   // zero.  Then everything is delete'd for real.  Note that no operations are
115   // valid on an object that has "dropped all references", except operator 
116   // delete.
117   //
118   void dropAllReferences();
119
120   // removePredecessor - This method is used to notify a BasicBlock that the
121   // specified Predecessor of the block is no longer able to reach it.  This is
122   // actually not used to update the Predecessor list, but is actually used to 
123   // update the PHI nodes that reside in the block.  Note that this should be
124   // called while the predecessor still refers to this block.
125   //
126   void removePredecessor(BasicBlock *Pred);
127
128   // splitBasicBlock - This splits a basic block into two at the specified
129   // instruction.  Note that all instructions BEFORE the specified iterator stay
130   // as part of the original basic block, an unconditional branch is added to 
131   // the new BB, and the rest of the instructions in the BB are moved to the new
132   // BB, including the old terminator.  The newly formed BasicBlock is returned.
133   // This function invalidates the specified iterator.
134   //
135   // Note that this only works on well formed basic blocks (must have a 
136   // terminator), and 'I' must not be the end of instruction list (which would
137   // cause a degenerate basic block to be formed, having a terminator inside of
138   // the basic block).
139   //
140   BasicBlock *splitBasicBlock(iterator I);
141 };
142
143 #endif