1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*--=//
5 /// This file contains the declaration of the BasicBlock class, which represents
6 /// a single basic block in the VM.
8 /// Note that basic blocks themselves are Value's, because they are referenced
9 /// by instructions like branches and can go in switch tables and stuff...
11 ///===---------------------------------------------------------------------===//
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.
17 /// This code allows malformed basic blocks to occur, because it may be useful
18 /// in the intermediate stage modification to a program.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
25 #include "llvm/Instruction.h"
26 #include "llvm/SymbolTableListTraits.h"
27 #include "Support/ilist"
30 template <class _Term, class _BB> class SuccIterator; // Successor Iterator
31 template <class _Ptr, class _USE_iterator> class PredIterator;
33 template<> struct ilist_traits<Instruction>
34 : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
35 // createNode is used to create a node that marks the end of the list...
36 static Instruction *createNode();
37 static iplist<Instruction> &getList(BasicBlock *BB);
40 class BasicBlock : public Value { // Basic blocks are data objects also
42 typedef iplist<Instruction> InstListType;
44 InstListType InstList;
45 BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
47 void setParent(Function *parent);
48 void setNext(BasicBlock *N) { Next = N; }
49 void setPrev(BasicBlock *N) { Prev = N; }
50 friend class SymbolTableListTraits<BasicBlock, Function, Function>;
52 BasicBlock(const BasicBlock &); // Do not implement
53 void operator=(const BasicBlock &); // Do not implement
56 /// Instruction iterators...
57 typedef InstListType::iterator iterator;
58 typedef InstListType::const_iterator const_iterator;
59 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
60 typedef std::reverse_iterator<iterator> reverse_iterator;
62 /// BasicBlock ctor - If the function parameter is specified, the basic block
63 /// is automatically inserted at the end of the function.
65 BasicBlock(const std::string &Name = "", Function *Parent = 0);
67 /// BasicBlock ctor - If the InsertBefore parameter is specified, the basic
68 /// block is automatically inserted right before the specified block.
69 BasicBlock(const std::string &Name, BasicBlock *InsertBefore);
72 // Specialize setName to take care of symbol table majik
73 virtual void setName(const std::string &name, SymbolTable *ST = 0);
75 /// getParent - Return the enclosing method, or null if none
77 const Function *getParent() const { return InstList.getParent(); }
78 Function *getParent() { return InstList.getParent(); }
80 // getNext/Prev - Return the next or previous basic block in the list.
81 BasicBlock *getNext() { return Next; }
82 const BasicBlock *getNext() const { return Next; }
83 BasicBlock *getPrev() { return Prev; }
84 const BasicBlock *getPrev() const { return Prev; }
86 /// getTerminator() - If this is a well formed basic block, then this returns
87 /// a pointer to the terminator instruction. If it is not, then you get a
88 /// null pointer back.
90 TerminatorInst *getTerminator();
91 const TerminatorInst *const getTerminator() const;
93 // Provide a scoped predecessor and successor iterator
94 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
95 typedef PredIterator<const BasicBlock,
96 Value::use_const_iterator> pred_const_iterator;
98 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
99 typedef SuccIterator<const TerminatorInst*,
100 const BasicBlock> succ_const_iterator;
103 //===--------------------------------------------------------------------===//
104 /// Instruction iterator methods
106 inline iterator begin() { return InstList.begin(); }
107 inline const_iterator begin() const { return InstList.begin(); }
108 inline iterator end () { return InstList.end(); }
109 inline const_iterator end () const { return InstList.end(); }
111 inline reverse_iterator rbegin() { return InstList.rbegin(); }
112 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
113 inline reverse_iterator rend () { return InstList.rend(); }
114 inline const_reverse_iterator rend () const { return InstList.rend(); }
116 inline unsigned size() const { return InstList.size(); }
117 inline bool empty() const { return InstList.empty(); }
118 inline const Instruction &front() const { return InstList.front(); }
119 inline Instruction &front() { return InstList.front(); }
120 inline const Instruction &back() const { return InstList.back(); }
121 inline Instruction &back() { return InstList.back(); }
123 /// getInstList() - Return the underlying instruction list container. You
124 /// need to access it directly if you want to modify it currently.
126 const InstListType &getInstList() const { return InstList; }
127 InstListType &getInstList() { return InstList; }
129 virtual void print(std::ostream &OS) const;
131 /// Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const BasicBlock *BB) { return true; }
133 static inline bool classof(const Value *V) {
134 return V->getValueType() == Value::BasicBlockVal;
137 /// hasConstantReferences() - This predicate is true if there is a
138 /// reference to this basic block in the constant pool for this method. For
139 /// example, if a block is reached through a switch table, that table resides
140 /// in the constant pool, and the basic block is reference from it.
142 bool hasConstantReferences() const;
144 /// dropAllReferences() - This function causes all the subinstructions to "let
145 /// go" of all references that they are maintaining. This allows one to
146 /// 'delete' a whole class at a time, even though there may be circular
147 /// references... first all references are dropped, and all use counts go to
148 /// zero. Then everything is delete'd for real. Note that no operations are
149 /// valid on an object that has "dropped all references", except operator
152 void dropAllReferences();
154 /// removePredecessor - This method is used to notify a BasicBlock that the
155 /// specified Predecessor of the block is no longer able to reach it. This is
156 /// actually not used to update the Predecessor list, but is actually used to
157 /// update the PHI nodes that reside in the block. Note that this should be
158 /// called while the predecessor still refers to this block.
160 void removePredecessor(BasicBlock *Pred);
162 /// splitBasicBlock - This splits a basic block into two at the specified
163 /// instruction. Note that all instructions BEFORE the specified iterator
164 /// stay as part of the original basic block, an unconditional branch is added
165 /// to the new BB, and the rest of the instructions in the BB are moved to the
166 /// new BB, including the old terminator. The newly formed BasicBlock is
167 /// returned. This function invalidates the specified iterator.
169 /// Note that this only works on well formed basic blocks (must have a
170 /// terminator), and 'I' must not be the end of instruction list (which would
171 /// cause a degenerate basic block to be formed, having a terminator inside of
172 /// the basic block).
174 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");