1f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*-===// 2f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 3f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// The LLVM Compiler Infrastructure 4f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 5f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This file is distributed under the University of Illinois Open Source 6f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// License. See LICENSE.TXT for details. 7f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 8f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 9f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 10f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This file contains the declaration of the BasicBlock class. 11f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 12f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 13f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 14f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#ifndef LLVM_IR_BASICBLOCK_H 15f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#define LLVM_IR_BASICBLOCK_H 16f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 17f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm-c/Types.h" 18f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/Twine.h" 19f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/ilist.h" 20f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/ilist_node.h" 21f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/iterator.h" 22f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/iterator_range.h" 23f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/IR/Instruction.h" 24f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/IR/SymbolTableListTraits.h" 25f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/IR/Value.h" 26f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Support/CBindingWrapping.h" 27f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Support/Casting.h" 28f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Support/Compiler.h" 29f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <cassert> 30f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <cstddef> 31f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <iterator> 32f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 33f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace llvm { 34f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 35f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass CallInst; 36f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Function; 37f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LandingPadInst; 38f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LLVMContext; 39f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Module; 40f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass PHINode; 41f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass TerminatorInst; 42f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass ValueSymbolTable; 43f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 44f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief LLVM Basic Block Representation 45f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 46f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// This represents a single basic block in LLVM. A basic block is simply a 47f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// container of instructions that execute sequentially. Basic blocks are Values 48f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// because they are referenced by instructions such as branches and switch 49f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 50f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// represents a label to which a branch can jump. 51f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 52f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// A well formed basic block is formed of a list of non-terminating 53f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// instructions followed by a single TerminatorInst instruction. 54f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// TerminatorInst's may not occur in the middle of basic blocks, and must 55f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// terminate the blocks. The BasicBlock class allows malformed basic blocks to 56f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// occur because it may be useful in the intermediate stage of constructing or 57f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// modifying a program. However, the verifier will ensure that basic blocks 58f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// are "well formed". 59f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass BasicBlock final : public Value, // Basic blocks are data objects also 60f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot public ilist_node_with_parent<BasicBlock, Function> { 61f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 62f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using InstListType = SymbolTableList<Instruction>; 63f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 64f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 65f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot friend class BlockAddress; 66f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot friend class SymbolTableListTraits<BasicBlock>; 67f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 68f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot InstListType InstList; 69f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Function *Parent; 70f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 71f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void setParent(Function *parent); 72f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 73f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Constructor. 74f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 75f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// If the function parameter is specified, the basic block is automatically 76f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// inserted at either the end of the function (if InsertBefore is null), or 77f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// before the specified basic block. 78f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot explicit BasicBlock(LLVMContext &C, const Twine &Name = "", 79f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Function *Parent = nullptr, 80f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *InsertBefore = nullptr); 81f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 82f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 83f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock(const BasicBlock &) = delete; 84f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock &operator=(const BasicBlock &) = delete; 85f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot ~BasicBlock(); 86f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 87f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the context in which this basic block lives. 88f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LLVMContext &getContext() const; 89f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 90f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Instruction iterators... 91f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using iterator = InstListType::iterator; 92f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using const_iterator = InstListType::const_iterator; 93f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using reverse_iterator = InstListType::reverse_iterator; 94f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using const_reverse_iterator = InstListType::const_reverse_iterator; 95f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 96f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Creates a new BasicBlock. 97f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 98f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// If the Parent parameter is specified, the basic block is automatically 99f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// inserted at either the end of the function (if InsertBefore is 0), or 100f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// before the specified basic block. 101f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", 102f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Function *Parent = nullptr, 103f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *InsertBefore = nullptr) { 104f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return new BasicBlock(Context, Name, Parent, InsertBefore); 105f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 106f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 107f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the enclosing method, or null if none. 108f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Function *getParent() const { return Parent; } 109f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Function *getParent() { return Parent; } 110f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 111f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the module owning the function this basic block belongs to, 112f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// or nullptr it the function does not have a module. 113f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 114f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Note: this is undefined behavior if the block does not have a parent. 115f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Module *getModule() const; 116f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Module *getModule() { 117f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<Module *>( 118f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getModule()); 119f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 120f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 121f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns the terminator instruction if the block is well formed or 122f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// null if the block is not well formed. 123f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const TerminatorInst *getTerminator() const LLVM_READONLY; 124f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot TerminatorInst *getTerminator() { 125f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<TerminatorInst *>( 126f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getTerminator()); 127f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 128f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 129f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns the call instruction calling @llvm.experimental.deoptimize 130f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// prior to the terminating return instruction of this basic block, if such a 131f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// call is present. Otherwise, returns null. 132f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const CallInst *getTerminatingDeoptimizeCall() const; 133f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot CallInst *getTerminatingDeoptimizeCall() { 134f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<CallInst *>( 135f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); 136f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 137f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 138f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns the call instruction marked 'musttail' prior to the 139f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// terminating return instruction of this basic block, if such a call is 140f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// present. Otherwise, returns null. 141f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const CallInst *getTerminatingMustTailCall() const; 142f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot CallInst *getTerminatingMustTailCall() { 143f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<CallInst *>( 144f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); 145f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 146f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 147f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns a pointer to the first instruction in this block that is 148f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// not a PHINode instruction. 149f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 150f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// When adding instructions to the beginning of the basic block, they should 151f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// be added before the returned value, not before the first instruction, 152f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// which might be PHI. Returns 0 is there's no non-PHI instruction. 153f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Instruction* getFirstNonPHI() const; 154f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Instruction* getFirstNonPHI() { 155f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<Instruction *>( 156f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getFirstNonPHI()); 157f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 158f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 159f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns a pointer to the first instruction in this block that is not 160f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// a PHINode or a debug intrinsic. 161f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Instruction* getFirstNonPHIOrDbg() const; 162f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Instruction* getFirstNonPHIOrDbg() { 163f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<Instruction *>( 164f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg()); 165f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 166f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 167f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns a pointer to the first instruction in this block that is not 168f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// a PHINode, a debug intrinsic, or a lifetime intrinsic. 169f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Instruction* getFirstNonPHIOrDbgOrLifetime() const; 170f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Instruction* getFirstNonPHIOrDbgOrLifetime() { 171f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<Instruction *>( 172f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime()); 173f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 174f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 175f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns an iterator to the first instruction in this block that is 176f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// suitable for inserting a non-PHI instruction. 177f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 178f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// In particular, it skips all PHIs and LandingPad instructions. 179f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const_iterator getFirstInsertionPt() const; 180f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot iterator getFirstInsertionPt() { 181f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return static_cast<const BasicBlock *>(this) 182f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot ->getFirstInsertionPt().getNonConst(); 183f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 184f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 185f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Unlink 'this' from the containing function, but do not delete it. 186f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void removeFromParent(); 187f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 188f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Unlink 'this' from the containing function and delete it. 189f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 190f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot // \returns an iterator pointing to the element after the erased one. 191f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot SymbolTableList<BasicBlock>::iterator eraseFromParent(); 192f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 193f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Unlink this basic block from its current function and insert it 194f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// into the function that \p MovePos lives in, right before \p MovePos. 195f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void moveBefore(BasicBlock *MovePos); 196f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 197f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Unlink this basic block from its current function and insert it 198f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// right after \p MovePos in the function \p MovePos lives in. 199f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void moveAfter(BasicBlock *MovePos); 200f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 201f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Insert unlinked basic block into a function. 202f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 203f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is 204f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// provided, inserts before that basic block, otherwise inserts at the end. 205f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 206f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \pre \a getParent() is \c nullptr. 207f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); 208f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 209f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the predecessor of this block if it has a single predecessor 210f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// block. Otherwise return a null pointer. 211f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BasicBlock *getSinglePredecessor() const; 212f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *getSinglePredecessor() { 213f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<BasicBlock *>( 214f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getSinglePredecessor()); 215f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 216f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 217f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the predecessor of this block if it has a unique predecessor 218f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// block. Otherwise return a null pointer. 219f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 220f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Note that unique predecessor doesn't mean single edge, there can be 221f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// multiple edges from the unique predecessor to this block (for example a 222f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// switch statement with multiple cases having the same destination). 223f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BasicBlock *getUniquePredecessor() const; 224f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *getUniquePredecessor() { 225f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<BasicBlock *>( 226f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getUniquePredecessor()); 227f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 228f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 229f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the successor of this block if it has a single successor. 230f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Otherwise return a null pointer. 231f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 232f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This method is analogous to getSinglePredecessor above. 233f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BasicBlock *getSingleSuccessor() const; 234f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *getSingleSuccessor() { 235f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<BasicBlock *>( 236f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getSingleSuccessor()); 237f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 238f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 239f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the successor of this block if it has a unique successor. 240f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Otherwise return a null pointer. 241f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 242f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This method is analogous to getUniquePredecessor above. 243f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BasicBlock *getUniqueSuccessor() const; 244f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *getUniqueSuccessor() { 245f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<BasicBlock *>( 246f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); 247f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 248f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 249f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot //===--------------------------------------------------------------------===// 250f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Instruction iterator methods 251f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 252f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline iterator begin() { return InstList.begin(); } 253f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const_iterator begin() const { return InstList.begin(); } 254f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline iterator end () { return InstList.end(); } 255f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const_iterator end () const { return InstList.end(); } 256f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 257f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline reverse_iterator rbegin() { return InstList.rbegin(); } 258f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } 259f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline reverse_iterator rend () { return InstList.rend(); } 260f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const_reverse_iterator rend () const { return InstList.rend(); } 261f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 262f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline size_t size() const { return InstList.size(); } 263f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline bool empty() const { return InstList.empty(); } 264f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const Instruction &front() const { return InstList.front(); } 265f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline Instruction &front() { return InstList.front(); } 266f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline const Instruction &back() const { return InstList.back(); } 267f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot inline Instruction &back() { return InstList.back(); } 268f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 269f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Iterator to walk just the phi nodes in the basic block. 270f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> 271f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot class phi_iterator_impl 272f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, 273f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::forward_iterator_tag, PHINodeT> { 274f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot friend BasicBlock; 275f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 276f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot PHINodeT *PN; 277f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 278f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot phi_iterator_impl(PHINodeT *PN) : PN(PN) {} 279f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 280f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot public: 281f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot // Allow default construction to build variables, but this doesn't build 282f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot // a useful iterator. 283f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot phi_iterator_impl() = default; 284f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 285f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot // Allow conversion between instantiations where valid. 286f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot template <typename PHINodeU, typename BBIteratorU> 287f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) 288f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot : PN(Arg.PN) {} 289f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 290f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } 291f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 292f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot PHINodeT &operator*() const { return *PN; } 293f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 294f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using phi_iterator_impl::iterator_facade_base::operator++; 295f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot phi_iterator_impl &operator++() { 296f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot assert(PN && "Cannot increment the end iterator!"); 297f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); 298f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return *this; 299f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 300f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot }; 301f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using phi_iterator = phi_iterator_impl<>; 302f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot using const_phi_iterator = 303f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; 304f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 305f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Returns a range that iterates over the phis in the basic block. 306f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 307f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Note that this cannot be used with basic blocks that have no terminator. 308f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot iterator_range<const_phi_iterator> phis() const { 309f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<BasicBlock *>(this)->phis(); 310f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 311f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot iterator_range<phi_iterator> phis(); 312f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 313f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the underlying instruction list container. 314f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 315f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Currently you need to access the underlying instruction list container 316f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// directly if you want to modify it. 317f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const InstListType &getInstList() const { return InstList; } 318f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot InstListType &getInstList() { return InstList; } 319f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 320f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns a pointer to a member of the instruction list. 321f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static InstListType BasicBlock::*getSublistAccess(Instruction*) { 322f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return &BasicBlock::InstList; 323f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 324f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 325f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns a pointer to the symbol table if one exists. 326f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot ValueSymbolTable *getValueSymbolTable(); 327f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 328f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Methods for support type inquiry through isa, cast, and dyn_cast. 329f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static bool classof(const Value *V) { 330f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return V->getValueID() == Value::BasicBlockVal; 331f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 332f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 333f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Cause all subinstructions to "let go" of all the references that 334f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// said subinstructions are maintaining. 335f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 336f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This allows one to 'delete' a whole class at a time, even though there may 337f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// be circular references... first all references are dropped, and all use 338f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// counts go to zero. Then everything is delete'd for real. Note that no 339f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// operations are valid on an object that has "dropped all references", 340f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// except operator delete. 341f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void dropAllReferences(); 342f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 343f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer 344f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// able to reach it. 345f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 346f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This is actually not used to update the Predecessor list, but is actually 347f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// used to update the PHI nodes that reside in the block. Note that this 348f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// should be called while the predecessor still refers to this block. 349f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); 350f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 351f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool canSplitPredecessors() const; 352f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 353f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Split the basic block into two basic blocks at the specified 354f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// instruction. 355f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 356f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Note that all instructions BEFORE the specified iterator stay as part of 357f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// the original basic block, an unconditional branch is added to the original 358f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// BB, and the rest of the instructions in the BB are moved to the new BB, 359f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// including the old terminator. The newly formed BasicBlock is returned. 360f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This function invalidates the specified iterator. 361f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 362f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Note that this only works on well formed basic blocks (must have a 363f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// terminator), and 'I' must not be the end of instruction list (which would 364f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// cause a degenerate basic block to be formed, having a terminator inside of 365f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// the basic block). 366f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 367f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Also note that this doesn't preserve any passes. To split blocks while 368f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// keeping loop information consistent, use the SplitBlock utility function. 369f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = ""); 370f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "") { 371f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return splitBasicBlock(I->getIterator(), BBName); 372f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 373f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 374f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Returns true if there are any uses of this basic block other than 375f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// direct branches, switches, etc. to it. 376f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } 377f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 378f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Update all phi nodes in this basic block's successors to refer to 379f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// basic block \p New instead of to it. 380f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void replaceSuccessorsPhiUsesWith(BasicBlock *New); 381f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 382f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return true if this basic block is an exception handling block. 383f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } 384f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 385f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return true if this basic block is a landing pad. 386f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 387f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Being a ``landing pad'' means that the basic block is the destination of 388f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// the 'unwind' edge of an invoke instruction. 389f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool isLandingPad() const; 390f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 391f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the landingpad instruction associated with the landing pad. 392f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const LandingPadInst *getLandingPadInst() const; 393f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LandingPadInst *getLandingPadInst() { 394f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<LandingPadInst *>( 395f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static_cast<const BasicBlock *>(this)->getLandingPadInst()); 396f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 397f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 398f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return true if it is legal to hoist instructions into this block. 399f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool isLegalToHoistInto() const; 400f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 401f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 402f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Increment the internal refcount of the number of BlockAddresses 403f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// referencing this BasicBlock by \p Amt. 404f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 405f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// This is almost always 0, sometimes one possibly, but almost never 2, and 406f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// inconceivably 3 or more. 407f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void AdjustBlockAddressRefCount(int Amt) { 408f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot setValueSubclassData(getSubclassDataFromValue()+Amt); 409f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot assert((int)(signed char)getSubclassDataFromValue() >= 0 && 410f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot "Refcount wrap-around"); 411f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 412f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 413f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Shadow Value::setValueSubclassData with a private forwarding method 414f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// so that any future subclasses cannot accidentally use it. 415f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void setValueSubclassData(unsigned short D) { 416f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Value::setValueSubclassData(D); 417f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 418f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 419f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 420f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// Create wrappers for C Binding types (see CBindingWrapping.h). 421f3014761c955345d6e05491608e73228d014afbandroid-build-team RobotDEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) 422f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 423f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} // end namespace llvm 424f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 425f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#endif // LLVM_IR_BASICBLOCK_H 426