1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the BasicBlock class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BASICBLOCK_H
15 #define LLVM_BASICBLOCK_H
17 #include "llvm/Instruction.h"
18 #include "llvm/SymbolTableListTraits.h"
19 #include "llvm/ADT/ilist.h"
20 #include "llvm/Support/DataTypes.h"
26 template<> struct ilist_traits<Instruction>
27 : public SymbolTableListTraits<Instruction, BasicBlock> {
28 // createSentinel is used to create a node that marks the end of the list...
29 Instruction *createSentinel() const {
30 return const_cast<Instruction*>(static_cast<const Instruction*>(&Sentinel));
32 static void destroySentinel(Instruction *I) { I = I; }
33 static iplist<Instruction> &getList(BasicBlock *BB);
34 static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
35 static int getListOffset();
37 ilist_node<Instruction> Sentinel;
40 /// This represents a single basic block in LLVM. A basic block is simply a
41 /// container of instructions that execute sequentially. Basic blocks are Values
42 /// because they are referenced by instructions such as branches and switch
43 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
44 /// represents a label to which a branch can jump.
46 /// A well formed basic block is formed of a list of non-terminating
47 /// instructions followed by a single TerminatorInst instruction.
48 /// TerminatorInst's may not occur in the middle of basic blocks, and must
49 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
50 /// occur because it may be useful in the intermediate stage of constructing or
51 /// modifying a program. However, the verifier will ensure that basic blocks
52 /// are "well formed".
53 /// @brief LLVM Basic Block Representation
54 class BasicBlock : public Value, // Basic blocks are data objects also
55 public ilist_node<BasicBlock> {
58 typedef iplist<Instruction> InstListType;
60 InstListType InstList;
63 void setParent(Function *parent);
64 friend class SymbolTableListTraits<BasicBlock, Function>;
66 BasicBlock(const BasicBlock &); // Do not implement
67 void operator=(const BasicBlock &); // Do not implement
69 /// BasicBlock ctor - If the function parameter is specified, the basic block
70 /// is automatically inserted at either the end of the function (if
71 /// InsertBefore is null), or before the specified basic block.
73 explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
74 BasicBlock *InsertBefore = 0);
76 /// Instruction iterators...
77 typedef InstListType::iterator iterator;
78 typedef InstListType::const_iterator const_iterator;
80 /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
81 /// the basic block is automatically inserted at either the end of the
82 /// function (if InsertBefore is 0), or before the specified basic block.
83 static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
84 BasicBlock *InsertBefore = 0) {
85 return new BasicBlock(Name, Parent, InsertBefore);
89 /// getParent - Return the enclosing method, or null if none
91 const Function *getParent() const { return Parent; }
92 Function *getParent() { return Parent; }
94 /// use_back - Specialize the methods defined in Value, as we know that an
95 /// BasicBlock can only be used by Instructions (specifically PHI and terms).
96 Instruction *use_back() { return cast<Instruction>(*use_begin());}
97 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
99 /// getTerminator() - If this is a well formed basic block, then this returns
100 /// a pointer to the terminator instruction. If it is not, then you get a
101 /// null pointer back.
103 TerminatorInst *getTerminator();
104 const TerminatorInst *getTerminator() const;
106 /// Returns a pointer to the first instructon in this block that is not a
107 /// PHINode instruction. When adding instruction to the beginning of the
108 /// basic block, they should be added before the returned value, not before
109 /// the first instruction, which might be PHI.
110 /// Returns 0 is there's no non-PHI instruction.
111 Instruction* getFirstNonPHI();
112 const Instruction* getFirstNonPHI() const {
113 return const_cast<BasicBlock*>(this)->getFirstNonPHI();
116 /// removeFromParent - This method unlinks 'this' from the containing
117 /// function, but does not delete it.
119 void removeFromParent();
121 /// eraseFromParent - This method unlinks 'this' from the containing function
124 void eraseFromParent();
126 /// moveBefore - Unlink this basic block from its current function and
127 /// insert it into the function that MovePos lives in, right before MovePos.
128 void moveBefore(BasicBlock *MovePos);
130 /// moveAfter - Unlink this basic block from its current function and
131 /// insert it into the function that MovePos lives in, right after MovePos.
132 void moveAfter(BasicBlock *MovePos);
135 /// getSinglePredecessor - If this basic block has a single predecessor block,
136 /// return the block, otherwise return a null pointer.
137 BasicBlock *getSinglePredecessor();
138 const BasicBlock *getSinglePredecessor() const {
139 return const_cast<BasicBlock*>(this)->getSinglePredecessor();
142 /// getUniquePredecessor - If this basic block has a unique predecessor block,
143 /// return the block, otherwise return a null pointer.
144 /// Note that unique predecessor doesn't mean single edge, there can be
145 /// multiple edges from the unique predecessor to this block (for example
146 /// a switch statement with multiple cases having the same destination).
147 BasicBlock *getUniquePredecessor();
148 const BasicBlock *getUniquePredecessor() const {
149 return const_cast<BasicBlock*>(this)->getUniquePredecessor();
152 //===--------------------------------------------------------------------===//
153 /// Instruction iterator methods
155 inline iterator begin() { return InstList.begin(); }
156 inline const_iterator begin() const { return InstList.begin(); }
157 inline iterator end () { return InstList.end(); }
158 inline const_iterator end () const { return InstList.end(); }
160 inline size_t size() const { return InstList.size(); }
161 inline bool empty() const { return InstList.empty(); }
162 inline const Instruction &front() const { return InstList.front(); }
163 inline Instruction &front() { return InstList.front(); }
164 inline const Instruction &back() const { return InstList.back(); }
165 inline Instruction &back() { return InstList.back(); }
167 /// getInstList() - Return the underlying instruction list container. You
168 /// need to access it directly if you want to modify it currently.
170 const InstListType &getInstList() const { return InstList; }
171 InstListType &getInstList() { return InstList; }
173 /// Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const BasicBlock *) { return true; }
175 static inline bool classof(const Value *V) {
176 return V->getValueID() == Value::BasicBlockVal;
179 /// dropAllReferences() - This function causes all the subinstructions to "let
180 /// go" of all references that they are maintaining. This allows one to
181 /// 'delete' a whole class at a time, even though there may be circular
182 /// references... first all references are dropped, and all use counts go to
183 /// zero. Then everything is delete'd for real. Note that no operations are
184 /// valid on an object that has "dropped all references", except operator
187 void dropAllReferences();
189 /// removePredecessor - This method is used to notify a BasicBlock that the
190 /// specified Predecessor of the block is no longer able to reach it. This is
191 /// actually not used to update the Predecessor list, but is actually used to
192 /// update the PHI nodes that reside in the block. Note that this should be
193 /// called while the predecessor still refers to this block.
195 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
197 /// splitBasicBlock - This splits a basic block into two at the specified
198 /// instruction. Note that all instructions BEFORE the specified iterator
199 /// stay as part of the original basic block, an unconditional branch is added
200 /// to the original BB, and the rest of the instructions in the BB are moved
201 /// to the new BB, including the old terminator. The newly formed BasicBlock
202 /// is returned. This function invalidates the specified iterator.
204 /// Note that this only works on well formed basic blocks (must have a
205 /// terminator), and 'I' must not be the end of instruction list (which would
206 /// cause a degenerate basic block to be formed, having a terminator inside of
207 /// the basic block).
209 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
212 static unsigned getInstListOffset() {
214 return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
219 ilist_traits<Instruction>::getListOffset() {
220 return BasicBlock::getInstListOffset();
223 } // End llvm namespace