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"
29 class MachineCodeForBasicBlock;
30 template <class _Term, class _BB> class SuccIterator; // Successor Iterator
31 template <class _Ptr, class _USE_iterator> class PredIterator;
33 class BasicBlock : public Value { // Basic blocks are data objects also
35 typedef ValueHolder<Instruction, BasicBlock, Method> InstListType;
37 InstListType InstList;
38 MachineCodeForBasicBlock* machineInstrVec;
40 friend class ValueHolder<BasicBlock,Method,Method>;
41 void setParent(Method *parent);
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;
51 BasicBlock(const std::string &Name = "", Method *Parent = 0);
54 // Specialize setName to take care of symbol table majik
55 virtual void setName(const std::string &name, SymbolTable *ST = 0);
57 // getParent - Return the enclosing method, or null if none
58 const Method *getParent() const { return InstList.getParent(); }
59 Method *getParent() { return InstList.getParent(); }
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
65 TerminatorInst *getTerminator();
66 const TerminatorInst *const getTerminator() const;
68 // Machine code accessor...
69 inline MachineCodeForBasicBlock& getMachineInstrVec() const {
70 return *machineInstrVec;
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;
78 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
79 typedef SuccIterator<const TerminatorInst*,
80 const BasicBlock> succ_const_iterator;
83 //===--------------------------------------------------------------------===//
84 // Instruction iterator methods
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(); }
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(); }
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(); }
103 // getInstList() - Return the underlying instruction list container. You need
104 // to access it directly if you want to modify it currently.
106 const InstListType &getInstList() const { return InstList; }
107 InstListType &getInstList() { return InstList; }
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);