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(); }
47 static void noteHead(Instruction*, Instruction*) {}
49 mutable ilist_node<Instruction> Sentinel;
52 /// This represents a single basic block in LLVM. A basic block is simply a
53 /// container of instructions that execute sequentially. Basic blocks are Values
54 /// because they are referenced by instructions such as branches and switch
55 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
56 /// represents a label to which a branch can jump.
58 /// A well formed basic block is formed of a list of non-terminating
59 /// instructions followed by a single TerminatorInst instruction.
60 /// TerminatorInst's may not occur in the middle of basic blocks, and must
61 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
62 /// occur because it may be useful in the intermediate stage of constructing or
63 /// modifying a program. However, the verifier will ensure that basic blocks
64 /// are "well formed".
65 /// @brief LLVM Basic Block Representation
66 class BasicBlock : public Value, // Basic blocks are data objects also
67 public ilist_node<BasicBlock> {
70 typedef iplist<Instruction> InstListType;
72 InstListType InstList;
75 void setParent(Function *parent);
76 friend class SymbolTableListTraits<BasicBlock, Function>;
78 BasicBlock(const BasicBlock &); // Do not implement
79 void operator=(const BasicBlock &); // Do not implement
81 /// BasicBlock ctor - If the function parameter is specified, the basic block
82 /// is automatically inserted at either the end of the function (if
83 /// InsertBefore is null), or before the specified basic block.
85 explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
86 BasicBlock *InsertBefore = 0);
88 /// Instruction iterators...
89 typedef InstListType::iterator iterator;
90 typedef InstListType::const_iterator const_iterator;
92 /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
93 /// the basic block is automatically inserted at either the end of the
94 /// function (if InsertBefore is 0), or before the specified basic block.
95 static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
96 BasicBlock *InsertBefore = 0) {
97 return new BasicBlock(Name, Parent, InsertBefore);
101 /// getParent - Return the enclosing method, or null if none
103 const Function *getParent() const { return Parent; }
104 Function *getParent() { return Parent; }
106 /// use_back - Specialize the methods defined in Value, as we know that an
107 /// BasicBlock can only be used by Instructions (specifically PHI nodes and
109 Instruction *use_back() { return cast<Instruction>(*use_begin());}
110 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
112 /// getTerminator() - If this is a well formed basic block, then this returns
113 /// a pointer to the terminator instruction. If it is not, then you get a
114 /// null pointer back.
116 TerminatorInst *getTerminator();
117 const TerminatorInst *getTerminator() const;
119 /// Returns a pointer to the first instructon in this block that is not a
120 /// PHINode instruction. When adding instruction to the beginning of the
121 /// basic block, they should be added before the returned value, not before
122 /// the first instruction, which might be PHI.
123 /// Returns 0 is there's no non-PHI instruction.
124 Instruction* getFirstNonPHI();
125 const Instruction* getFirstNonPHI() const {
126 return const_cast<BasicBlock*>(this)->getFirstNonPHI();
129 /// removeFromParent - This method unlinks 'this' from the containing
130 /// function, but does not delete it.
132 void removeFromParent();
134 /// eraseFromParent - This method unlinks 'this' from the containing function
137 void eraseFromParent();
139 /// moveBefore - Unlink this basic block from its current function and
140 /// insert it into the function that MovePos lives in, right before MovePos.
141 void moveBefore(BasicBlock *MovePos);
143 /// moveAfter - Unlink this basic block from its current function and
144 /// insert it into the function that MovePos lives in, right after MovePos.
145 void moveAfter(BasicBlock *MovePos);
148 /// getSinglePredecessor - If this basic block has a single predecessor block,
149 /// return the block, otherwise return a null pointer.
150 BasicBlock *getSinglePredecessor();
151 const BasicBlock *getSinglePredecessor() const {
152 return const_cast<BasicBlock*>(this)->getSinglePredecessor();
155 /// getUniquePredecessor - If this basic block has a unique predecessor block,
156 /// return the block, otherwise return a null pointer.
157 /// Note that unique predecessor doesn't mean single edge, there can be
158 /// multiple edges from the unique predecessor to this block (for example
159 /// a switch statement with multiple cases having the same destination).
160 BasicBlock *getUniquePredecessor();
161 const BasicBlock *getUniquePredecessor() const {
162 return const_cast<BasicBlock*>(this)->getUniquePredecessor();
165 //===--------------------------------------------------------------------===//
166 /// Instruction iterator methods
168 inline iterator begin() { return InstList.begin(); }
169 inline const_iterator begin() const { return InstList.begin(); }
170 inline iterator end () { return InstList.end(); }
171 inline const_iterator end () const { return InstList.end(); }
173 inline size_t size() const { return InstList.size(); }
174 inline bool empty() const { return InstList.empty(); }
175 inline const Instruction &front() const { return InstList.front(); }
176 inline Instruction &front() { return InstList.front(); }
177 inline const Instruction &back() const { return InstList.back(); }
178 inline Instruction &back() { return InstList.back(); }
180 /// getInstList() - Return the underlying instruction list container. You
181 /// need to access it directly if you want to modify it currently.
183 const InstListType &getInstList() const { return InstList; }
184 InstListType &getInstList() { return InstList; }
186 /// getSublistAccess() - returns pointer to member of instruction list
187 static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
188 return &BasicBlock::InstList;
191 /// getValueSymbolTable() - returns pointer to symbol table (if any)
192 ValueSymbolTable *getValueSymbolTable();
194 /// Methods for support type inquiry through isa, cast, and dyn_cast:
195 static inline bool classof(const BasicBlock *) { return true; }
196 static inline bool classof(const Value *V) {
197 return V->getValueID() == Value::BasicBlockVal;
200 /// dropAllReferences() - This function causes all the subinstructions to "let
201 /// go" of all references that they are maintaining. This allows one to
202 /// 'delete' a whole class at a time, even though there may be circular
203 /// references... first all references are dropped, and all use counts go to
204 /// zero. Then everything is delete'd for real. Note that no operations are
205 /// valid on an object that has "dropped all references", except operator
208 void dropAllReferences();
210 /// removePredecessor - This method is used to notify a BasicBlock that the
211 /// specified Predecessor of the block is no longer able to reach it. This is
212 /// actually not used to update the Predecessor list, but is actually used to
213 /// update the PHI nodes that reside in the block. Note that this should be
214 /// called while the predecessor still refers to this block.
216 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
218 /// splitBasicBlock - This splits a basic block into two at the specified
219 /// instruction. Note that all instructions BEFORE the specified iterator
220 /// stay as part of the original basic block, an unconditional branch is added
221 /// to the original BB, and the rest of the instructions in the BB are moved
222 /// to the new BB, including the old terminator. The newly formed BasicBlock
223 /// is returned. This function invalidates the specified iterator.
225 /// Note that this only works on well formed basic blocks (must have a
226 /// terminator), and 'I' must not be the end of instruction list (which would
227 /// cause a degenerate basic block to be formed, having a terminator inside of
228 /// the basic block).
230 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
233 } // End llvm namespace