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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 34 | class AssemblyAnnotationWriter; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 35 | class CallInst; |
| 36 | class Function; |
| 37 | class LandingPadInst; |
| 38 | class LLVMContext; |
| 39 | class Module; |
| 40 | class PHINode; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | class ValueSymbolTable; |
| 42 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 43 | /// LLVM Basic Block Representation |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | /// |
| 45 | /// This represents a single basic block in LLVM. A basic block is simply a |
| 46 | /// container of instructions that execute sequentially. Basic blocks are Values |
| 47 | /// because they are referenced by instructions such as branches and switch |
| 48 | /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block |
| 49 | /// represents a label to which a branch can jump. |
| 50 | /// |
| 51 | /// 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] | 52 | /// instructions followed by a single terminator instruction. Terminator |
| 53 | /// instructions may not occur in the middle of basic blocks, and must terminate |
| 54 | /// the blocks. The BasicBlock class allows malformed basic blocks to occur |
| 55 | /// because it may be useful in the intermediate stage of constructing or |
| 56 | /// modifying a program. However, the verifier will ensure that basic blocks are |
| 57 | /// "well formed". |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 58 | class BasicBlock final : public Value, // Basic blocks are data objects also |
| 59 | public ilist_node_with_parent<BasicBlock, Function> { |
| 60 | public: |
| 61 | using InstListType = SymbolTableList<Instruction>; |
| 62 | |
| 63 | private: |
| 64 | friend class BlockAddress; |
| 65 | friend class SymbolTableListTraits<BasicBlock>; |
| 66 | |
| 67 | InstListType InstList; |
| 68 | Function *Parent; |
| 69 | |
| 70 | void setParent(Function *parent); |
| 71 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 72 | /// Constructor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | /// |
| 74 | /// If the function parameter is specified, the basic block is automatically |
| 75 | /// inserted at either the end of the function (if InsertBefore is null), or |
| 76 | /// before the specified basic block. |
| 77 | explicit BasicBlock(LLVMContext &C, const Twine &Name = "", |
| 78 | Function *Parent = nullptr, |
| 79 | BasicBlock *InsertBefore = nullptr); |
| 80 | |
| 81 | public: |
| 82 | BasicBlock(const BasicBlock &) = delete; |
| 83 | BasicBlock &operator=(const BasicBlock &) = delete; |
| 84 | ~BasicBlock(); |
| 85 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | /// Get the context in which this basic block lives. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | LLVMContext &getContext() const; |
| 88 | |
| 89 | /// Instruction iterators... |
| 90 | using iterator = InstListType::iterator; |
| 91 | using const_iterator = InstListType::const_iterator; |
| 92 | using reverse_iterator = InstListType::reverse_iterator; |
| 93 | using const_reverse_iterator = InstListType::const_reverse_iterator; |
| 94 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | /// Creates a new BasicBlock. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | /// |
| 97 | /// If the Parent parameter is specified, the basic block is automatically |
| 98 | /// inserted at either the end of the function (if InsertBefore is 0), or |
| 99 | /// before the specified basic block. |
| 100 | static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", |
| 101 | Function *Parent = nullptr, |
| 102 | BasicBlock *InsertBefore = nullptr) { |
| 103 | return new BasicBlock(Context, Name, Parent, InsertBefore); |
| 104 | } |
| 105 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 106 | /// Return the enclosing method, or null if none. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 107 | const Function *getParent() const { return Parent; } |
| 108 | Function *getParent() { return Parent; } |
| 109 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 110 | /// Return the module owning the function this basic block belongs to, or |
| 111 | /// nullptr if the function does not have a module. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | /// |
| 113 | /// Note: this is undefined behavior if the block does not have a parent. |
| 114 | const Module *getModule() const; |
| 115 | Module *getModule() { |
| 116 | return const_cast<Module *>( |
| 117 | static_cast<const BasicBlock *>(this)->getModule()); |
| 118 | } |
| 119 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 120 | /// Returns the terminator instruction if the block is well formed or null |
| 121 | /// if the block is not well formed. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 122 | const Instruction *getTerminator() const LLVM_READONLY; |
| 123 | Instruction *getTerminator() { |
| 124 | return const_cast<Instruction *>( |
| 125 | static_cast<const BasicBlock *>(this)->getTerminator()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 128 | /// Returns the call instruction calling \@llvm.experimental.deoptimize |
| 129 | /// prior to the terminating return instruction of this basic block, if such |
| 130 | /// a call is present. Otherwise, returns null. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 131 | const CallInst *getTerminatingDeoptimizeCall() const; |
| 132 | CallInst *getTerminatingDeoptimizeCall() { |
| 133 | return const_cast<CallInst *>( |
| 134 | static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); |
| 135 | } |
| 136 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 137 | /// Returns the call instruction calling \@llvm.experimental.deoptimize |
| 138 | /// that is present either in current basic block or in block that is a unique |
| 139 | /// successor to current block, if such call is present. Otherwise, returns null. |
| 140 | const CallInst *getPostdominatingDeoptimizeCall() const; |
| 141 | CallInst *getPostdominatingDeoptimizeCall() { |
| 142 | return const_cast<CallInst *>( |
| 143 | static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall()); |
| 144 | } |
| 145 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 146 | /// Returns the call instruction marked 'musttail' prior to the terminating |
| 147 | /// return instruction of this basic block, if such a call is present. |
| 148 | /// Otherwise, returns null. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 149 | const CallInst *getTerminatingMustTailCall() const; |
| 150 | CallInst *getTerminatingMustTailCall() { |
| 151 | return const_cast<CallInst *>( |
| 152 | static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); |
| 153 | } |
| 154 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 155 | /// Returns a pointer to the first instruction in this block that is not a |
| 156 | /// PHINode instruction. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 157 | /// |
| 158 | /// When adding instructions to the beginning of the basic block, they should |
| 159 | /// be added before the returned value, not before the first instruction, |
| 160 | /// which might be PHI. Returns 0 is there's no non-PHI instruction. |
| 161 | const Instruction* getFirstNonPHI() const; |
| 162 | Instruction* getFirstNonPHI() { |
| 163 | return const_cast<Instruction *>( |
| 164 | static_cast<const BasicBlock *>(this)->getFirstNonPHI()); |
| 165 | } |
| 166 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 167 | /// Returns a pointer to the first instruction in this block that is not a |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 168 | /// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp |
| 169 | /// is true. |
| 170 | const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = false) const; |
| 171 | Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = false) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 172 | return const_cast<Instruction *>( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 173 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg( |
| 174 | SkipPseudoOp)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 177 | /// Returns a pointer to the first instruction in this block that is not a |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 178 | /// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo |
| 179 | /// operation if \c SkipPseudoOp is true. |
| 180 | const Instruction * |
| 181 | getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = false) const; |
| 182 | Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = false) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 183 | return const_cast<Instruction *>( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 184 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime( |
| 185 | SkipPseudoOp)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 188 | /// Returns an iterator to the first instruction in this block that is |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 189 | /// suitable for inserting a non-PHI instruction. |
| 190 | /// |
| 191 | /// In particular, it skips all PHIs and LandingPad instructions. |
| 192 | const_iterator getFirstInsertionPt() const; |
| 193 | iterator getFirstInsertionPt() { |
| 194 | return static_cast<const BasicBlock *>(this) |
| 195 | ->getFirstInsertionPt().getNonConst(); |
| 196 | } |
| 197 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 198 | /// Return a const iterator range over the instructions in the block, skipping |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 199 | /// any debug instructions. Skip any pseudo operations as well if \c |
| 200 | /// SkipPseudoOp is true. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 201 | iterator_range<filter_iterator<BasicBlock::const_iterator, |
| 202 | std::function<bool(const Instruction &)>>> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 203 | instructionsWithoutDebug(bool SkipPseudoOp = false) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 204 | |
| 205 | /// Return an iterator range over the instructions in the block, skipping any |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 206 | /// debug instructions. Skip and any pseudo operations as well if \c |
| 207 | /// SkipPseudoOp is true. |
| 208 | iterator_range< |
| 209 | filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>> |
| 210 | instructionsWithoutDebug(bool SkipPseudoOp = false); |
| 211 | |
| 212 | /// Return the size of the basic block ignoring debug instructions |
| 213 | filter_iterator<BasicBlock::const_iterator, |
| 214 | std::function<bool(const Instruction &)>>::difference_type |
| 215 | sizeWithoutDebug() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 216 | |
| 217 | /// Unlink 'this' from the containing function, but do not delete it. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 218 | void removeFromParent(); |
| 219 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 220 | /// Unlink 'this' from the containing function and delete it. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 221 | /// |
| 222 | // \returns an iterator pointing to the element after the erased one. |
| 223 | SymbolTableList<BasicBlock>::iterator eraseFromParent(); |
| 224 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 225 | /// Unlink this basic block from its current function and insert it into |
| 226 | /// the function that \p MovePos lives in, right before \p MovePos. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 227 | void moveBefore(BasicBlock *MovePos); |
| 228 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 229 | /// Unlink this basic block from its current function and insert it |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 230 | /// right after \p MovePos in the function \p MovePos lives in. |
| 231 | void moveAfter(BasicBlock *MovePos); |
| 232 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 233 | /// Insert unlinked basic block into a function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 234 | /// |
| 235 | /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is |
| 236 | /// provided, inserts before that basic block, otherwise inserts at the end. |
| 237 | /// |
| 238 | /// \pre \a getParent() is \c nullptr. |
| 239 | void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); |
| 240 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 241 | /// Return the predecessor of this block if it has a single predecessor |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 242 | /// block. Otherwise return a null pointer. |
| 243 | const BasicBlock *getSinglePredecessor() const; |
| 244 | BasicBlock *getSinglePredecessor() { |
| 245 | return const_cast<BasicBlock *>( |
| 246 | static_cast<const BasicBlock *>(this)->getSinglePredecessor()); |
| 247 | } |
| 248 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 249 | /// Return the predecessor of this block if it has a unique predecessor |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 250 | /// block. Otherwise return a null pointer. |
| 251 | /// |
| 252 | /// Note that unique predecessor doesn't mean single edge, there can be |
| 253 | /// multiple edges from the unique predecessor to this block (for example a |
| 254 | /// switch statement with multiple cases having the same destination). |
| 255 | const BasicBlock *getUniquePredecessor() const; |
| 256 | BasicBlock *getUniquePredecessor() { |
| 257 | return const_cast<BasicBlock *>( |
| 258 | static_cast<const BasicBlock *>(this)->getUniquePredecessor()); |
| 259 | } |
| 260 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 261 | /// Return true if this block has exactly N predecessors. |
| 262 | bool hasNPredecessors(unsigned N) const; |
| 263 | |
| 264 | /// Return true if this block has N predecessors or more. |
| 265 | bool hasNPredecessorsOrMore(unsigned N) const; |
| 266 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 267 | /// Return the successor of this block if it has a single successor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 268 | /// Otherwise return a null pointer. |
| 269 | /// |
| 270 | /// This method is analogous to getSinglePredecessor above. |
| 271 | const BasicBlock *getSingleSuccessor() const; |
| 272 | BasicBlock *getSingleSuccessor() { |
| 273 | return const_cast<BasicBlock *>( |
| 274 | static_cast<const BasicBlock *>(this)->getSingleSuccessor()); |
| 275 | } |
| 276 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 277 | /// Return the successor of this block if it has a unique successor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 278 | /// Otherwise return a null pointer. |
| 279 | /// |
| 280 | /// This method is analogous to getUniquePredecessor above. |
| 281 | const BasicBlock *getUniqueSuccessor() const; |
| 282 | BasicBlock *getUniqueSuccessor() { |
| 283 | return const_cast<BasicBlock *>( |
| 284 | static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); |
| 285 | } |
| 286 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 287 | /// Print the basic block to an output stream with an optional |
| 288 | /// AssemblyAnnotationWriter. |
| 289 | void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr, |
| 290 | bool ShouldPreserveUseListOrder = false, |
| 291 | bool IsForDebug = false) const; |
| 292 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 293 | //===--------------------------------------------------------------------===// |
| 294 | /// Instruction iterator methods |
| 295 | /// |
| 296 | inline iterator begin() { return InstList.begin(); } |
| 297 | inline const_iterator begin() const { return InstList.begin(); } |
| 298 | inline iterator end () { return InstList.end(); } |
| 299 | inline const_iterator end () const { return InstList.end(); } |
| 300 | |
| 301 | inline reverse_iterator rbegin() { return InstList.rbegin(); } |
| 302 | inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } |
| 303 | inline reverse_iterator rend () { return InstList.rend(); } |
| 304 | inline const_reverse_iterator rend () const { return InstList.rend(); } |
| 305 | |
| 306 | inline size_t size() const { return InstList.size(); } |
| 307 | inline bool empty() const { return InstList.empty(); } |
| 308 | inline const Instruction &front() const { return InstList.front(); } |
| 309 | inline Instruction &front() { return InstList.front(); } |
| 310 | inline const Instruction &back() const { return InstList.back(); } |
| 311 | inline Instruction &back() { return InstList.back(); } |
| 312 | |
| 313 | /// Iterator to walk just the phi nodes in the basic block. |
| 314 | template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> |
| 315 | class phi_iterator_impl |
| 316 | : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, |
| 317 | std::forward_iterator_tag, PHINodeT> { |
| 318 | friend BasicBlock; |
| 319 | |
| 320 | PHINodeT *PN; |
| 321 | |
| 322 | phi_iterator_impl(PHINodeT *PN) : PN(PN) {} |
| 323 | |
| 324 | public: |
| 325 | // Allow default construction to build variables, but this doesn't build |
| 326 | // a useful iterator. |
| 327 | phi_iterator_impl() = default; |
| 328 | |
| 329 | // Allow conversion between instantiations where valid. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 330 | template <typename PHINodeU, typename BBIteratorU, |
| 331 | typename = std::enable_if_t< |
| 332 | std::is_convertible<PHINodeU *, PHINodeT *>::value>> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 333 | phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) |
| 334 | : PN(Arg.PN) {} |
| 335 | |
| 336 | bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } |
| 337 | |
| 338 | PHINodeT &operator*() const { return *PN; } |
| 339 | |
| 340 | using phi_iterator_impl::iterator_facade_base::operator++; |
| 341 | phi_iterator_impl &operator++() { |
| 342 | assert(PN && "Cannot increment the end iterator!"); |
| 343 | PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); |
| 344 | return *this; |
| 345 | } |
| 346 | }; |
| 347 | using phi_iterator = phi_iterator_impl<>; |
| 348 | using const_phi_iterator = |
| 349 | phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; |
| 350 | |
| 351 | /// Returns a range that iterates over the phis in the basic block. |
| 352 | /// |
| 353 | /// Note that this cannot be used with basic blocks that have no terminator. |
| 354 | iterator_range<const_phi_iterator> phis() const { |
| 355 | return const_cast<BasicBlock *>(this)->phis(); |
| 356 | } |
| 357 | iterator_range<phi_iterator> phis(); |
| 358 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 359 | /// Return the underlying instruction list container. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 360 | /// |
| 361 | /// Currently you need to access the underlying instruction list container |
| 362 | /// directly if you want to modify it. |
| 363 | const InstListType &getInstList() const { return InstList; } |
| 364 | InstListType &getInstList() { return InstList; } |
| 365 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 366 | /// Returns a pointer to a member of the instruction list. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 367 | static InstListType BasicBlock::*getSublistAccess(Instruction*) { |
| 368 | return &BasicBlock::InstList; |
| 369 | } |
| 370 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 371 | /// Returns a pointer to the symbol table if one exists. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 372 | ValueSymbolTable *getValueSymbolTable(); |
| 373 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 374 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 375 | static bool classof(const Value *V) { |
| 376 | return V->getValueID() == Value::BasicBlockVal; |
| 377 | } |
| 378 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 379 | /// Cause all subinstructions to "let go" of all the references that said |
| 380 | /// subinstructions are maintaining. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 381 | /// |
| 382 | /// This allows one to 'delete' a whole class at a time, even though there may |
| 383 | /// be circular references... first all references are dropped, and all use |
| 384 | /// counts go to zero. Then everything is delete'd for real. Note that no |
| 385 | /// operations are valid on an object that has "dropped all references", |
| 386 | /// except operator delete. |
| 387 | void dropAllReferences(); |
| 388 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 389 | /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred. |
| 390 | /// Note that this function does not actually remove the predecessor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 391 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 392 | /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with |
| 393 | /// zero or one incoming values, and don't simplify PHIs with all incoming |
| 394 | /// values the same. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 395 | void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 396 | |
| 397 | bool canSplitPredecessors() const; |
| 398 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 399 | /// Split the basic block into two basic blocks at the specified instruction. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 400 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 401 | /// If \p Before is true, splitBasicBlockBefore handles the |
| 402 | /// block splitting. Otherwise, execution proceeds as described below. |
| 403 | /// |
| 404 | /// Note that all instructions BEFORE the specified iterator |
| 405 | /// stay as part of the original basic block, an unconditional branch is added |
| 406 | /// to the original BB, and the rest of the instructions in the BB are moved |
| 407 | /// to the new BB, including the old terminator. The newly formed basic block |
| 408 | /// is returned. This function invalidates the specified iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 409 | /// |
| 410 | /// Note that this only works on well formed basic blocks (must have a |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 411 | /// terminator), and \p 'I' must not be the end of instruction list (which |
| 412 | /// would cause a degenerate basic block to be formed, having a terminator |
| 413 | /// inside of the basic block). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 414 | /// |
| 415 | /// Also note that this doesn't preserve any passes. To split blocks while |
| 416 | /// keeping loop information consistent, use the SplitBlock utility function. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 417 | BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "", |
| 418 | bool Before = false); |
| 419 | BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "", |
| 420 | bool Before = false) { |
| 421 | return splitBasicBlock(I->getIterator(), BBName, Before); |
| 422 | } |
| 423 | |
| 424 | /// Split the basic block into two basic blocks at the specified instruction |
| 425 | /// and insert the new basic blocks as the predecessor of the current block. |
| 426 | /// |
| 427 | /// This function ensures all instructions AFTER and including the specified |
| 428 | /// iterator \p I are part of the original basic block. All Instructions |
| 429 | /// BEFORE the iterator \p I are moved to the new BB and an unconditional |
| 430 | /// branch is added to the new BB. The new basic block is returned. |
| 431 | /// |
| 432 | /// Note that this only works on well formed basic blocks (must have a |
| 433 | /// terminator), and \p 'I' must not be the end of instruction list (which |
| 434 | /// would cause a degenerate basic block to be formed, having a terminator |
| 435 | /// inside of the basic block). \p 'I' cannot be a iterator for a PHINode |
| 436 | /// with multiple incoming blocks. |
| 437 | /// |
| 438 | /// Also note that this doesn't preserve any passes. To split blocks while |
| 439 | /// keeping loop information consistent, use the SplitBlockBefore utility |
| 440 | /// function. |
| 441 | BasicBlock *splitBasicBlockBefore(iterator I, const Twine &BBName = ""); |
| 442 | BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "") { |
| 443 | return splitBasicBlockBefore(I->getIterator(), BBName); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 446 | /// 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] | 447 | /// direct branches, switches, etc. to it. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 448 | bool hasAddressTaken() const { |
| 449 | return getBasicBlockBits().BlockAddressRefCount != 0; |
| 450 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 451 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 452 | /// Update all phi nodes in this basic block to refer to basic block \p New |
| 453 | /// instead of basic block \p Old. |
| 454 | void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New); |
| 455 | |
| 456 | /// Update all phi nodes in this basic block's successors to refer to basic |
| 457 | /// block \p New instead of basic block \p Old. |
| 458 | void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New); |
| 459 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 460 | /// Update all phi nodes in this basic block's successors to refer to basic |
| 461 | /// block \p New instead of to it. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 462 | void replaceSuccessorsPhiUsesWith(BasicBlock *New); |
| 463 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 464 | /// Return true if this basic block is an exception handling block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 465 | bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } |
| 466 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 467 | /// Return true if this basic block is a landing pad. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 468 | /// |
| 469 | /// Being a ``landing pad'' means that the basic block is the destination of |
| 470 | /// the 'unwind' edge of an invoke instruction. |
| 471 | bool isLandingPad() const; |
| 472 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 473 | /// Return the landingpad instruction associated with the landing pad. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 474 | const LandingPadInst *getLandingPadInst() const; |
| 475 | LandingPadInst *getLandingPadInst() { |
| 476 | return const_cast<LandingPadInst *>( |
| 477 | static_cast<const BasicBlock *>(this)->getLandingPadInst()); |
| 478 | } |
| 479 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 480 | /// Return true if it is legal to hoist instructions into this block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 481 | bool isLegalToHoistInto() const; |
| 482 | |
| 483 | Optional<uint64_t> getIrrLoopHeaderWeight() const; |
| 484 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 485 | /// Returns true if the Order field of child Instructions is valid. |
| 486 | bool isInstrOrderValid() const { |
| 487 | return getBasicBlockBits().InstrOrderValid; |
| 488 | } |
| 489 | |
| 490 | /// Mark instruction ordering invalid. Done on every instruction insert. |
| 491 | void invalidateOrders() { |
| 492 | validateInstrOrdering(); |
| 493 | BasicBlockBits Bits = getBasicBlockBits(); |
| 494 | Bits.InstrOrderValid = false; |
| 495 | setBasicBlockBits(Bits); |
| 496 | } |
| 497 | |
| 498 | /// Renumber instructions and mark the ordering as valid. |
| 499 | void renumberInstructions(); |
| 500 | |
| 501 | /// Asserts that instruction order numbers are marked invalid, or that they |
| 502 | /// are in ascending order. This is constant time if the ordering is invalid, |
| 503 | /// and linear in the number of instructions if the ordering is valid. Callers |
| 504 | /// should be careful not to call this in ways that make common operations |
| 505 | /// O(n^2). For example, it takes O(n) time to assign order numbers to |
| 506 | /// instructions, so the order should be validated no more than once after |
| 507 | /// each ordering to ensure that transforms have the same algorithmic |
| 508 | /// complexity when asserts are enabled as when they are disabled. |
| 509 | void validateInstrOrdering() const; |
| 510 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 511 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 512 | #if defined(_AIX) && (!defined(__GNUC__) || defined(__ibmxl__)) |
| 513 | // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words |
| 514 | // and give the `pack` pragma push semantics. |
| 515 | #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)") |
| 516 | #define END_TWO_BYTE_PACK() _Pragma("pack(pop)") |
| 517 | #else |
| 518 | #define BEGIN_TWO_BYTE_PACK() |
| 519 | #define END_TWO_BYTE_PACK() |
| 520 | #endif |
| 521 | |
| 522 | BEGIN_TWO_BYTE_PACK() |
| 523 | /// Bitfield to help interpret the bits in Value::SubclassData. |
| 524 | struct BasicBlockBits { |
| 525 | unsigned short BlockAddressRefCount : 15; |
| 526 | unsigned short InstrOrderValid : 1; |
| 527 | }; |
| 528 | END_TWO_BYTE_PACK() |
| 529 | |
| 530 | #undef BEGIN_TWO_BYTE_PACK |
| 531 | #undef END_TWO_BYTE_PACK |
| 532 | |
| 533 | /// Safely reinterpret the subclass data bits to a more useful form. |
| 534 | BasicBlockBits getBasicBlockBits() const { |
| 535 | static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short), |
| 536 | "too many bits for Value::SubclassData"); |
| 537 | unsigned short ValueData = getSubclassDataFromValue(); |
| 538 | BasicBlockBits AsBits; |
| 539 | memcpy(&AsBits, &ValueData, sizeof(AsBits)); |
| 540 | return AsBits; |
| 541 | } |
| 542 | |
| 543 | /// Reinterpret our subclass bits and store them back into Value. |
| 544 | void setBasicBlockBits(BasicBlockBits AsBits) { |
| 545 | unsigned short D; |
| 546 | memcpy(&D, &AsBits, sizeof(D)); |
| 547 | Value::setValueSubclassData(D); |
| 548 | } |
| 549 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 550 | /// Increment the internal refcount of the number of BlockAddresses |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 551 | /// referencing this BasicBlock by \p Amt. |
| 552 | /// |
| 553 | /// This is almost always 0, sometimes one possibly, but almost never 2, and |
| 554 | /// inconceivably 3 or more. |
| 555 | void AdjustBlockAddressRefCount(int Amt) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 556 | BasicBlockBits Bits = getBasicBlockBits(); |
| 557 | Bits.BlockAddressRefCount += Amt; |
| 558 | setBasicBlockBits(Bits); |
| 559 | assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 562 | /// Shadow Value::setValueSubclassData with a private forwarding method so |
| 563 | /// that any future subclasses cannot accidentally use it. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 564 | void setValueSubclassData(unsigned short D) { |
| 565 | Value::setValueSubclassData(D); |
| 566 | } |
| 567 | }; |
| 568 | |
| 569 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 570 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) |
| 571 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 572 | /// Advance \p It while it points to a debug instruction and return the result. |
| 573 | /// This assumes that \p It is not at the end of a block. |
| 574 | BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It); |
| 575 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 576 | #ifdef NDEBUG |
| 577 | /// In release builds, this is a no-op. For !NDEBUG builds, the checks are |
| 578 | /// implemented in the .cpp file to avoid circular header deps. |
| 579 | inline void BasicBlock::validateInstrOrdering() const {} |
| 580 | #endif |
| 581 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 582 | } // end namespace llvm |
| 583 | |
| 584 | #endif // LLVM_IR_BASICBLOCK_H |