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/System/DataTypes.h"
28 template<> struct ilist_traits<Instruction>
29 : public SymbolTableListTraits<Instruction, BasicBlock> {
30 // createSentinel is used to get hold of a node that marks the end of
32 // The sentinel is relative to this instance, so we use a non-static
34 Instruction *createSentinel() const {
35 // since i(p)lists always publicly derive from the corresponding
36 // traits, placing a data member in this class will augment i(p)list.
37 // But since the NodeTy is expected to publicly derive from
38 // ilist_node<NodeTy>, there is a legal viable downcast from it
39 // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
40 // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
41 // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
43 return static_cast<Instruction*>(&Sentinel);
45 static void destroySentinel(Instruction*) {}
47 Instruction *provideInitialHead() const { return createSentinel(); }
48 Instruction *ensureHead(Instruction*) const { return createSentinel(); }
49 static void noteHead(Instruction*, Instruction*) {}
51 mutable ilist_half_node<Instruction> Sentinel;
54 /// This represents a single basic block in LLVM. A basic block is simply a
55 /// container of instructions that execute sequentially. Basic blocks are Values
56 /// because they are referenced by instructions such as branches and switch
57 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
58 /// represents a label to which a branch can jump.
60 /// A well formed basic block is formed of a list of non-terminating
61 /// instructions followed by a single TerminatorInst instruction.
62 /// TerminatorInst's may not occur in the middle of basic blocks, and must
63 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
64 /// occur because it may be useful in the intermediate stage of constructing or
65 /// modifying a program. However, the verifier will ensure that basic blocks
66 /// are "well formed".
67 /// @brief LLVM Basic Block Representation
68 class BasicBlock : public Value, // Basic blocks are data objects also
69 public ilist_node<BasicBlock> {
70 friend class BlockAddress;
72 typedef iplist<Instruction> InstListType;
74 InstListType InstList;
77 void setParent(Function *parent);
78 friend class SymbolTableListTraits<BasicBlock, Function>;
80 BasicBlock(const BasicBlock &); // Do not implement
81 void operator=(const BasicBlock &); // Do not implement
83 /// BasicBlock ctor - If the function parameter is specified, the basic block
84 /// is automatically inserted at either the end of the function (if
85 /// InsertBefore is null), or before the specified basic block.
87 explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
88 Function *Parent = 0, BasicBlock *InsertBefore = 0);
90 /// getContext - Get the context in which this basic block lives.
91 LLVMContext &getContext() const;
93 /// Instruction iterators...
94 typedef InstListType::iterator iterator;
95 typedef InstListType::const_iterator const_iterator;
97 /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
98 /// the basic block is automatically inserted at either the end of the
99 /// function (if InsertBefore is 0), or before the specified basic block.
100 static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
101 Function *Parent = 0,BasicBlock *InsertBefore = 0) {
102 return new BasicBlock(Context, Name, Parent, InsertBefore);
106 /// getParent - Return the enclosing method, or null if none
108 const Function *getParent() const { return Parent; }
109 Function *getParent() { return Parent; }
111 /// use_back - Specialize the methods defined in Value, as we know that an
112 /// BasicBlock can only be used by Users (specifically PHI nodes, terminators,
113 /// and BlockAddress's).
114 User *use_back() { return cast<User>(*use_begin());}
115 const User *use_back() const { return cast<User>(*use_begin());}
117 /// getTerminator() - If this is a well formed basic block, then this returns
118 /// a pointer to the terminator instruction. If it is not, then you get a
119 /// null pointer back.
121 TerminatorInst *getTerminator();
122 const TerminatorInst *getTerminator() const;
124 /// Returns a pointer to the first instructon in this block that is not a
125 /// PHINode instruction. When adding instruction to the beginning of the
126 /// basic block, they should be added before the returned value, not before
127 /// the first instruction, which might be PHI.
128 /// Returns 0 is there's no non-PHI instruction.
129 Instruction* getFirstNonPHI();
130 const Instruction* getFirstNonPHI() const {
131 return const_cast<BasicBlock*>(this)->getFirstNonPHI();
134 /// removeFromParent - This method unlinks 'this' from the containing
135 /// function, but does not delete it.
137 void removeFromParent();
139 /// eraseFromParent - This method unlinks 'this' from the containing function
142 void eraseFromParent();
144 /// moveBefore - Unlink this basic block from its current function and
145 /// insert it into the function that MovePos lives in, right before MovePos.
146 void moveBefore(BasicBlock *MovePos);
148 /// moveAfter - Unlink this basic block from its current function and
149 /// insert it into the function that MovePos lives in, right after MovePos.
150 void moveAfter(BasicBlock *MovePos);
153 /// getSinglePredecessor - If this basic block has a single predecessor block,
154 /// return the block, otherwise return a null pointer.
155 BasicBlock *getSinglePredecessor();
156 const BasicBlock *getSinglePredecessor() const {
157 return const_cast<BasicBlock*>(this)->getSinglePredecessor();
160 /// getUniquePredecessor - If this basic block has a unique predecessor block,
161 /// return the block, otherwise return a null pointer.
162 /// Note that unique predecessor doesn't mean single edge, there can be
163 /// multiple edges from the unique predecessor to this block (for example
164 /// a switch statement with multiple cases having the same destination).
165 BasicBlock *getUniquePredecessor();
166 const BasicBlock *getUniquePredecessor() const {
167 return const_cast<BasicBlock*>(this)->getUniquePredecessor();
170 //===--------------------------------------------------------------------===//
171 /// Instruction iterator methods
173 inline iterator begin() { return InstList.begin(); }
174 inline const_iterator begin() const { return InstList.begin(); }
175 inline iterator end () { return InstList.end(); }
176 inline const_iterator end () const { return InstList.end(); }
178 inline size_t size() const { return InstList.size(); }
179 inline bool empty() const { return InstList.empty(); }
180 inline const Instruction &front() const { return InstList.front(); }
181 inline Instruction &front() { return InstList.front(); }
182 inline const Instruction &back() const { return InstList.back(); }
183 inline Instruction &back() { return InstList.back(); }
185 /// getInstList() - Return the underlying instruction list container. You
186 /// need to access it directly if you want to modify it currently.
188 const InstListType &getInstList() const { return InstList; }
189 InstListType &getInstList() { return InstList; }
191 /// getSublistAccess() - returns pointer to member of instruction list
192 static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
193 return &BasicBlock::InstList;
196 /// getValueSymbolTable() - returns pointer to symbol table (if any)
197 ValueSymbolTable *getValueSymbolTable();
199 /// Methods for support type inquiry through isa, cast, and dyn_cast:
200 static inline bool classof(const BasicBlock *) { return true; }
201 static inline bool classof(const Value *V) {
202 return V->getValueID() == Value::BasicBlockVal;
205 /// dropAllReferences() - This function causes all the subinstructions to "let
206 /// go" of all references that they are maintaining. This allows one to
207 /// 'delete' a whole class at a time, even though there may be circular
208 /// references... first all references are dropped, and all use counts go to
209 /// zero. Then everything is delete'd for real. Note that no operations are
210 /// valid on an object that has "dropped all references", except operator
213 void dropAllReferences();
215 /// removePredecessor - This method is used to notify a BasicBlock that the
216 /// specified Predecessor of the block is no longer able to reach it. This is
217 /// actually not used to update the Predecessor list, but is actually used to
218 /// update the PHI nodes that reside in the block. Note that this should be
219 /// called while the predecessor still refers to this block.
221 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
223 /// splitBasicBlock - This splits a basic block into two at the specified
224 /// instruction. Note that all instructions BEFORE the specified iterator
225 /// stay as part of the original basic block, an unconditional branch is added
226 /// to the original BB, and the rest of the instructions in the BB are moved
227 /// to the new BB, including the old terminator. The newly formed BasicBlock
228 /// is returned. This function invalidates the specified iterator.
230 /// Note that this only works on well formed basic blocks (must have a
231 /// terminator), and 'I' must not be the end of instruction list (which would
232 /// cause a degenerate basic block to be formed, having a terminator inside of
233 /// the basic block).
235 /// Also note that this doesn't preserve any passes. To split blocks while
236 /// keeping loop information consistent, use the SplitBlock utility function.
238 BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
240 /// hasAddressTaken - returns true if there are any uses of this basic block
241 /// other than direct branches, switches, etc. to it.
242 bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
245 /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
246 /// objects using it. This is almost always 0, sometimes one, possibly but
247 /// almost never 2, and inconceivably 3 or more.
248 void AdjustBlockAddressRefCount(int Amt) {
249 setValueSubclassData(getSubclassDataFromValue()+Amt);
250 assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
251 "Refcount wrap-around");
253 // Shadow Value::setValueSubclassData with a private forwarding method so that
254 // any future subclasses cannot accidentally use it.
255 void setValueSubclassData(unsigned short D) {
256 Value::setValueSubclassData(D);
260 } // End llvm namespace