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 get hold of a node that marks the end of
30 // The sentinel is relative to this instance, so we use a non-static
32 Instruction *createSentinel() const {
33 // since i(p)lists always publicly derive from the corresponding
34 // traits, placing a data member in this class will augment i(p)list.
35 // But since the NodeTy is expected to publicly derive from
36 // ilist_node<NodeTy>, there is a legal viable downcast from it
37 // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
38 // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
39 // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
41 return static_cast<Instruction*>(&Sentinel);
43 static void destroySentinel(Instruction*) {}
45 Instruction *provideInitialHead() const { return createSentinel(); }
46 Instruction *ensureHead(Instruction*) const { return createSentinel(); }
48 static iplist<Instruction> &getList(BasicBlock *BB);
49 static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
50 static int getListOffset();
52 mutable ilist_node<Instruction> Sentinel;
55 /// This represents a single basic block in LLVM. A basic block is simply a
56 /// container of instructions that execute sequentially. Basic blocks are Values
57 /// because they are referenced by instructions such as branches and switch
58 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
59 /// represents a label to which a branch can jump.
61 /// A well formed basic block is formed of a list of non-terminating
62 /// instructions followed by a single TerminatorInst instruction.
63 /// TerminatorInst's may not occur in the middle of basic blocks, and must
64 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
65 /// occur because it may be useful in the intermediate stage of constructing or
66 /// modifying a program. However, the verifier will ensure that basic blocks
67 /// are "well formed".
68 /// @brief LLVM Basic Block Representation
69 class BasicBlock : public Value, // Basic blocks are data objects also
70 public ilist_node<BasicBlock> {
73 typedef iplist<Instruction> InstListType;
75 InstListType InstList;
78 void setParent(Function *parent);
79 friend class SymbolTableListTraits<BasicBlock, Function>;
81 BasicBlock(const BasicBlock &); // Do not implement
82 void operator=(const BasicBlock &); // Do not implement
84 /// BasicBlock ctor - If the function parameter is specified, the basic block
85 /// is automatically inserted at either the end of the function (if
86 /// InsertBefore is null), or before the specified basic block.
88 explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
89 BasicBlock *InsertBefore = 0);
91 /// Instruction iterators...
92 typedef InstListType::iterator iterator;
93 typedef InstListType::const_iterator const_iterator;
95 /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
96 /// the basic block is automatically inserted at either the end of the
97 /// function (if InsertBefore is 0), or before the specified basic block.
98 static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
99 BasicBlock *InsertBefore = 0) {
100 return new BasicBlock(Name, Parent, InsertBefore);
104 /// getParent - Return the enclosing method, or null if none
106 const Function *getParent() const { return Parent; }
107 Function *getParent() { return Parent; }
109 /// use_back - Specialize the methods defined in Value, as we know that an
110 /// BasicBlock can only be used by Instructions (specifically PHI nodes and
112 Instruction *use_back() { return cast<Instruction>(*use_begin());}
113 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
115 /// getTerminator() - If this is a well formed basic block, then this returns
116 /// a pointer to the terminator instruction. If it is not, then you get a
117 /// null pointer back.
119 TerminatorInst *getTerminator();
120 const TerminatorInst *getTerminator() const;
122 /// Returns a pointer to the first instructon in this block that is not a
123 /// PHINode instruction. When adding instruction to the beginning of the
124 /// basic block, they should be added before the returned value, not before
125 /// the first instruction, which might be PHI.
126 /// Returns 0 is there's no non-PHI instruction.
127 Instruction* getFirstNonPHI();
128 const Instruction* getFirstNonPHI() const {
129 return const_cast<BasicBlock*>(this)->getFirstNonPHI();
132 /// removeFromParent - This method unlinks 'this' from the containing
133 /// function, but does not delete it.
135 void removeFromParent();
137 /// eraseFromParent - This method unlinks 'this' from the containing function
140 void eraseFromParent();
142 /// moveBefore - Unlink this basic block from its current function and
143 /// insert it into the function that MovePos lives in, right before MovePos.
144 void moveBefore(BasicBlock *MovePos);
146 /// moveAfter - Unlink this basic block from its current function and
147 /// insert it into the function that MovePos lives in, right after MovePos.
148 void moveAfter(BasicBlock *MovePos);
151 /// getSinglePredecessor - If this basic block has a single predecessor block,
152 /// return the block, otherwise return a null pointer.
153 BasicBlock *getSinglePredecessor();
154 const BasicBlock *getSinglePredecessor() const {
155 return const_cast<BasicBlock*>(this)->getSinglePredecessor();
158 /// getUniquePredecessor - If this basic block has a unique predecessor block,
159 /// return the block, otherwise return a null pointer.
160 /// Note that unique predecessor doesn't mean single edge, there can be
161 /// multiple edges from the unique predecessor to this block (for example
162 /// a switch statement with multiple cases having the same destination).
163 BasicBlock *getUniquePredecessor();
164 const BasicBlock *getUniquePredecessor() const {
165 return const_cast<BasicBlock*>(this)->getUniquePredecessor();
168 //===--------------------------------------------------------------------===//
169 /// Instruction iterator methods
171 inline iterator begin() { return InstList.begin(); }
172 inline const_iterator begin() const { return InstList.begin(); }
173 inline iterator end () { return InstList.end(); }
174 inline const_iterator end () const { return InstList.end(); }
176 inline size_t size() const { return InstList.size(); }
177 inline bool empty() const { return InstList.empty(); }
178 inline const Instruction &front() const { return InstList.front(); }
179 inline Instruction &front() { return InstList.front(); }
180 inline const Instruction &back() const { return InstList.back(); }
181 inline Instruction &back() { return InstList.back(); }
183 /// getInstList() - Return the underlying instruction list container. You
184 /// need to access it directly if you want to modify it currently.
186 const InstListType &getInstList() const { return InstList; }
187 InstListType &getInstList() { return InstList; }
189 /// Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const BasicBlock *) { return true; }
191 static inline bool classof(const Value *V) {
192 return V->getValueID() == Value::BasicBlockVal;
195 /// dropAllReferences() - This function causes all the subinstructions to "let
196 /// go" of all references that they are maintaining. This allows one to
197 /// 'delete' a whole class at a time, even though there may be circular
198 /// references... first all references are dropped, and all use counts go to
199 /// zero. Then everything is delete'd for real. Note that no operations are
200 /// valid on an object that has "dropped all references", except operator
203 void dropAllReferences();
205 /// removePredecessor - This method is used to notify a BasicBlock that the
206 /// specified Predecessor of the block is no longer able to reach it. This is
207 /// actually not used to update the Predecessor list, but is actually used to
208 /// update the PHI nodes that reside in the block. Note that this should be
209 /// called while the predecessor still refers to this block.
211 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
213 /// splitBasicBlock - This splits a basic block into two at the specified
214 /// instruction. Note that all instructions BEFORE the specified iterator
215 /// stay as part of the original basic block, an unconditional branch is added
216 /// to the original BB, and the rest of the instructions in the BB are moved
217 /// to the new BB, including the old terminator. The newly formed BasicBlock
218 /// is returned. This function invalidates the specified iterator.
220 /// Note that this only works on well formed basic blocks (must have a
221 /// terminator), and 'I' must not be the end of instruction list (which would
222 /// cause a degenerate basic block to be formed, having a terminator inside of
223 /// the basic block).
225 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
228 static unsigned getInstListOffset() {
230 return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
235 ilist_traits<Instruction>::getListOffset() {
236 return BasicBlock::getInstListOffset();
239 } // End llvm namespace