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/Instruction.h"
24 #include "llvm/SymbolTableListTraits.h"
25 #include "Support/ilist"
28 template <class _Term, class _BB> class SuccIterator; // Successor Iterator
29 template <class _Ptr, class _USE_iterator> class PredIterator;
31 template<> struct ilist_traits<Instruction>
32 : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
33 // createNode is used to create a node that marks the end of the list...
34 static Instruction *createNode();
35 static iplist<Instruction> &getList(BasicBlock *BB);
38 class BasicBlock : public Value { // Basic blocks are data objects also
40 typedef iplist<Instruction> InstListType;
42 InstListType InstList;
43 BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
45 void setParent(Function *parent) { InstList.setParent(parent); }
46 void setNext(BasicBlock *N) { Next = N; }
47 void setPrev(BasicBlock *N) { Prev = N; }
48 friend class SymbolTableListTraits<BasicBlock, Function, Function>;
50 BasicBlock(const BasicBlock &); // Do not implement
51 void operator=(const BasicBlock &); // Do not implement
54 // Instruction iterators...
55 typedef InstListType::iterator iterator;
56 typedef InstListType::const_iterator const_iterator;
57 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
58 typedef std::reverse_iterator<iterator> reverse_iterator;
61 BasicBlock(const std::string &Name = "", Function *Parent = 0);
64 // Specialize setName to take care of symbol table majik
65 virtual void setName(const std::string &name, SymbolTable *ST = 0);
67 // getParent - Return the enclosing method, or null if none
68 const Function *getParent() const { return InstList.getParent(); }
69 Function *getParent() { return InstList.getParent(); }
71 // getNext/Prev - Return the next or previous basic block in the list.
72 BasicBlock *getNext() { return Next; }
73 const BasicBlock *getNext() const { return Next; }
74 BasicBlock *getPrev() { return Prev; }
75 const BasicBlock *getPrev() const { return Prev; }
77 // getTerminator() - If this is a well formed basic block, then this returns
78 // a pointer to the terminator instruction. If it is not, then you get a null
81 TerminatorInst *getTerminator();
82 const TerminatorInst *const getTerminator() const;
84 // Provide a scoped predecessor and successor iterator
85 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
86 typedef PredIterator<const BasicBlock,
87 Value::use_const_iterator> pred_const_iterator;
89 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
90 typedef SuccIterator<const TerminatorInst*,
91 const BasicBlock> succ_const_iterator;
94 //===--------------------------------------------------------------------===//
95 // Instruction iterator methods
97 inline iterator begin() { return InstList.begin(); }
98 inline const_iterator begin() const { return InstList.begin(); }
99 inline iterator end () { return InstList.end(); }
100 inline const_iterator end () const { return InstList.end(); }
102 inline reverse_iterator rbegin() { return InstList.rbegin(); }
103 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
104 inline reverse_iterator rend () { return InstList.rend(); }
105 inline const_reverse_iterator rend () const { return InstList.rend(); }
107 inline unsigned size() const { return InstList.size(); }
108 inline bool empty() const { return InstList.empty(); }
109 inline const Instruction &front() const { return InstList.front(); }
110 inline Instruction &front() { return InstList.front(); }
111 inline const Instruction &back() const { return InstList.back(); }
112 inline Instruction &back() { return InstList.back(); }
114 // getInstList() - Return the underlying instruction list container. You need
115 // to access it directly if you want to modify it currently.
117 const InstListType &getInstList() const { return InstList; }
118 InstListType &getInstList() { return InstList; }
120 virtual void print(std::ostream &OS) const;
122 // Methods for support type inquiry through isa, cast, and dyn_cast:
123 static inline bool classof(const BasicBlock *BB) { return true; }
124 static inline bool classof(const Value *V) {
125 return V->getValueType() == Value::BasicBlockVal;
128 // hasConstantReferences() - This predicate is true if there is a
129 // reference to this basic block in the constant pool for this method. For
130 // example, if a block is reached through a switch table, that table resides
131 // in the constant pool, and the basic block is reference from it.
133 bool hasConstantReferences() const;
135 // dropAllReferences() - This function causes all the subinstructions to "let
136 // go" of all references that they are maintaining. This allows one to
137 // 'delete' a whole class at a time, even though there may be circular
138 // references... first all references are dropped, and all use counts go to
139 // zero. Then everything is delete'd for real. Note that no operations are
140 // valid on an object that has "dropped all references", except operator
143 void dropAllReferences();
145 // removePredecessor - This method is used to notify a BasicBlock that the
146 // specified Predecessor of the block is no longer able to reach it. This is
147 // actually not used to update the Predecessor list, but is actually used to
148 // update the PHI nodes that reside in the block. Note that this should be
149 // called while the predecessor still refers to this block.
151 void removePredecessor(BasicBlock *Pred);
153 // splitBasicBlock - This splits a basic block into two at the specified
154 // instruction. Note that all instructions BEFORE the specified iterator stay
155 // as part of the original basic block, an unconditional branch is added to
156 // the new BB, and the rest of the instructions in the BB are moved to the new
157 // BB, including the old terminator. The newly formed BasicBlock is returned.
158 // This function invalidates the specified iterator.
160 // Note that this only works on well formed basic blocks (must have a
161 // terminator), and 'I' must not be the end of instruction list (which would
162 // cause a degenerate basic block to be formed, having a terminator inside of
165 BasicBlock *splitBasicBlock(iterator I);