LLVM  3.7.0
BasicBlock.h
Go to the documentation of this file.
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the BasicBlock class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_BASICBLOCK_H
15 #define LLVM_IR_BASICBLOCK_H
16 
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/IR/Instruction.h"
22 #include "llvm/Support/DataTypes.h"
23 
24 namespace llvm {
25 
26 class CallInst;
27 class LandingPadInst;
28 class TerminatorInst;
29 class LLVMContext;
30 class BlockAddress;
31 class Function;
32 
33 // Traits for intrusive list of basic blocks...
34 template<> struct ilist_traits<BasicBlock>
35  : public SymbolTableListTraits<BasicBlock, Function> {
36 
37  BasicBlock *createSentinel() const;
38  static void destroySentinel(BasicBlock*) {}
39 
42  static void noteHead(BasicBlock*, BasicBlock*) {}
43 
44  static ValueSymbolTable *getSymTab(Function *ItemParent);
45 private:
46  mutable ilist_half_node<BasicBlock> Sentinel;
47 };
48 
49 
50 /// \brief LLVM Basic Block Representation
51 ///
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.
57 ///
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 class BasicBlock : public Value, // Basic blocks are data objects also
66  public ilist_node<BasicBlock> {
67  friend class BlockAddress;
68 public:
70 private:
71  InstListType InstList;
72  Function *Parent;
73 
74  void setParent(Function *parent);
76 
77  BasicBlock(const BasicBlock &) = delete;
78  void operator=(const BasicBlock &) = delete;
79 
80  /// \brief Constructor.
81  ///
82  /// If the function parameter is specified, the basic block is automatically
83  /// inserted at either the end of the function (if InsertBefore is null), or
84  /// before the specified basic block.
85  explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
86  Function *Parent = nullptr,
87  BasicBlock *InsertBefore = nullptr);
88 public:
89  /// \brief Get the context in which this basic block lives.
90  LLVMContext &getContext() const;
91 
92  /// Instruction iterators...
97 
98  /// \brief Creates a new BasicBlock.
99  ///
100  /// If the Parent parameter is specified, the basic block is automatically
101  /// inserted at either the end of the function (if InsertBefore is 0), or
102  /// before the specified basic block.
103  static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
104  Function *Parent = nullptr,
105  BasicBlock *InsertBefore = nullptr) {
106  return new BasicBlock(Context, Name, Parent, InsertBefore);
107  }
108  ~BasicBlock() override;
109 
110  /// \brief Return the enclosing method, or null if none.
111  const Function *getParent() const { return Parent; }
112  Function *getParent() { return Parent; }
113 
114  /// \brief Return the module owning the function this basic block belongs to,
115  /// or nullptr it the function does not have a module.
116  ///
117  /// Note: this is undefined behavior if the block does not have a parent.
118  const Module *getModule() const;
119  Module *getModule();
120 
121  /// \brief Returns the terminator instruction if the block is well formed or
122  /// null if the block is not well formed.
124  const TerminatorInst *getTerminator() const;
125 
126  /// \brief Returns the call instruction marked 'musttail' prior to the
127  /// terminating return instruction of this basic block, if such a call is
128  /// present. Otherwise, returns null.
131  return const_cast<BasicBlock *>(this)->getTerminatingMustTailCall();
132  }
133 
134  /// \brief Returns a pointer to the first instruction in this block that is
135  /// not a PHINode instruction.
136  ///
137  /// When adding instructions to the beginning of the basic block, they should
138  /// be added before the returned value, not before the first instruction,
139  /// which might be PHI. Returns 0 is there's no non-PHI instruction.
141  const Instruction* getFirstNonPHI() const {
142  return const_cast<BasicBlock*>(this)->getFirstNonPHI();
143  }
144 
145  /// \brief Returns a pointer to the first instruction in this block that is not
146  /// a PHINode or a debug intrinsic.
149  return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg();
150  }
151 
152  /// \brief Returns a pointer to the first instruction in this block that is not
153  /// a PHINode, a debug intrinsic, or a lifetime intrinsic.
156  return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime();
157  }
158 
159  /// \brief Returns an iterator to the first instruction in this block that is
160  /// suitable for inserting a non-PHI instruction.
161  ///
162  /// In particular, it skips all PHIs and LandingPad instructions.
165  return const_cast<BasicBlock*>(this)->getFirstInsertionPt();
166  }
167 
168  /// \brief Unlink 'this' from the containing function, but do not delete it.
169  void removeFromParent();
170 
171  /// \brief Unlink 'this' from the containing function and delete it.
172  ///
173  // \returns an iterator pointing to the element after the erased one.
175 
176  /// \brief Unlink this basic block from its current function and insert it
177  /// into the function that \p MovePos lives in, right before \p MovePos.
178  void moveBefore(BasicBlock *MovePos);
179 
180  /// \brief Unlink this basic block from its current function and insert it
181  /// right after \p MovePos in the function \p MovePos lives in.
182  void moveAfter(BasicBlock *MovePos);
183 
184  /// \brief Insert unlinked basic block into a function.
185  ///
186  /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is
187  /// provided, inserts before that basic block, otherwise inserts at the end.
188  ///
189  /// \pre \a getParent() is \c nullptr.
190  void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
191 
192  /// \brief Return the predecessor of this block if it has a single predecessor
193  /// block. Otherwise return a null pointer.
196  return const_cast<BasicBlock*>(this)->getSinglePredecessor();
197  }
198 
199  /// \brief Return the predecessor of this block if it has a unique predecessor
200  /// block. Otherwise return a null pointer.
201  ///
202  /// Note that unique predecessor doesn't mean single edge, there can be
203  /// multiple edges from the unique predecessor to this block (for example a
204  /// switch statement with multiple cases having the same destination).
207  return const_cast<BasicBlock*>(this)->getUniquePredecessor();
208  }
209 
210  /// \brief Return the successor of this block if it has a single successor.
211  /// Otherwise return a null pointer.
212  ///
213  /// This method is analogous to getSinglePredecessor above.
216  return const_cast<BasicBlock*>(this)->getSingleSuccessor();
217  }
218 
219  /// \brief Return the successor of this block if it has a unique successor.
220  /// Otherwise return a null pointer.
221  ///
222  /// This method is analogous to getUniquePredecessor above.
225  return const_cast<BasicBlock*>(this)->getUniqueSuccessor();
226  }
227 
228  //===--------------------------------------------------------------------===//
229  /// Instruction iterator methods
230  ///
231  inline iterator begin() { return InstList.begin(); }
232  inline const_iterator begin() const { return InstList.begin(); }
233  inline iterator end () { return InstList.end(); }
234  inline const_iterator end () const { return InstList.end(); }
235 
236  inline reverse_iterator rbegin() { return InstList.rbegin(); }
237  inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
238  inline reverse_iterator rend () { return InstList.rend(); }
239  inline const_reverse_iterator rend () const { return InstList.rend(); }
240 
241  inline size_t size() const { return InstList.size(); }
242  inline bool empty() const { return InstList.empty(); }
243  inline const Instruction &front() const { return InstList.front(); }
244  inline Instruction &front() { return InstList.front(); }
245  inline const Instruction &back() const { return InstList.back(); }
246  inline Instruction &back() { return InstList.back(); }
247 
248  /// \brief Return the underlying instruction list container.
249  ///
250  /// Currently you need to access the underlying instruction list container
251  /// directly if you want to modify it.
252  const InstListType &getInstList() const { return InstList; }
253  InstListType &getInstList() { return InstList; }
254 
255  /// \brief Returns a pointer to a member of the instruction list.
257  return &BasicBlock::InstList;
258  }
259 
260  /// \brief Returns a pointer to the symbol table if one exists.
262 
263  /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
264  static inline bool classof(const Value *V) {
265  return V->getValueID() == Value::BasicBlockVal;
266  }
267 
268  /// \brief Cause all subinstructions to "let go" of all the references that
269  /// said subinstructions are maintaining.
270  ///
271  /// This allows one to 'delete' a whole class at a time, even though there may
272  /// be circular references... first all references are dropped, and all use
273  /// counts go to zero. Then everything is delete'd for real. Note that no
274  /// operations are valid on an object that has "dropped all references",
275  /// except operator delete.
276  void dropAllReferences();
277 
278  /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer
279  /// able to reach it.
280  ///
281  /// This is actually not used to update the Predecessor list, but is actually
282  /// used to update the PHI nodes that reside in the block. Note that this
283  /// should be called while the predecessor still refers to this block.
284  void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
285 
286  /// \brief Split the basic block into two basic blocks at the specified
287  /// instruction.
288  ///
289  /// Note that all instructions BEFORE the specified iterator stay as part of
290  /// the original basic block, an unconditional branch is added to the original
291  /// BB, and the rest of the instructions in the BB are moved to the new BB,
292  /// including the old terminator. The newly formed BasicBlock is returned.
293  /// This function invalidates the specified iterator.
294  ///
295  /// Note that this only works on well formed basic blocks (must have a
296  /// terminator), and 'I' must not be the end of instruction list (which would
297  /// cause a degenerate basic block to be formed, having a terminator inside of
298  /// the basic block).
299  ///
300  /// Also note that this doesn't preserve any passes. To split blocks while
301  /// keeping loop information consistent, use the SplitBlock utility function.
302  BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
303 
304  /// \brief Returns true if there are any uses of this basic block other than
305  /// direct branches, switches, etc. to it.
306  bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
307 
308  /// \brief Update all phi nodes in this basic block's successors to refer to
309  /// basic block \p New instead of to it.
311 
312  /// \brief Return true if this basic block is a landing pad.
313  ///
314  /// Being a ``landing pad'' means that the basic block is the destination of
315  /// the 'unwind' edge of an invoke instruction.
316  bool isLandingPad() const;
317 
318  /// \brief Return the landingpad instruction associated with the landing pad.
320  const LandingPadInst *getLandingPadInst() const;
321 
322 private:
323  /// \brief Increment the internal refcount of the number of BlockAddresses
324  /// referencing this BasicBlock by \p Amt.
325  ///
326  /// This is almost always 0, sometimes one possibly, but almost never 2, and
327  /// inconceivably 3 or more.
328  void AdjustBlockAddressRefCount(int Amt) {
329  setValueSubclassData(getSubclassDataFromValue()+Amt);
330  assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
331  "Refcount wrap-around");
332  }
333  /// \brief Shadow Value::setValueSubclassData with a private forwarding method
334  /// so that any future subclasses cannot accidentally use it.
335  void setValueSubclassData(unsigned short D) {
337  }
338 };
339 
340 // createSentinel is used to get hold of the node that marks the end of the
341 // list... (same trick used here as in ilist_traits<Instruction>)
343  return static_cast<BasicBlock*>(&Sentinel);
344 }
345 
346 // Create wrappers for C Binding types (see CBindingWrapping.h).
348 
349 } // End llvm namespace
350 
351 #endif
BasicBlock * getUniqueSuccessor()
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:246
This class provides a symbol table of name/value pairs.
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:224
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
Definition: BasicBlock.cpp:266
InstListType & getInstList()
Definition: BasicBlock.h:253
static void noteHead(BasicBlock *, BasicBlock *)
Definition: BasicBlock.h:42
const Instruction & back() const
Definition: BasicBlock.h:245
Various leaf nodes.
Definition: ISDOpcodes.h:60
void insertInto(Function *Parent, BasicBlock *InsertBefore=nullptr)
Insert unlinked basic block into a function.
Definition: BasicBlock.cpp:54
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
reverse_iterator rend()
Definition: ilist.h:379
CallInst * getTerminatingMustTailCall()
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
Definition: BasicBlock.cpp:134
CallInst - This class represents a function call, abstracting a target machine's calling convention...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
const Instruction & front() const
Definition: BasicBlock.h:243
reverse_iterator rend()
Definition: BasicBlock.h:238
reverse_iterator rbegin()
Definition: BasicBlock.h:236
iterator begin()
Definition: ilist.h:359
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
const_iterator begin() const
Definition: BasicBlock.h:232
reverse_iterator rbegin()
Definition: ilist.h:377
Instruction * getFirstNonPHIOrDbg()
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Definition: BasicBlock.cpp:172
BlockAddress - The address of a basic block.
Definition: Constants.h:802
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:165
const_reverse_iterator rend() const
Definition: BasicBlock.h:239
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:306
const BasicBlock * getSingleSuccessor() const
Definition: BasicBlock.h:215
const_reverse_iterator rbegin() const
Definition: BasicBlock.h:237
const BasicBlock * getSinglePredecessor() const
Definition: BasicBlock.h:195
bool empty() const
Definition: BasicBlock.h:242
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:94
iplist< Instruction > InstListType
Definition: BasicBlock.h:69
const_iterator getFirstInsertionPt() const
Definition: BasicBlock.h:164
InstListType::reverse_iterator reverse_iterator
Definition: BasicBlock.h:95
const Instruction * getFirstNonPHI() const
Definition: BasicBlock.h:141
void replaceSuccessorsPhiUsesWith(BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of to it...
Definition: BasicBlock.cpp:390
const BasicBlock * getUniquePredecessor() const
Definition: BasicBlock.h:206
Instruction & front()
Definition: BasicBlock.h:244
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
size_type LLVM_ATTRIBUTE_UNUSED_RESULT size() const
Definition: ilist.h:539
Function * getParent()
Definition: BasicBlock.h:112
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:418
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:252
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:362
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
const BasicBlock * getUniqueSuccessor() const
Definition: BasicBlock.h:224
BasicBlock * provideInitialHead() const
Definition: BasicBlock.h:40
Instruction & back()
Definition: BasicBlock.h:246
Instruction * getFirstNonPHIOrDbgOrLifetime()
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic...
Definition: BasicBlock.cpp:179
const CallInst * getTerminatingMustTailCall() const
Definition: BasicBlock.h:130
void removeFromParent()
Unlink 'this' from the containing function, but do not delete it.
Definition: BasicBlock.cpp:93
const Instruction * getFirstNonPHIOrDbgOrLifetime() const
Definition: BasicBlock.h:155
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
Definition: BasicBlock.cpp:110
static iplist< Instruction > BasicBlock::* getSublistAccess(Instruction *)
Returns a pointer to a member of the instruction list.
Definition: BasicBlock.h:256
struct LLVMOpaqueBasicBlock * LLVMBasicBlockRef
Represents a basic block of instructions in LLVM IR.
Definition: Core.h:99
iterator end()
Definition: BasicBlock.h:233
const_iterator end() const
Definition: BasicBlock.h:234
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: ilist.h:385
std::reverse_iterator< iterator > reverse_iterator
Definition: ilist.h:349
void setValueSubclassData(unsigned short D)
Definition: Value.h:508
~BasicBlock() override
Definition: BasicBlock.cpp:64
BasicBlock * getSingleSuccessor()
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:238
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:211
reference front()
Definition: ilist.h:390
unsigned short getSubclassDataFromValue() const
Definition: Value.h:507
ilist_node - Base class that provides next/prev services for nodes that use ilist_nextprev_traits or ...
Definition: ilist_node.h:43
BasicBlock * ensureHead(BasicBlock *) const
Definition: BasicBlock.h:41
iplist< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition: BasicBlock.cpp:97
ValueSymbolTable * getValueSymbolTable()
Returns a pointer to the symbol table if one exists.
Definition: BasicBlock.cpp:26
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:413
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: BasicBlock.h:264
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition: Value.cpp:138
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:348
size_t size() const
Definition: BasicBlock.h:241
reference back()
Definition: ilist.h:398
static NodeTy * createSentinel()
createSentinel - create the dynamic sentinel
Definition: ilist.h:78
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
iterator end()
Definition: ilist.h:367
LLVM Value Representation.
Definition: Value.h:69
static void destroySentinel(BasicBlock *)
Definition: BasicBlock.h:38
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: ilist.h:348
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:194
InstListType::const_reverse_iterator const_reverse_iterator
Definition: BasicBlock.h:96
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition: BasicBlock.cpp:103
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:93
void dropAllReferences()
Cause all subinstructions to "let go" of all the references that said subinstructions are maintaining...
Definition: BasicBlock.cpp:204
const Instruction * getFirstNonPHIOrDbg() const
Definition: BasicBlock.h:148