148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9febdf58538c2510ec1c24d3a856420792c53debeChris Lattner//
10bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer// This file contains the declaration of the BasicBlock class.
1102dea8b39f3acad5de1df36273444d149145e7fcDan Gohman//
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_BASICBLOCK_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_BASICBLOCK_H
16009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
175a96f93573439be771ce9609fa2a5754c94485b6Chris Lattner#include "llvm/ADT/Twine.h"
18255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/ilist.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.h"
200b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/SymbolTableListTraits.h"
2140be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#include "llvm/Support/CBindingWrapping.h"
221f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
23009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
26e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendlingclass LandingPadInst;
27009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass TerminatorInst;
2812ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext;
293821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattnerclass BlockAddress;
30009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3118961504fc2b299578dba817900a0696cf3ccc4dChris Lattnertemplate<> struct ilist_traits<Instruction>
3217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner  : public SymbolTableListTraits<Instruction, BasicBlock> {
33a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman
34a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Return a node that marks the end of a list.
35a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
36a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// The sentinel is relative to this instance, so we use a non-static
37a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// method.
380a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greif  Instruction *createSentinel() const {
390e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // Since i(p)lists always publicly derive from their corresponding traits,
400e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // placing a data member in this class will augment the i(p)list.  But since
410e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // the NodeTy is expected to be publicly derive from ilist_node<NodeTy>,
420e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // there is a legal viable downcast from it to NodeTy. We use this trick to
430e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // superimpose an i(p)list with a "ghostly" NodeTy, which becomes the
440e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // sentinel. Dereferencing the sentinel is forbidden (save the
450e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman    // ilist_node<NodeTy>), so no one will ever notice the superposition.
461012919ed8b3e2cd4b421c104ce9d8f4e20ced9dGabor Greif    return static_cast<Instruction*>(&Sentinel);
470a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greif  }
487eef1800ea8745a814b86c8610086e9f16312dcaGabor Greif  static void destroySentinel(Instruction*) {}
49c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif
50c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif  Instruction *provideInitialHead() const { return createSentinel(); }
51c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif  Instruction *ensureHead(Instruction*) const { return createSentinel(); }
52f3841fcbd587c31aa9842b3f33bd57de40c9f443Gabor Greif  static void noteHead(Instruction*, Instruction*) {}
530a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greifprivate:
547309be6735666143bd9835b275dc8501617a2591Gabor Greif  mutable ilist_half_node<Instruction> Sentinel;
5518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner};
5618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner
57a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman/// \brief LLVM Basic Block Representation
58a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman///
59bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// This represents a single basic block in LLVM. A basic block is simply a
60bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// container of instructions that execute sequentially. Basic blocks are Values
61bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// because they are referenced by instructions such as branches and switch
62bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
63bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// represents a label to which a branch can jump.
64bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer///
651f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// A well formed basic block is formed of a list of non-terminating
661f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// instructions followed by a single TerminatorInst instruction.
671f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// TerminatorInst's may not occur in the middle of basic blocks, and must
68bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// terminate the blocks. The BasicBlock class allows malformed basic blocks to
69bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// occur because it may be useful in the intermediate stage of constructing or
70bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// modifying a program. However, the verifier will ensure that basic blocks
71bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// are "well formed".
72fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohmanclass BasicBlock : public Value, // Basic blocks are data objects also
73fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman                   public ilist_node<BasicBlock> {
743821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner  friend class BlockAddress;
751fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic:
7618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  typedef iplist<Instruction> InstListType;
770a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greifprivate:
78009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  InstListType InstList;
7917fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner  Function *Parent;
8018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner
81fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner  void setParent(Function *parent);
8217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner  friend class SymbolTableListTraits<BasicBlock, Function>;
83009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
849f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  BasicBlock(const BasicBlock &) LLVM_DELETED_FUNCTION;
859f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  void operator=(const BasicBlock &) LLVM_DELETED_FUNCTION;
86009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
87a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Constructor.
88fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner  ///
89a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// If the function parameter is specified, the basic block is automatically
90a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// inserted at either the end of the function (if InsertBefore is null), or
91a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// before the specified basic block.
921d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
93dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                      Function *Parent = nullptr,
94dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                      BasicBlock *InsertBefore = nullptr);
95051a950000e21935165db56695e35bade668193bGabor Greifpublic:
96a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Get the context in which this basic block lives.
97e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &getContext() const;
981f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
99051a950000e21935165db56695e35bade668193bGabor Greif  /// Instruction iterators...
100a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  typedef InstListType::iterator iterator;
101a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  typedef InstListType::const_iterator const_iterator;
102a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  typedef InstListType::reverse_iterator reverse_iterator;
103a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  typedef InstListType::const_reverse_iterator const_reverse_iterator;
104051a950000e21935165db56695e35bade668193bGabor Greif
105a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Creates a new BasicBlock.
106a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
107a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// If the Parent parameter is specified, the basic block is automatically
108a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// inserted at either the end of the function (if InsertBefore is 0), or
109a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// before the specified basic block.
1101f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer  static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
111dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            Function *Parent = nullptr,
112dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            BasicBlock *InsertBefore = nullptr) {
1131d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    return new BasicBlock(Context, Name, Parent, InsertBefore);
114051a950000e21935165db56695e35bade668193bGabor Greif  }
115afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen  ~BasicBlock();
116009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
117a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Return the enclosing method, or null if none.
11817fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner  const Function *getParent() const { return Parent; }
11917fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner        Function *getParent()       { return Parent; }
120009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const DataLayout *getDataLayout() const;
12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
123a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns the terminator instruction if the block is well formed or
124a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// null if the block is not well formed.
125009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  TerminatorInst *getTerminator();
12650cdabcfd52e88381ade61450d98a1c757195befDan Gohman  const TerminatorInst *getTerminator() const;
1271f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
128a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns a pointer to the first instruction in this block that is
129a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// not a PHINode instruction.
130a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
131a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// When adding instructions to the beginning of the basic block, they should
132a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// be added before the returned value, not before the first instruction,
133a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// which might be PHI. Returns 0 is there's no non-PHI instruction.
134dd49dbfe44098eb53b1ac29f017e422147572bbbVladimir Prus  Instruction* getFirstNonPHI();
13502dea8b39f3acad5de1df36273444d149145e7fcDan Gohman  const Instruction* getFirstNonPHI() const {
13602dea8b39f3acad5de1df36273444d149145e7fcDan Gohman    return const_cast<BasicBlock*>(this)->getFirstNonPHI();
13702dea8b39f3acad5de1df36273444d149145e7fcDan Gohman  }
1387249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen
139a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns a pointer to the first instruction in this block that is not
140a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// a PHINode or a debug intrinsic.
1417249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen  Instruction* getFirstNonPHIOrDbg();
1427249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen  const Instruction* getFirstNonPHIOrDbg() const {
1437249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen    return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg();
1447249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen  }
1451f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
146a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns a pointer to the first instruction in this block that is not
147a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// a PHINode, a debug intrinsic, or a lifetime intrinsic.
14877a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola  Instruction* getFirstNonPHIOrDbgOrLifetime();
14977a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola  const Instruction* getFirstNonPHIOrDbgOrLifetime() const {
15077a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola    return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime();
15177a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola  }
15277a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola
153a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns an iterator to the first instruction in this block that is
154a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// suitable for inserting a non-PHI instruction.
155a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
156a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// In particular, it skips all PHIs and LandingPad instructions.
157d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling  iterator getFirstInsertionPt();
158d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling  const_iterator getFirstInsertionPt() const {
159d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling    return const_cast<BasicBlock*>(this)->getFirstInsertionPt();
160d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling  }
161d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling
162a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Unlink 'this' from the containing function, but do not delete it.
163b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner  void removeFromParent();
164b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner
165a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Unlink 'this' from the containing function and delete it.
166b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner  void eraseFromParent();
1671f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
168a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Unlink this basic block from its current function and insert it
169a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// into the function that \p MovePos lives in, right before \p MovePos.
17007216eabb7e749dc38f521e73485db553cefc8d4Chris Lattner  void moveBefore(BasicBlock *MovePos);
1711f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
172a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Unlink this basic block from its current function and insert it
173a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// right after \p MovePos in the function \p MovePos lives in.
174a71965b1adf6bfeddfd3b38fdf7df9b4412bc6c2Chris Lattner  void moveAfter(BasicBlock *MovePos);
1751f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
176b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner
177dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Return the predecessor of this block if it has a single predecessor
178dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// block. Otherwise return a null pointer.
179ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner  BasicBlock *getSinglePredecessor();
180ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner  const BasicBlock *getSinglePredecessor() const {
181ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner    return const_cast<BasicBlock*>(this)->getSinglePredecessor();
182ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner  }
183b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner
184dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Return the predecessor of this block if it has a unique predecessor
185dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// block. Otherwise return a null pointer.
186a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
1871f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer  /// Note that unique predecessor doesn't mean single edge, there can be
188a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// multiple edges from the unique predecessor to this block (for example a
189a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// switch statement with multiple cases having the same destination).
19087f1e7796d02ea991bdbf084f312879988732a26Torok Edwin  BasicBlock *getUniquePredecessor();
19187f1e7796d02ea991bdbf084f312879988732a26Torok Edwin  const BasicBlock *getUniquePredecessor() const {
19287f1e7796d02ea991bdbf084f312879988732a26Torok Edwin    return const_cast<BasicBlock*>(this)->getUniquePredecessor();
19387f1e7796d02ea991bdbf084f312879988732a26Torok Edwin  }
19487f1e7796d02ea991bdbf084f312879988732a26Torok Edwin
1951020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  //===--------------------------------------------------------------------===//
196fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner  /// Instruction iterator methods
197fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner  ///
1981020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  inline iterator                begin()       { return InstList.begin(); }
1991020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  inline const_iterator          begin() const { return InstList.begin(); }
2001020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  inline iterator                end  ()       { return InstList.end();   }
2011020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  inline const_iterator          end  () const { return InstList.end();   }
2021020b3982c9eae15844c5612b0cf251917931b1dChris Lattner
203a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  inline reverse_iterator        rbegin()       { return InstList.rbegin(); }
204a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  inline const_reverse_iterator  rbegin() const { return InstList.rbegin(); }
205a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  inline reverse_iterator        rend  ()       { return InstList.rend();   }
206a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth  inline const_reverse_iterator  rend  () const { return InstList.rend();   }
207a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth
208c063502e326fe0206942192773b263a3d88d93f5Chris Lattner  inline size_t                   size() const { return InstList.size();  }
2091020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  inline bool                    empty() const { return InstList.empty(); }
21018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  inline const Instruction      &front() const { return InstList.front(); }
21118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  inline       Instruction      &front()       { return InstList.front(); }
212a4c6c522ee029e97aedccb6ee7cca912d52a5599Misha Brukman  inline const Instruction       &back() const { return InstList.back();  }
213a4c6c522ee029e97aedccb6ee7cca912d52a5599Misha Brukman  inline       Instruction       &back()       { return InstList.back();  }
2141020b3982c9eae15844c5612b0cf251917931b1dChris Lattner
215a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Return the underlying instruction list container.
21626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
217a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// Currently you need to access the underlying instruction list container
218a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// directly if you want to modify it.
2191020b3982c9eae15844c5612b0cf251917931b1dChris Lattner  const InstListType &getInstList() const { return InstList; }
2201020b3982c9eae15844c5612b0cf251917931b1dChris Lattner        InstListType &getInstList()       { return InstList; }
2210dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif
222a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns a pointer to a member of the instruction list.
223b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif  static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
224b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif    return &BasicBlock::InstList;
225b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif  }
2261020b3982c9eae15844c5612b0cf251917931b1dChris Lattner
227a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns a pointer to the symbol table if one exists.
2280dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif  ValueSymbolTable *getValueSymbolTable();
2290dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif
230a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
231b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner  static inline bool classof(const Value *V) {
232a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == Value::BasicBlockVal;
2339636a91649f168f41b477cba705287665e054f79Chris Lattner  }
2349636a91649f168f41b477cba705287665e054f79Chris Lattner
235a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Cause all subinstructions to "let go" of all the references that
236a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// said subinstructions are maintaining.
23726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
238a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// This allows one to 'delete' a whole class at a time, even though there may
239a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// be circular references... first all references are dropped, and all use
240a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// counts go to zero.  Then everything is delete'd for real.  Note that no
241a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// operations are valid on an object that has "dropped all references",
242a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// except operator delete.
243009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  void dropAllReferences();
244009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
245a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer
246a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// able to reach it.
24726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
248a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// This is actually not used to update the Predecessor list, but is actually
249a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// used to update the PHI nodes that reside in the block.  Note that this
250a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// should be called while the predecessor still refers to this block.
251280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky  void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
2527b6f5a3ee9a68d0f6206b1695f71551a467e33c4Chris Lattner
253a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Split the basic block into two basic blocks at the specified
254a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// instruction.
255a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
256a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// Note that all instructions BEFORE the specified iterator stay as part of
257a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// the original basic block, an unconditional branch is added to the original
258a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// BB, and the rest of the instructions in the BB are moved to the new BB,
259a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// including the old terminator.  The newly formed BasicBlock is returned.
260a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// This function invalidates the specified iterator.
26126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
2629769ab22265b313171d201b5928688524a01bd87Misha Brukman  /// Note that this only works on well formed basic blocks (must have a
26326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// terminator), and 'I' must not be the end of instruction list (which would
26426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// cause a degenerate basic block to be formed, having a terminator inside of
26526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// the basic block).
26626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
267dc404a23ca92d6a4a58b693fc0234a617539600eDan Gohman  /// Also note that this doesn't preserve any passes. To split blocks while
268dc404a23ca92d6a4a58b693fc0234a617539600eDan Gohman  /// keeping loop information consistent, use the SplitBlock utility function.
2696e0d1cb30957a636c53158d3089e6fb88348a57aDaniel Dunbar  BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
270eeb8ef1f37a8d106a9fb77b9dd6a7ab6866904e5Dan Gohman
271a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Returns true if there are any uses of this basic block other than
272a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// direct branches, switches, etc. to it.
273cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
2741f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
275a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Update all phi nodes in this basic block's successors to refer to
276a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// basic block \p New instead of to it.
27795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad  void replaceSuccessorsPhiUsesWith(BasicBlock *New);
27895c3e48f9557adb6064d580684bb14cacec2f826Jay Foad
279a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Return true if this basic block is a landing pad.
280a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
281a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// Being a ``landing pad'' means that the basic block is the destination of
282a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// the 'unwind' edge of an invoke instruction.
283e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling  bool isLandingPad() const;
284e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling
285a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Return the landingpad instruction associated with the landing pad.
286e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling  LandingPadInst *getLandingPadInst();
2877750c3fc9f1dc47fce14c5dbb6c17bf5b52f3ba1Bill Wendling  const LandingPadInst *getLandingPadInst() const;
288e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling
2893821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattnerprivate:
290a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// \brief Increment the internal refcount of the number of BlockAddresses
291a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// referencing this BasicBlock by \p Amt.
292a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  ///
293a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// This is almost always 0, sometimes one possibly, but almost never 2, and
294a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman  /// inconceivably 3 or more.
2953821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner  void AdjustBlockAddressRefCount(int Amt) {
296cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    setValueSubclassData(getSubclassDataFromValue()+Amt);
297cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
298cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner           "Refcount wrap-around");
299cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  }
3000e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman  /// \brief Shadow Value::setValueSubclassData with a private forwarding method
3010e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman  /// so that any future subclasses cannot accidentally use it.
302cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  void setValueSubclassData(unsigned short D) {
303cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    Value::setValueSubclassData(D);
3043821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner  }
305009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
306009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30740be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
30840be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
30940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
310d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
311d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
312009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
313