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