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 2637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass CallInst; 27e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendlingclass LandingPadInst; 28009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass TerminatorInst; 2912ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext; 303821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattnerclass BlockAddress; 312c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainarclass Function; 32009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 332c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar// Traits for intrusive list of basic blocks... 342c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainartemplate<> struct ilist_traits<BasicBlock> 352c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar : public SymbolTableListTraits<BasicBlock, Function> { 36a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman 372c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar BasicBlock *createSentinel() const; 382c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar static void destroySentinel(BasicBlock*) {} 392c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar 402c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar BasicBlock *provideInitialHead() const { return createSentinel(); } 412c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); } 422c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar static void noteHead(BasicBlock*, BasicBlock*) {} 43c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif 442c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar static ValueSymbolTable *getSymTab(Function *ItemParent); 450a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greifprivate: 462c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar mutable ilist_half_node<BasicBlock> Sentinel; 4718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner}; 4818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner 492c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar 50a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman/// \brief LLVM Basic Block Representation 51a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman/// 52bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// This represents a single basic block in LLVM. A basic block is simply a 53bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// container of instructions that execute sequentially. Basic blocks are Values 54bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// because they are referenced by instructions such as branches and switch 55bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 56bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// represents a label to which a branch can jump. 57bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// 581f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// A well formed basic block is formed of a list of non-terminating 591f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// instructions followed by a single TerminatorInst instruction. 601f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer/// TerminatorInst's may not occur in the middle of basic blocks, and must 61bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// terminate the blocks. The BasicBlock class allows malformed basic blocks to 62bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// occur because it may be useful in the intermediate stage of constructing or 63bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// modifying a program. However, the verifier will ensure that basic blocks 64bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// are "well formed". 65fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohmanclass BasicBlock : public Value, // Basic blocks are data objects also 66fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman public ilist_node<BasicBlock> { 673821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner friend class BlockAddress; 681fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic: 6918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner typedef iplist<Instruction> InstListType; 700a0e68a7eac0513505aff3079e2d5d6864e51895Gabor Greifprivate: 71009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner InstListType InstList; 7217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner Function *Parent; 7318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner 74fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner void setParent(Function *parent); 7517fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner friend class SymbolTableListTraits<BasicBlock, Function>; 76009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BasicBlock(const BasicBlock &) = delete; 78ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void operator=(const BasicBlock &) = delete; 79009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 80a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Constructor. 81fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner /// 82a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// If the function parameter is specified, the basic block is automatically 83a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// inserted at either the end of the function (if InsertBefore is null), or 84a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// before the specified basic block. 851d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson explicit BasicBlock(LLVMContext &C, const Twine &Name = "", 86dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Function *Parent = nullptr, 87dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BasicBlock *InsertBefore = nullptr); 88051a950000e21935165db56695e35bade668193bGabor Greifpublic: 89a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Get the context in which this basic block lives. 90e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson LLVMContext &getContext() const; 911f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 92051a950000e21935165db56695e35bade668193bGabor Greif /// Instruction iterators... 93a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth typedef InstListType::iterator iterator; 94a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth typedef InstListType::const_iterator const_iterator; 95a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth typedef InstListType::reverse_iterator reverse_iterator; 96a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth typedef InstListType::const_reverse_iterator const_reverse_iterator; 97051a950000e21935165db56695e35bade668193bGabor Greif 98a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Creates a new BasicBlock. 99a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 100a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// If the Parent parameter is specified, the basic block is automatically 101a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// inserted at either the end of the function (if InsertBefore is 0), or 102a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// before the specified basic block. 1031f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", 104dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Function *Parent = nullptr, 105dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BasicBlock *InsertBefore = nullptr) { 1061d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson return new BasicBlock(Context, Name, Parent, InsertBefore); 107051a950000e21935165db56695e35bade668193bGabor Greif } 1082c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar ~BasicBlock() override; 109009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 110a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Return the enclosing method, or null if none. 11117fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner const Function *getParent() const { return Parent; } 11217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner Function *getParent() { return Parent; } 113009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 1144c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// \brief Return the module owning the function this basic block belongs to, 1154c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// or nullptr it the function does not have a module. 1164c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// 1174c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// Note: this is undefined behavior if the block does not have a parent. 1184c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar const Module *getModule() const; 11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 120a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns the terminator instruction if the block is well formed or 121a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// null if the block is not well formed. 122009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner TerminatorInst *getTerminator(); 12350cdabcfd52e88381ade61450d98a1c757195befDan Gohman const TerminatorInst *getTerminator() const; 1241f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 12537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief Returns the call instruction marked 'musttail' prior to the 12637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// terminating return instruction of this basic block, if such a call is 12737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// present. Otherwise, returns null. 12837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CallInst *getTerminatingMustTailCall(); 12937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines const CallInst *getTerminatingMustTailCall() const { 13037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return const_cast<BasicBlock *>(this)->getTerminatingMustTailCall(); 13137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 13237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 133a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns a pointer to the first instruction in this block that is 134a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// not a PHINode instruction. 135a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 136a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// When adding instructions to the beginning of the basic block, they should 137a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// be added before the returned value, not before the first instruction, 138a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// which might be PHI. Returns 0 is there's no non-PHI instruction. 139dd49dbfe44098eb53b1ac29f017e422147572bbbVladimir Prus Instruction* getFirstNonPHI(); 14002dea8b39f3acad5de1df36273444d149145e7fcDan Gohman const Instruction* getFirstNonPHI() const { 14102dea8b39f3acad5de1df36273444d149145e7fcDan Gohman return const_cast<BasicBlock*>(this)->getFirstNonPHI(); 14202dea8b39f3acad5de1df36273444d149145e7fcDan Gohman } 1437249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen 144a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns a pointer to the first instruction in this block that is not 145a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// a PHINode or a debug intrinsic. 1467249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen Instruction* getFirstNonPHIOrDbg(); 1477249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen const Instruction* getFirstNonPHIOrDbg() const { 1487249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg(); 1497249ef04557cc6f9af7b6df93728683be3b65048Dale Johannesen } 1501f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 151a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns a pointer to the first instruction in this block that is not 152a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// a PHINode, a debug intrinsic, or a lifetime intrinsic. 15377a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola Instruction* getFirstNonPHIOrDbgOrLifetime(); 15477a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola const Instruction* getFirstNonPHIOrDbgOrLifetime() const { 15577a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime(); 15677a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola } 15777a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola 158a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns an iterator to the first instruction in this block that is 159a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// suitable for inserting a non-PHI instruction. 160a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 161a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// In particular, it skips all PHIs and LandingPad instructions. 162d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling iterator getFirstInsertionPt(); 163d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling const_iterator getFirstInsertionPt() const { 164d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling return const_cast<BasicBlock*>(this)->getFirstInsertionPt(); 165d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling } 166d2e103daf8a093e0e25ddbaa7549c083887196e8Bill Wendling 167a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Unlink 'this' from the containing function, but do not delete it. 168b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner void removeFromParent(); 169b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner 170a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Unlink 'this' from the containing function and delete it. 1712c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar /// 1722c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar // \returns an iterator pointing to the element after the erased one. 1732c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar iplist<BasicBlock>::iterator eraseFromParent(); 1741f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 175a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Unlink this basic block from its current function and insert it 176a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// into the function that \p MovePos lives in, right before \p MovePos. 17707216eabb7e749dc38f521e73485db553cefc8d4Chris Lattner void moveBefore(BasicBlock *MovePos); 1781f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 179a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Unlink this basic block from its current function and insert it 180a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// right after \p MovePos in the function \p MovePos lives in. 181a71965b1adf6bfeddfd3b38fdf7df9b4412bc6c2Chris Lattner void moveAfter(BasicBlock *MovePos); 1821f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 18337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief Insert unlinked basic block into a function. 18437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// 18537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is 18637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// provided, inserts before that basic block, otherwise inserts at the end. 18737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// 18837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \pre \a getParent() is \c nullptr. 18937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); 190b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner 191dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /// \brief Return the predecessor of this block if it has a single predecessor 192dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /// block. Otherwise return a null pointer. 193ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner BasicBlock *getSinglePredecessor(); 194ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner const BasicBlock *getSinglePredecessor() const { 195ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner return const_cast<BasicBlock*>(this)->getSinglePredecessor(); 196ad993cbb77b26b36cee938686b3377c0d92abd5eChris Lattner } 197b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner 198dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /// \brief Return the predecessor of this block if it has a unique predecessor 199dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /// block. Otherwise return a null pointer. 200a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 2011f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer /// Note that unique predecessor doesn't mean single edge, there can be 202a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// multiple edges from the unique predecessor to this block (for example a 203a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// switch statement with multiple cases having the same destination). 20487f1e7796d02ea991bdbf084f312879988732a26Torok Edwin BasicBlock *getUniquePredecessor(); 20587f1e7796d02ea991bdbf084f312879988732a26Torok Edwin const BasicBlock *getUniquePredecessor() const { 20687f1e7796d02ea991bdbf084f312879988732a26Torok Edwin return const_cast<BasicBlock*>(this)->getUniquePredecessor(); 20787f1e7796d02ea991bdbf084f312879988732a26Torok Edwin } 20887f1e7796d02ea991bdbf084f312879988732a26Torok Edwin 209ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// Return the successor of this block if it has a unique successor. 210ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// Otherwise return a null pointer. This method is analogous to 211ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// getUniquePredeccessor above. 212ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BasicBlock *getUniqueSuccessor(); 213ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines const BasicBlock *getUniqueSuccessor() const { 214ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return const_cast<BasicBlock*>(this)->getUniqueSuccessor(); 215ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2171020b3982c9eae15844c5612b0cf251917931b1dChris Lattner //===--------------------------------------------------------------------===// 218fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner /// Instruction iterator methods 219fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner /// 2201020b3982c9eae15844c5612b0cf251917931b1dChris Lattner inline iterator begin() { return InstList.begin(); } 2211020b3982c9eae15844c5612b0cf251917931b1dChris Lattner inline const_iterator begin() const { return InstList.begin(); } 2221020b3982c9eae15844c5612b0cf251917931b1dChris Lattner inline iterator end () { return InstList.end(); } 2231020b3982c9eae15844c5612b0cf251917931b1dChris Lattner inline const_iterator end () const { return InstList.end(); } 2241020b3982c9eae15844c5612b0cf251917931b1dChris Lattner 225a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth inline reverse_iterator rbegin() { return InstList.rbegin(); } 226a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } 227a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth inline reverse_iterator rend () { return InstList.rend(); } 228a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth inline const_reverse_iterator rend () const { return InstList.rend(); } 229a455fdd7e1047ff10ee32070aa3cf1c16182c202Chandler Carruth 230c063502e326fe0206942192773b263a3d88d93f5Chris Lattner inline size_t size() const { return InstList.size(); } 2311020b3982c9eae15844c5612b0cf251917931b1dChris Lattner inline bool empty() const { return InstList.empty(); } 23218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner inline const Instruction &front() const { return InstList.front(); } 23318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner inline Instruction &front() { return InstList.front(); } 234a4c6c522ee029e97aedccb6ee7cca912d52a5599Misha Brukman inline const Instruction &back() const { return InstList.back(); } 235a4c6c522ee029e97aedccb6ee7cca912d52a5599Misha Brukman inline Instruction &back() { return InstList.back(); } 2361020b3982c9eae15844c5612b0cf251917931b1dChris Lattner 237a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Return the underlying instruction list container. 23826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 239a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// Currently you need to access the underlying instruction list container 240a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// directly if you want to modify it. 2411020b3982c9eae15844c5612b0cf251917931b1dChris Lattner const InstListType &getInstList() const { return InstList; } 2421020b3982c9eae15844c5612b0cf251917931b1dChris Lattner InstListType &getInstList() { return InstList; } 2430dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif 244a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns a pointer to a member of the instruction list. 245b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) { 246b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif return &BasicBlock::InstList; 247b547a181005cc255fa57c61c1c0dbafca5375fb4Gabor Greif } 2481020b3982c9eae15844c5612b0cf251917931b1dChris Lattner 249a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns a pointer to the symbol table if one exists. 2500dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif ValueSymbolTable *getValueSymbolTable(); 2510dd2a6a89f49438b239638ab147ac5746d6c32c3Gabor Greif 252a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Methods for support type inquiry through isa, cast, and dyn_cast. 253b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner static inline bool classof(const Value *V) { 254a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman return V->getValueID() == Value::BasicBlockVal; 2559636a91649f168f41b477cba705287665e054f79Chris Lattner } 2569636a91649f168f41b477cba705287665e054f79Chris Lattner 257a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Cause all subinstructions to "let go" of all the references that 258a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// said subinstructions are maintaining. 25926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 260a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// This allows one to 'delete' a whole class at a time, even though there may 261a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// be circular references... first all references are dropped, and all use 262a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// counts go to zero. Then everything is delete'd for real. Note that no 263a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// operations are valid on an object that has "dropped all references", 264a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// except operator delete. 265009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner void dropAllReferences(); 266009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 267a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer 268a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// able to reach it. 26926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 270a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// This is actually not used to update the Predecessor list, but is actually 271a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// used to update the PHI nodes that reside in the block. Note that this 272a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// should be called while the predecessor still refers to this block. 273280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); 2747b6f5a3ee9a68d0f6206b1695f71551a467e33c4Chris Lattner 275a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Split the basic block into two basic blocks at the specified 276a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// instruction. 277a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 278a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// Note that all instructions BEFORE the specified iterator stay as part of 279a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// the original basic block, an unconditional branch is added to the original 280a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// BB, and the rest of the instructions in the BB are moved to the new BB, 281a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// including the old terminator. The newly formed BasicBlock is returned. 282a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// This function invalidates the specified iterator. 28326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 2849769ab22265b313171d201b5928688524a01bd87Misha Brukman /// Note that this only works on well formed basic blocks (must have a 28526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// terminator), and 'I' must not be the end of instruction list (which would 28626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// cause a degenerate basic block to be formed, having a terminator inside of 28726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// the basic block). 28826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 289dc404a23ca92d6a4a58b693fc0234a617539600eDan Gohman /// Also note that this doesn't preserve any passes. To split blocks while 290dc404a23ca92d6a4a58b693fc0234a617539600eDan Gohman /// keeping loop information consistent, use the SplitBlock utility function. 2916e0d1cb30957a636c53158d3089e6fb88348a57aDaniel Dunbar BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = ""); 292eeb8ef1f37a8d106a9fb77b9dd6a7ab6866904e5Dan Gohman 293a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Returns true if there are any uses of this basic block other than 294a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// direct branches, switches, etc. to it. 295cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } 2961f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 297a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Update all phi nodes in this basic block's successors to refer to 298a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// basic block \p New instead of to it. 29995c3e48f9557adb6064d580684bb14cacec2f826Jay Foad void replaceSuccessorsPhiUsesWith(BasicBlock *New); 30095c3e48f9557adb6064d580684bb14cacec2f826Jay Foad 301a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Return true if this basic block is a landing pad. 302a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 303a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// Being a ``landing pad'' means that the basic block is the destination of 304a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// the 'unwind' edge of an invoke instruction. 305e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling bool isLandingPad() const; 306e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling 307a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Return the landingpad instruction associated with the landing pad. 308e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling LandingPadInst *getLandingPadInst(); 3097750c3fc9f1dc47fce14c5dbb6c17bf5b52f3ba1Bill Wendling const LandingPadInst *getLandingPadInst() const; 310e6e8826870bee3facb04f950f0bd725f8a88623dBill Wendling 3113821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattnerprivate: 312a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// \brief Increment the internal refcount of the number of BlockAddresses 313a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// referencing this BasicBlock by \p Amt. 314a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// 315a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// This is almost always 0, sometimes one possibly, but almost never 2, and 316a1456e1cb8ade1befa7fc9cdac8fd542862cbcd8Michael Gottesman /// inconceivably 3 or more. 3173821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner void AdjustBlockAddressRefCount(int Amt) { 318cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner setValueSubclassData(getSubclassDataFromValue()+Amt); 319cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner assert((int)(signed char)getSubclassDataFromValue() >= 0 && 320cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner "Refcount wrap-around"); 321cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner } 3220e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman /// \brief Shadow Value::setValueSubclassData with a private forwarding method 3230e1a9f27bd6451897144f99ff2b2081986077896Michael Gottesman /// so that any future subclasses cannot accidentally use it. 324cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner void setValueSubclassData(unsigned short D) { 325cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner Value::setValueSubclassData(D); 3263821176b2eb9fe5e66929f3df6f204fa6cb2e4d6Chris Lattner } 327009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}; 328009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 3292c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar// createSentinel is used to get hold of the node that marks the end of the 3302c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar// list... (same trick used here as in ilist_traits<Instruction>) 3312c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainarinline BasicBlock *ilist_traits<BasicBlock>::createSentinel() const { 3322c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar return static_cast<BasicBlock*>(&Sentinel); 3332c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar} 3342c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar 33540be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h). 33640be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) 33740be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo 338d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 339d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 340009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif 341