1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*--=//
3 // This file contains the declaration of the BasicBlock class, which represents
4 // a single basic block in the VM.
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...
9 //===----------------------------------------------------------------------===//
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.
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_BASICBLOCK_H
21 #define LLVM_BASICBLOCK_H
23 #include "llvm/ValueHolder.h"
24 #include "llvm/Value.h"
27 class MachineCodeForBasicBlock;
28 template <class _Term, class _BB> class SuccIterator; // Successor Iterator
29 template <class _Ptr, class _USE_iterator> class PredIterator;
31 class BasicBlock : public Value { // Basic blocks are data objects also
33 typedef ValueHolder<Instruction, BasicBlock, Function> InstListType;
35 InstListType InstList;
36 MachineCodeForBasicBlock* machineInstrVec;
38 friend class ValueHolder<BasicBlock,Function,Function>;
39 void setParent(Function *parent);
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;
49 BasicBlock(const std::string &Name = "", Function *Parent = 0);
52 // Specialize setName to take care of symbol table majik
53 virtual void setName(const std::string &name, SymbolTable *ST = 0);
55 // getParent - Return the enclosing method, or null if none
56 const Function *getParent() const { return InstList.getParent(); }
57 Function *getParent() { return InstList.getParent(); }
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
63 TerminatorInst *getTerminator();
64 const TerminatorInst *const getTerminator() const;
66 // Machine code accessor...
67 inline MachineCodeForBasicBlock& getMachineInstrVec() const {
68 return *machineInstrVec;
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;
76 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
77 typedef SuccIterator<const TerminatorInst*,
78 const BasicBlock> succ_const_iterator;
81 //===--------------------------------------------------------------------===//
82 // Instruction iterator methods
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(); }
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(); }
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(); }
101 // getInstList() - Return the underlying instruction list container. You need
102 // to access it directly if you want to modify it currently.
104 const InstListType &getInstList() const { return InstList; }
105 InstListType &getInstList() { return InstList; }
107 virtual void print(std::ostream &OS) const;
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;
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.
120 bool hasConstantReferences() const;
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
130 void dropAllReferences();
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.
138 void removePredecessor(BasicBlock *Pred);
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.
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
152 BasicBlock *splitBasicBlock(iterator I);